#define MAINMENU_TIME_DELAY		45

string isUsersName="";

void InitInterface(string iniName)
{
    GameInterface.title = "# ";
	SetEventHandler("CriptData","DatSet",0);
	SetEventHandler("evntCheckFailure","ProcStop",0);

	SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);

	SetSelectable("B_NEWGAME",sti(InterfaceStates.Buttons.New.enable));
	SetSelectable("B_RESUMEGAME",sti(InterfaceStates.Buttons.Resume.enable));
	SetSelectable("B_LOAD",sti(InterfaceStates.Buttons.Load.enable));
	SetSelectable("B_SAVE",sti(InterfaceStates.Buttons.Save.enable));
	SetSelectable("B_OPTIONS",sti(InterfaceStates.Buttons.Options.enable));
	SetSelectable("B_CONTROLS",sti(InterfaceStates.Buttons.Controls.enable));
	SetSelectable("B_CREDITS",sti(InterfaceStates.Buttons.Credits.enable));
	SetSelectable("B_QUIT",sti(InterfaceStates.Buttons.Quit.enable));

	if(bSeaActive) SetSelectable("B_SAVE",false);
	if(LAi_IsBoardingProcess() || bMutinyDeckStarted) SetSelectable("B_SAVE",false);

	SendMessage(&GameInterface,"lsls",MSG_INTERFACE_MSG_TO_NODE,"BACKPICTURE",3, "interfaces\mainback");
	SendMessage(&GameInterface,"lsls",MSG_INTERFACE_MSG_TO_NODE,"B_BUILD",0,XI_ConvertString("AboutBuild")); // MAXIMUS 07.08.2006

    string currentversion = " - BUILD " + BUILDVERSION;
	CreateString(true,"Version","Pirates Ahoy! Community" + currentversion,FONT_QUESTBOOK,COLOR_GREEN_LIGHT,-20,430,SCRIPT_ALIGN_LEFT,0.8);
    CreateString(true,"Info","http://"+PA_INET,FONT_QUESTBOOK,COLOR_BLUE_LIGHT,-20,470,SCRIPT_ALIGN_LEFT,0.8);
    CreateString(true,"Name",isUsersName,FONT_NORMAL,COLOR_NORMAL,20,430,SCRIPT_ALIGN_LEFT,0.5);

	//MAXIMUS -->
	SetNodeUsing("P_1",false);
	SetNodeUsing("P_2",false);
	SetNodeUsing("P_3",false);
	if(bNewInterface)
	{
		SetNodeUsing("SEADOGSLOGO_RUS",false);
		SetNodeUsing("LOGOGLOWS_RUS",false);
		SetNodeUsing("SEADOGSLOGO",false);
		SetNodeUsing("LOGOGLOWS",false);
		SetNodeUsing("RAMKA",false);

		SetNodeUsing("LOGOGLOWS_SPECIAL",true);
		SetNodeUsing("LOGO_RUS",true);
		SetNodeUsing("LOGO",true);
		SetNodeUsing("TV",true);

		SetNewVideoPicture("TV", "back");
		SetNewPicture("BACKGROUND","interfaces\background\frame.tga");
		if(sti(InterfaceStates.Buttons.Resume.enable))
		{
			SetNodeUsing("P_3",true);
			SetNewPicture("P_3", "interfaces\portraits\256\face_"+GetFacePicture(GetMainCharacterIndex())+".tga");
		}
		else
		{
			string bNode = "P_"+sti(Rand(2)+1);
			SetNodeUsing(bNode,true);
			switch(bNode)
			{
				case "P_1": SetNewPicture(bNode, "interfaces\\background\\"+GetRandSubString("compass,anchor,treasure")+".tga"); break;
				case "P_2": SetNewPicture(bNode, "loading\\"+GetRandSubString("mutiny,mutiny_death,prisoner,landing")+".tga"); break;
				case "P_3": SetNewPicture(bNode, "interfaces\\background\\"+GetRandSubString("sails,ransack,port,fort_death_1,cargo,cannons_fire,battle,cabin,aboardage")+".tga"); break;
			}
			GameInterface.mainmenu.timeclock1 = 10;
			SetEventHandler("evntTime","TimeCalculate",0);
			PostEvent("evntTime", 500);
		}
		CreateString(true,"VersionInfo","Pirates Ahoy! Community",FONT_NORMAL,COLOR_NORMAL,330,405,SCRIPT_ALIGN_CENTER,0.8);
		CreateString(true,"Version","BUILD " + BUILDVERSION,"DIALOG2",COLOR_MONEY,330,427,SCRIPT_ALIGN_CENTER,0.8);
		CreateString(true,"Info","http://"+PA_INET,FONT_NORMAL,COLOR_NORMAL,330,455,SCRIPT_ALIGN_CENTER,0.8);

		switch(LanguageGetLanguage())
		{
			case "Russian":
				CreateImage("LogoPicture","POTC_LOGO_"+LanguageGetLanguage(),"seadogslogo",129,-30,541,24);
			break;

			CreateImage("LogoPicture","POTC_LOGO_ENGLISH","seadogslogo",129,-30,541,24);
		}
	}
	//MAXIMUS <--

	SetEventHandler("InterfaceBreak","ProcessBreakExit",0)
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("ConfirmYes","ProcessDelete_yes",0);
	SetEventHandler("ConfirmNo","ProcessDelete_no",0);
	SetEventHandler("ResumeGamePress","ResumeGamePress",0);
	SetEventHandler("QuitPress","QuitPress",0);

	SetEventHandler("evntLowStorageBreak","LowStorageNext",0);

	SetEventHandler("UpPress","UpPress",0);
	SetEventHandler("DownPress","DownPress",0);
	SetEventHandler("ievnt_command","ProcCommand",0);
	SetEventHandler("BreakExit","procBreakExit",0);

	string saveName;
	GameInterface.SavePath = DEFAULT_PATH; // MAXIMUS -[new interface options]-
	int ntmp;
	SetSelectable("B_LOAD",SendMessage(&GameInterface,"llee",MSG_INTERFACE_SAVE_FILE_FIND,0,&saveName,&ntmp)!=0);
	SetCurrentUpNode("B_SAVE");
	if(sti(InterfaceStates.Buttons.Resume.enable)) { SetCurrentNode("B_RESUMEGAME"); }//MAXIMUS

	if(!sti(InterfaceStates.Buttons.Resume.enable) && !SKIP_LOGOVIDEO)//MAXIMUS
	{
		GameInterface.mainmenu.timeclock = MAINMENU_TIME_DELAY;
		SetEventHandler("evntTimeCalculate","procTimeCalculate",0);
		PostEvent("evntTimeCalculate",1000);
// KK -->
		if (GetMusicScheme() != "PGMUS") SetMusic("music_main_menu"); // PG // NK 04-09-17 because it's handled now in the alias ini // KK
	}
	// fix music handling 05-04-29
	if(GetMusicScheme() == "PGMUS")  // KK
	{
		ref pch = GetMainCharacter();
		pch.musfix.old = oldMusicName;
		pch.musfix.oldid = oldMusicID;
		pch.musfix.cur = musicName;
		pch.musfix.curid = musicID;
		SetMusicNoPause("music_main_menu"); // PG // NK 04-09-17 ditto
	}
	PostEvent("DoInfoShower",1,"sl","MainMenuLaunch",false);
	SetEventHandler("CheckAgain","procCheckAgain",0);
	SetEventHandler("CheckCancel","procCheckCancel",0);
}

