EvaluateScript("console_scene.js");
EvaluateScript("wipe_intro.js");
EvaluateScript("wipe_outro.js");
EvaluateScript("oblit_scene.js");

EvaluateScript("id_intro.js");
EvaluateScript("id_outro.js");


function Intros()
{
//	ShowCredits2();
//	SupernautEndScene();
	//return 

	var skip = 0;
	
	var infile = OpenFile("../game.sgm");
	if (infile.read("intro", 0) == 0)
		skip = 0;
	else
		skip = 1;

//	skip = 0;
	infile.write("intro", 1);
	infile.close();

	var sound = new Array();
	sound[0] = LoadSound("echo1.wav");
	sound[1] = LoadSound("voice1.wav");
	sound[2] = LoadSound("voice2.wav");
	sound[3] = LoadSound("voice3.wav");
	sound[4] = LoadSound("voice4.wav");
	sound[5] = LoadSound("voice5.wav");
	sound[6] = LoadSound("voice6.wav");
	sound[7] = LoadSound("voice7.wav");
	sound[8] = LoadSound("voice8.wav");
	sound[9] = LoadSound("wind2.ogg");

	var images = new Array(9);
	images[0] = LoadImage("guy1.png");
	images[1] = LoadImage("guy2.png");
	images[2] = LoadImage("guy3.png");
	images[3] = LoadImage("guy4.png");
	images[4] = LoadImage("guy5.png");
	images[5] = LoadImage("guy6.png");

//eye

/*
	images[6] = LoadImage("face1.png");
	images[7] = LoadImage("face2.png");
	images[8] = LoadImage("face3.png");


	images[9] = LoadImage("mid1.png");
	images[10] = LoadImage("mid2.png");
	images[11] = LoadImage("mid3.png");

	images[12] = LoadImage("front1_01.png");
	images[13] = LoadImage("front1_02.png");
	images[14] = LoadImage("front1_03.png");
	images[15] = LoadImage("front1_04.png");
	images[16] = LoadImage("front1_05.png");
	images[17] = LoadImage("front1_06.png");
	images[18] = LoadImage("front1_07.png");
	images[19] = LoadImage("front1_08.png");
	images[20] = LoadImage("front1_09.png");
	images[21] = LoadImage("front1_10.png");
	images[22] = LoadImage("front1_11.png");
  images[23] = LoadImage("front1_12.png");


  images[24] = LoadImage("lock1_01.png");
  images[25] = LoadImage("lock1_02.png");
  images[26] = LoadImage("lock1_03.png");
  images[27] = LoadImage("lock1_04.png");
  images[28] = LoadImage("lock1_05.png");
  images[29] = LoadImage("lock1_06.png");
  images[30] = LoadImage("lock1_07.png");
  images[31] = LoadImage("lock1_08.png");
  images[32] = LoadImage("lock1_09.png");

  images[33] = LoadImage("ani_01.png");
  images[34] = LoadImage("ani_02.png");
  images[35] = LoadImage("ani_03.png");
  images[36] = LoadImage("ani_04.png");
  images[37] = LoadImage("ani_05.png");
  images[38] = LoadImage("ani_06.png");

  images[39] = LoadImage("door_open1_01.png");
  images[40] = LoadImage("door_open1_02.png");
  images[41] = LoadImage("door_open1_03.png");
  images[42] = LoadImage("door_open1_04.png");
  images[43] = LoadImage("door_open1_05.png");
  images[44] = LoadImage("door_open1_06.png");
  images[45] = LoadImage("door_open1_07.png");
  images[46] = LoadImage("door_open1_08.png");
  images[47] = LoadImage("door_open1_09.png");
  images[48] = LoadImage("door_open1_10.png");
  images[49] = LoadImage("door_open1_11.png");
  images[50] = LoadImage("door_open1_12.png");

  images[51] = LoadImage("guy_gun1_01.png");
  images[52] = LoadImage("guy_gun1_02.png");
  images[53] = LoadImage("guy_gun1_03.png");
  images[54] = LoadImage("guy_gun1_04.png");
  images[55] = LoadImage("guy_gun1_05.png");
  images[56] = LoadImage("guy_gun1_06.png");
  images[57] = LoadImage("guy_gun1_07.png");
*/
	
	var frames = 0;
	var frametime = 100;
	
	var lasttime = GetTime();
	var x=GetScreenWidth()-images[0].width;
	var y=(GetScreenHeight()-images[0].height)/2;
	var amt=1;
	
	var maxshade = 40;
	var shade = 0;
	
	
	var phase = 0;
	var starttime = GetTime();
	
		
	function xadd()
	{
		if (shade > 0)
		{
		  Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,shade*255/maxshade));
		  shade++;
		}
		if (phase==0)
			x+=amt;
			
		frames++;
		while(GetTime()-starttime < frametime*frames);

	}

	function DoBlink()
	{
		lasttime = GetTime();
		images[1].blit(x,y);
		xadd();
		IntroRecord();
		FlipScreen();
		lasttime = GetTime();
		images[2].blit(x,y);
		xadd();
		IntroRecord();
		FlipScreen();
		lasttime = GetTime();
		images[1].blit(x,y);
		xadd();
		IntroRecord();
		FlipScreen();
		lasttime = GetTime();
		images[0].blit(x,y);
		xadd();
		IntroRecord();
		FlipScreen();
	}

	function DoTalk(num, start)
	{
		lasttime = GetTime();
		if (start == undefined)
			start = 3;
		images[start].blit(x,y);
		xadd();
		IntroRecord();
		FlipScreen();
		if (num > 1)
		{
			lasttime = GetTime();
			images[start+1].blit(x,y);
			xadd();
			IntroRecord();
			FlipScreen();

			if (num > 2)
			{
				lasttime = GetTime();
				images[start+2].blit(x,y);
				xadd();
				IntroRecord();
				FlipScreen();
				while(GetTime()-lasttime < frametime);
				lasttime = GetTime();
				images[start+1].blit(x,y);
				xadd();
				IntroRecord();
				FlipScreen();
			}
			lasttime = GetTime();
			images[start].blit(x,y);
			xadd();
			IntroRecord();
			FlipScreen();
			while(GetTime()-lasttime < frametime);
		}
		lasttime = GetTime();
		images[start].blit(x,y);
		xadd();
		IntroRecord();
		FlipScreen();
	}
	function DoWait(num,imagey)
	{
		for (i=0;i<num;i++)
		{
			lasttime = GetTime();
			if (imagey==undefined)
				imagey=0;
			images[imagey].blit(x,y);
			xadd();
			IntroRecord();
			FlipScreen();
		}
	}
	
	
	function Display(number,xbool)
	{
		lasttime = GetTime();
		images[number].blit(x,y);
		xadd();
		IntroRecord();
		FlipScreen();
	}


	
	if (skip <1)
	{
		
		for (i=0;i<20;i++)
		{
			lasttime = GetTime();
			images[0].blit(x,y);
			xadd();
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-i*255/20));
			//IntroRecord();
			FlipScreen();
		}
	//	DoWait(20);
