
#define ITEM_MODE_SWAP		0
#define ITEM_MODE_TAKE		1
#define ITEM_MODE_NONE		2
#define ITEM_MODE_INFO		3

aref g_refItems;
ref pchar;

int nCurScroll1;
int nCurScroll2;

string oldNodeName="";
int oldItemMode = -1;

bool g_bIBoxUsed;

string oldCurScrollName = "ONE_ITEMSLIST";

int idLngFile;

bool bEnableDown = false;
bool isItemsBox = true;
bool noConfirm = false;

void InitInterface_RS(string iniName,ref itemsRef,string faceID)
{	
	pchar = GetMainCharacter();
	SetTimeScale(0.0);
	locCameraSleep(true);

	EngineLayersOffOn(true);

	if(faceID=="") { GameInterface.title = "titleItemsBox"; }
	else
	{
		if(bDeadExchange) { GameInterface.title = "titleExchangeWithDead"; isItemsBox = false; }
		else { GameInterface.title = "titleExchangeItems"; }
	}
	g_refItems = itemsRef;

	idLngFile = LanguageOpenFile(GetStorylineDir(FindCurrentStoryline()) + "ItemsDescribe.txt"); // KK

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

	CreateImage("MyPic",GetFaceGroupName(nMainCharacterIndex),GetFacePictureName(nMainCharacterIndex),46,82,174,210);

	GameInterface.pictures.OtherPic.tex = "";
	GameInterface.pictures.OtherPic.pic = "";
	SetNodeUsing( "FACEPICT", faceID!="" );
	if(faceID=="") { g_bIBoxUsed = true; CreateImage("OtherPic","BOX_IMAGE","BoxImage",5,302,133,430); }
	else
	{
		g_bIBoxUsed = false;
		if(bDeadExchange)
		{
		    SetNewPicture("EN_PIC", "interfaces\portraits\128\face_skull.tga");
			SetNewPicture("FACEPICT", "interfaces\portraits\128\face_skull.tga");
		}
		else
		{
		    SetNewPicture("EN_PIC", "interfaces\portraits\64\face_" + faceID + ".tga");
			SetNewPicture("FACEPICT", "interfaces\portraits\128\face_" + faceID + ".tga");
		}
	}

	EnableButtons(true);

	SetEventHandler("InterfaceBreak","ProcessCancelExit",0);
	SetEventHandler("exitCancel","ProcessCancelExit",0);
	SetEventHandler("frame","FrameProcess",1);

	SetEventHandler("ievnt_command","ProcInterfaceCommand",0);

	SetEventHandler("ExitPress","ProcessExit",0);
	SetEventHandler("ExitNo","ProcessExit_no",0);
	SetEventHandler("ExitYes","ProcessExit_yes",0);
	SetEventHandler("ConfirmExitClick","ConfirmExitClick",0);

	SetItemMode(ITEM_MODE_SWAP);

	IGotoNewNode("TWO_ITEMSLIST");
	IGotoNewNode("ONE_ITEMSLIST");

	SetUpDownUsed();
	SetScrollsUsing();
}

void IDoExit(int exitCode)
{
	LanguageCloseFile(idLngFile);
	if(!g_bIBoxUsed)
	{
		if( GetCharacterSkill(g_refItems,"fencing") > 0 )
		{
			EquipCharacterByItem(g_refItems, FindCharacterItemByGroup(g_refItems,BLADE_ITEM_TYPE));
			EquipCharacterByItem(g_refItems, FindCharacterItemByGroup(g_refItems,GUN_ITEM_TYPE));
		}
	}
	DelEventHandler("InterfaceBreak","ProcessCancelExit");
	DelEventHandler("exitCancel","ProcessCancelExit");
	DelEventHandler("frame","FrameProcess");
	DelEventHandler("ievnt_command","ProcInterfaceCommand");
	DelEventHandler("ExitPress","ProcessExit");
	DelEventHandler("ExitNo","ProcessExit_no");
	DelEventHandler("ExitYes","ProcessExit_yes");
	DelEventHandler("ConfirmExitClick","ConfirmExitClick");

	SetTimeScale(1.0);
	locCameraSleep(false);

	EngineLayersOffOn(false);
	BLI_EnableShow();

	interfaceResultCommand = exitCode;
	if(interfaceResultCommand == RC_INTERFACE_DO_RESUME_GAME) { DeleteEntitiesByType("scrshoter"); }
	EndCancelInterface(true);
  
	if(bDeadExchange) { bDeadExchange = false; }
}

