// Inventory Tab Handler.
// Worthless Bums 2012.


function getGuardModeTooltip ()
{//echo("getGuardModeTooltip()");
	return "Guard Mode (" @ strreplace($GuardModeBind, "keyboard ", "") @ "): Fire during the enemy turn.";
}
function getReloadTooltip ()
{//echo("getReloadTooltip()");
	return "Reload (" @ strreplace($ReloadBind, "keyboard ", "") @ "): Costs all AP; need at least 1 AP.";
}
// Updates Control Panel tooltips based on current keybindings.
function updateControlPanelTooltips ()
{//echo("updateControlPanelTooltips()");
	// Hook other UI update stuff here!
	initializeFirearmUiButtons();
	initializeArmorUiButtons();
	
	// Facing buttons.
	FaceNorthButton.tooltip = "Face Up (" @ strreplace($FaceUpBind, "keyboard ", "") @ ")";
	FaceSouthButton.tooltip = "Face Down (" @ strreplace($FaceDownBind, "keyboard ", "") @ ")";
	FaceWestButton.tooltip = "Face Left (" @ strreplace($FaceLeftBind, "keyboard ", "") @ ")";
	FaceEastButton.tooltip = "Face Right (" @ strreplace($FaceRightBind, "keyboard ", "") @ ")";
	
	// Move buttons.
	MoveNorthButton.tooltip = "Move Up (" @ strreplace($UpBind, "keyboard ", "") @ ")";
	MoveSouthButton.tooltip = "Move Down (" @ strreplace($DownBind, "keyboard ", "") @ ")";
	MoveWestButton.tooltip = "Move Left (" @ strreplace($LeftBind, "keyboard ", "") @ ")";
	MoveEastButton.tooltip = "Move Right (" @ strreplace($RightBind, "keyboard ", "") @ ")";
	
	// Fire Control buttons.
	FireButton.tooltip = "Fire (" @ strreplace($PrimaryBind, "keyboard ", "") @ ")";
	ReloadButton.tooltip = "Reload (" @ strreplace($ReloadBind, "keyboard ", "") @ "): Costs all AP; need at least 1 AP.";
	
	// Interaction buttons.
	InteractButton.tooltip = "Interact (" @ strreplace($InteractBind, "keyboard ", "") @ ")";
	GrabButton.tooltip = "Grab (" @ strreplace($GrabBind, "keyboard ", "") @ ")";
	
	// Mode buttons.
	GuardModeButton.tooltip = "Guard Mode (" @ strreplace($GuardModeBind, "keyboard ", "") @ "): Fire during the enemy turn.";
	
	// Menu buttons.
	// NOTE: Add escape/main menu button? Ugh, clutter!
	PromotionButton.tooltip = "Promotions (" @ strreplace($PromotionsBind, "keyboard ", "") @ ")";
	
	// Squad/Turn handling buttons.
	EndTurnButton.tooltip = "End Turn (" @ strreplace($EndTurnBind, "keyboard ", "") @ ")";
	SquadOverviewButton.tooltip = "Overview (" @ strreplace($SquadInfoBind, "keyboard ", "") @ ")";
	
	ElevatorButton.tooltip = "Use Elevator (" @ strreplace($EnterLevelBind, "keyboard ", "") @ ")";
}

