// ImageReady-Assisted Preload Script (suitmask.psd) (heavily modified since initial construct)

function newImage(arg)
	{
	if (document.images)
		{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
		}
	}

function preloadImages()
	{
	if (document.images)
		{
		prop_gloves_off = newImage("images/prop-gloves-off.gif");
		prop_codpiece_off = newImage("images/prop-cod-piece-off.gif");
		prop_helmet_off = newImage("images/prop-helmet-off.gif");
		prop_controlchestplate_off = newImage("images/prop-control-chestplate-off.gif");
		prop_soundeffects_off = newImage("images/prop-sound-effects-off.gif");
		prop_boots_off = newImage("images/prop-boots-off.gif");
		prop_shinguards_off = newImage("images/prop-shinguards-off.gif");
		prop_bodysuit_off = newImage("images/prop-body-suit-off.gif");
		prop_chestarmour_off = newImage("images/prop-chest-armour-off.gif");
		prop_hermeticseal_off = newImage("images/prop-hermetic-seal-off.gif");
		prop_lightsabre_off = newImage("images/prop-lightsabre-off.gif");
		prop_underthesuit_off = newImage("images/prop-under-the-suit-off.gif");
		prop_systemfunctionbelt_off = newImage("images/prop-systemfunctionbelt-off.gif");

		prop_gloves_on = newImage("images/prop-gloves-on.gif");
		prop_codpiece_on = newImage("images/prop-cod-piece-on.gif");
		prop_helmet_on = newImage("images/prop-helmet-on.gif");
		prop_controlchestplate_on = newImage("images/prop-control-chestplate-on.gif");
		prop_soundeffects_on = newImage("images/prop-sound-effects-on.gif");
		prop_boots_on = newImage("images/prop-boots-on.gif");
		prop_shinguards_on = newImage("images/prop-shinguards-on.gif");
		prop_bodysuit_on = newImage("images/prop-body-suit-on.gif");
		prop_chestarmour_on = newImage("images/prop-chest-armour-on.gif");
		prop_hermeticseal_on = newImage("images/prop-hermetic-seal-on.gif");
		prop_lightsabre_on = newImage("images/prop-lightsabre-on.gif");
		prop_underthesuit_on = newImage("images/prop-under-the-suit-on.gif");
		prop_systemfunctionbelt_on = newImage("images/prop-systemfunctionbelt-on.gif");

		preloadFlag = true;
		}
	}

var preloadFlag = false;
var minO = 25;
var maxO = 100;
var stepO = 5;
var buildingLinks = new Array();
var linkOpacity = new Array;

for (x in buildingLinks)
	{
	linkOpacity[x] = new Array(2);
	// to start with, both target and current opacity is 100
	linkOpacity[x][0] = maxO;	//current
	linkOpacity[x][1] = maxO;	//target
	}

function changeImages()
	{
	if (document.images && (preloadFlag == true))
		{
		propName = changeImages.arguments[0];
		bitFlag = changeImages.arguments[1];

		if(bitFlag == 1)
			{
			var divLink = document.getElementById('div-' + propName);
			divLink.style.backgroundImage = "url(images/prop-" + propName + "-on.gif)";
			setOpacity(minO, propName, 100);
			}
		else
			{
			setOpacity(maxO, propName, 100);
			}
		}
	}

function setOpacity(newAmount, divName, startAt)
	{
	if (startAt == null)
		{
		startAt = maxO;
		}

	var bFound = 0;
	for (x in buildingLinks)
		{
		if(divName == buildingLinks[x])
			{
			bFound = 1;
			break;
			}
		}

	if(bFound == 0)
		{
		var iNew = buildingLinks.length;
		x = iNew;
		buildingLinks[iNew] = divName;

		linkOpacity[iNew] = new Array(2);
		linkOpacity[iNew][0] = startAt;	//current
		linkOpacity[iNew][1] = startAt;	//current
		}

	if(linkOpacity[x][1] == linkOpacity[x][0])
		{
		var kickOff = true;
		}
	else
		{
		var kickOff = false;
		}

	linkOpacity[x][1] = newAmount;		// Set the new target.

	if(kickOff)
		{
		opacity(x);
		}
	}

function opacity(x)
	{
	//Based on work by:
	//http://www.brainerror.net/scripts_js_blendtrans.php

	//speed for each frame
	var speed = Math.round(2000/100);

	currentAmount = linkOpacity[x][0];
	targetAmount = linkOpacity[x][1];

	//determine the direction for the blending, if start and end are the same nothing happens
	if(currentAmount > targetAmount)
		{
		currentAmount = currentAmount-stepO;
		changeOpac(currentAmount, buildingLinks[x])
		}
	else if(currentAmount < targetAmount)
		{
		currentAmount = currentAmount+(stepO/2);
		changeOpac(currentAmount, buildingLinks[x])
		}

	// In case it gets out of hand...
	if(currentAmount >= maxO)
		{
		changeOpac(maxO, buildingLinks[x])
		currentAmount = maxO;
		targetAmount = maxO;
		var divLink = document.getElementById('div-' + buildingLinks[x]);
		if(divLink != null)
			{
			divLink.style.backgroundImage = "none";
			}
		}
	else if(currentAmount <= minO)
		{
		changeOpac(minO, buildingLinks[x]);
		currentAmount = minO;
		targetAmount = minO;
		}

	linkOpacity[x][0] = currentAmount;

	if(currentAmount != targetAmount)
		{
		setTimeout("opacity(" + x + ")", speed);
		}
	}

//change the opacity for different browsers
function changeOpac(op, id)
	{
	var theObject = document.getElementById(id).style;
	theObject.opacity = (op / 100);
	theObject.MozOpacity = (op / 100);
	theObject.KhtmlOpacity = (op / 100);
	theObject.filter = "alpha(opacity=" + op + ")";
	}