void ShowConfirmWindow(bool show)
{
	if(show)
	{
		SetCurrentNode("CONFIRM_WINDOW_MB_NO");
		SetNodeUsing("MAIN_WINDOW", true);
		SetNodeUsing("CONFIRM_WINDOW", false);
		SetNodeUsing("CONFIRM_WINDOW", true);
//		EI_CreateFrame("CONFIRM_WINDOW_BORDER",190,190,610,360);
		SetNodeUsing("REL_PIC",true);
		SetNodeUsing("EN_PIC",true);
		EnableButtons(false);
	}
	else
	{
		SetNodeUsing("CONFIRM_WINDOW", true);
		SetNodeUsing("CONFIRM_WINDOW", false);
		SetNodeUsing("MAIN_WINDOW", false);
		SetNodeUsing("REL_PIC",false);
		SetNodeUsing("EN_PIC",false);
		EnableButtons(true);
	}
}

void EnableButtons(bool bEnable)
{
	if(bEnable)
	{
		SetNodeUsing("TEXTWINDOW",false);
		SetNodeUsing("REL_PIC",false);
		SetNodeUsing("EN_PIC",false);
		SetNodeUsing("EXIT_BUTTON",true);
		SetSelectable("EXIT_BUTTON",true);
		SetSelectable("INFO_BUTTON",true);
		SetItemMode(ITEM_MODE_SWAP);

		IGotoNewNode("TWO_ITEMSLIST");
		IGotoNewNode("ONE_ITEMSLIST");

		SetUpDownUsed();
		SetScrollsUsing();
	}
	else
	{
		SetNodeUsing("EXIT_BUTTON",false);
		SetSelectable("EXIT_BUTTON",false);
		SetSelectable("INFO_BUTTON",false);
		SetSelectable("ONE_ITEMSLIST",false);
		SetSelectable("TWO_ITEMSLIST",false);
		SetSelectable("ONE_LEFTSCROLLBUTTON",false);
		SetSelectable("ONE_RIGHTSCROLLBUTTON",false);
		SetSelectable("TWO_LEFTSCROLLBUTTON",false);
		SetSelectable("TWO_RIGHTSCROLLBUTTON",false);
		SetSelectable("ONEUP_BUTTON",false);
		SetSelectable("ONEDOWN_BUTTON",false);
		SetSelectable("SWAP_BUTTON",false);
		SetSelectable("ALLUP_BUTTON",false);
		SetSelectable("ALLDOWN_BUTTON",false);
	}
}

void ConfirmExitClick()
{
	int nCmdIdx = GetEventData();
	string sNodName = GetEventData();

	if(sNodName == "CONFIRM_EXIT_YES" && nCmdIdx != ACTION_DEACTIVATE)
	{
		ProcessExit_yes();
	} 
	else 
	{
		ProcessExit_no();
	}
}

void ProcessExit()
{
	ShowConfirmWindow(true);
}

void ProcessExit_no()
{
	ShowConfirmWindow(false);
}

void ProcessExit_yes()
{
	if(oldItemMode==ITEM_MODE_INFO) SetItemMode(ITEM_MODE_SWAP);
	else IDoExit(RC_INTERFACE_DO_RESUME_GAME);
}

void FrameProcess()
{
	IGotoNewNode(GetCurrentNode());

	if( nCurScroll1 != sti(GameInterface.one_itemslist.current) ||
		nCurScroll2 != sti(GameInterface.two_itemslist.current) )
	{
		nCurScroll1 = sti(GameInterface.one_itemslist.current);
		nCurScroll2 = sti(GameInterface.two_itemslist.current);
		if(oldItemMode!=ITEM_MODE_SWAP)	{SetItemMode(ITEM_MODE_SWAP);}
		SetUpDownUsed();
	}
}