// Updates all GUI elements that need re-positioning on aspect ratio changes.
function updateGuiElementPositioning ()
{//echo("updateGuiElementPositioning()");
	updateControlPanelPosition();
	updateTalentBarsPositions();
	updateFirearmReadoutPosition();
	schedule(10, 0, "updateActiveCharacterReadoutPosition");	// NOTE: This relies on function updateTalentBarsPositions()!
	//schedule(10, 0, "updateDeckStatisticsPosition");			// NOTE: This relies on function updateTalentBarsPositions()!
	schedule(10, 0, "updateAmmoReadoutPosition");				// NOTE: This relies on function updateTalentBarsPositions()!
	updateMissionObjectivesText();
	updateDeckStats();
	updateDeckInformationPanelPosition();
	updateWeaponAndArmorDisplayPositions();
	updateInventorySlotsPosition();
}
// Updates Control Panel position based on screen resolution.
function updateControlPanelPosition ()
{//echo("updateControlPanelPosition()");
	%x = getWord($Game::Resolution, 0) - getWord(ControlPanel, 0) - 16 - 384 - 48;
	%y = getWord($Game::Resolution, 1) - getWord(ControlPanel, 1) - 16 -  31;
	ControlPanel.setPosition(%x, %y);
}
// Updates talent bars' positions based on screen resolution.
function updateTalentBarsPositions ()
{//echo("updateTalentBarsPositions()");
	%x = getWord($Game::Resolution, 0);
	%y = getWord($Game::Resolution, 1);
	%x -= getWord(leaderTalentBar.extent, 0) + 192;
	%y -= getWord(leaderTalentBar.extent, 1) + 42;
	leaderTalentBar.setPosition(%x, %y);
	scoutTalentBar.setPosition(%x, %y);
	grenadierTalentBar.setPosition(%x, %y);
	supportTalentBar.setPosition(%x, %y);
}
// Updates active character readout position based on screen resolution.
function updateActiveCharacterReadoutPosition ()
{//echo("updateActiveCharacterReadoutPosition()");
	// Get Leader Talent Bar position.
	%x = getWord(leaderTalentBar.getPosition(), 0);
	%y = getWord(leaderTalentBar.getPosition(), 1);
	
	// Screen padding.
	%xPadding = 9;
	%yPadding = 13;
	// Pad based on Control Panel extent.
	%x += %xPadding;
	%y -= %yPadding;
	
	// Set Control Panel position.
	activeCharacterReadout.setPosition(%x, %y);
}
// Updates Deck Information Panel position based on screen resolution.
function updateDeckInformationPanelPosition ()
{//echo("updateDeckInformationPanelPosition()");
	// Update position.
	//DeckInformationPanel.position = 0 SPC (getWord($Game::Resolution, 1) / 2);
}
// Updates Weapon and Armor Display positions based on screen resolution.
function updateWeaponAndArmorDisplayPositions ()
{//echo("updateWeaponAndArmorDisplayPositions()");
	// Update inventoryTabControl position.
	%xPos = getWord($Game::Resolution, 0) - getWord(inventoryTabControl.extent, 0);
	inventoryTabControl.position = %xPos SPC 10;
	// Update InventoryTabToggleButton position.
	%xPos = getWord($Game::Resolution, 0) - getWord(InventoryTabToggleButton.extent, 0);
	InventoryTabToggleButton.position = %xPos SPC 14;
}
// Updates Inventory Slots Panel position based on screen resolution.
function updateInventorySlotsPosition ()
{//echo("updateInventorySlotsPosition()");
	// Update inventoryTabControl position.
	%xPos = getWord($Game::Resolution, 0) - getWord(InventorySlotsPanel.extent, 0) - 21;
	%yPos = 64;
	inventoryTabControl.position = %xPos SPC %yPos;
	// Hook the Money Counter position to the inventory tab slots!
	updateMoneyCounter();
	moneyCounterButton.position = %xPos SPC (%yPos - 32);
}
// Updates Ammo Rreadout position based on screen resolution.
function updateAmmoReadoutPosition ()
{//echo("updateAmmoReadoutPosition()");
}

function updateFirearmReadoutPosition ()
{//echo("updateFirearmReadoutPosition()");
	%x = getWord($Game::Resolution, 0) - 174;
	%y = getWord($Game::Resolution, 1) - 78;
	FirearmReadout.setPosition(%x, %y);
	FirearmArmorUIBackPanel.setPosition(getWord($Game::Resolution, 0) - getWord(FirearmArmorUIBackPanel.extent, 0) + 177,	// 811
										getWord($Game::Resolution, 1) - getWord(FirearmArmorUIBackPanel.extent, 1) + 7);	// 577
}

// Toggles Inventory Tab side panel.
function toggleSidePanel ()
{//echo("toggleSidePanel()");
	// Check flag.
	if ($inventorySidePanelPushed)
	{
		$inventorySidePanelPushed = false;
		inventoryTabControl.setVisible(0);
	}
	else
	{
		$inventorySidePanelPushed = true;
		inventoryTabControl.setVisible(1);
	}
}

