int nCurIsland = 1;
int nCurMap = 1;

ref chm = &NullCharacter;
int num_labels = 0;

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

	chm = GetMainCharacter();
	string pic1,pic2,pic3,pic4;
	ref officChar_1 = GetCharacter(GetOfficersIndex(chm,1));
	ref officChar_2 = GetCharacter(GetOfficersIndex(chm,2));
	ref officChar_3 = GetCharacter(GetOfficersIndex(chm,3));

// KK -->
	CreateString(true, "SelIsland", "", FONT_NORMAL, COLOR_NORMAL, 605, 73, SCRIPT_ALIGN_CENTER, 0.6);
	CreateString(true, "SelMap", "", FONT_NORMAL, COLOR_NORMAL, 605, 103, SCRIPT_ALIGN_CENTER, 0.6);

	CreateImage("face1","EMPTYFACE","emptyface",540,296,600,356);
	CreateImage("face2","EMPTYFACE","emptyface",610,296,670,356);
	CreateImage("face3","EMPTYFACE","emptyface",540,373,600,433);
	CreateImage("face4","EMPTYFACE","emptyface",610,373,670,433);
	
	pic1 = GetFacePicture(GetOfficersIndex(chm,0));
	pic2 = GetFacePicture(GetOfficersIndex(chm,1));
	pic3 = GetFacePicture(GetOfficersIndex(chm,2));
	pic4 = GetFacePicture(GetOfficersIndex(chm,3));

	GameInterface.pictures.face1.pic = GetFacePictureName(sti(pic1));
	GameInterface.pictures.face1.tex = GetFaceGroupNameFromFace(sti(pic1));
	CreateString(true,"OFFIC_1",XI_ConvertString("Captain"),FONT_NORMAL,COLOR_NORMAL,570,358,SCRIPT_ALIGN_CENTER,0.5);

	if (GetOfficersIndex(chm, 1) > -1)
	{
		GameInterface.pictures.face2.pic = GetFacePictureName(sti(pic2));
		GameInterface.pictures.face2.tex = GetFaceGroupNameFromFace(sti(pic2));
		if(CheckAttribute(officChar_1,"quest.officertype")) CreateString(true,"OFFIC_2",XI_ConvertString(officChar_1.quest.officertype),FONT_NORMAL,COLOR_NORMAL,640,358,SCRIPT_ALIGN_CENTER,0.5);
		else CreateString(true,"OFFIC_2",XI_ConvertString("Comrade-In-Arms"),FONT_NORMAL,COLOR_NORMAL,640,358,SCRIPT_ALIGN_CENTER,0.5);
	}

	if (GetOfficersIndex(chm, 2) > -1)
	{
		GameInterface.pictures.face3.pic = GetFacePictureName(sti(pic3));
		GameInterface.pictures.face3.tex = GetFaceGroupNameFromFace(sti(pic3));
		if(CheckAttribute(officChar_2,"quest.officertype")) CreateString(true,"OFFIC_3",XI_ConvertString(officChar_2.quest.officertype),FONT_NORMAL,COLOR_NORMAL,570,435,SCRIPT_ALIGN_CENTER,0.5);
		else CreateString(true,"OFFIC_3",XI_ConvertString("Comrade-In-Arms"),FONT_NORMAL,COLOR_NORMAL,570,435,SCRIPT_ALIGN_CENTER,0.5);
	}

	if (GetOfficersIndex(chm, 3) > -1)
	{
		GameInterface.pictures.face4.pic = GetFacePictureName(sti(pic4));
		GameInterface.pictures.face4.tex = GetFaceGroupNameFromFace(sti(pic4));
		if(CheckAttribute(officChar_3,"quest.officertype")) CreateString(true,"OFFIC_4",XI_ConvertString(officChar_3.quest.officertype),FONT_NORMAL,COLOR_NORMAL,640,435,SCRIPT_ALIGN_CENTER,0.5);
		else CreateString(true,"OFFIC_4",XI_ConvertString("Comrade-In-Arms"),FONT_NORMAL,COLOR_NORMAL,640,435,SCRIPT_ALIGN_CENTER,0.5);
	}
// <-- KK

	SetSelectable("EXIT_BUTTON",true);
	SetSelectable("FACE",true);

	InitLists(); // KK

	//MAXIMUS: showing current ship-position on map -->
	SelectMap(map); // KK
	//MAXIMUS: showing current ship-position on map <--

	SetEventHandler("InterfaceBreak","ProcessCancelExit",0);
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("ievnt_command","ProcCommand",0);
// KK -->
	SetEventHandler("ChangeIslandLeft", "DoChangeIslandLeft", 0);
	SetEventHandler("ChangeIslandRight", "DoChangeIslandRight", 0);
	SetEventHandler("ChangeMapLeft", "DoChangeMapLeft", 0);
	SetEventHandler("ChangeMapRight", "DoChangeMapRight", 0);