void UpPress()
{
	SetCurrentUpNode(GetCurrentNode());
}

void DownPress()
{
	SetCurrentDownNode(GetCurrentNode());
}

void SetCurrentUpNode(string curNode)
{
	string nextNode="";

	switch(curNode)
	{
	case "B_NEWGAME":		nextNode = "B_QUIT";		break;
	case "B_RESUMEGAME":	nextNode = "B_NEWGAME";		break;
	case "B_LOAD":			nextNode = "B_RESUMEGAME";	break;
	case "B_SAVE":			nextNode = "B_LOAD";		break;
	case "B_OPTIONS":		nextNode = "B_SAVE";		break;
	case "B_CONTROLS":		nextNode = "B_OPTIONS";		break;
	case "B_BUILD":			nextNode = "B_CONTROLS";	break;		// added by KAM
	case "B_CREDITS":		nextNode = "B_BUILD";		break;		// changed by KAM
	case "B_QUIT":			nextNode = "B_CREDITS";		break;
	}

	if(GetSelectable(nextNode))
	{
		SetCurrentNode(nextNode);
	}
	else
	{
		if(nextNode!=GetCurrentNode())
		{
			SetCurrentUpNode(nextNode);
		}
	}
}

void SetCurrentDownNode(string curNode)
{
	string nextNod;

	switch(curNode)
	{
	case "B_NEWGAME":		nextNod = "B_RESUMEGAME";	break;
	case "B_RESUMEGAME":	nextNod = "B_LOAD";			break;
	case "B_LOAD":			nextNod = "B_SAVE";			break;
	case "B_SAVE":			nextNod = "B_OPTIONS";		break;
	case "B_OPTIONS":		nextNod = "B_CONTROLS";		break;
	case "B_CONTROLS":		nextNod = "B_BUILD";		break;		//changed by KAM
	case "B_BUILD":			nextNod = "B_CREDITS";		break;		//added by KAM
	case "B_CREDITS":		nextNod = "B_QUIT";			break;
	case "B_QUIT":			nextNod = "B_NEWGAME";		break;
	}

	if(GetSelectable(nextNod))
	{
		SetCurrentNode(nextNod);
	}
	else
	{
		if(nextNod!=GetCurrentNode())
		{
			SetCurrentDownNode(nextNod);
		}
	}
}