// Toggles Control Panel.
function toggleControlPanel ()
{//echo("toggleControlPanel()");
	// Check flag.
	if ($controlPanelPushed)
	{
		$controlPanelPushed = false;
		ControlPanel.setVisible(0);
	}
	else
	{
		$controlPanelPushed = true;
		ControlPanel.setVisible(1);
	}
}

// Toggles Scrolling Event Log.
function toggleScrollingEventLog ()
{//echo("toggleScrollingEventLog()");
	// Check flag.
	if ($scrollingEventLogPushed)
	{
		$scrollingEventLogPushed = false;
		partiallyOpaquePanel.setVisible(0);
	}
	else
	{
		$scrollingEventLogPushed = true;
		partiallyOpaquePanel.setVisible(1);
	}
}
function getCharacterClassName (%characterClass)
{
	switch (%characterClass)
	{
		case 0:	return "Leader";
		case 1:	return "Scout";
		case 2:	return "Grenadier";
		case 3:	return "Support";
		case 4:	return "Engineer";
	}
}
// Updates activeCharacterReadout.
function updateActiveCharacterReadout ()
{//echo("updateActiveCharacterReadout()");
	// Need to check for squad death!
	enforceSquadPersistence();
	if (!$leaderPersist		&&
		!$scoutPersist		&&
		!$grenadierPersist	&&
		!$supportPersist)
	{
		return;
	}
	// Don't update with a dead squad member!
	if (!isObject($playerSquad[$activeCharacter]))
	{
		return;
	}
	%characterClass = getCharacterClassName($playerSquad[$activeCharacter].characterClass);
	%text = /*"\c8" @ */getRankName($playerSquad[$activeCharacter].rank) @ ". " @ $playerSquad[$activeCharacter].name SPC "(" @ %characterClass @ ")";
	activeCharacterRankAndNameReadout.setText(%text);
	
	// Set ActiveCharacterExperienceBarText.
	%nextExperienceLevelCap = getExperienceLevelCap($playerSquad[$activeCharacter].level);
	%text = $playerSquad[$activeCharacter].experience @ " / " @ %nextExperienceLevelCap @ " XP";
	ActiveCharacterExperienceBarText.setText(%text);
	
	// Update ActiveCharacterExperienceBar.
	%barHeight = 13;
	%barWidth = $playerSquad[$activeCharacter].experience / %nextExperienceLevelCap;
	%barWidth = mFloor(%barWidth * 174);
	ActiveCharacterExperienceBar.extent = %barWidth SPC %barHeight;
	
	// Update Ammo Readout.
	updateAmmoReadout();
}