// <-- KK

//CreateString(true, "test", "Temple", font_seadogs, color_normal, 140, 150, SCRIPT_ALIGN_LEFT, 0.7);
}

void ProcessCancelExit()
{
	IDoExit( RC_INTERFACE_MAP_EXIT );
}

void IDoExit(int exitCode)
{
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
	DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("ievnt_command","ProcCommand");
// KK -->
	DelEventHandler("ChangeIslandLeft", "DoChangeIslandLeft");
	DelEventHandler("ChangeIslandRight", "DoChangeIslandRight");
	DelEventHandler("ChangeMapLeft", "DoChangeMapLeft");
	DelEventHandler("ChangeMapRight", "DoChangeMapRight");
// <-- KK

	DeleteAttribute(GameInterface, "islandlist");
	DeleteAttribute(GameInterface, "maplist");
	PostEvent("LaunchIAfterFrame",1,"sl", "I_ITEMS", 2);
	InterfaceStack.SelectMenu_node = "I_ITEMS";
	interfaceResultCommand = exitCode;
	EndCancelInterface(false);
}

void ProcCommand()
{
	string comName = GetEventData();
	string nodName = GetEventData();

	if (nodName == "SELECT_MAP_BUTTON") {
		if (comName == "activate" || comName == "click") {
			aref arIsland; makearef(arIsland, GameInterface.maplist);
			int num = GetAttributesNum(arIsland);
			for (int n = 0; n < num; n++) {
				aref arCurIsland = GetAttributeN(arIsland, n);
				if (sti(GetAttributeValue(arCurIsland)) == nCurIsland) {
					num = GetAttributesNum(arCurIsland);
					for (int m = 0; m < num; m++) {
						aref arMap = GetAttributeN(arCurIsland, m);
						if (m == nCurMap) {
							int map = sti(GetAttributeValue(arMap));
							SelectMap(map);
							return;
						}
					}
				}
			}
		}
	}
}

void DoChangeIslandLeft()
{
	aref arIsland; makearef(arIsland, GameInterface.maplist);
	int num = GetAttributesNum(arIsland);
	for (int n = num - 1; n >= 0; n--) {
		aref arCurIsland = GetAttributeN(arIsland, n);
		if (sti(GetAttributeValue(arCurIsland)) == nCurIsland - 1) {
			nCurIsland--;
			string name = GetAttributeName(arCurIsland);
			aref arMap; makearef(arMap, arIsland.(name));
			GameInterface.strings.SelIsland = TranslateString("", FindIslandName(Items[sti(arMap.island)].island));
			num = GetAttributesNum(arMap);
			nCurMap = 0;
			ShowMaps(num > 1);
			GameInterface.strings.SelMap = TranslateString("", "island");
			return;
		}
	}
}

void DoChangeIslandRight()
{
	aref arIsland; makearef(arIsland, GameInterface.maplist);
	int num = GetAttributesNum(arIsland);
	for (int n = nCurIsland; n < num; n++) {
		aref arCurIsland = GetAttributeN(arIsland, n);
		if (sti(GetAttributeValue(arCurIsland)) == nCurIsland + 1) {
			nCurIsland++;
			string name = GetAttributeName(arCurIsland);
			aref arMap; makearef(arMap, arIsland.(name));
			GameInterface.strings.SelIsland = TranslateString("", FindIslandName(Items[sti(arMap.island)].island));
			num = GetAttributesNum(arMap);
			nCurMap = 0;
			ShowMaps(num > 1);
			GameInterface.strings.SelMap = TranslateString("", "island");
			return;
		}
	}
}

void DoChangeMapLeft()
{
	aref arIsland; makearef(arIsland, GameInterface.maplist);
	aref arCurIsland = GetAttributeN(arIsland, nCurIsland - 1);
	string island = GetAttributeName(arCurIsland);
	aref arMap; makearef(arMap, GameInterface.maplist.(island));
	int num = GetAttributesNum(arMap);
	if (nCurMap > 0)
		nCurMap--;
	else
		nCurMap = 0;
	string item = GetAttributeName(GetAttributeN(arMap, nCurMap));
	string type = GetMapType(item);
	GameInterface.strings.SelMap = TranslateString("", type);
}

void DoChangeMapRight()
{
	aref arIsland; makearef(arIsland, GameInterface.maplist);
	aref arCurIsland = GetAttributeN(arIsland, nCurIsland - 1);
	string island = GetAttributeName(arCurIsland);
	aref arMap; makearef(arMap, GameInterface.maplist.(island));
	int num = GetAttributesNum(arMap);
	if (nCurMap <= 0)
		nCurMap++;
	else
		nCurMap = num - 1;
	string item = GetAttributeName(GetAttributeN(arMap, nCurMap));
	string type = GetMapType(item);
	GameInterface.strings.SelMap = TranslateString("", type);
}

