// Reloading the images to randomise the flashiness
// Requires upload of both header.tpl and index_top.tpl

var preloadFlag = false;
var aryLoadedLED = new Array();
var preloaded = new Array();
var skinLocation = false;

function changeImages() 	
	{
	if (document.images && preloadFlag && skinLocation!=false) 
		{
		// Only if we can access images, have preloaded, and have a skinlocation
		for (var i=0; i<changeImages.arguments.length; i+=2) 
			{						
			document.getElementById(changeImages.arguments[i]).src = skinLocation + changeImages.arguments[i+1];
			}
		}
	}	

function preloadImagesList() 
	{
	// Any number of images can be chucked this way, which will then be
	// preloaded and cached by the browser
	if (!skinLocation) 
		{
		return false;
		}
	for (var i = 0; i < arguments.length; i++)
		{
        	preloaded[i] = document.createElement('img');
        	preloaded[i].setAttribute('src',skinLocation + arguments[i]);
    		};
    	return true;
	};

function getSkinLocation(sampleImage)
	{
	// the sampleImage is purely used to find out the location of the skin's images
	// It should be an ID of an image used for this skin
	currentSrc = document.getElementById(sampleImage);
	
	if (currentSrc) 
		{
		startOfFileName = currentSrc.src.lastIndexOf('/') + 1;
		return currentSrc.src.substring(0,startOfFileName);
		}
	else
		{
		return false;
		}	
	}

function preloadImages()
	{
	// This one is kicked off onLoad of body.
	// It preloads all images for flashy letters and blinky LEDs
	// and then starts the process of restarting the letter animations
	
	if (document.images)
		{
		// We need the image directory for this skin before anything else
		skinLocation = getSkinLocation("title-1");
		
		preloadFlag = preloadImagesList(
				'llamalantitle_01b.gif',
				'llamalantitle_02b.gif',
				'llamalantitle_03b.gif',
				'llamalantitle_04b.gif',
				'llamalantitle_05b.gif',
				'llamalantitle_06b.gif',
				'llamalantitle_07b.gif',
				'llamalantitle_08b.gif',
				'llamalantitle_08b.gif',
				'llamalantitle_08b.gif',
				'switch-led-attending.gif',
				'switch-led-notattending.gif',
				'switch-led-toPay.gif'
			      );

		if(preloadFlag)
			{
			setInterval("reloadTitle();",600);
			}
		}
	}

function reloadTitle()
	{
	// Randomly picks one of the eight LLAMALAN letters
	// and flashes it
	var x = Math.round(Math.random()*7)+1;
	whackLetter(x);
	}

function whackLetter(x)
	{
	// Restarts the animation for the xth letter
	changeImages("title-" + x, "llamalantitle_0" + x + ".gif");
	}

function whackLetter2(x)
	{
	// Flashes the xth letter 	
	changeImages("title-" + x, "llamalantitle_0" + x + "b.gif");
	}

function blinkyLight(x, y)
	{
	// blink the light every 1 in 3 function calls
	var randChance = Math.round(Math.random()*2);

	if(randChance == 2)
		{
		changeImages(x, y);
		}
	}
	
function evalBlinky(x, y)
	{
	// evaluate if this light is supposed to blink
	// which is the case for any LEDs for future events
	// If it passes the test, kick off the interval
	
	// Pick random interval time between 3 and 6 seconds
	
	reloadIntervalTime = Math.round(Math.random()*3) + 3;
	
	if(y.lastIndexOf('attended') == -1 && !aryLoadedLED[x])
		{
		// NOTE: the array is VERY important.
		// without record, this function
		// is reiterated with every image load:
		// every time it is updated!
		// The script will create a resonance cascade event 
		// the likes have never been seen
		// since Black Mesa!
		
		aryLoadedLED[x] = 1;
		
		// Phew - narrowly averted.
		
		jsCommand = "blinkyLight('" + x + "', '" + y + "')";
		setInterval(jsCommand, reloadIntervalTime * 300);
		}
	}