// Updates Ammo Readout.
function updateAmmoReadout ()
{//echo("updateAmmoReadout()");
	// Leader.
	// Set Ammo Bar text.
	%weapon = $playerSquad[0].weapon;
	%text = %weapon.currentAmmo @ " / " @ %weapon.capacity @ " / " @ $playerSquad[0].ammo @ " Ammo";	// Why doesn't "\c8" Work here?!?
	LeaderAmmoBarText.setText(%text);
	// Update Ammo Bar extent.
	%barHeight = 13;
	%barWidth = %weapon.currentAmmo / %weapon.capacity;
	// For additional ammo over magazine capacity (e.g. new magazine reload mechanics).
	if (%barWidth > 1)
	{
		%barWidth = 1;
	}
	%barWidth = mFloor(%barWidth * 174);
	LeaderAmmoBar.extent = %barWidth SPC %barHeight;
	
	// Scout.
	// Set Ammo Bar text.
	%weapon = $playerSquad[1].weapon;
	%text = %weapon.currentAmmo @ " / " @ %weapon.capacity @ " / " @ $playerSquad[1].ammo @ " Ammo";	// Why doesn't "\c8" Work here?!?
	ScoutAmmoBarText.setText(%text);
	// Update Ammo Bar extent.
	%barHeight = 13;
	%barWidth = %weapon.currentAmmo / %weapon.capacity;
	// For additional ammo over magazine capacity (e.g. new magazine reload mechanics).
	if (%barWidth > 1)
	{
		%barWidth = 1;
	}
	%barWidth = mFloor(%barWidth * 174);
	ScoutAmmoBar.extent = %barWidth SPC %barHeight;
	
	// Grenadier.
	// Set Ammo Bar text.
	%weapon = $playerSquad[2].weapon;
	%text = %weapon.currentAmmo @ " / " @ %weapon.capacity @ " / " @ $playerSquad[2].ammo @ " Ammo";	// Why doesn't "\c8" Work here?!?
	GrenadierAmmoBarText.setText(%text);
	// Update Ammo Bar extent.
	%barHeight = 13;
	%barWidth = %weapon.currentAmmo / %weapon.capacity;
	// For additional ammo over magazine capacity (e.g. new magazine reload mechanics).
	if (%barWidth > 1)
	{
		%barWidth = 1;
	}
	%barWidth = mFloor(%barWidth * 174);
	GrenadierAmmoBar.extent = %barWidth SPC %barHeight;
	
	// Support.
	// Set Ammo Bar text.
	%weapon = $playerSquad[3].weapon;
	%text = %weapon.currentAmmo @ " / " @ %weapon.capacity @ " / " @ $playerSquad[3].ammo @ " Ammo";	// Why doesn't "\c8" Work here?!?
	SupportAmmoBarText.setText(%text);
	// Update Ammo Bar extent.
	%barHeight = 13;
	%barWidth = %weapon.currentAmmo / %weapon.capacity;
	// For additional ammo over magazine capacity (e.g. new magazine reload mechanics).
	if (%barWidth > 1)
	{
		%barWidth = 1;
	}
	%barWidth = mFloor(%barWidth * 174);
	SupportAmmoBar.extent = %barWidth SPC %barHeight;
}