//		return;
		sound[0].play(false);
		DoTalk(1);
		DoTalk(3);
		DoTalk(1);
		DoTalk(2);
		DoTalk(3);
	//	DoTalk(1);
	
		DoWait(5);
		DoBlink();
		DoWait(5);
		DoTalk(1);
		DoTalk(2);
		DoTalk(1);
		DoTalk(3);
		DoWait(8);
		DoTalk(1);
		DoTalk(2);
		DoTalk(1);
		DoTalk(3);
	
		DoWait(10);
		DoBlink();
		DoWait(5);
		DoTalk(1);
		DoTalk(2);
		DoTalk(1);
		DoTalk(2);
		DoWait(1);
		DoTalk(3);
		shade=1;
	
	
		DoWait(maxshade);
	if (skip == -1)
	{
			
		x=0;
		shade=0;
		phase=1;
			
		var fmax = 10;
		sound[9].play(false);
		sound[9].setVolume(48);
		for (i=0;i<fmax;i++)
		{
			lasttime = GetTime();	
			if (i==5)
				sound[1].play(false);
			images[6].blit(x,y);
			xadd();
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-i*255/fmax));
			FlipScreen();
		}
		
		DoWait(5, 6);
	
	
		Display(7);
		Display(8);
		Display(7);
		Display(6);
		DoWait(5, 6);
		DoWait(5, 6);
		Display(9);
		DoWait(5, 9);
		sound[2].play(false);
		Display(11);
		Display(10);
		Display(9);
		Display(10);
		Display(9);
		Display(10);
		Display(9);
		Display(10);
		Display(9);
		Display(11);
		Display(10);
		Display(9);
		Display(11);
		Display(10);
		Display(9);
		Display(10);
	
		DoWait(5, 9);
		
	
		Display(12);
		DoWait(3, 12);
		sound[3].play(false);
		Display(13);
		Display(12);
		Display(14);
		Display(13);
		Display(12);
		Display(13);
		Display(12);
		Display(13);
		Display(12);
		Display(14);
		Display(13);
		Display(12);
		Display(13);
		Display(12);
		Display(14);
		Display(13);
		Display(12);
		Display(14);
		Display(13);
		Display(12);
		Display(13);
		Display(12);
		Display(13);
		Display(12);
		
		DoWait(6, 12);
		Display(18);
		Display(19);
		Display(18);
		DoWait(6, 12);
		sound[4].play(false);
	//	Display(12);
		Display(15);
		Display(16);
		Display(17);
		DoWait(3, 17);
		Display(16);
		Display(15);
	
	
		DoWait(9, 12);
		Display(14);
		Display(13);
		Display(12);
		Display(14);
		Display(13);
		Display(12);
		Display(13);
		Display(12);
		Display(14);
		Display(13);
		Display(12);
		Display(13);
		Display(14);
		Display(13);
		Display(12);
		
		DoWait(6, 12);
		Display(18);
		Display(19);
		Display(18);
		DoWait(6, 12);
		
		Display(20);
		Display(21);
		Display(22);
		Display(23);
		
	
		Display(24);
		DoWait(25, 24);
		Display(25);
		Display(26);
		Display(27);
		Display(28);
		Display(29);
		Display(30);
		Display(31);
		DoWait(2, 31);
		Display(32);
		DoWait(5, 32);
		Display(33);
		DoWait(5, 33);
		Display(34);
		Display(35);
		Display(36);
		Display(37);
		Display(38);
		DoWait(8, 38);
	
		Display(39);
		DoWait(3, 39);
		sound[5].play(false);
		DoWait(2, 39);
		Display(40);
		Display(41);
		Display(42);
		Display(43);
		Display(44);
		Display(45);
		Display(46);
		Display(47);
		Display(48);
		Display(49);
		Display(50);
		DoWait(5, 50);
		Display(51);
		DoWait(3, 51);
		
		Display(52);
		Display(53);
		sound[8].play(false);
		DoWait(3, 53);
		Display(54);
		Display(53);
		Display(54);
		DoWait(10, 53);
		Display(52);
		Display(51);
	
		DoWait(5, 51);
		sound[6].play(false);
		
		Display(56);
		Display(55);
	
		Display(57);
		Display(56);
		Display(55);
	
		Display(56);
		Display(55);
	
		Display(56);
		Display(55);
		
	
		Display(57);
		Display(56);
		Display(55);
	
		for (i=0;i<fmax;i++)
		{
			lasttime = GetTime();	
			images[51].blit(x,y);
			xadd();
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,i*255/fmax));
			FlipScreen();
		}
		FlipScreen();
	
		for (i=0;i<fmax;i++)
		{
			xadd();
			FlipScreen();
		}
	
	
		sound[7].play(false);
		
		while(sound[7].isPlaying());
		}
	}
	else if (skip == 2)
	{
//		DoFirstConsoleScene()
	
	}
	return ShowLogo();
}




function 	ShowLogo()
{
	var image = LoadImage("logo1.png");
	var lasttime = GetTime();
	var frametime = 40;
	//fade in;
	
	var fadeout = 0;
	while(AreKeysLeft())
		GetKey();

	var string1 = "Team";
	var string2 = "Happy Rainbow Panda Bears";
	var string3 = "Presents";

	var fx1 = 160-font.getStringWidth(string1)/2;
	var fy1 = 90;
	var fx2 = 160-font.getStringWidth(string2)/2;
	var fy2 = 110;
	var fx3 = 160-font.getStringWidth(string3)/2;
	var fy3 = 130;
	var counter = 0;
	
	var fontr = GetSystemFont();
	fontr.setColorMask(CreateColor(255,0,0,180));
	
	while (counter <= 320)
	{
		image.blit(0,0);
	
		if (counter >= 60 && counter < 100)
		{
		  fontr.drawText(fx1,fy1, string1);
			Rectangle(fx1,fy1,GetScreenWidth(),20,CreateColor(0,0,0,255-255*(counter-60)/40));
		}
		else if (counter >= 100 && counter < 140)
		{
		  fontr.drawText(fx1,fy1, string1);
		  fontr.drawText(fx2,fy2, string2);
			Rectangle(fx2,fy2,GetScreenWidth(),20,CreateColor(0,0,0,255-255*(counter-100)/40));
		}
		else if (counter >= 140 && counter < 180)
		{
		  fontr.drawText(fx1,fy1, string1);
		  fontr.drawText(fx2,fy2, string2);
		  fontr.drawText(fx3,fy3, string3);
			Rectangle(fx3,fy3,GetScreenWidth(),20,CreateColor(0,0,0,255-255*(counter-140)/40));
		}
		else if (counter >=180)
		{
		  fontr.drawText(fx1,fy1, string1);
		  fontr.drawText(fx2,fy2, string2);
		  fontr.drawText(fx3,fy3, string3);
		}
		  
		if (fadeout > 0)
			Rectangle(0,0,GetScreenWidth(),GetScreenHeight(),CreateColor(0,0,0,255*(fadeout)/40));
		else if (counter < 40)
			Rectangle(0,0,GetScreenWidth(),GetScreenHeight(),CreateColor(0,0,0,255-255*counter/40));
	
		FlipScreen();
			while(GetTime()-lasttime < frametime);
		lasttime = GetTime();
		
		if (AreKeysLeft())
		{
			if (fadeout == 0)
			{
				if (counter < 40)
				  fadeout = 40-counter;
				else
					fadeout = 1;
			}
		}
		
		counter++;
		if (counter >=280 && fadeout == 0)
		  fadeout = 1;
		else if (fadeout > 0)
		  fadeout++;
		  
		if (fadeout>=40)
		  break;
	}
	FlipScreen();
	
	return Intros1();
}