void FillScroll()
{
    int i,j,q;
    string attributeName, sItem;
	aref curItem,arItem,rootItems;
	aref arImgGrp;

	if(CheckAttribute(&GameInterface,"one_itemslist.current"))
		nCurScroll1 = sti(GameInterface.one_itemslist.current);
	else	nCurScroll1 = 0;
	if(CheckAttribute(&GameInterface,"two_itemslist.current"))
		nCurScroll2 = sti(GameInterface.two_itemslist.current);
	else	nCurScroll2 = 0;

	DeleteAttribute(&GameInterface,"one_itemslist");
	DeleteAttribute(&GameInterface,"two_itemslist");

	//============================================================================
	// Наши вещи
	//============================================================================
	GameInterface.one_itemslist.NotUsed = 5;
	GameInterface.one_itemslist.current = nCurScroll1;
	GameInterface.one_itemslist.BadTex1 = GetItemPictureTexture("one_itemslist.ImagesGroup","");
	GameInterface.one_itemslist.BadPic1 = GetItemPictureName("");
	makearef(arImgGrp,GameInterface.one_itemslist.ImagesGroup);
	FillImagesGroupForItems(arImgGrp);

	i=0;
	// Пропишем деньги
	if( sti(pchar.Money)>0 )
	{
		GameInterface.one_itemslist.pic1.img1 = "itm35";//GetItemPictureName("Gold");
		GameInterface.one_itemslist.pic1.tex1 = "ITEMS_1";//GetItemPictureTexture("one_itemslist.ImagesGroup","Gold");
		GameInterface.one_itemslist.pic1.itemID = "Money";
		GameInterface.one_itemslist.pic1.str1 = "#"+pchar.Money;
		i++;
	}
	// Заполним вещами от нас
	makearef(rootItems,pchar.Items);
    for(j=0; j<GetAttributesNum(rootItems); j++)
    {
		attributeName = "pic" + (i+1);
		curItem = GetAttributeN(rootItems,j);

		if(Items_FindItem(GetAttributeName(curItem),&arItem)>=0)
		{
			if(CheckAttribute(arItem, "groupID"))
			{
				if(arItem.groupID == GUN_ITEM_TYPE) { sItem = "gun"+arItem.picIndex; }
				if(arItem.groupID == BLADE_ITEM_TYPE) { sItem = "blade"+arItem.picIndex; }
				if(arItem.groupID == SPYGLASS_ITEM_TYPE)
				{
					if(arItem.id == "spyglass5") { arItem.picIndex = 4; }
					sItem = "spyglass"+arItem.picIndex;
				}

			}
			else { sItem = GetItemPictureName(arItem.id); }

			GameInterface.one_itemslist.(attributeName).img1 = sItem;//GetItemPictureName(arItem.id);
			GameInterface.one_itemslist.(attributeName).tex1 = GetItemPictureTexture("one_itemslist.ImagesGroup",arItem.id);
			GameInterface.one_itemslist.(attributeName).itemID = arItem.id;
			if(CheckConsumedItem(arItem.id))
				GameInterface.one_itemslist.(attributeName).str1 = "#"+sti(GetAttributeValue(curItem));
		}
		else
		{
			trace("WARNING! Can`t find item ID = "+GetAttributeName(curItem));
			GameInterface.one_itemslist.(attributeName).img1 = GameInterface.one_itemslist.BadPic1;
			GameInterface.one_itemslist.(attributeName).tex1 = GameInterface.one_itemslist.BadTex1;
		}
		i++;
    }
	GameInterface.one_itemslist.ListSize = i;

	//============================================================================
	// Чужие вещи
	//============================================================================
	GameInterface.two_itemslist.NotUsed = 5;
	GameInterface.two_itemslist.current = nCurScroll2;
	GameInterface.two_itemslist.BadTex1 = GetItemPictureTexture("itemslist.ImagesGroup","");
	GameInterface.two_itemslist.BadPic1 = GetItemPictureName("");
	makearef(arImgGrp,GameInterface.two_itemslist.ImagesGroup);
	FillImagesGroupForItems(arImgGrp);

	i=0;
	// Пропишем деньги
	if(!CheckAttribute(g_refItems,"Money"))	g_refItems.Money = 0;

	makearef(rootItems,g_refItems.Items);
	q = GetAttributesNum(rootItems);

	// Найдем все монеты и переведем их в деньги
	for(j=0; j<q; j++)
	{
		curItem = GetAttributeN(rootItems,j);
		if(sti(GetAttributeValue(curItem))<=0)	continue;
		if( Items_FindItem(GetAttributeName(curItem),&arItem)>=0 )
		{
			if( CheckAttribute(arItem,"Gold") )
			{
				g_refItems.Money = sti(g_refItems.Money) + sti(arItem.Gold)*GetCharacterItem(g_refItems,arItem.Gold);
				DeleteAttribute(rootItems,arItem.id);
				j--; q--;
			}
		}
	}
	if( sti(g_refItems.Money)>0 )
	{
		GameInterface.two_itemslist.pic1.img1 = "itm35";//GetItemPictureName("Gold");
		GameInterface.two_itemslist.pic1.tex1 = "ITEMS_1";//GetItemPictureTexture("two_itemslist.ImagesGroup","Gold");
		GameInterface.two_itemslist.pic1.itemID = "Money";
		GameInterface.two_itemslist.pic1.str1 = "#"+g_refItems.Money;
		i++;
	}

	for(j=0; j<q; j++)
	{
		attributeName = "pic" + (i+1);
		curItem = GetAttributeN(rootItems,j);

		if(sti(GetAttributeValue(curItem))<=0)	continue;

		if( Items_FindItem(GetAttributeName(curItem),&arItem)>=0 )
		{
			if(CheckAttribute(arItem, "groupID"))
			{
				if(arItem.groupID == GUN_ITEM_TYPE) { sItem = "gun"+arItem.picIndex; }
				if(arItem.groupID == BLADE_ITEM_TYPE) { sItem = "blade"+arItem.picIndex; }
				if(arItem.groupID == SPYGLASS_ITEM_TYPE)
				{
					if(arItem.id == "spyglass5") { arItem.picIndex = 4; }
					sItem = "spyglass"+arItem.picIndex;
				}

			}
			else { sItem = GetItemPictureName(arItem.id); }

			GameInterface.two_itemslist.(attributeName).img1 = sItem;//GetItemPictureName(arItem.id);
			GameInterface.two_itemslist.(attributeName).tex1 = GetItemPictureTexture("two_itemslist.ImagesGroup",arItem.id);
			GameInterface.two_itemslist.(attributeName).itemID = arItem.id;
			if(CheckConsumedItem(arItem.id)) {
				GameInterface.two_itemslist.(attributeName).str1 = "#"+sti(GetAttributeValue(curItem));
			}
		}
		else
		{
			trace("WARNING! Can`t find item ID = "+GetAttributeName(curItem));
			GameInterface.two_itemslist.(attributeName).img1 = GameInterface.two_itemslist.BadPic1;
			GameInterface.two_itemslist.(attributeName).tex1 = GameInterface.two_itemslist.BadTex1;
		}
		i++;
	}
	GameInterface.two_itemslist.ListSize = i;
	if(i==0) noConfirm = true;

	nCurScroll1 = sti(GameInterface.one_itemslist.current);
	nCurScroll2 = sti(GameInterface.two_itemslist.current);
}