// Updates the Inventory Tab information.
function updateInventoryTabInformation ()
{//echo("updateInventoryTabInformation()");
	// Don't update if the active character doesn't exist!
	if (!isObject($playerSquad[$activeCharacter]))
	{
		return;
	}
	// Don't update if weapons haven't sync'd!
	if (($leaderPersist && ($playerSquad[0].weapon).imagemapname $= "")		||
		($scoutPersist && ($playerSquad[1].weapon).imagemapname $= "")		||
		($grenadierPersist && ($playerSquad[2].weapon).imagemapname $= "")	||
		($supportPersist && ($playerSquad[3].weapon).imagemapname $= ""))
	{
		return;
	}
	
	// Hook updates to inventory tab! Hackerish? Yeah...
	updateSquadInformationPanels();
	updateActiveCharacterReadout();
	//updateArmorUI();
	//updateAmmoReadout();
	
	// Set readout information text.
	// Set the readout information.
	// NOTE: Should this be hooked to the $activeCharacter variable or a separate one?
	if ($playerSquad[$activeCharacter].characterClass == 0	||
		$playerSquad[$activeCharacter].characterClass == 1	||
		$playerSquad[$activeCharacter].characterClass == 2	||
		$playerSquad[$activeCharacter].characterClass == 3	||
		$playerSquad[$activeCharacter].characterClass == 4)
	{
		// Gather squad member information.
		%pSceneLayer = $playerSquad[$activecharacter];
		%characterName = %player.name;
		%weapon = %player.weapon;
		%armor = %player.armor;
		switch ($playerSquad[$activeCharacter].characterClass)
		{
			case 0:	%characterClass = "Leader";
			case 1:	%characterClass = "Scout";
			case 2:	%characterClass = "Grenadier";
			case 3:	%characterClass = "Support";
			case 4:	%characterClass = "Engineer";
		}
		
		// Calculate bonus weapon accuracy.
		%weaponAccuracy = 0;
		switch ($playerSquad[$activeCharacter].characterClass)
		{
			case 0:	// Leader.
				if ($leaderWeaponAttachment == 1)
				{
					%weaponAccuracy = 10;		// +10% firearm accuracy from Laser attachment.
				}
				
			case 1:	// Scout.
				if ($scoutWeaponAttachment == 1)
				{
					%weaponAccuracy = 10;		// +10% firearm accuracy from Laser attachment.
				}
				
			case 2:	// Grenadier.
				if ($grenadierWeaponAttachment == 1)
				{
					%weaponAccuracy = 10;		// +10% firearm accuracy from Laser attachment.
				}
				
			case 3:	// Support.
				if ($supportWeaponAttachment == 1)
				{
					%weaponAccuracy = 10;		// +10% firearm accuracy from Laser attachment.
				}
				
			case 4:	// Engineer.
				if ($engineerWeaponAttachment == 1)
				{
					%weaponAccuracy = 10;		// +10% firearm accuracy from Laser attachment.
				}
		}
		%weaponAccuracy += %player.arms * $armsAccuracyModifier;
		// Ammo status text color.
		if (%player.ammo >= %weapon.ammo)
		{
			%ammoColor = "";
			%totalAmmoColor = $greyColor;
		}
		else
		{
			%ammoColor = $damageColor;
			%totalAmmoColor = $damageColor;
		}
		
		if ($leaderPersist	&&
			isObject($playerSquad[0]))
		{//echo("($playerSquad[0].weapon).imagemapname =" SPC ($playerSquad[0].weapon).imagemapname);
			// Get weapon and armor.
			%weapon = $playerSquad[0].weapon;
			%armor = $playerSquad[0].armor;
			// Set readout to be visible.
			LeaderLoadoutReadout.visible = true;
			// Set weapon bitmap.
			LeaderWeaponIconBitmap.setBitmap(getWeaponBitmapFromImagemap(($playerSquad[0].weapon).imagemapname));
			// Set weapon info text!
			LeaderWeaponNameText.setText("\c4" @ %weapon.name);
			LeaderWeaponDamageText.setText(%weapon.damage);
			LeaderWeaponRangeText.setText(%weapon.range);
			LeaderWeaponAccuracyText.setText(%weapon.accuracy);
			LeaderWeaponFireAmmoText.setText(%weapon.ammo);
			LeaderWeaponCapacityText.setText(%weapon.capacity);
			LeaderWeaponApToFireText.setText(%weapon.fireCost);
			LeaderWeaponApToReloadText.setText(%weapon.reload);
			// Set armor bitmap.
			LeaderArmorIconBitmap.setBitmap(getArmorBitmapFromImagemap(($playerSquad[0].armor).imagemapname));
			// Set armor info text!
			LeaderArmorNameText.setText("\c4" @ %armor.name);
			LeaderArmorArmorRatingText.setText(%armor.armorRating);
			LeaderArmorSpeedText.setText(%armor.speed);
			LeaderArmorRepairText.setText(%armor.repair);
			LeaderArmorFireResistanceText.setText(%armor.fireResistance);
			LeaderArmorFrostResistanceText.setText(%armor.frostResistance);
			LeaderArmorAcidResistanceText.setText(%armor.acidResistance);
			LeaderArmorExplosiveResistanceText.setText(%armor.explosiveResistance);
		}
		else
		{
			// Set readout to not be visible.
			LeaderLoadoutReadout.visible = false;
		}
		
		if ($scoutPersist	&&
			isObject($playerSquad[1]))
		{//echo("($playerSquad[1].weapon).imagemapname =" SPC ($playerSquad[1].weapon).imagemapname);
			// Get weapon and armor.
			%weapon = $playerSquad[1].weapon;
			%armor = $playerSquad[1].armor;
			// Set readout to be visible.
			ScoutLoadoutReadout.visible = true;
			// Set weapon bitmap.
			ScoutWeaponIconBitmap.setBitmap(getWeaponBitmapFromImagemap(($playerSquad[1].weapon).imagemapname));
			// Set weapon info text!
			ScoutWeaponNameText.setText("\c4" @ %weapon.name);
			ScoutWeaponDamageText.setText(%weapon.damage);
			ScoutWeaponRangeText.setText(%weapon.range);
			ScoutWeaponAccuracyText.setText(%weapon.accuracy);
			ScoutWeaponFireAmmoText.setText(%weapon.ammo);
			ScoutWeaponCapacityText.setText(%weapon.capacity);
			ScoutWeaponApToFireText.setText(%weapon.fireCost);
			ScoutWeaponApToReloadText.setText(%weapon.reload);
			// Set armor bitmap.
			ScoutArmorIconBitmap.setBitmap(getArmorBitmapFromImagemap(($playerSquad[1].armor).imagemapname));
			// Set armor info text!
			ScoutArmorNameText.setText("\c4" @ %armor.name);
			ScoutArmorArmorRatingText.setText(%armor.armorRating);
			ScoutArmorSpeedText.setText(%armor.speed);
			ScoutArmorRepairText.setText(%armor.repair);
			ScoutArmorFireResistanceText.setText(%armor.fireResistance);
			ScoutArmorFrostResistanceText.setText(%armor.frostResistance);
			ScoutArmorAcidResistanceText.setText(%armor.acidResistance);
			ScoutArmorExplosiveResistanceText.setText(%armor.explosiveResistance);
		}
		else
		{
			// Set readout to not be visible.
			ScoutLoadoutReadout.visible = false;
		}
		
		if ($grenadierPersist	&&
			isObject($playerSquad[2]))
		{//echo("($playerSquad[2].weapon).imagemapname =" SPC ($playerSquad[2].weapon).imagemapname);
			// Get weapon and armor.
			%weapon = $playerSquad[2].weapon;
			%armor = $playerSquad[2].armor;
			// Set readout to be visible.
			GrenadierLoadoutReadout.visible = true;
			// Set weapon bitmap.
			GrenadierWeaponIconBitmap.setBitmap(getWeaponBitmapFromImagemap(($playerSquad[2].weapon).imagemapname));
			// Set weapon info text!
			GrenadierWeaponNameText.setText("\c4" @ %weapon.name);
			GrenadierWeaponDamageText.setText(%weapon.damage);
			GrenadierWeaponRangeText.setText(%weapon.range);
			GrenadierWeaponAccuracyText.setText(%weapon.accuracy);
			GrenadierWeaponFireAmmoText.setText(%weapon.ammo);
			GrenadierWeaponCapacityText.setText(%weapon.capacity);
			GrenadierWeaponApToFireText.setText(%weapon.fireCost);
			GrenadierWeaponApToReloadText.setText(%weapon.reload);
			// Set armor bitmap.
			GrenadierArmorIconBitmap.setBitmap(getArmorBitmapFromImagemap(($playerSquad[2].armor).imagemapname));
			// Set armor info text!
			GrenadierArmorNameText.setText("\c4" @ %armor.name);
			GrenadierArmorArmorRatingText.setText(%armor.armorRating);
			GrenadierArmorSpeedText.setText(%armor.speed);
			GrenadierArmorRepairText.setText(%armor.repair);
			GrenadierArmorFireResistanceText.setText(%armor.fireResistance);
			GrenadierArmorFrostResistanceText.setText(%armor.frostResistance);
			GrenadierArmorAcidResistanceText.setText(%armor.acidResistance);
			GrenadierArmorExplosiveResistanceText.setText(%armor.explosiveResistance);
		}
		else
		{
			// Set readout to not be visible.
			GrenadierLoadoutReadout.visible = false;
		}
		
		if ($supportPersist	&&
			isObject($playerSquad[3]))
		{//echo("($playerSquad[3].weapon).imagemapname =" SPC ($playerSquad[3].weapon).imagemapname);
			// Get weapon and armor.
			%weapon = $playerSquad[3].weapon;
			%armor = $playerSquad[3].armor;
			// Set readout to be visible.
			SupportLoadoutReadout.visible = true;
			// Set weapon bitmap.
			SupportWeaponIconBitmap.setBitmap(getWeaponBitmapFromImagemap(($playerSquad[3].weapon).imagemapname));
			// Set weapon info text!
			SupportWeaponNameText.setText("\c4" @ %weapon.name);
			SupportWeaponDamageText.setText(%weapon.damage);
			SupportWeaponRangeText.setText(%weapon.range);
			SupportWeaponAccuracyText.setText(%weapon.accuracy);
			SupportWeaponFireAmmoText.setText(%weapon.ammo);
			SupportWeaponCapacityText.setText(%weapon.capacity);
			SupportWeaponApToFireText.setText(%weapon.fireCost);
			SupportWeaponApToReloadText.setText(%weapon.reload);
			// Set armor bitmap.
			SupportArmorIconBitmap.setBitmap(getArmorBitmapFromImagemap(($playerSquad[3].armor).imagemapname));
			// Set armor info text!
			SupportArmorNameText.setText("\c4" @ %armor.name);
			SupportArmorArmorRatingText.setText(%armor.armorRating);
			SupportArmorSpeedText.setText(%armor.speed);
			SupportArmorRepairText.setText(%armor.repair);
			SupportArmorFireResistanceText.setText(%armor.fireResistance);
			SupportArmorFrostResistanceText.setText(%armor.frostResistance);
			SupportArmorAcidResistanceText.setText(%armor.acidResistance);
			SupportArmorExplosiveResistanceText.setText(%armor.explosiveResistance);
		}
		else
		{
			// Set readout to not be visible.
			SupportLoadoutReadout.visible = false;
		}
	}
	else
	{
		inventoryTabMainPanelReadout.setText("");
	}
	
	// Handles Inventory Tab inventory slot updating.
	handleInventoryTabSlots();
}

