---------------------------------------------------------------------------------------------------
--  Copyright © 2009 PoBros, Incorporated. All Rights Reserved.
--
--  This source code is the proprietary confidential property of PoBros Incorporated.
--  Modification; reproduction; publication; or distribution in any form to any party 
--  other than the licensee is strictly prohibited.
--
--  FOR USE BY AUTHORIZED PERSONS ONLY
---------------------------------------------------------------------------------------------------

require( "scripts/style.lua" );

local kButtonY = kMax - 50;
local kButtonX = 260;
local kTextY = 495;
local kCharacterFadeTime = 300;
local kCharacterTotalDelay = 500;
local kCharacterInitialDelay = 500;
local kCharacterDelay = 100;

---------------------------------------------------------------------------------------------------

function CheckUsers()
	if( GetNumUsers() == 0 ) then
		name = DisplayDialog
			   {
				   "scripts/createplayer.lua",
				   nocancel = true
			   };
		
		if( not( name == nil ) ) then
			CreateNewUser( name );
			NewGame();
			LoadProfile();
			SetWelcomeName();
			return true;
		end
		
		return false;
	else
		return true;
	end
end

---------------------------------------------------------------------------------------------------

MakeDialog
{
	name = "mainmenu";
	SetStyle( GameWindowStyle );
	
	MainMenu
	{
		scrim = false;
		music = gs_musicMainMenu;
		fade = true;
		popchildren = true;
		SetStyle( DefaultStyle );
		
		Bitmap
		{
			image = "backgrounds/mainmenu";
			
			ParticleWindow
			{
				x = 0;
				y = 0;
				w = kMax;
				h = kMax;
				script = "scripts/particles/mainmenu.lua";
				initialstarttime = 0;
				
				data =
				{
					dLocus = { 400, 300 };
					dWidth = { 400 };
					dHeight = { 50 };
				};
			};

			AnimatedBitmap
			{
				image = "animations/godray";
				name = "godray";
				x = 0;
				y = 0;
				flags = kHAlignLeft + kVAlignTop;
				xcenter = 0;
				ycenter = 0;
				framespeed = 1500;
				crossfade = true;
				animation = "wrap";
			};

			AnimatedBitmap
			{
				image = "backgrounds/char_cyril";
				name = "char3";
				x = 485;
				y = 164;
				flags = kHAlignLeft + kVAlignTop;
				xcenter = 0;
				ycenter = 0;
				initialalpha = 0;

				fade =
				{
					beginopacity = 0;
					endopacity = 1;
					duration = kCharacterFadeTime;
					initialdelay = kCharacterTotalDelay + kCharacterInitialDelay + ( kCharacterDelay * 2 );
				};
			};
			
			AnimatedBitmap
			{
				image = "backgrounds/char_cia";
				name = "char4";
				x = 373;
				y = 112;
				flags = kHAlignLeft + kVAlignTop;
				xcenter = 0;
				ycenter = 0;
				initialalpha = 0;
				
				fade =
				{
					beginopacity = 0;
					endopacity = 1;
					duration = kCharacterFadeTime;
					initialdelay = kCharacterTotalDelay + kCharacterInitialDelay + ( kCharacterDelay * 1 );
				};
			};
			
			AnimatedBitmap
			{
				image = "backgrounds/char_gramps";
				name = "char2";
				x = 156;
				y = 130;
				flags = kHAlignLeft + kVAlignTop;
				xcenter = 0;
				ycenter = 0;
				initialalpha = 0;
				
				fade =
				{
					beginopacity = 0;
					endopacity = 1;
					duration = kCharacterFadeTime;
					initialdelay = kCharacterTotalDelay + kCharacterInitialDelay;
				};
			};
			
			ParticleWindow
			{
				x = 0;
				y = 0;
				w = kMax;
				h = kMax;
				script = "scripts/particles/mainmenu.lua";
				initialstarttime = 0;
				
				data =
				{
					dLocus = { 400, 475 };
					dWidth = { 400 };
					dHeight = { 50 };
				};
			};
			
			AnimatedBitmap
			{
				image = "backgrounds/char_autumn";
				name = "char1";
				x = 211;
				y = 160;
				flags = kHAlignLeft + kVAlignTop;
				xcenter = 0;
				ycenter = 0;
				initialalpha = 0;
				
				fade =
				{
					beginopacity = 0;
					endopacity = 1;
					duration = kCharacterFadeTime;
					initialdelay = kCharacterTotalDelay;
				};
			};
			
			Bitmap
			{
				image = "backgrounds/table";
				x = 0;
				y = 172;
			};

			AnimatedBitmap
			{
				image = "animations/globe";
				x = 414;
				y = 343;
				framespeed = 100;
				animation = "wrap";
			};

			Text
			{
				font = WelcomeFont;
				name = "welcome";
				label = "welcome";
				x = kCenter;
				y = kTextY - 10;
				w = 300;
				h = 50;
				flags = kHAlignCenter + kVAlignTop;
			};
			
			SetStyle( ButtonStyle );
			
			Button
			{
				x = kCenter;
				y = kTextY;
				font = WelcomeFont;
				fontrollover = WelcomeHilightFont;
				fontclick = WelcomeHilightFont;
				name = "changeplayer";
				text = "changeplayer";
				scaletext = false;
				buttongraphics = {};
				w = 180;
				h = 30;
				soundenter = "";
				soundexit = "";
				
				command =
					function()
						DoModal( "scripts/selectplayer.lua" );
					end
			};
			
			Button
			{
				x = kCenter;
				y = kTextY + 27;
				name = "top10clickhere";
				text = "top10clickhere";
				font = WelcomeFont;
				fontrollover = WelcomeHilightFont;
				fontclick = WelcomeHilightFont;
				buttongraphics = {};
				w = 180;
				h = 20;
				soundenter = "";
				soundexit = "";

				command =
					function()
						DoModal( "scripts/highscore.lua" );
					end
			};

			Button
			{
				x = kCenter;
				y = kButtonY;
				name = "play";
				text = "play";
				flags = kHAlignCenter + kVAlignCenter;
				buttongraphics = MediumButtonGraphics;

				command =
					function()
						if( CheckUsers() ) then
							if( CheckForValidLicense() ) then
								gamestate = GetGameState();
								
								if( gamestate == 0 ) then
									SwapWithStoryboard( "mainmenu" );
								else
									action = DoModal( "scripts/continuegame.lua" );
									
									if( action == "continue" ) then
										if( gamestate == 1 ) then
											FadeOut( "SwapToModal( 'scripts/map.lua' );" );
										elseif( gamestate == 2 ) then
											SwapWithStoryboard( "mainmenu" );
										elseif( gamestate == 3 ) then
											FadeOut( "SwapToModal( 'scripts/gameplay.lua' );" );
										end
									elseif( action == "startnew" ) then
										NewGame();
										SwapWithStoryboard( "mainmenu" );
									end
								end
							end
						end
					end
			};
			
			Button
			{
				x = kCenter - kButtonX;
				y = kButtonY;
				name = "options";
				text = "options";
				flags = kHAlignCenter + kVAlignCenter;

				command =
					function()
						DoModal( "scripts/options.lua" );
					end
			};
			
			Button
			{
				x = kCenter + kButtonX;
				y = kButtonY;
				name = "exit";
				text = "exit";
				flags = kHAlignCenter + kVAlignCenter;

				command =
					function()
						DisplayQuitVerify();
					end
			};
		};
	};
};

---------------------------------------------------------------------------------------------------

SetWelcomeName();