void InitInterface(string iniName)
{
    GameInterface.title = "titleReserved";

    SendMessage(&GameInterface,"ls",MSG_INTERFACE_INIT,iniName);
	CreateExitString();//MAXIMUS: standard exit-string for exit-button

    ref chm = GetMainCharacter();
	CreateString(true,"TIME",GetDataDay()+" "+XI_ConvertString("target_month_" + GetDataMonth())+" "+GetDataYear()+" - "+GetStringTime(GetTime()),"seadogs_small",COLOR_BLUE_LIGHT,620,445,SCRIPT_ALIGN_RIGHT,0.8);

	HideToolT();
	ShowMap(DISPLAY_MAP_POSITION);
	ShowButtons();

	SetEventHandler("InterfaceBreak","ProcessCancelExit",0);
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("ievnt_command","ProcessCommandExecute",0);
	SetEventHandler("frame","HideToolT",1);
}

void ShowMap(bool bInfo)
{
	object fakeMap;
	ref chm = GetMainCharacter();

	float startX = makefloat(189.0)+makefloat(23.0);
	float startY = makefloat(50.0)+makefloat(23.0);
	float endX = makefloat(653.0)-makefloat(23.0);
	float endY = makefloat(493.0)-makefloat(23.0);

	float FAKE_MAP_TO_SEA_SCALE_X = makefloat(2000/stf(endX-startX));
	float FAKE_MAP_TO_SEA_SCALE_Y = makefloat(2000/stf(endY-startY));

	float fakeMapPosX = makefloat(sti(stf(endX-startX)/2)+startX);
	float fakeMapPosY = makefloat(sti(stf(endY-startY)/2)+startY);

	fakeMap.X = fakeMapPosX;
	fakeMap.Y = fakeMapPosY;

	fakeMap.poz.X = makefloat(makefloat(worldMap.playerShipX)/FAKE_MAP_TO_SEA_SCALE_X);
	fakeMap.poz.Y = makefloat(makefloat(worldMap.playerShipZ)/FAKE_MAP_TO_SEA_SCALE_Y);

	int fakeShipPosX = makeint(makefloat(fakeMap.X) - makefloat(fakeMap.poz.X));
	int fakeShipPosY = makeint(makefloat(fakeMap.Y) + makefloat(fakeMap.poz.Y));

	ref shipRef = GetShipByType(GetCharacterShipType(chm));
	aref arship; makearef(arship, chm.ship);
	string texGroupName = GetShipTextureName(sti(shipRef.index));
	string texName = GetShipPicName(sti(shipRef.index));
	int picScale = 7;
	CreateImage("SHIP", "", "", 0, 0, 0, 0);
	CreateImage("MARK", "", "", 0, 0, 0, 0);

	// PB: Sextant and Chronometer -->
	bool ShowPosition = bInfo;
	if(CheckCharacterItem(chm,"clock2") && CheckCharacterItem(chm,"sextant")) ShowPosition = true;
	if(ShowPosition)
	{
		CreateImage("SHIP", texGroupName, texName, sti(fakeShipPosX-picScale), sti(fakeShipPosY-picScale), sti(fakeShipPosX+picScale), sti(fakeShipPosY+picScale));
		SetPictureBlind("SHIP",true,minBlindColor,maxBlindColor);
	}
	// PB: Sextant and Chronometer <--

	aref refIsl; makearef(refIsl, worldMap.islands);
	for(int n=0; n<GetAttributesNum(refIsl); n++)
	{
		string isIsland = GetAttributeName(GetAttributeN(refIsl,n));
		if(!CheckAttribute(worldMap,"islands."+isIsland)) continue;

		string isName = worldMap.islands.(isIsland).label.text;
		float pozX = makefloat(makefloat(worldMap.islands.(isIsland).position.x)/FAKE_MAP_TO_SEA_SCALE_X);
		float pozY = makefloat(makefloat(worldMap.islands.(isIsland).position.z)/FAKE_MAP_TO_SEA_SCALE_Y);
		int fakeX = makeint(makefloat(fakeMap.X) - pozX);
		int fakeY = makeint(makefloat(fakeMap.Y) + pozY)-15;
		if(isIsland!="Battle_Rocks") CreateString(bInfo,isIsland,isName,"seadogs_small",COLOR_GREEN_LIGHT,fakeX,fakeY,SCRIPT_ALIGN_RIGHT,0.7);
		aref wIsland; makearef(wIsland, worldMap.islands.(isIsland).locations);
		for(int c=0; c<GetAttributesNum(wIsland); c++)
		{
			string cityNum = "city"+sti(c+1);
			if(CheckAttribute(wIsland,cityNum) && CheckAttribute(wIsland,cityNum+".real"))
			{
				string colonyName = wIsland.(cityNum).label.text;
				pozX = makefloat(makefloat(wIsland.(cityNum).position.x)/FAKE_MAP_TO_SEA_SCALE_X);
				pozY = makefloat(makefloat(wIsland.(cityNum).position.z)/FAKE_MAP_TO_SEA_SCALE_Y);
				int fakeXr = makeint(makefloat(fakeMap.X) - pozX);
				int fakeYr = makeint(makefloat(fakeMap.Y) + pozY)-15;
				if(isIsland=="Hispaniola" && cityNum=="city3") CreateString(bInfo,wIsland.(cityNum).name,colonyName,"seadogs_small",COLOR_GREEN_LIGHT,fakeXr,fakeYr,SCRIPT_ALIGN_RIGHT,0.7);
				if(HasSubStr(chm.location,"tavern") && CheckAttribute(chm,"quest.generate_convoy_quest.destination") && chm.quest.generate_convoy_quest.destination==wIsland.(cityNum).real)
				{
					CreateImage("MARK", "MARK", "mark", fakeXr-12, fakeYr+8, fakeXr-4, fakeYr+16);
				}
				if(HasSubStr(chm.location,"store") && CheckAttribute(chm,"quest.generate_trade_quest.iTradeColony") && chm.quest.generate_trade_quest.iTradeColony==wIsland.(cityNum).real)
				{
					CreateImage("MARK", "MARK", "mark", fakeXr-12, fakeYr+8, fakeXr-4, fakeYr+16);
				}
			}
		}
	}
}