// Handles Inventory Tab inventory slot updating.
function handleInventoryTabSlots ()
{//echo("handleInventoryTabSlots()");
	if (!isObject($squadInventory))
	{
		return;
	}
	for (%i = 0; %i < 12; %i++)
	{
		%iconObject = (itemIcon @ (%i + 1));
		if (%i < $squadInventory.getCount())
		{
			%itemType = ($squadInventory.getObject(%i)).itemType;
			%itemBitmap = getItemBitmap(%itemType, ($squadInventory.getObject(%i)).imageMapName);
			%itemBitmap = strreplace(%itemBitmap, ".png", "");
			if (%itemType == -1)		// Weapons.
			{
				//%itemBitmap = %itemBitmap @ "Icon";
			}
			else
			{
				%itemBitmap = getItemBitmap(%itemType, ($squadInventory.getObject(%i)).imageMapName);
				%itemBitmap = strreplace(%itemBitmap, ".png", "");
			}
			%iconObject.setBitmap(%itemBitmap);
			%iconObject.setVisible(true);
		}
		else
		{
			%iconObject.setVisible(false);
		}
	}
}

function leaderSidePanelClicked ()
{//echo("leaderSidePanelClicked()");
	// Select the Leader.
	selectSquadMember(0);
}
function scoutSidePanelClicked ()
{//echo("scoutSidePanelClicked()");
	// Select the Scout.
	selectSquadMember(1);
}
function grenadierSidePanelClicked ()
{//echo("grenadierSidePanelClicked()");
	// Select the Grenadier.
	selectSquadMember(2);
}
function supportSidePanelClicked ()
{//echo("supportSidePanelClicked()");
	// Select the Support.
	selectSquadMember(3);
}

