#Include "TextLib" as TL
#Include "MathLib" as ML

main() {
	declare ButtonQuit <=> Page.GetFirstChild("buttonquit");
	EnableMenuNavigation(True, False, ButtonQuit, 9);
	
	declare Integer CurrentCategory = 0;
	declare Integer CurrentSlot = -1;
	
	while(True) {
		foreach(Event in PendingEvents) {
			switch(Event.Type) {
				case CMlEvent::Type::MenuNavigation : {
					switch (Event.MenuNavAction) {
						case CMlEvent::EMenuNavAction::Up : {
							if (CurrentSlot > 0) {
								CurrentSlot = ML::Max(CurrentSlot-4, 0);
								SelectSlot(CurrentSlot);
							}
						}
						case CMlEvent::EMenuNavAction::Right : {
							if (CurrentSlot < InventoryContent[CurrentCategory].SlotsItems.count-1) {
								CurrentSlot = ML::Min(CurrentSlot+1, InventoryContent[CurrentCategory].SlotsItems.count-1);
								SelectSlot(CurrentSlot);
							}
						}
						case CMlEvent::EMenuNavAction::Left : {
							if (CurrentSlot > 0) {
								CurrentSlot = ML::Max(CurrentSlot-1, 0);
								SelectSlot(CurrentSlot);
							}
						}
						case CMlEvent::EMenuNavAction::Down : {
							if (CurrentSlot < InventoryContent[CurrentCategory].SlotsItems.count-1) {
								CurrentSlot = ML::Min(CurrentSlot+4, InventoryContent[CurrentCategory].SlotsItems.count-1);
								SelectSlot(CurrentSlot);
							}
						}
						case CMlEvent::EMenuNavAction::Select : {
							if (CurrentSlot == -1)
								continue;
							if (InventoryContent[CurrentCategory].SlotsItems[CurrentSlot].MaxLevel == 0)
								DropSelectedSlot();
							else
								EquipSelectedSlot();
						}
						case CMlEvent::EMenuNavAction::Cancel :	Quit();
						case CMlEvent::EMenuNavAction::PageUp : {
							if (CurrentCategory > 0) {
								CurrentCategory -= 1;
								SetCategory(CurrentCategory);
							}
						}
						case CMlEvent::EMenuNavAction::PageDown : {
							if (CurrentCategory < InventoryContent.count-1 && InventoryContent[CurrentCategory+1] != Null) {
								CurrentCategory += 1;
								SetCategory(CurrentCategory);
							}
						}
					}
				}
				case CMlEvent::Type::MouseClick : {
					switch(Event.ControlId) {
						case "Button_Equip" :	EquipSelectedSlot();
						case "Button_Drop" : 	DropSelectedSlot();
						case "buttonquit" : 	Quit();
						case "quadcardbg" : {
							CurrentSlot = TL::ToInteger(Event.Control.DataAttributeGet("itemid"));
							SelectSlot(CurrentSlot);
						}
						case "quadcategoryiconbg" : {
							CurrentCategory = TL::ToInteger(Event.Control.DataAttributeGet("categoryid"));
							SetCategory(CurrentCategory);
						}
					}
				}
			}
		}
		yield;
	}
}