void ProcessCancelExit()
{
	IDoExit( RC_INTERFACE_MAIN_MENU_EXIT, true );
	if(!sti(InterfaceStates.Buttons.Resume.enable))
	{
		ResetSound();
	}
}

void ProcessBreakExit()
{
	if( GetSelectable("B_RESUMEGAME") )	IDoExit( RC_INTERFACE_MAIN_MENU_EXIT, true );
}

void NewGamePress()
{
	IDoExit( RC_INTERFACE_DO_START, false ); // added by MAXIMUS [choose character MOD]
}

void NewGameConfirm()
{
	if( sti(InterfaceStates.Buttons.Resume.enable) )
	{
		SetNodeUsing("CONFIRM_RECTANGLE",true);
		SetNodeUsing("TEXTWINDOW",true);
		SetNodeUsing("CONFIRM_YES_BUTTON",true);
		SetNodeUsing("CONFIRM_NO_BUTTON",true);
		SetNodeUsing("EN_PIC",true);
		SetCurrentNode("CONFIRM_NO_BUTTON");
		SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "CONFIRM_NO_BUTTON");
		SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "CONFIRM_YES_BUTTON");

		int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
//		SetFormatedText("TEXTWINDOW",LanguageConvertString(tmpLangFileID,"New game confirm"));
		LanguageCloseFile(tmpLangFileID);
	}
	else NewGamePress();
}

void ProcessDelete_no()
{
	SetNodeUsing("CONFIRM_RECTANGLE",false);
	SetNodeUsing("TEXTWINDOW",false);
	SetNodeUsing("CONFIRM_YES_BUTTON",false);
	SetNodeUsing("CONFIRM_NO_BUTTON",false);
	SetNodeUsing("EN_PIC",false);
	SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);

	SetCurrentNode("B_RESUMEGAME");
}

void ProcessDelete_yes()
{
	SetNodeUsing("CONFIRM_RECTANGLE",false);
	SetNodeUsing("TEXTWINDOW",false);
	SetNodeUsing("CONFIRM_YES_BUTTON",false);
	SetNodeUsing("CONFIRM_NO_BUTTON",false);
	SetNodeUsing("EN_PIC",false);
	SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);

	NewGamePress();
}

void LoadPress()
{
	IDoExit( RC_INTERFACE_DO_LOAD_GAME, false );
}

void SavePress()
{
	IDoExit( RC_INTERFACE_DO_SAVE_GAME, false );
}