// Toggles the inventory tab.
function toggleInventoryTab ()
{//echo("toggleInventoryTab()");
	/*if (hasInventoryTabMovementEvents())
	{
		return;
	}*/
	
	if ($waitInventoryTab)
	{
		return;
	}
	
	
	
	if ($inventoryTabOut)
	{
		// Play Inventory Tab push in sound effect.
		playAudio("Audio:inventoryTabPushIn", EffectsAudioVolume.value);
		// Push in Inventory Tab.
		pushInInventoryTab();
	}
	else
	{
		// Push Inventory Tab GUI on top!
		Canvas.pushDialog(inventoryTabGui, 0);
		// Play Inventory Tab pull out sound effect.
		playAudio("Audio:inventoryTabPullOut", EffectsAudioVolume.value);
		// Pull out Inventory Tab.
		pullOutInventoryTab();
	}
}

// Clears $inventoryTabMovementEvents.
function clearInventoryTabMovementEvents ()
{//echo("clearInventoryTabMovementEvents()");
	if (isObject($inventoryTabMovementEvents))
	{
		for (%i = 0; %i < $inventoryTabMovementEvents.getCount(); %i++)
		{
			cancel(($inventoryTabMovementEvents.getObject(%i)));
		}
	}
	else
	{
		$inventoryTabMovementEvents = new SimSet();
	}
}