void ProcessCancelExit()
{isItemsBox = false;bDeadExchange = true;
	if(!isItemsBox)
	{
		if(bDeadExchange)
		{
			if(noConfirm) ProcessExit_yes();
			else ProcessExit();
		}
		else
		{
	        if(oldItemMode==ITEM_MODE_INFO) SetItemMode(ITEM_MODE_SWAP);
	        else IDoExit(RC_INTERFACE_DO_RESUME_GAME);
		}
	}
	else
	{
	    if(oldItemMode==ITEM_MODE_INFO) SetItemMode(ITEM_MODE_SWAP);
	    else IDoExit(RC_INTERFACE_DO_RESUME_GAME);
	}
}

void IGotoNewNode(string newNode)
{
	if(oldNodeName==newNode) return;
	oldNodeName=newNode;
	if(newNode=="ONE_ITEMSLIST")	SetScroll(true);
	if(newNode=="TWO_ITEMSLIST")	SetScroll(false);

	if(oldItemMode==ITEM_MODE_SWAP)
	{
		if(newNode=="DESCRIBEWINDOW")	{SetItemMode(ITEM_MODE_INFO);}
	}
	else
	{
		if(newNode!="DESCRIBEWINDOW")
		{
			SetItemMode(ITEM_MODE_SWAP);
			SetUpDownUsed();
		}
	}
}

void SetScroll(bool bIsOne)
{
	if(bIsOne)
	{
		if(oldCurScrollName=="TWO_ITEMSLIST")
		{
			SetNodeUsing("TWO_LEFTSCROLLBUTTON",false);
			SetNodeUsing("TWO_RIGHTSCROLLBUTTON",false);
		}
		SetNodeUsing("ONE_LEFTSCROLLBUTTON",true);
		SetNodeUsing("ONE_RIGHTSCROLLBUTTON",true);
		oldCurScrollName = "ONE_ITEMSLIST";
	}
	else
	{
		if(oldCurScrollName=="ONE_ITEMSLIST")
		{
			SetNodeUsing("ONE_LEFTSCROLLBUTTON",false);
			SetNodeUsing("ONE_RIGHTSCROLLBUTTON",false);
		}
		SetNodeUsing("TWO_LEFTSCROLLBUTTON",true);
		SetNodeUsing("TWO_RIGHTSCROLLBUTTON",true);
		oldCurScrollName = "TWO_ITEMSLIST";
	}
}

