﻿var revolutions = new Array();
var myImages = new Array();
var bAutoRun = false;

function preloadImages()
{
   for (x =0; x<preloadImages.arguments.length; x++)
   {
       myImages[x] = new Image();
       myImages[x].src = "/sitecollectionimages/" + preloadImages.arguments[x];
   }
}

preloadImages("EnvirolineFrontPage.jpg","P53.jpg","PiltunB.jpg","Ecotech.jpg");


// enter the amount of image revolutions you require - syntax is :
// 	homepageSnippet([image location], [title], [introduction text], [hyperlink]

/*revolutions[0] = new homepageSnippet("MelbourneAirport.jpg", "Melbourne Airport", "Lorem ipsum dolor sit amet, consectetuer	adipiscing elit maecenas quis lectus phasellus", "http://www.msn.com", "Melbourne Airport");
revolutions[1] = new homepageSnippet("Beijing Airport_950x417.jpg", "Beijing Airport", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit maecenas quis lectus phasellus", "http://www.google.com", "Beijing Airport");
revolutions[2] = new homepageSnippet("Piltun B_950x417.jpg", "Piltun B Oil Rig", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit maecenas quis lectus phasellus", "http://www.awlgrip.com", "Oil Rig");
revolutions[3] = new homepageSnippet("Quadrant_950x417.jpg", "Quadrant Building", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit maecenas quis lectus phasellus", "http://www.askjeeves.com", "Block of Flats");*/

// set the amount of time in milliseconds for the speed of the rotations
var delay = 5000;

// no need to amend below here!
var currentPos = 0;
var t;

// generic function to return an element object
function getObject(s)
{
	var o = null;

	if ( document.all )
		o = document.all[s];
	else
		o = document.getElementById(s);

	return o;
}

// define a new homepage snippet container
function homepageSnippet(img, title, intro, link, alt)
{
	this.img = img;
	this.title = title;
	this.intro = intro;
	this.link = link;
	this.alt = alt;
}

// initialise the show!
function startHomepageInit()
{
	try
	{
		if ( createArea() )
				swapImage(0);
				
		if ( !bAutoRun )
			changeImageClick(Math.floor(Math.random()*revolutions.length));
	}
	catch(e)
	{
		window.alert(e.description);
	}
}

// test we have the correct elements in the page
function createArea()
{
	var homecontainer = getObject("homepagecontainer");
	var navigation = getObject("homepagenavigation");
	var rtn = false;

	try
	{
		if ( homecontainer != null && navigation != null )
		{
			var nav = "<ul id=\"line2\">";

			for ( var i = 0; i < revolutions.length; i++ )
				nav += "<li><a href=\"#\" onClick=\"return changeImageClick(" + i + ");\">" + (i+1) + "</a></li>";
		
			nav += "</ul>"; 

			navigation.innerHTML = nav;

			rtn = true;
		}
		else
			window.alert("missing containers");
	}
	catch(e)
	{
		window.alert(e.description);
	}

	return rtn;
}

function swapImage(timer)
{
	if ( bAutoRun  )			
		t = setTimeout("doChanges()", timer);
}

function doChanges()
{
	if ( bAutoRun  )
	{
		changeImage(currentPos);
		currentPos++;
	
		if ( currentPos <= revolutions.length )
		swapImage(delay);
		else 
		changeImageClick(0);
	}
}
function changeImageClick(loc)
{
	changeImage(loc);
	clearTimeout(t); // prevent the timer from running anymore
}
function changeImage(loc)
{
	currentPos = loc;

	var homepagesnippet = revolutions[currentPos];
	var imgContainer = getObject("homepageImg");
	var homepagetext = getObject("homepagetext");
	var hometitle = getObject("homepagetitle");
	var anchor = getObject("homeanchor");

	if ( imgContainer != null && homepagesnippet != null && homepagetext != null
		&& hometitle != null && anchor != null)
	{
		hometitle.innerHTML = "<a href=\"" + homepagesnippet.link + "\" Title=\"" + homepagesnippet.alt + "\">" + homepagesnippet.title + "</a>";
		imgContainer.src = "../sitecollectionimages/" + homepagesnippet.img;
//		imgContainer.src = myImages[loc].src; //"../sitecollectionimages/" + homepagesnippet.img;
		homepagetext.innerHTML = homepagesnippet.intro;
		anchor.href = homepagesnippet.link;
		anchor.title = homepagesnippet.alt;
		imgContainer.alt = homepagesnippet.alt;
	}

	setNavigation();

	return false;
}
function setNavigation()
{
	if ( currentPos < revolutions.length)
	{
		var nav = document.getElementById("homepagenavigation").getElementsByTagName("a");
	
		for(var i = 0; i < nav.length; i++)
			nav[i].className = (i != currentPos) ? "" : "active";
	}
}