void OptionsPress()
{
	IDoExit( RC_INTERFACE_DO_OPTIONS, false );
}

void ControlsPress()
{
	IDoExit( RC_INTERFACE_DO_CONTROLS, false );
}

// added by MAXIMUS -->
void AboutBuildPress()
{
	IDoExit( RC_INTERFACE_DO_ABOUT_BUILD, false );
}
// added by MAXIMUS <--

void CreditsPress()
{
	SetEventHandler(EVENT_END_VIDEO,"LaunchMainMenu_afterVideo",0);
	bMainMenuLaunchAfterVideo = true;
// KK -->
	switch (LanguageGetLanguage()) {
		case "Polish":
			StartPostVideo("credits_pol",1);
		break;
		// default:
			StartPostVideo("credits",1);
	}
// <-- KK
}

void QuitPress()
{
	if(IsEntity(&worldMap)) DelEventHandler("WorldMap_DeleteShipEncounter", "wdmEvent_DeleteShipEncounter");
	ExitProgram();
}

void ResumeGamePress()
{
	IDoExit( RC_INTERFACE_DO_RESUME_GAME, true );
}

void IDoExit(int exitCode, bool bClear)
{
	if(bClear==true && GetSelectable("B_RESUMEGAME")) ClearScreenShoter();//MAXIMUS: used for QuickSave
	// if(PGMUS) SetMusic(oldMusicName); // PG
	// fix music handling 05-04-29
	ref pch = GetMainCharacter();
	if(CheckAttribute(pch,"musfix"))
	{
		SetMusicNoPause(pch.musfix.cur);
		oldMusicName = pch.musfix.old;
		//oldMusicID = pch.musfix.old.id;
		DeleteAttribute(pch,"musfix");
	}
	// NK <--
	DelEventHandler("InterfaceBreak","ProcessBreakExit");
	DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("ConfirmYes","ProcessDelete_yes");
	DelEventHandler("ConfirmNo","ProcessDelete_no");
	DelEventHandler("ResumeGamePress","ResumeGamePress");
	DelEventHandler("QuitPress","QuitPress");
	DelEventHandler("evntLowStorageBreak","LowStorageNext");
	DelEventHandler("CriptData","DatSet");
	DelEventHandler("evntCheckFailure","ProcStop");
	DelEventHandler("UpPress","UpPress");
	DelEventHandler("DownPress","DownPress");
	DelEventHandler("ievnt_command","ProcCommand");
	DelEventHandler("evntTimeCalculate","procTimeCalculate");
	DelEventHandler("BreakExit","procBreakExit");
	DelEventHandler("CheckAgain","procCheckAgain");
	DelEventHandler("evntTime","TimeCalculate");//MAXIMUS

	interfaceResultCommand = exitCode;
	EndCancelInterface(bClear);
}

void DatSet()
{
	isUsersName = GetEventData();
}

void procTimeCalculate()
{
	int n = sti(GameInterface.mainmenu.timeclock) - 1;

	if(n>0)
	{
		GameInterface.mainmenu.timeclock = n;
		PostEvent("evntTimeCalculate",1000);
		return;
	}

	if(!SKIP_LOGOVIDEO)
	{
		SetEventHandler(EVENT_END_VIDEO,"LaunchMainMenu_afterVideo",0);
		StartPostVideo("trailer",1);
		SetEventHandler("Control Activation","procAttractExit",1);
	}
}

void TimeCalculate()
{
	int n = sti(GameInterface.mainmenu.timeclock1) - 1;

	if(n>0)
	{
		GameInterface.mainmenu.timeclock1 = n;
		PostEvent("evntTime",500);
		return;
	}

	SetNodeUsing("P_1",false);
	SetNodeUsing("P_2",false);
	SetNodeUsing("P_3",false);

	if(bNewInterface)
	{
		GameInterface.mainmenu.timeclock1 = 10;
		PostEvent("evntTime",500);
		string bNode = "P_"+sti(Rand(2)+1);
		SetNodeUsing(bNode,true);
		switch(bNode)
		{
			case "P_1": SetNewPicture(bNode, "interfaces\\background\\"+GetRandSubString("compass,anchor,treasure")+".tga"); break;
			case "P_2": SetNewPicture(bNode, "loading\\"+GetRandSubString("mutiny,mutiny_death,prisoner,landing")+".tga"); break;
			case "P_3": SetNewPicture(bNode, "interfaces\\background\\"+GetRandSubString("sails,ransack,port,fort_death_1,cargo,cannons_fire,battle,cabin,aboardage")+".tga"); break;
		}
	}
}