void SetItemMode(int mode)
{
	switch(oldItemMode)
	{
	case ITEM_MODE_SWAP:
		SetNodeUsing("INFO_BUTTON",false);
		SetNodeUsing("SWAP_BUTTON",false);
		SetNodeUsing("ONEUP_BUTTON",false);
		SetNodeUsing("ONEDOWN_BUTTON",false);
		SetNodeUsing("ALLUP_BUTTON",false);
		SetNodeUsing("ALLDOWN_BUTTON",false);
	break;
	case ITEM_MODE_INFO:	SetNodeUsing("DESCRIBEWINDOW",false);	break;
	}

	oldItemMode = mode;

	switch(mode)
	{
	case ITEM_MODE_SWAP:
		SetNodeUsing("INFO_BUTTON",true);
		SetNodeUsing("SWAP_BUTTON",true);
		SetNodeUsing("ONEUP_BUTTON",true);
		SetNodeUsing("ONEDOWN_BUTTON",true);
		SetNodeUsing("ALLUP_BUTTON",true);
		SetNodeUsing("ALLDOWN_BUTTON",true);
	break;
	case ITEM_MODE_INFO:	SetNodeUsing("DESCRIBEWINDOW",true);	break;
	}
}

void ProcInterfaceCommand()
{
	string comName = GetEventData();
	string nodeName = GetEventData();

	switch(nodeName)
	{
	case "INFO_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="rightstep") {SelectButtonRight("ONEDOWN_BUTTON");}
		if(comName=="deactivate") {ProcessExit();}
		if(comName=="activate" || comName=="click")
		{
			SetItemMode(ITEM_MODE_INFO);
			SetDescribeText();
			SetCurrentNode("DESCRIBEWINDOW");
		}
	break;

	case "ONEDOWN_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("INFO_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("ONEUP_BUTTON");}
		if(comName=="activate" || comName=="click")	{GiveOneItem();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ONEUP_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("ONEDOWN_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("SWAP_BUTTON");}
		if(comName=="activate" || comName=="click")	{TakeOneItem();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "SWAP_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("ONEUP_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("ALLUP_BUTTON");}
		if(comName=="activate" || comName=="click")	{ChangeItemsSlot();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ALLUP_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("SWAP_BUTTON");}
		if(comName=="rightstep") {SelectButtonRight("ALLDOWN_BUTTON");}
		if(comName=="activate" || comName=="click")	{TakeItemsCollection();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ALLDOWN_BUTTON":
		if(comName=="upstep") {GoToTopScroll();}
		if(comName=="downstep") {GoToBottomScroll();}
		if(comName=="leftstep") {SelectButtonLeft("ALLUP_BUTTON");}
		if(comName=="activate" || comName=="click")	{GiveItemsCollection();}
		if(comName=="deactivate")	{SetCurrentNode(oldCurScrollName);}
	break;

	case "ONE_ITEMSLIST":
		if(comName=="downstep")	{SelectButtonLeft("SWAP_BUTTON");}
	break;

	case "TWO_ITEMSLIST":
		if(comName=="upstep")	{SelectButtonLeft("SWAP_BUTTON");}
	break;

	case "DESCRIBEWINDOW":
		if(comName=="downstep")
		{
			if(!bEnableDown)
			{
				SendMessage(&GameInterface,"lslll",MSG_INTERFACE_MSG_TO_NODE,"DESCRIBEWINDOW",-1, 0,ACTION_UPSTEP);
			} else {
				bEnableDown = SendMessage(&GameInterface,"lsll",MSG_INTERFACE_MSG_TO_NODE,"DESCRIBEWINDOW", 6, 0);
			}
		}
		if(comName=="upstep")
		{
			bEnableDown = SendMessage(&GameInterface,"lsll",MSG_INTERFACE_MSG_TO_NODE,"DESCRIBEWINDOW", 6, 0);
		}
	break;

	case "CONFIRM_WINDOW_MB_YES":
		if(comName=="click" || comName=="activate") { ProcessExit_yes(); }
	break;

	case "CONFIRM_WINDOW_MB_NO":
		if(comName=="click" || comName=="activate") { ProcessExit_no(); SetCurrentNode("INFO_BUTTON"); }
	break;
	}
}

void ChangeItemsSlot()
{
	string itm1 = GetItemIDByOrder(true,nCurScroll1);
	string itm2 = GetItemIDByOrder(false,nCurScroll2);
	if(itm1=="" || itm1!=itm2)
	{
		if( !CanCharacterTakeItem(true,itm2) ) return;
	}

	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);

	if(iq1>0)
	{
		if(itm1=="Money")
		{
			pchar.Money = 0;
			g_refItems.Money = iq1+sti(g_refItems.Money);
		}
		else
		{
			TakeNItems(pchar,itm1,-iq1);
			if( CheckAttribute(g_refItems,"Items."+itm1) )
			{
				iq1 = iq1 + sti(g_refItems.Items.(itm1));
			}
			g_refItems.Items.(itm1) = iq1;
		}
	}
	if(iq2>0)
	{
		if(itm2=="Money")
		{
			pchar.Money = sti(pchar.Money)+iq2;
			g_refItems.Money = sti(g_refItems.Money)-iq2;
		}
		else
		{
			TakeNItems(pchar,itm2,iq2);
			g_refItems.Items.(itm2) = sti(g_refItems.Items.(itm2))-iq2;
			if( sti(g_refItems.Items.(itm2))<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
		}
	}

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}
void TakeOneItem()
{
	string itm2 = GetItemIDByOrder(false,nCurScroll2);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);
	if(iq2<=0) return;

	iq2--;
	if(itm2=="Money")
	{
		pchar.Money = sti(pchar.Money)+1;
		g_refItems.Money = iq2;
		pchar.stealmoney = sti(pchar.stealmoney)+1;
	}
	else
	{
		for(int i = 0; i <10; i++)
		{
			string sStealItem = "Stealitem" + i;
			if (!checkAttribute(pchar, sStealItem))
			{
				break;
			}
		}		
		pchar.(sStealItem) = itm2;
		TakeNItems(pchar,itm2,1);
		g_refItems.Items.(itm2) = iq2;
		if( iq2<=0 ) DeleteAttribute(g_refItems,"Items."+itm2);
	}

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}
void GiveOneItem()
{
	string itm1 = GetItemIDByOrder(true,nCurScroll1);
	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	if(iq1<=0) return;

	iq1--;
	if(itm1=="Money")
	{
		pchar.Money = sti(pchar.Money)-1;
		g_refItems.Money = sti(g_refItems.Money)+1;
		pchar.stealmoney = sti(pchar.stealmoney)-1;
	}
	else
	{
		for(int i = 0; i <10; i++)
		{
			string sStealItem = "Stealitem" + i;
			if (checkAttribute(pchar, sStealItem))
			{
				if (pchar.(sStealItem) == itm1)
				{
					DeleteAttribute(pchar, sStealItem);
				}
			}
		}		
		TakeNItems(pchar,itm1,-1);
		if( CheckAttribute(g_refItems,"Items."+itm1) )
			g_refItems.Items.(itm1) = 1 + sti(g_refItems.Items.(itm1));
		else	g_refItems.Items.(itm1) = 1;
	}

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}

void TakeItemsCollection()
{
	string itm2 = GetItemIDByOrder(false,nCurScroll2);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);
	if(iq2<=0) return;

	if(itm2=="Money")
	{
		pchar.Money = sti(pchar.Money)+iq2;
		g_refItems.Money = 0;
		pchar.stealmoney = sti(pchar.stealmoney)+iq2;
	}
	else
	{
		for(int i = 0; i <10; i++)
		{
			string sStealItem = "Stealitem" + i;
			if (!checkAttribute(pchar, sStealItem))
			{
				break;
			}
		}		
		pchar.(sStealItem) = itm2;
		TakeNItems(pchar,itm2,iq2);
		DeleteAttribute(g_refItems,"Items."+itm2);
	}

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}