function Intros1()
{
	var skipme = 0;
	if (skipme == 0)
	{		
	
		music[21].setVolume(music_vol[21]*music_level);
		music[21].setPosition(0);
		music[21].play();
		
		while(AreKeysLeft())
			GetKey();
	
		var images = new Array();
		images[0] = LoadImage("intro/reworks1.png");
		images[1] = LoadImage("intro/liegh_gun1.png");
		images[2] = LoadImage("intro/liegh_gun_prepare1.png");
		images[3] = LoadImage("intro/liegh_base1_a.png");
		images[4] = LoadImage("intro/liegh_base1_b.png");
		images[5] = LoadImage("intro/black.png");
	
		var frames = 0;
		var frametime = 100;
		
		var lasttime = GetTime();
		var x=GetScreenWidth()-images[0].width;
		var y=(GetScreenHeight()-images[0].height)/2;
		
		var maxshade = 40;
		var shade = 0;
		
		function xadd()
		{
			if (shade > 0)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,shade*255/maxshade));
			
			IsOver();
	
			if (AreKeysLeft())
				shade++;
				
	
			frames++;
			while(GetTime()-lasttime < frametime);
			lasttime = GetTime();
		}
		
		var cur_text = "";
		var text_length = 0;
		
		function DrawText()
		{
			font.drawText(5,227, cur_text.substr(0,text_length));
		}
		
		function IsOver()
		{
			if (/*shade>=40 &&*/ AreKeysLeft())
				isover = true;
		}
		
		function SetText(string)
		{
			cur_text = string;
			text_length = 0;
		}
		
		function ShowImage(num)
		{
			if (isover==false)
			{
				images[num].blit(x,y);
				if (num >= 3)
					Rectangle(0,0,GetScreenWidth(), 7, CreateColor(0,0,0,255));
				DrawText();
				if (text_length < cur_text.length)
					text_length++;
				xadd();
				FlipScreen();
			}
		} 
		
	
		function ShowImage2(num)
		{
			if (isover==false)
			{
				images[num].blit(x,y);
				Rectangle(0,0,GetScreenWidth(),GetScreenHeight(), CreateColor(0,0,0,255*shade/maxshade));
				images[num-1].blit(x,y);
				if (num >= 3)
					Rectangle(0,0,GetScreenWidth(), 7, CreateColor(0,0,0,255));
				DrawText();
				if (text_length < cur_text.length)
					text_length++;
	
				frames++;
				while(GetTime()-lasttime < frametime);
				lasttime = GetTime();
	
				FlipScreen();
			}
		} 
	
		shade=maxshade;
		x=GetScreenWidth()-images[0].width;
		y=-14;
		var isover = false;
	
		for (it=0;it<40;it++)
		{
			ShowImage(0);
			if (!AreKeysLeft())
				shade--;
			x++;
		}
	
		SetText("I don't know where they came from.");
		for (it=0;it<150;it++)
		{
			ShowImage(0);
			if (it == 85)
				SetText("All I know is that these 'things' are vicious.");
			
			x++;
		}
		
		for (it=0;it<40;it++)
		{
			ShowImage(0);
			shade++;
			x++;
		}
	
		SetText("");
		shade = maxshade;
	
		x=0;
	
		for (it=0;it<40;it++)
		{
			ShowImage(1);
			if (!AreKeysLeft())
				shade--;
		}
	
		SetText("...And I'm right in the middle of them,");
		for (it=0;it<50;it++)
		{
			ShowImage(1);
		}
		SetText("and only one gun to shoot.");
		for (it=0;it<40;it++)
		{
			ShowImage(1);
		}
		
		for (it=0;it<40;it++)
		{
			ShowImage(1);
			if (!AreKeysLeft())
				shade++;
		}
	
		SetText("");
	
		for (it=0;it<40;it++)
		{
			ShowImage(2);
			if (!AreKeysLeft())
				shade--;
		}
	
		SetText("The city's been evacuated,");
		for (it=0;it<50;it++)
		{
			ShowImage(2);
		}
		SetText("not a soul around for a hundred miles.");
		for (it=0;it<50;it++)
		{
			ShowImage(2);
		}
		SetText("Except for me, and a horde of 'reworks.'");
		for (it=0;it<50;it++)
		{
			ShowImage(2);
		}
	
	
		
		for (it=0;it<40;it++)
		{
			ShowImage(2);
			if (!AreKeysLeft())
				shade++;
		}
		
	
	
		y=-7;
		SetText("");
		for (it=0;it<40;it++)
		{
			ShowImage(3);
			if (!AreKeysLeft())
				shade--;
		}
	
		SetText("A friend of mine told me the military will");
		for (it=0;it<60;it++)
		{
			ShowImage(3);
		}
		
		SetText("\"blow the place to hell\" in about 2 hours.");
		for (it=0;it<60;it++)
		{
			ShowImage(3);
		}
	
		SetText("Told of a military base on the edge of town.");
		for (it=0;it<60;it++)
		{
			ShowImage(3);
		}
	
		shade = maxshade;
	
		for (it=0;it<40;it++)
		{
			ShowImage2(4);
			if (!AreKeysLeft())
				shade--;
		}
	
		SetText("It has the deepest bunker of anything nearby,");
		for (it=0;it<60;it++)
		{
			ShowImage(4);
		}
		SetText("and should be deep enough to avoid the blasts.");
	
		for (it=0;it<60;it++)
		{
			ShowImage(4);
		}
		
		for (it=0;it<=40;it++)
		{
			ShowImage(4);
			if (!AreKeysLeft())
				shade++;
		}
		shade = 0;
		
		SetText("...If I can make it down there in time.");
		for (it=0;it<=40;it++)
		{
			ShowImage(5);
		}
		for (it=0;it<=40;it++)
		{
			ShowImage(5);
			if (!AreKeysLeft())
				shade++;
		}
		
		if (isover==false)
		{		
			for (it=0;it<=40 && music[21].isPlaying() == true && isover==false;it++)
			{
				music[21].setVolume(music_vol[21]*(1-it/40));
			}
		}
		
		music[21].setVolume(0);
		if (music[21].isPlaying())
			music[21].stop();
	}
	
	var selecty = MainMenu();
	if (selecty == 1)
	{
		//start a new game;
		return 1;
	}
	if (selecty == 2)
	{
		//start training;
		return 1;
	}
	else if (selecty == 3)
	{
		//load a game;
		return 3;
	}
	else if (selecty == 7)
	{
		//quit
		return 7;
	}
}







var g_chosen_diff = 0;