void ProcCommand()
{
	GameInterface.mainmenu.timeclock = MAINMENU_TIME_DELAY;

	string comName = GetEventData();
	string nodName = GetEventData();

	if(comName=="activate" || comName=="click")
	{
		switch(nodName)
		{
		case "B_NEWGAME":	CheckStorageSpace("newgame"); break;
		case "B_LOAD":		CheckStorageSpace("load"); break;
		case "B_SAVE":		CheckStorageSpace("save"); break;
		case "B_OPTIONS":	CheckStorageSpace("options"); break;
		case "B_CONTROLS":	CheckStorageSpace("controls"); break;
		case "B_BUILD":		CheckStorageSpace("build"); break;
		case "B_CREDITS":	CheckStorageSpace("credits"); break;
		}
	}
}

void procBreakExit()
{
	if( GetSelectable("B_RESUMEGAME") ) ProcessCancelExit();
}

string confirmAction="";
void CheckStorageSpace(string action)
{
	confirmAction = action;
	if( g_bOptionsBreak )
	{
		SetEventHandler("evntOptionsBreak","OptionsBreakOk",0);
		PostEvent("DoInfoShower",1,"sl","OptionsBreak",true);
		return;
	}
	if( GetTargetPlatform()=="xbox" )
	{
		int nFreeBlocks = SendMessage(&GameInterface, "l", MSG_INTERFACE_GET_FREE_SPACE);
		if( nFreeBlocks < MAX_SAVE_STORE_BLOCKS ) {
			PostEvent("DoInfoShower",1,"sl","Low Storage",true);
			return;
		}
	}
	PostEvent("evntLowStorageBreak",1);
}
void LowStorageNext()
{
	switch(confirmAction)
	{
	case "newgame":		NewGameConfirm(); break;
	case "load":		LoadPress(); break;
	case "save":		SavePress(); break;
	case "options":		OptionsPress(); break;
	case "controls":	ControlsPress(); break;
	case "build":		AboutBuildPress(); break;
	case "credits":		CreditsPress(); break;
	}
}

void OptionsBreakOk()
{
	DelEventHandler("evntOptionsBreak","OptionsBreakOk");
	SaveGameOptions();
	g_bOptionsBreak = false;
	CheckStorageSpace(confirmAction);
	LoadGameOptions();
}

void ProcStop()
{
	SetNodeUsing("CONFIRM_RECTANGLE", true);
	SetNodeUsing("TEXTWINDOW", true);
	SetNodeUsing("STOP_OK", true);
	SetNodeUsing("STOP_EXIT", true);
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "STOP_OK");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "STOP_EXIT");

	int tmpLangFileID = LanguageOpenFile("interface_strings.txt");
	SetFormatedText("TEXTWINDOW",LanguageConvertString(tmpLangFileID,"Insert Disk Ok"));
	LanguageCloseFile(tmpLangFileID);

	SetCurrentNode("STOP_OK");
}

void procCheckAgain()
{
	if ( SendMessage(&GameInterface,"l", MSG_INTERFACE_CHECK_AGAIN)!=0 )
	{
		SetNodeUsing("CONFIRM_RECTANGLE", false);
		SetNodeUsing("TEXTWINDOW", false);
		SetNodeUsing("STOP_OK", false);
		SetNodeUsing("STOP_EXIT", false);
		SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);
		SetCurrentNode("B_NEWGAME");
	}
}

void procCheckCancel()
{
	if(IsEntity(&worldMap)) DelEventHandler("WorldMap_DeleteShipEncounter", "wdmEvent_DeleteShipEncounter");
	ExitProgram();
}