void GiveItemsCollection()
{
	string itm1 = GetItemIDByOrder(true,nCurScroll1);
	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	if(iq1<=0) return;

	if(itm1=="Money")
	{
		pchar.Money = 0;
		g_refItems.Money = sti(g_refItems.Money)+iq1;
		pchar.stealmoney = "";
	}
	else
	{
		for(int i = 0; i <10; i++)
		{
			string sStealItem = "Stealitem" + i;
			if (checkAttribute(pchar, sStealItem))
			{
				if (pchar.(sStealItem) == itm1)
				{
					DeleteAttribute(pchar, sStealItem);
				}
			}
		}
		DeleteAttribute(pchar,"Items."+itm1);
		if( CheckAttribute(g_refItems,"Items."+itm1) )
			g_refItems.Items.(itm1) = iq1 + sti(g_refItems.Items.(itm1));
		else	g_refItems.Items.(itm1) = iq1;
	}

	FillScroll();
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"ONE_ITEMSLIST",-1);
	SendMessage(&GameInterface,"lsl",MSG_INTERFACE_SCROLL_CHANGE,"TWO_ITEMSLIST",-1);
	SetUpDownUsed();
	SetScrollsUsing();
}

void SetDescribeText()
{
	aref arItem;
	string descrStr="";
	bool bYesDescribe = false;

	if(oldCurScrollName=="ONE_ITEMSLIST") {
		bYesDescribe = GetItemRefByOrder(true,nCurScroll1,&arItem);
	}
	if(oldCurScrollName=="TWO_ITEMSLIST") {
		bYesDescribe = GetItemRefByOrder(false,nCurScroll2,&arItem);
	}

	if( !bYesDescribe ) {
		SetFormatedText("DESCRIBEWINDOW","");
		bEnableDown = false;
		return;
	}

	if( CheckAttribute(arItem,"groupID") )
	{
		if(arItem.groupID==GUN_ITEM_TYPE)
		{
			descrStr = GetAssembledString(
				LanguageConvertString(idLngFile,"weapon gun parameters"),
				arItem);
		}
		if(arItem.groupID==BLADE_ITEM_TYPE)
		{
			descrStr = GetAssembledString(
				LanguageConvertString(idLngFile,"weapon blade parameters"),
				arItem);
		}
	}
	if( CheckAttribute(arItem,"describe") )
	{
		if(descrStr!="") {
			descrStr += LanguageConvertString(idLngFile,"new_string");
		}
		descrStr += LanguageConvertString(idLngFile,arItem.describe);
	}

	SetFormatedText("DESCRIBEWINDOW",descrStr);
	bEnableDown = SendMessage(&GameInterface,"lsll",MSG_INTERFACE_MSG_TO_NODE,"DESCRIBEWINDOW", 6, 0);
}