void ShowButtons()
{
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "EXIT_BUTTON");

	for(int i=0; i<ISLANDS_QUANTITY+1; i++)
	{
		string rcString = "RECTANGLE"+sti(i+2);
		SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, rcString);
	}
}

void ProcessCommandExecute()
{
	string comName = GetEventData();
	string nodName = GetEventData();
	if(comName!="click") return;

	for(int i=0; i<ISLANDS_QUANTITY+2; i++)
	{
		string rcString = "RECTANGLE"+sti(i+1);
		if(nodName!=rcString) continue;
		ShowIslandInfo(i); break;
	}
}

void ShowIslandInfo(int Idx) 
{
	string sHeader = "";
	string sText1 = "";
	string sText2 = "";
	string sText3 = "";
	string sText4 = "";
	string sGroupPicture = "";
	string sPicGroup = "";
	string sPicture = "";
	int isIdx = Idx;
	if(Idx==16) { isIdx = 9; }
	sHeader = Islands[isIdx].id;
	int colonyNation = -1;

	string isIsland = Islands[isIdx].id;
	if(CheckAttribute(Islands[isIdx],"worldmap")) { isIsland = Islands[isIdx].worldmap; sHeader = Islands[isIdx].worldmap; }
	
	for(int c=1; c<10; c++)
	{
		string cityNum = "city"+c;
		if(CheckAttribute(worldMap,"islands."+isIsland+".locations."+cityNum+".label.text") && CheckAttribute(worldMap,"islands."+isIsland+".locations."+cityNum+".real") && CheckAttribute(worldMap,"islands."+isIsland+".locations."+cityNum+".label.icon"))
		{
			string colonyName = worldMap.islands.(isIsland).locations.(cityNum).name;
			string colonyId = worldMap.islands.(isIsland).locations.(cityNum).name;
			colonyNation = sti(worldMap.islands.(isIsland).locations.(cityNum).label.icon);
			if(Idx==9 && cityNum=="city3") { sText1 += ""; colonyNation = sti(worldMap.islands.(isIsland).locations.city2.label.icon); }
			else sText1 += XI_ConvertString(colonyId)+" - "+XI_ConvertString(GetNationNameByType(colonyNation)) +"          ";
			if(Idx==16 && cityNum=="city3") { isIsland = worldMap.islands.(isIsland).locations.(cityNum).name; sText1 = XI_ConvertString(colonyId)+" - "+XI_ConvertString(GetNationNameByType(colonyNation)); }
		}
	}
	sHeader = TranslateString("", isIsland);
	switch(colonyNation)
	{
		case 0: sGroupPicture = "England"; sPicGroup = "NATIONS_2"; break;
		case 1: sGroupPicture = "France"; sPicGroup = "NATIONS_2"; break;
		case 2: sGroupPicture = "Spain"; sPicGroup = "NATIONS_1"; break;
		case 3: sGroupPicture = "Pirate"; sPicGroup = "NATIONS_2"; break;
		case 4: sGroupPicture = "Holland"; sPicGroup = "NATIONS_2"; break;
		case 5: sGroupPicture = "Portugal"; sPicGroup = "NATIONS_1"; break;
		sGroupPicture = "";
		sPicGroup = "";
	}

	CreateTooltip(sHeader, sText1, argb(255,255,255,255), sText2, argb(255,255,255,255), sText3, argb(255,255,255,255), sText4, argb(255,255,255,255), sPicture, sPicGroup, sGroupPicture, 64, 64);
}

void CreateTooltip(string header, string text1, int color1, string text2, int color2, string text3, int color3, string text4, int color4, string picTexture, string picGroup, string picImage, int nPicWidth, int nPicHeight)
{
	SetNodeUsing("WINDOW_FAD",true);
	SetNodeUsing("RECT",true);
	SetNodeUsing("INFO_PICT",true);
	SetNodeUsing("TEXT",true);
	SetCurrentNode("TEXT");
	SetFormatedText("TEXT",header);
	SetNodeUsing("INFO_TEXT",true);
	SetFormatedText("INFO_TEXT",text1);
	SetSelectable("EXIT_BUTTON",false);

	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 1, "WINDOW_FAD");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 2, "RECT");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 3, "INFO_PICT");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 3, "INFO_TEXT");
	SendMessage(&GameInterface,"lls", MSG_INTERFACE_LOCK_NODE, 3, "TEXT");

	ChangePicture("INFO_PICT", "interfaces\"+picImage+".tga", true);
}

void HideToolT()
{
	SendMessage(&GameInterface,"ll", MSG_INTERFACE_LOCK_NODE, 0);

	SetNodeUsing("WINDOW_FAD",false);
	SetNodeUsing("RECT",false);
	SetNodeUsing("INFO_PICT",false);
	SetNodeUsing("TEXT",false);
	SetNodeUsing("INFO_TEXT",false);
	SetSelectable("EXIT_BUTTON",true);
	SetCurrentNode("OFFIC_1");
}

void ProcessCancelExit()
{
	IDoExit( RC_INTERFACE_A_MAP_EXIT );
}

void IDoExit(int exitCode)
{
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
	DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("ievnt_command","ProcessCommandExecute");
	DelEventHandler("frame","HideToolT");

    interfaceResultCommand = exitCode;
	EndCancelInterface(true);
}