// Returns true iff $inventoryTabMovementEvents is not empty.
function hasInventoryTabMovementEvents ()
{//echo("hasInventoryTabMovementEvents()");
	if (isObject($inventoryTabMovementEvents))
	{//echo("$inventoryTabMovementEvents.getCount() =" SPC $inventoryTabMovementEvents.getCount());
		if ($inventoryTabMovementEvents.getCount() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		$inventoryTabMovementEvents = new SimSet();
		return false;
	}
}

// Unlocks $waitInventoryTab.
function unlockInventoryTab ()
{//echo("unlockInventoryTab()");
	$waitInventoryTab = false;
}

// Pulls out the inventory tab.
function pullOutInventoryTab ()
{//echo("pullOutInventoryTab()");
	// Set flag.
	$inventoryTabOut = true;
	// Show Inventory Tab GUI.
	inventoryTabGui.setVisible(1);
}

// Pushes in the inventory tab.
function pushInInventoryTab ()
{//echo("pushInInventoryTab()");
	// Set flag.
	$inventoryTabOut = false;
	// Hide Inventory Tab GUI.
	inventoryTabGui.setVisible(0);
}

// Trigger context sensitive GUI buttons.
function triggerContextSensitiveUI (%ignoreGrab)
{//echo("triggerContextSensitiveUI()");
//echo("%ignoreGrab =" SPC %ignoreGrab);
	if (!%ignoreGrab	&&
		hasObjectToGrab())
	{
		GrabButton.setVisible(true);
		playAudio("Audio:onItem", EffectsAudioVolume.value);
	}
	else
	{
		GrabButton.setVisible(false);
	}
	if (canEnvironmentInteract())
	{
		InteractButton.setVisible(true);
		playAudio("Audio:onItem", EffectsAudioVolume.value);
	}
	else
	{
		InteractButton.setVisible(false);
	}
	if (squadIsCloseToStairs()	&&
		$elevatorPowered)
	{
		ElevatorButton.setVisible(true);
		triggerElevatorDownAnimations();
		//playAudio("Audio:onItem", EffectsAudioVolume.value);
	}
	else
	{
		ElevatorButton.setVisible(false);
	}
}

//------------------------------------------------------------------

function setSelectedItemGuiPosition ()
{//echo("setSelectedItemGuiPosition()");
	%x = ($SelectedItemSlot % 4) * 32;
	%y = mFloor($SelectedItemSlot / 4) * 32;
	itemSelected.setPosition(%x, %y);
}
function selectNextItem ()
{//echo("selectNextItem()");
	$SelectedItemSlot++;
	if ($SelectedItemSlot >= 12)
	{
		$SelectedItemSlot = 0;
	}
	if ($SelectedItemSlot > $squadInventory.getCount() - 1)
	{
		$SelectedItemSlot = 0;
	}
	setSelectedItemGuiPosition();
}
function selectPreviousItem ()
{//echo("selectPreviousItem()");
	$SelectedItemSlot--;
	if ($SelectedItemSlot < 0)
	{
		$SelectedItemSlot = 11;
	}
	if ($SelectedItemSlot > $squadInventory.getCount())
	{
		$SelectedItemSlot = $squadInventory.getCount() - 1;
	}
	setSelectedItemGuiPosition();
}
function useSelectedItem ()
{//echo("useSelectedItem()");
	if (isObject($squadInventory.getObject($SelectedItemSlot)))
	{
		useInventoryItem($SelectedItemSlot);
	}
	if ($SelectedItemSlot > $squadInventory.getCount() - 1)
	{
		selectPreviousItem();
	}
}
function dropSelectedItem ()
{//echo("dropSelectedItem()");
	if ($SelectedItemSlot < 0)
	{
		$SelectedItemSlot = 11;
	}
	dropItemFromInventory($SelectedItemSlot);
	if ($SelectedItemSlot > $squadInventory.getCount() - 1)
	{
		selectPreviousItem();
	}
}