//==============================
// items utilites
//==============================
string GetItemIDByOrder(bool my,int num)
{
	string attrName = "pic"+(num+1);
	if(my)
	{
		if( CheckAttribute(&GameInterface,"one_itemslist."+attrName+".itemID") )
			return GameInterface.one_itemslist.(attrName).itemID;
	}
	else
	{
		if( CheckAttribute(&GameInterface,"two_itemslist."+attrName+".itemID") )
			return GameInterface.two_itemslist.(attrName).itemID;
	}
	return "";
}

bool GetItemRefByOrder(bool my,int num, ref refItem)
{
	aref ar;
	string itemID = GetItemIDByOrder(my,num);
	if(itemID=="Money") itemID=GOLD;
	if( Items_FindItem(itemID,&ar)<0 ) return false;
	refItem = ar;
	return true;
}

int GetItemQntByOrder(bool my,int num)
{
	string itmName = GetItemIDByOrder(my,num);
	if(my)
	{
		if(itmName=="Money") return sti(pchar.Money);
		return GetCharacterItem(pchar,itmName);
	}
	if(itmName=="Money") return sti(g_refItems.Money);
	if( CheckAttribute(g_refItems,"items."+itmName) )
		return sti(g_refItems.items.(itmName));
	return 0;
}

bool CheckConsumedItem(string itemID)
{
	aref arone;
	makearef(arone,pchar.items);
	aref artwo;
	makearef(artwo,g_refItems.items);

	int n=0;
	if( CheckAttribute(arone,itemID) ) n = sti(arone.(itemID));
	if( CheckAttribute(artwo,itemID) ) n = n + sti(artwo.(itemID));

	if(n>1) return true;
	return false;
}

bool CanCharacterTakeItem(bool myCh,string itemName)
{
	if(!myCh) return true;
	if( CheckAttribute(pchar,"items."+itemName) ) return true;
	if( GetChrItemQuantity(pchar)<MAX_ITEM_TAKE ) return true;
	return false;
}

void SetUpDownUsed()
{
	string itmName1 = GetItemIDByOrder(true,nCurScroll1);
	int iq1 = GetItemQntByOrder(true,nCurScroll1);
	string itmName2 = GetItemIDByOrder(false,nCurScroll2);
	int iq2 = GetItemQntByOrder(false,nCurScroll2);

	if(iq1<=0)
	{
		SetSelectable("ONEDOWN_BUTTON",false);
		SetSelectable("ALLDOWN_BUTTON",false);
	}
	else
	{
		SetSelectable("ONEDOWN_BUTTON",true);
		SetSelectable("ALLDOWN_BUTTON",true);
	}

	if(iq2<=0)
	{
		SetSelectable("ONEUP_BUTTON",false);
		SetSelectable("ALLUP_BUTTON",false);
	}
	else
	{
		if( CheckAttribute(pchar,itmName2) || GetAttributesNum(pchar)<MAX_ITEM_TAKE )
		{
			SetSelectable("ONEUP_BUTTON",true);
			SetSelectable("ALLUP_BUTTON",true);
		}
		else
		{
			SetSelectable("ONEUP_BUTTON",false);
			SetSelectable("ALLUP_BUTTON",false);
		}
	}

	if(itmName2=="" || itmName2=="Money" || GetAttributesNum(pchar)<MAX_ITEM_TAKE)
	{
		SetSelectable("SWAP_BUTTON",true);
	}
	else
	{
		if( CheckAttribute(pchar,itmName2) )	SetSelectable("SWAP_BUTTON",true);
		else
		{
			if(itmName1!="" && itmName1!="Money") SetSelectable("SWAP_BUTTON",true);
			else SetSelectable("SWAP_BUTTON",false);
		}
	}

	if( IsQuestUsedItem(itmName1) )
	{
		SetSelectable("SWAP_BUTTON",false);
		SetSelectable("ONEDOWN_BUTTON",false);
		SetSelectable("ALLDOWN_BUTTON",false);
	}

	ThatOneAndEquiped(itmName1,itmName2);

	if(!GetSelectable(GetCurrentNode()))	SetCurrentNode(oldCurScrollName);
}