function MainMenu(nw)
{


	SaveWeapPossess();

	var files = new Array();
	for (i=-1;i<4;i++)
		files[i] = new Object();

	for (i=-1;i<4;i++)
	{
		var filey = OpenFile("slot"+i+".rtd");
		files[i].level = filey.read("level", -1);
		if (files[i].level != -1)
		{
			files[i].name = filey.read("name", "empty");
			files[i].kills = filey.read("kills", -1);
			files[i].totaltime = filey.read("totaltime", 0);
			files[i].score = filey.read("score", 0);
			files[i].cur_weap = filey.read("cur_weap", 0);

			for (sts = 0; sts < 14;sts++)
			{
				files[i]["ammo"+sts] = filey.read("ammo"+sts, 0)
				files[i]["clip"+sts] = filey.read("clip"+sts, 0)
			}
			for (sts = 0; sts < 16;sts++)
			{
				files[i]["weap"+sts] = filey.read("weap"+sts, 0)
			}
	
			files[i].minutes = filey.read("d_minutes", 0);
			files[i].hours = filey.read("d_hours", 0);
			files[i].day = filey.read("d_day", 0);
			files[i].month = filey.read("d_month", 0);
			files[i].year = filey.read("d_year", 0);
			if (files[i].month < 10)
				files[i].month = "0"+files[i].month;
			if (files[i].day < 10)
				files[i].day = "0"+files[i].day;
			if (files[i].hours < 10)
				files[i].hours = "0"+files[i].hours;
			if (files[i].minutes < 10)
				files[i].minutes = "0"+files[i].minutes;



			files[i].diff = filey.read("diff", 0);

		}
		else
		{
			files[i].name = "empty";
			files[i].kills = 0;
			files[i].totaltime = 0;
			files[i].score = 0;
			files[i].cur_weap = 1;

			for (sts = 0; sts < 14;sts++)
			{
				files[i]["ammo"+sts] = 0;
				files[i]["clip"+sts] = 0;
			}
			for (sts = 0; sts < 16;sts++)
			{
				files[i]["weap"+sts] = 0;
			}
	
			files[i].minutes = 0;
			files[i].hours = 0;
			files[i].day = 0;
			files[i].month = 0;
			files[i].year = 0;

			files[i].diff = 0;
		}
	
	}

	while(AreKeysLeft())
		GetKey();

	var diff_array = new Array(3)
	diff_array[0] = "Easy";
	diff_array[1] = "Normal";
	diff_array[2] = "Hard";


	var window = LoadWindowStyle("window1.rws");
	var lets_go = LoadSound("lets_go.ogg",true);
	lets_go.setVolume(72);

	var images = new Array()
	images[0] = LoadImage("intro/main_menu_1.png");
	images[1] = LoadImage("intro/main_menu_2.png");
	images[2] = LoadImage("intro/main_menu_3.png");
	images[3] = LoadImage("intro/main_menu_4.png");
	images[4] = LoadImage("intro/main_menu_5.png");
	images[5] = LoadImage("intro/main_menu_6.png");
	images[6] = LoadImage("intro/main_menu_7.png");
	images[7] = LoadImage("intro/main_menu_8.png");
	images[8] = LoadImage("intro/main_menu_9.png");
	images[9] = LoadImage("intro/main_menu_10.png");
	images[10] = LoadImage("intro/main_menu_11.png");
	images[11] = LoadImage("intro/main_menu_12.png");
	
	var title = LoadImage("intro/title1.png");
	
	var buttons=new Array();
	buttons[0] = LoadImage("menu_return1.png");
	buttons[1] = LoadImage("menu_start1.png");
	buttons[2] = LoadImage("menu_training3.png");
	buttons[3] = LoadImage("menu_load1.png");
	buttons[4] = LoadImage("menu_diff1.png");
	buttons[5] = LoadImage("menu_diff2.png");
	buttons[6] = LoadImage("menu_diff3.png");
	buttons[7] = LoadImage("menu_sound1.png");
	buttons[8] = LoadImage("menu_sound2.png");
	buttons[9] = LoadImage("menu_control1.png");
	buttons[10] = LoadImage("menu_exit1.png");
	buttons[11] = LoadImage("intro/menu_panel1.png");
	buttons[12] = LoadImage("intro/menu_panel2.png");
	buttons[13] = LoadImage("menu_sound3.png");
	buttons[14] = LoadImage("menu_quit1.png");
	buttons[15] = LoadImage("menu_button1.png");
	buttons[16] = LoadImage("menu_button2.png");
	buttons[17] = LoadImage("menu_button3.png");
	buttons[18] = LoadImage("menu_button4.png");
	buttons[19] = LoadImage("menu_button5.png");

	buttons[20] = LoadImage("menu_sound_meter1.png");
	buttons[21] = LoadImage("menu_sound_meter2.png");
	buttons[22] = LoadImage("menu_sound_meter3.png");
	buttons[23] = LoadImage("menu_button_up.png");
	buttons[24] = LoadImage("menu_button_down.png");
	
	buttons[25] = LoadImage("menu_sound_button.png");
	buttons[26] = LoadImage("menu_quit2.png");
	buttons[27] = LoadImage("menu_quit3.png");

	buttons[28] = LoadImage("level_end_slot1.png");
	buttons[29] = LoadImage("menu_button6.png");
	buttons[30] = LoadImage("level_end_slot2.png");

	buttons[31] = LoadImage("menu_newgame1.png");
	buttons[32] = LoadImage("menu_newgame2.png");
	buttons[33] = LoadImage("menu_newgame3.png");

	buttons[34] = LoadImage("menu_button3b.png");

	buttons[35] = LoadImage("menu_newtrain1.png");
	buttons[36] = LoadImage("menu_newtrain2.png");
	buttons[37] = LoadImage("menu_newtrain3.png");

	buttons[38] = LoadImage("intro/menu_panel1b.png");
	buttons[39] = LoadImage("intro/menu_panel2b.png");



	var config_pic = LoadImage("menu_config.png");
	var load_pic = LoadImage("menu_load.png");
	var mouse_g = LoadImage("dot2.png")

	if (g_chosen_diff >= 0 && g_chosen_diff <= 2)
		var diff = g_chosen_diff;
	else
		var diff = 0;

	if (nw == 0)
		var cx = 32;
	else
		var cx = 0;
	var num = -1;
	
	var Text_array = new Array(8);
	Text_array[0] = "Return";
	Text_array[1] = "New Game";
	Text_array[2] = "Combat Pit";
	Text_array[3] = "Load";
	Text_array[4] = "Difficulty";
	Text_array[5] = "Music/SFX";
	Text_array[6] = "Controls";
	Text_array[7] = "Exit";
	
	var Diff_array = new Array(3);
	Diff_array[0] = "Easy:\nEnemy Dam.: -\nHp Regen:  +";
	Diff_array[1] = "Normal:\nEnemy Dam.: N\nHp Regen:  ~N";
	Diff_array[2] = "Hard:\nEnemy Dam.: +\nHp Regen:  @-\nAmmo:    @1/2";

	var diff_reminder1 = "**Difficulty changes take";
	var diff_reminder2 = "effect on new game start";
	var bg_draw = 0;
	
	var load_selected = -1;
	
	var pos_button = new Array();
	for (pb=0;pb<23;pb++)
	{
		pos_button[pb] = new Object();
		
		pos_button[pb].x = 0;
		pos_button[pb].y = 0;
		pos_button[pb].w = 0;
		pos_button[pb].h = 0;
	}

	pos_button[0].x = 100;
	pos_button[0].y = 40;
	pos_button[1].x = 100;
	pos_button[1].y = 40+17*1;
	pos_button[2].x = 100;
	pos_button[2].y = 40+17*2;
	pos_button[3].x = 100;
	pos_button[3].y = 40+17*3;
	pos_button[4].x = 100;
	pos_button[4].y = 40+17*4;
	pos_button[5].x = 100;
	pos_button[5].y = 40+17*5;
	pos_button[6].x = 150;
	pos_button[6].y = 40+17*6;
	pos_button[7].x = 150;
	pos_button[7].y = 40+17*7;
	pos_button[8].x = 60;
	pos_button[8].y = 40+17*8;

	pos_button[9].x = 260;
	pos_button[9].y = 40;
	pos_button[10].x = 260;
	pos_button[10].y = 40+17*1;
	pos_button[11].x = 260;
	pos_button[11].y = 40+17*2;
	pos_button[12].x = 260;
	pos_button[12].y = 40+17*3;
	pos_button[13].x = 260;
	pos_button[13].y = 40+17*4;
	pos_button[14].x = 260;
	pos_button[14].y = 40+17*5;
	pos_button[15].x = 260;
	pos_button[15].y = 40+17*6;
	pos_button[16].x = 260;
	pos_button[16].y = 40+17*7;
	pos_button[17].x = 195;
	pos_button[17].y = 207;

	pos_button[18].x = 225;
	pos_button[18].y = 20;
	pos_button[19].x = 245;
	pos_button[19].y = 20;
	pos_button[20].x = 275;
	pos_button[20].y = 20;

	pos_button[21].x = 115;
	pos_button[21].y = 225;

	pos_button[22].x = 221;
	pos_button[22].y = 226;
	


	for (pb=0;pb<20;pb++)
	{
		pos_button[pb].w = 32;
		pos_button[pb].h = 16;
	}
	pos_button[17].w = 16;
	pos_button[17].h = 16;
	pos_button[18].w = 16;
	pos_button[18].h = 16;
	pos_button[19].w = 16;
	pos_button[19].h = 16;
	pos_button[20].w = 24;
	pos_button[20].h = 16;
	pos_button[21].w = 24;
	pos_button[21].h = 16;
	pos_button[22].w = 78;
	pos_button[22].h = 14;
	
	
	function PlayMSound(num, voly)
	{
		if (num == 1)
		{
			if (voly == false)
				sounds[num+4].setVolume(sound_level*255);
			else if (voly == true)
				sounds[num+4].setVolume(music_level*255);
			else
				sounds[num+4].setVolume(255);
		}
	
		if (num == 2)
		{
			num = 86;
		}
		if (num == 0)
			num = 86;
			
		if (num == -4)
			num = 0;
	
		if (num < 3)
		{
		sounds[num+4].stop();
		sounds[num+4].setPosition(0);
		sounds[num+4].play(false);
		}
		else if (num == 3)
		{
			sounds[20].stop();
			sounds[20].setPosition(0);
			sounds[20].play(false);
		}
		else if (num > 80)
		{
			sounds[num].stop();
			sounds[num].setPosition(0);
			sounds[num].play(false);
		}
	}
	
	function GetNewKey(num)
	{
		while(AreKeysLeft())
			GetKey();
		if (num >=0)
		{
			if (num < 17)
			{ 
				DrawStuff(2);
				buttons[15].blit(pos_button[num].x, pos_button[num].y+cyf);
				FlipScreen();
				tempyy = GetKey();
				if (num == 0)
				  CKEY_A = tempyy;
				else if (num == 1)
				  CKEY_D = tempyy;
				else if (num == 2)
				  CKEY_W = tempyy;
				else if (num == 3)
				  CKEY_E = tempyy;
				else if (num == 4)
				  CKEY_F = tempyy;
				else if (num == 5)
				  CKEY_Q = tempyy;
				else if (num == 6)
				  CKEY_Z = tempyy;
				else if (num == 7)
				  CKEY_X = tempyy;
				else if (num == 8)
				  CKEY_SHIFT = tempyy;

				else if (num == 9)
				  CKEY_1 = tempyy;
				else if (num == 10)
				  CKEY_2 = tempyy;
				else if (num == 11)
				  CKEY_3 = tempyy;
				else if (num == 12)
				  CKEY_4 = tempyy;
				else if (num == 13)
				  CKEY_5 = tempyy;
				else if (num == 14)
				  CKEY_6 = tempyy;
				else if (num == 15)
				  CKEY_7 = tempyy;
				else if (num == 16)
				  CKEY_8 = tempyy;
				PlayMSound(2);
			}
			else if (num == 17)
			{
				//swap left and right Mouse
				if (CMOUSE_LEFT != MOUSE_LEFT)
				{
					CMOUSE_LEFT = MOUSE_LEFT;
					CMOUSE_RIGHT= MOUSE_RIGHT;
				}
				else
				{
					CMOUSE_LEFT = MOUSE_RIGHT;
					CMOUSE_RIGHT= MOUSE_LEFT;
				}
				PlayMSound(2);
			}
			else if (num == 18)
			{
				LoadDefaultInput(1);
				PlayMSound(2);
			}
			else if (num == 19)
			{
				LoadDefaultInput(2);
				PlayMSound(2);
			}
			else if (num == 20)
			{
				return -1;
			}
			else if (num == 21)
			{
				//toggle mouse management
				g_do_mouse_management=(g_do_mouse_management+1)%2 
				PlayMSound(2);
			}
			else if (num == 22)
			{
				//mouse sensitivity
				Rectangle(221, 221+cyf, Math.floor(78*((msentivity-sen_min)/(sen_max-sen_min))), 14, CreateColor(160,0,0,255));

				msentivity = Math.round(sen_range*(sen_min+(sen_max-sen_min)*(GetMouseX()-221)/78))/sen_range;
				PlayMSound(1);
			}
		}
	}

	var cyf = -10;
	
	function DrawStuff(booy)
	{
		title.blit(0,0);
		images[bg_draw].blit(0,208-images[0].height);
		if (cx == 32)
			buttons[0].blit(0,207);//0
		buttons[1].blit(cx+0,207);//1
		buttons[2].blit(cx+32,207);//2
		buttons[3].blit(cx+64,207);//3

		if (diff == 0)
			buttons[4].blit(cx+96,207);//4
		else if (diff == 1)
			buttons[5].blit(cx+96,207);//4
		else if (diff == 2)
			buttons[6].blit(cx+96,207);//4
		buttons[7].blit(cx+128,207);//5
		
		SetClippingRectangle(0,207+32-(32-9)*sound_level, GetScreenWidth(), (32-9)*sound_level);
		buttons[8].blit(cx+128,207+9);

		SetClippingRectangle(0,207+32-(32-9)*music_level, GetScreenWidth(), (32-9)*music_level);
		buttons[13].blit(cx+128,207+9);

		SetClippingRectangle(0,0, GetScreenWidth(), GetScreenHeight());
		buttons[9].blit(cx+160,207);//6
		buttons[10].blit(320-32,207);//7

		num = GetNum();

		if (booy == undefined)
		{
			if (GetNum() == 4)
			{

				if (cx==32)
				{
					buttons[38].blit(160+cx+32,207-64);
				}
				else
				{
					buttons[39].blit(160+cx+32,207-64);
				}
				//draw text here
				var diffstring="";
				if (diff == 0)
					diffstring = Diff_array[0];
				else if (diff == 1)
					diffstring = Diff_array[1];
				else if (diff == 2)
					diffstring = Diff_array[2];
				font3.drawTextBox(cx+160+4+buttons[4].width, 207-64+4, 88, 240, 0, diffstring);
				font.drawText(320-1-font.getStringWidth(diff_reminder1), 30+1,diff_reminder1);
				font.drawText(320-1-font.getStringWidth(diff_reminder2), 40+1,diff_reminder2);
			}


		}
		//redraw exit button
		buttons[10].blit(320-32,207);//7

		if (cx==32)
		{
			buttons[11].blit(160+cx+32,207);
			if (num > -1)
			{
				font.drawText(cx+196+32-4-font.getStringWidth(Text_array[num])/2, 222, Text_array[num]);
			}
		}
		else
		{
			buttons[12].blit(160+cx+32,207);
			if (num > -1)
			{
				font.drawText(cx+196+32-4+16-font.getStringWidth(Text_array[num])/2, 222, Text_array[num]);
			}
		}

		if (booy == 1)
		{
			if (GetMouseX() >= 112 && GetMouseX()<112+32 && GetMouseY()>=104 && GetMouseY() < 104+32)
				buttons[26].blit(112,104);
			else if (GetMouseX() >= 112+63 && GetMouseX()<112+32+63 && GetMouseY()>=104 && GetMouseY() < 104+32)
				buttons[27].blit(112,104);
			else
				buttons[14].blit(112,104);
		
		}
		else if (booy == 2)
		{
			window.drawWindow(20,20+cyf,280,220);
			
			//draw keys stuff here.
			buttons[15].blit(100,40+cyf);
			buttons[15].blit(100,40+17*1+cyf);
			buttons[15].blit(100,40+17*2+cyf);
			buttons[15].blit(100,40+17*3+cyf);
			buttons[15].blit(100,40+17*4+cyf);
			buttons[15].blit(100,40+17*5+cyf);
			buttons[15].blit(150,40+17*6+cyf);
			buttons[15].blit(150,40+17*7+cyf);
			buttons[15].blit(60,40+17*8+cyf);


		
			buttons[15].blit(260,40+cyf);
			buttons[15].blit(260,40+17*1+cyf);
			buttons[15].blit(260,40+17*2+cyf);
			buttons[15].blit(260,40+17*3+cyf);
			buttons[15].blit(260,40+17*4+cyf);
			buttons[15].blit(260,40+17*5+cyf);
			buttons[15].blit(260,40+17*6+cyf);
			buttons[15].blit(260,40+17*7+cyf);
			buttons[16].blit(195,207+cyf);

			buttons[18].blit(225,20+cyf);
			buttons[19].blit(245,20+cyf);
			buttons[17].blit(275,20+cyf);


			font.drawText(20, 42+cyf, "Move Left:");
			font.drawText(20, 42+17*1+cyf, "Move Right:");
			font.drawText(20, 42+17*2+cyf, "Jump:");
			font.drawText(20, 42+17*3+cyf, "Reload:");
			font.drawText(20, 42+17*4+cyf, "Use:");
			font.drawText(20, 42+17*5+cyf, "Adrenaline:");
			font.drawText(20, 42+17*6+cyf, "Weapon Cycle Left:");
			font.drawText(20, 42+17*7+cyf, "Weapon Cycle Right:");
			font.drawText(20, 42+17*8+cyf, "Sprint:");

			font.setColorMask(CreateColor(160,0,0,255));

			font.drawText(100+16-font.getStringWidth(inputs[CKEY_A])/2, 42+cyf, inputs[CKEY_A]);
			font.drawText(100+16-font.getStringWidth(inputs[CKEY_D])/2, 42+17*1+cyf, inputs[CKEY_D]);
			font.drawText(100+16-font.getStringWidth(inputs[CKEY_W])/2, 42+17*2+cyf, inputs[CKEY_W]);
			font.drawText(100+16-font.getStringWidth(inputs[CKEY_E])/2, 42+17*3+cyf, inputs[CKEY_E]);
			font.drawText(100+16-font.getStringWidth(inputs[CKEY_F])/2, 42+17*4+cyf, inputs[CKEY_F]);
			font.drawText(100+16-font.getStringWidth(inputs[CKEY_Q])/2, 42+17*5+cyf, inputs[CKEY_Q]);
			font.drawText(150+16-font.getStringWidth(inputs[CKEY_Z])/2, 42+17*6+cyf, inputs[CKEY_Z]);
			font.drawText(150+16-font.getStringWidth(inputs[CKEY_X])/2, 42+17*7+cyf, inputs[CKEY_X]);
			font.drawText(60+16-font.getStringWidth(inputs[CKEY_SHIFT])/2, 42+17*8+cyf, inputs[CKEY_SHIFT]);

				font.setColorMask(CreateColor(255,255,255,255));

			
			font.drawText(195, 42+cyf, "Weapon 1:");
			font.drawText(195, 42+17*1+cyf, "Weapon 2:");
			font.drawText(195, 42+17*2+cyf, "Weapon 3:");
			font.drawText(195, 42+17*3+cyf, "Weapon 4:");
			font.drawText(195, 42+17*4+cyf, "Weapon 5:");
			font.drawText(195, 42+17*5+cyf, "Weapon 6:");
			font.drawText(195, 42+17*6+cyf, "Weapon 7:");
			font.drawText(195, 42+17*7+cyf, "Weapon 8:");

			font.setColorMask(CreateColor(160,0,0,255));

			font.drawText(260+16-font.getStringWidth(inputs[CKEY_1])/2, 42+cyf, inputs[CKEY_1]);
			font.drawText(260+16-font.getStringWidth(inputs[CKEY_2])/2, 42+17*1+cyf, inputs[CKEY_2]);
			font.drawText(260+16-font.getStringWidth(inputs[CKEY_3])/2, 42+17*2+cyf, inputs[CKEY_3]);
			font.drawText(260+16-font.getStringWidth(inputs[CKEY_4])/2, 42+17*3+cyf, inputs[CKEY_4]);
			font.drawText(260+16-font.getStringWidth(inputs[CKEY_5])/2, 42+17*4+cyf, inputs[CKEY_5]);
			font.drawText(260+16-font.getStringWidth(inputs[CKEY_6])/2, 42+17*5+cyf, inputs[CKEY_6]);
			font.drawText(260+16-font.getStringWidth(inputs[CKEY_7])/2, 42+17*6+cyf, inputs[CKEY_7]);
			font.drawText(260+16-font.getStringWidth(inputs[CKEY_8])/2, 42+17*7+cyf, inputs[CKEY_8]);

				font.setColorMask(CreateColor(255,255,255,255));


			Rectangle(20,187,280,5, CreateColor(32,32,32,255));

			if (CMOUSE_LEFT == MOUSE_LEFT)
				font.drawText(20, 208+cyf, "Mouse Left/Right: Fire/Alt");
			else 
				font.drawText(20, 208+cyf, "Mouse Left/Right: Alt/Fire");
				
			config_pic.blit(20, 18+cyf);
			//mouse button swap option

			//exit
			font.drawText(115, 22+cyf, "Default Settings:");
			buttons[18].blit(225,20+cyf);
			buttons[19].blit(245,20+cyf);
			
			buttons[17].blit(275,20+cyf);
			

			//new!  mouse options!
			font.drawText(20, 227+cyf, "Capture Mouse:");
			buttons[34].blit(115,225+cyf);
			
			if (g_do_mouse_management)
			{
				font.setColorMask(CreateColor(160,0,0,255));
	
				font.drawText(116, 227+cyf, "Yes");
				font.setColorMask(CreateColor(255,255,255,255));
				font.drawText(145, 227+cyf, "Sensitivity: ");
				Rectangle(220, 225+cyf, 80, 16, CreateColor(0,0,0,255));
				Rectangle(221, 226+cyf, Math.floor(78*((msentivity-sen_min)/(sen_max-sen_min))), 14, CreateColor(160,0,0,255));
			}
			else
			{
				font.setColorMask(CreateColor(160,0,0,255));
				font.drawText(120, 227+cyf, "No");
				font.setColorMask(CreateColor(255,255,255,255));
//gray this out:
				font.setColorMask(CreateColor(60,60,60,255));
				font.drawText(145, 227+cyf, "Sensitivity: ");
				font.setColorMask(CreateColor(255,255,255,255));
				Rectangle(220, 225+cyf, 80, 16, CreateColor(0,0,0,255));
				Rectangle(221, 226+cyf, Math.floor(78*((msentivity-sen_min)/(sen_max-sen_min))), 14, CreateColor(60,60,60,255));
			}
		}
		else if (booy == 3)
		{
			buttons[20].blit(160-16,120-32);

			SetClippingRectangle(0,120-16+48-sound_level*48,320,sound_level*48);
			buttons[21].blit(160-16,120-16);

			SetClippingRectangle(0,120-16+48-music_level*48,320,music_level*48);
			buttons[22].blit(160-16,120-16);

			SetClippingRectangle(0,0, GetScreenWidth(), GetScreenHeight());
			
			buttons[23].blit(160-16,120-48);
			buttons[23].blit(160,120-48);

			buttons[24].blit(160-16,120+32);
			buttons[24].blit(160,120+32);

			buttons[25].blit(160-16, 120-48-20);

			buttons[17].blit(160-12, 120+56);

		}
		else if (booy == 4)
		{
			//load menu
			window.drawWindow(40,60,240,120);
			load_pic.blit(160-load_pic.width/2, 62);


			if (load_selected>=-1)
			{
				if (load_selected == -1)
					Rectangle(43, 88+load_selected*20, buttons[30].width+4, buttons[30].height+4, CreateColor(32,160,64,255))
				else
					Rectangle(43, 88+load_selected*20, buttons[28].width+4, buttons[28].height+4, CreateColor(32,160,64,255))
			}

			buttons[30].blit(45,70);
			if (files[0].level != 1)
			{
				font.drawText(47,72, files[-1].name);
			}
			else
				font.drawText(47,72, "empty");


			buttons[28].blit(45,90);
			if (files[0].level != 1)
			{
				font.drawText(47,92, files[0].name);
			}
			else
				font.drawText(47,92, "empty");

			buttons[28].blit(45,110);
			if (files[1].level != 1)
			{
				font.drawText(47,112, files[1].name);
			}
			else
				font.drawText(47,112, "empty");

			buttons[28].blit(45,130);
			if (files[2].level != 1)
			{
				font.drawText(47,132, files[2].name);
			}
			else
				font.drawText(47,132, "empty");
			
			buttons[28].blit(45,150);
			if (files[3].level != 1)
			{
				font.drawText(47,152, files[3].name);
			}
			else
				font.drawText(47,152, "empty");

			//selected info
			if (load_selected > -2)
			{
				if (files[load_selected].level > 1)
				{
					font.drawText(140, 90, "Level: "+ldis[files[load_selected].level]);
					font.drawText(140, 105, "Kills: "+files[load_selected].kills);
					font.drawText(140, 120, "Score: "+files[load_selected].score);
					font.drawText(140, 135, "Difficulty: "+diff_array[files[load_selected].diff]);
					font.drawText(140, 150, "Date: "+files[load_selected].year+"/"+files[load_selected].month+"/"+files[load_selected].day+"/"+files[load_selected].hours+":"+files[load_selected].minutes);
				}
			}

			buttons[17].blit(160-12, 110+56);
			buttons[29].blit(270-12, 110+56);
		}
		else if (booy == 5)
		{
			if (GetMouseX() >= 112 && GetMouseX()<112+32 && GetMouseY()>=104 && GetMouseY() < 104+32)
				buttons[32].blit(112,104);
			else if (GetMouseX() >= 112+63 && GetMouseX()<112+32+63 && GetMouseY()>=104 && GetMouseY() < 104+32)
				buttons[33].blit(112,104);
			else
				buttons[31].blit(112,104);
		}
		else if (booy == 6)
		{
			if (GetMouseX() >= 112 && GetMouseX()<112+32 && GetMouseY()>=104 && GetMouseY() < 104+32)
				buttons[36].blit(112,104);
			else if (GetMouseX() >= 112+63 && GetMouseX()<112+32+63 && GetMouseY()>=104 && GetMouseY() < 104+32)
				buttons[37].blit(112,104);
			else
				buttons[35].blit(112,104);
		}
		
		mouse_g.blit(GetMouseX(),GetMouseY());
	}
	
	function MouseIn()
	{	
		for (pb=0;pb<=22;pb++)
		{
			if (GetMouseX()>pos_button[pb].x && GetMouseX()<pos_button[pb].x+pos_button[pb].w &&
					GetMouseY()>pos_button[pb].y+cyf && GetMouseY()<pos_button[pb].y+pos_button[pb].h+cyf)
				return pb;
		}
		return -1;
	}
	
	var tempyy = 0;
	
	function GetNum()
	{
	  var rtn = -1;
		if (GetMouseY()>=207)
		{
			if (cx > 0)
			{
				if (GetMouseX()<32)
				  rtn = 0;
				else if (GetMouseX()<64)
				  rtn = 1;
				else if (GetMouseX()<96)
				  rtn = 2;
				else if (GetMouseX()<128)
				  rtn = 3;
				else if (GetMouseX()<160)
				  rtn = 4;
				else if (GetMouseX()<192)
				  rtn = 5;
				else if (GetMouseX()<224)
				  rtn = 6;
				else if (GetMouseX()>288)
				  rtn = 7;
			}
			else
			{
				if (GetMouseX()<32)
				  rtn = 1;
				else if (GetMouseX()<64)
				  rtn = 2;
				else if (GetMouseX()<96)
				  rtn = 3;
				else if (GetMouseX()<128)
				  rtn = 4;
				else if (GetMouseX()<160)
				  rtn = 5;
				else if (GetMouseX()<192)
				  rtn = 6;
				else if (GetMouseX()>288)
				  rtn = 7;
			}
		}
		return rtn;
	}
	
	var SetClick = false;
	var MenuChoice;
	
	function GetInput(booy)
	{

		while(AreKeysLeft())
		{
			if (GetKey() == KEY_ESCAPE)
			{
				if (booy == 1)
				{
					return 0;
				}
				else if (booy == 2)
				{
					PlayMSound(0);
					SaveKeys();
					return 0;
				}
				else if (booy == 3)
				{
					PlayMSound(0);
					return 0;				
				}
				else if (booy == 4)
				{
					PlayMSound(0);
					return 0;				
				}
				else if (booy == 5)
				{
					return 0;
				}
				else if (booy == 6)
				{
					return 0;
				}
				else
				{
					if (cx > 0)
					{
						//return to game
						return -1;
					}
				}
			}
		}
	
	
		if (IsMouseButtonPressed(MOUSE_LEFT))
		{
			if (SetClick == true)
			{
				if (booy == 1)
				{
					if (GetMouseY()> 120-16 && GetMouseY()<120+16)
					{
						if (GetMouseX()<160-16 && GetMouseX()>160-48)
						{
							PlayMSound(0);
						  MenuChoice = 7;
						  counter = 51;
						  return 1;
							//quit
						}
						else if (GetMouseX()>160+16 && GetMouseX()<160+48)
						{
							return 0;
						}
					}
					return -1;
				}
				else if (booy == 2)
				{
					if (GetNewKey(MouseIn())==-1)
					{
						PlayMSound(0);
						SaveKeys();
						return 0;
					}
				}
				else if (booy == 3)
				{
					if (GetMouseX() > 160-16 && GetMouseX()<160+16)
					{
					  if (GetMouseX() < 160)
					  {
							//music level
							if (GetMouseY() > 120-48 && GetMouseY()<120-32)
							{
								PlayMSound(1,true);
								music_level+=0.1;
								if (music_level>1)
									music_level = 1;
							}
							else if (GetMouseY() > 120+32 && GetMouseY()<120+48)
							{
								PlayMSound(1,true);
								music_level-=0.1;
								if (music_level<0)
									music_level = 0;
							}
					  }
					  else
					  {
							if (GetMouseY() > 120-48 && GetMouseY()<120-32)
							{
								sound_level+=0.1;
								if (sound_level>1)
									sound_level = 1;
								PlayMSound(1, false);
							}
							else if (GetMouseY() > 120+32 && GetMouseY()<120+48)
							{
								sound_level-=0.1;
								if (sound_level<0)
									sound_level = 0;
								PlayMSound(1, false);
							}
					  }
					}

					if (GetMouseX()>160-12 && GetMouseX()<160+12 && GetMouseY()>120+56 && GetMouseY() < 120+56+16)
					{
						PlayMSound(0);
						return 0;
					}
				
				}
				else if (booy == 4)
				{
					//load menu here.

					if (GetMouseX()>160-12 && GetMouseX()<160+12 && GetMouseY()>110+56 && GetMouseY() < 110+56+16)
					{
						PlayMSound(0);
						return 0;
					}

					if (GetMouseX()>270-12 && GetMouseX()<270+12 && GetMouseY()>110+56 && GetMouseY() < 110+56+16)
					{
						if (load_selected >-2)
						{
							if (files[load_selected].level > 1)
							{
								PlayMSound(2);
								return 1;
							}
							else
							{
								PlayMSound(3);
							}
						}
						else
						{
							PlayMSound(3);
						}
					}
					
					if (GetMouseX() > 45 && GetMouseX()<45+buttons[28].width)
					{
						for (sts = -1;sts <4;sts++)
						{
							if (GetMouseY()>90+20*sts && GetMouseY()<90+20*sts+buttons[28].height)
							{
								load_selected = sts;
								PlayMSound(2);
								break;
							}
						}
					}
				}
				else if (booy == 5)
				{
					if (GetMouseY()> 120-16 && GetMouseY()<120+16)
					{
						if (GetMouseX()<160-16 && GetMouseX()>160-48)
						{
							return 1;
						}
						else if (GetMouseX()>160+16 && GetMouseX()<160+48)
						{
							return 0;
						}
					}
					return -1;
				}
				else if (booy == 6)
				{
					if (GetMouseY()> 120-16 && GetMouseY()<120+16)
					{
						if (GetMouseX()<160-16 && GetMouseX()>160-48)
						{
							return 1;
						}
						else if (GetMouseX()>160+16 && GetMouseX()<160+48)
						{
							return 0;
						}
					}
					return -1;
				}
				else
				{
					MenuChoice = GetNum();
					if (MenuChoice > -1)
					{
						if (MenuChoice == 1 && cx == 32)
						{
							SetClick = false;
							PlayMSound(-4);
							if (NewGameMenu() == 1)
							{
								MenuChoice = 1;
								counter = 51;
							}
						}
						else if (MenuChoice == 2 && cx == 32)
						{
							SetClick = false;
							PlayMSound(-4);
							if (NewTrainMenu() == 1)
							{
								MenuChoice = 2;
								counter = 51;
							}
						}
						else if (MenuChoice < 3)
						{
							MenuChoice = GetNum();
							counter = 51;
							//return
							//start new
							//training.
						}
						else if (MenuChoice == 3)
						{
							//load
							SetClick = false;
							PlayMSound(-4);
							load_selected = -2;
							if (LoadMenu() == 3)
								counter = 51;
						}
						else if (MenuChoice == 4)
						{
							//diff
							diff = (diff+1)%3;
							//g_chosen_diff = diff;
							PlayMSound(-4);
						}
						else if (MenuChoice == 5)
						{
							SetClick = false;
							PlayMSound(-4);
							SoundMenu();
						}
						else if (MenuChoice == 6)
						{
							SetClick = false;
							PlayMSound(-4);
							ControlMenu();
							//controls
						}
						else if (MenuChoice == 7)
						{
							//quit
							SetClick = false;
							PlayMSound(-4);
							QuitMenu();
						}
					}
				}
				
				SetClick = false;
			}
			else
				SetClick = false;
		}
		if (!IsMouseButtonPressed(MOUSE_LEFT))
		{
			SetClick = true;
		}
	}
	var counter = 0;
	
	function QuitMenu()
	{
		while(true)
		{
			DrawStuff(1);
			FlipScreen();
			if (GetInput(1) > -1)
			{
				PlayMSound(0);
			  return;
			}

			while(GetTime()-lasttime < frate);
			lasttime = GetTime();
		}
	}

	function NewGameMenu()
	{
		var nuu = -1;
		while(true)
		{
			DrawStuff(5);
			FlipScreen();
			nuu=GetInput(5);
			if (nuu > -1)
			{
				PlayMSound(0);
				if (nuu == 0)
					return 0;
				if (nuu == 1)
					return 1
			}

			while(GetTime()-lasttime < frate);
			lasttime = GetTime();
		}
	}

	function NewTrainMenu()
	{
		var nuu = -1;
		while(true)
		{
			DrawStuff(6);
			FlipScreen();
			nuu=GetInput(6);
			if (nuu > -1)
			{
				PlayMSound(0);
				if (nuu == 0)
					return 0;
				if (nuu == 1)
					return 1
			}

			while(GetTime()-lasttime < frate);
			lasttime = GetTime();
		}
	}
	function ControlMenu()
	{
		while(true)
		{
			DrawStuff(2);
			FlipScreen();
			if (GetInput(2) > -1)
			{
//				PlayMSound(2);
			  return;
			}

			while(GetTime()-lasttime < frate);
			lasttime = GetTime();
		}
	}
	function LoadMenu()
	{
		var funy = 0;
		while(true)
		{
			DrawStuff(4);
			FlipScreen();
			funy = GetInput(4);
			if (funy == 0)
			{
				PlayMSound(0);
			  return;
			}
			else if (funy == 1)
			{
			  return 3;
			}

			while(GetTime()-lasttime < frate);
			lasttime = GetTime();
		}
	}

	function SoundMenu()
	{
		while(true)
		{
			DrawStuff(3);
			FlipScreen();
			if (GetInput(3) > -1)
			{
				SaveSoundConfig();
//				PlayMSound(2);
			  return;
			}

			while(GetTime()-lasttime < frate);
			lasttime = GetTime();
		}
	}

	var lasttime = GetTime();
	var frate = 1000/20;
	
	if (nw == 0)
		counter = 50;

	while(counter<=100)
	{
		DrawStuff();
		if (counter < 50)
		{
			counter+=2;
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*counter/50));
		}
		else if (counter > 50)
		{
			if (MenuChoice == 0)
				counter = 101;
			else
			{
				if (counter == 51 && MenuChoice == 1)
					lets_go.play(false);
	
				if (counter %2 == 0 && bg_draw<11 && MenuChoice ==1)
				{
					bg_draw++;
				}
				else if (MenuChoice !=1 && counter <75)
					counter = 75;
				counter++;
				if (counter >= 75)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*(counter-75)/25));
			}
		}

		FlipScreen();
		if (counter == 50)
		{
			if (GetInput() == -1)
				return 0;
		}
		while(GetTime()-lasttime < frate);
		lasttime = GetTime();
	}
	
	if (MenuChoice == 0)
		return 0;//return to game
	else if (MenuChoice == 1)
	{
		g_chosen_diff = diff;
		return 1;//new game
	}
	else if (MenuChoice == 2)
	{
		if (nw == 0)
		{
			//loaded during gameplay
			WriteResetInfo(-5);
			RestartGame();
		}
		else
		{
			WriteResetInfo(-5);
			RestartGame();
		}
	}
	else if (MenuChoice == 3)
	{
	
		var stfile = OpenFile("../game.sgm");
		stfile.write("asf", load_selected);
		stfile.flush();

		stfile.close();	



		cur_weap = files[load_selected].cur_weap;
		g_current_level = files[load_selected].level;
		g_total_kills = files[load_selected].kills;
		g_total_time = files[load_selected].totaltime;
		g_score = files[load_selected].score;
		g_diff = files[load_selected].diff;
		g_chosen_diff = g_diff;

		for (sts = 0; sts < 14;sts++)
		{
			ammo_array[sts].curammo = files[load_selected]["ammo"+sts];
			ammo_array[sts].curclip = files[load_selected]["clip"+sts];
		}
		for (sts = 0; sts < 16;sts++)
		{
			weap_possess[sts] = files[load_selected]["weap"+sts];
		}

		g_reset_load = load_selected;
		
		if (nw == 0)
		{
			//loaded during gameplay
			WriteResetInfo(load_selected);
			RestartGame();
		}
		else
			return 3;//load game
	}
	else if (MenuChoice == 7)
		return 7;//exit game
}