void InitLists()
{
	int i = 1;
	int iIsland = 1;
	if (CheckAttribute(GameInterface, "maplist")) DeleteAttribute(GameInterface, "maplist");
	int n = FindCharacterMap(chm, i);
	while (n >= 0) {
		aref arMap;
		if (Items_FindItem(Items[n].id, &arMap) >= 0 && CheckCharacterItem(chm, Items[n].id) == true) {
			string island = arMap.island;
			string type = GetMapType(arMap.id);
			if (!CheckAttribute(GameInterface, "maplist." + island)) {
				GameInterface.maplist.(island) = iIsland;
				iIsland++;
			}
			GameInterface.maplist.(island).(type) = n;
		} 
		i++;
		n = FindCharacterMap(chm, i);
	}
	if (CheckAttribute(GameInterface, "maplist")) {
		aref arList; makearef(arList, GameInterface.maplist);
		if (GetAttributesNum(arList) > 1) {
			SetNodeUsing("ISLANDBOX", false);
			SetNodeUsing("ISLANDLIST", true);
		}
	}
}

void SelectMap(int idx)
{
	if (idx < 0) return;
	string item_id = Items[idx].id;
	int n = 0;
	aref arMap;
	if (CheckCharacterItem(chm, item_id) == true && Items_FindItem(item_id, &arMap) >= 0) {
		for (n = 1; n <= num_labels; n++) {
			DisableString("label_" + n);
		}
		string dispname = GetAssembledString(TranslateString("", arMap.name), arMap);
		CreateStringCheckCase(true, "fakeTitle", dispname, "interface_title", COLOR_NORMAL, 320, 5, SCRIPT_ALIGN_CENTER, 1.0, true);
		string island = arMap.island;
		aref arIsland; makearef(arIsland, GameInterface.maplist.(island));
		int num = GetAttributesNum(arIsland);
		ShowMaps(num > 1);
		GameInterface.strings.SelIsland = TranslateString("", FindIslandName(arMap.island));
		nCurIsland = sti(GameInterface.maplist.(island));
		GameInterface.ItemIdx = idx;
		for (n = 0; n < num; n++) {
			aref arCurMap = GetAttributeN(arIsland, n);
			int curVal = sti(GetAttributeValue(arCurMap));
			if (curVal == idx) {
				nCurMap = n;
				string type = GetMapType(arMap.id);
				GameInterface.strings.SelMap = TranslateString("", type);
			}
		}

		SetNodeUsing("MAP", true);
		SetNewPicture("MAP", "INTERFACES\Maps\" + item_id + ".tga");
		// PB: Sextant and Chronometer -->
		bool ShowPosition = DISPLAY_MAP_POSITION;
		if(item_id == "map" && CheckCharacterItem(chm,"clock2") == true && CheckCharacterItem(chm,"sextant") == true) ShowPosition = true;
		if(ShowPosition)
		{
			arMap.poz.X = makefloat(stf(worldMap.playerShipX)/stf(arMap.map_to_sea_scale.x));
			arMap.poz.Y = makefloat(stf(worldMap.playerShipZ)/stf(arMap.map_to_sea_scale.y));

			int fakeShipPosX = makeint(stf(arMap.X) - stf(arMap.poz.X));
			int fakeShipPosY = makeint(stf(arMap.Y) + stf(arMap.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 = 9;

			CreateImage("SHIP", texGroupName, texName, makeint(fakeShipPosX-picScale), makeint(fakeShipPosY-picScale), makeint(fakeShipPosX+picScale), makeint(fakeShipPosY+picScale));
			SetPictureBlind("SHIP",true,minBlindColor,maxBlindColor);
		}
		// PB: Sextant and Chronometer <--
		aref arLabels; makearef(arLabels, arMap.labels);
		num_labels = GetAttributesNum(arLabels);
		for (n = 0; n < num_labels; n++) {
			aref arlbl = GetAttributeN(arLabels, n);
			CreateString(true, "label_" + (n + 1), arlbl.label, "seadogs_small", COLOR_GREEN, sti(arlbl.pos.x), sti(arlbl.pos.y), SCRIPT_ALIGN_LEFT, 0.7);
		}
	} else {
		for (n = 1; n <= num_labels; n++) {
			DisableString("label_" + n);
		}
		SetNodeUsing("MAP", false);
	}
}

void ShowMaps(bool bShow)
{
	SetNodeUsing("MAPBOX", !bShow);
	SetNodeUsing("MAPLIST", bShow);
}

int GetMapIslandNumber(string island)
{
	return sti(GameInterface.maplist.(island));
}

string GetMapType(string id)
{
	if (hasSubStr(id, "Cave")) return "cave";
	if (hasSubStr(id, "Dungeon")) return "dungeon";
	if (hasSubStr(id, "Temple")) return "temple";
	return "island";
}