void SelectButtonLeft(string startBtnNode)
{
	if(GetSelectable(startBtnNode))	SetCurrentNode(startBtnNode);
	else
	{
		switch(startBtnNode)
		{
		case "ONEDOWN_BUTTON":	SelectButtonLeft("INFO_BUTTON"); break;
		case "ONEUP_BUTTON":	SelectButtonLeft("ONEDOWN_BUTTON"); break;
		case "SWAP_BUTTON":		SelectButtonLeft("ONEUP_BUTTON"); break;
		case "ALLUP_BUTTON":	SelectButtonLeft("SWAP_BUTTON"); break;
		case "ALLDOWN_BUTTON":	SelectButtonLeft("ALLUP_BUTTON"); break;
		}
	}
}

void SelectButtonRight(string startBtnNode)
{
	if(GetSelectable(startBtnNode))	SetCurrentNode(startBtnNode);
	else
	{
		switch(startBtnNode)
		{
		case "INFO_BUTTON":		SelectButtonRight("ONEDOWN_BUTTON"); break;
		case "ONEDOWN_BUTTON":	SelectButtonRight("ONEUP_BUTTON"); break;
		case "ONEUP_BUTTON":	SelectButtonRight("SWAP_BUTTON"); break;
		case "SWAP_BUTTON":		SelectButtonRight("ALLUP_BUTTON"); break;
		case "ALLUP_BUTTON":	SelectButtonRight("ALLDOWN_BUTTON"); break;
		}
	}
}

void ThatOneAndEquiped(string itmName1, string itmName2)
{
	if( IsEquipCharacterByItem(pchar,itmName1) )
	{
		if( GetCharacterItem(pchar,itmName1)<=1 )
		{
			SetSelectable("ONEDOWN_BUTTON",false);
		}
		SetSelectable("ALLDOWN_BUTTON",false);
		SetSelectable("SWAP_BUTTON",false);
	}

	if( IsEquipCharacterByItem(g_refItems,itmName2) )
	{
		if( GetCharacterItem(g_refItems,itmName2)<=1 )
		{
			SetSelectable("ONEUP_BUTTON",false);
		}
		SetSelectable("ALLUP_BUTTON",false);
		SetSelectable("SWAP_BUTTON",false);
	}
}

void SetScrollsUsing()
{
	if( CheckAttribute(&GameInterface,"two_itemslist.pic1") ) {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"TWO_ITEMSLIST",0, true);
		SetSelectable("TWO_ITEMSLIST",true);
		SetSelectable("TWO_LEFTSCROLLBUTTON",true);
		SetSelectable("TWO_RIGHTSCROLLBUTTON",true);
	} else {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"TWO_ITEMSLIST",0, false);
		SetSelectable("TWO_ITEMSLIST",false);
		SetSelectable("TWO_LEFTSCROLLBUTTON",false);
		SetSelectable("TWO_RIGHTSCROLLBUTTON",false);
		if( GetCurrentNode()=="TWO_ITEMSLIST" ) {
			SetCurrentNode("ONE_ITEMSLIST");
		}
	}

	if( CheckAttribute(&GameInterface,"one_itemslist.pic1") ) {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"ONE_ITEMSLIST",0, true);
		SetSelectable("ONE_ITEMSLIST",true);
		SetSelectable("ONE_LEFTSCROLLBUTTON",true);
		SetSelectable("ONE_RIGHTSCROLLBUTTON",true);
	} else {
		SendMessage(&GameInterface,"lsll", MSG_INTERFACE_MSG_TO_NODE,"ONE_ITEMSLIST",0, false);
		SetSelectable("ONE_ITEMSLIST",false);
		SetSelectable("ONE_LEFTSCROLLBUTTON",false);
		SetSelectable("ONE_RIGHTSCROLLBUTTON",false);
		if( GetCurrentNode()=="ONE_ITEMSLIST" ) {
			if( GetSelectable("TWO_ITEMSLIST") ) {SetCurrentNode("TWO_ITEMSLIST");}
		}
	}
}

void GoToTopScroll()
{
	if( GetSelectable("ONE_ITEMSLIST") ) SetCurrentNode("ONE_ITEMSLIST");
}

void GoToBottomScroll()
{
	if( GetSelectable("TWO_ITEMSLIST") ) SetCurrentNode("TWO_ITEMSLIST");
}
