DEBUG=false; //Set to true for debugging

//Track which image, and timers
currentWide=0; //current image
lastButOneWide=0; //last but one image
wideOpacity=1; //current opacity
fadeTimer=0; //timer for fading
imagesLoaded=0; //make sure we preload images
imageDetails=new Array();

// Details for each front page image:
//format is: (project's file name, title)
//Empty string for filename links to project directory
imageDetails[0]=new Array("commercialst.html","Lighting at Commercial Street");
imageDetails[1]=new Array("johncleesehouse.html","Garden at John Cleese's House");
imageDetails[2]=new Array("laycockschool.html","Kitchen at Laycock School");
imageDetails[3]=new Array("esso.html","Plant room at Esso Offices");
imageDetails[4]=new Array("northburghhouse.html","Detail of Northburgh House");
imageDetails[5]=new Array("olivemorrishouse.html","Computer model of Olive Morris House");
imageDetails[6]=new Array("bractoncentre.html","Boilers at the Bracton Centre");
imageDetails[7]=new Array("no1museum.html","Fountain at No. 1 Museum, Kew Gardens");
imageDetails[8]=new Array("bootstrap.html","Workbenches at Bootstrap");
imageDetails[9]=new Array("stpaulscathedral.html","Restaurant at St Paul's cathedral");
imageDetails[10]=new Array("christchurchchislehurst.html","Foyet at Christ Church, Chislehurst");
imageDetails[11]=new Array("stpaulsmillhill.html","St Paul's Mill Hill");
imageDetails[12]=new Array("yorkhall.html","Turkish baths at York Hall Spa");
imageDetails[13]=new Array("dreadnought.html","Dreadnought Library, Greenwich University");
imageDetails[14]=new Array("twyford.html","The IT suite at Twyford School");
imageDetails[15]=new Array("southwarknightclubs.html","Interior of a nightclub");

maxWide=imageDetails.length;

function preloadFirstWideImages(num) {
	if (num>maxWide || !num) {
		num=maxWide; //Sanity checking!
	}
	for (var i=0; i<num; i++) {
		var tmpImage=new Image();
		tmpImage.onload=function (){wideImgLoaded(num)};
		tmpImage.src="Images/wide"+i+".jpg";
		if (DEBUG) {
			buglog("loaded Image:"+i);
		}
	}
}

function preloadRestWideImages() {
	for (var i=imagesLoaded; i<maxWide; i++) {
		var tmpImage=new Image();
		tmpImage.onload=wideImgLoaded;
		tmpImage.src="Images/wide"+i+".jpg";
		if (DEBUG) {
			buglog("loaded Image:"+i);
		}
	}
	
}

function wideImgLoaded(num) {
	imagesLoaded++;
	if (imagesLoaded==num) {
		imgFadeInit();
		preloadRestWideImages();
	}
}

function changeWide() {
	if (DEBUG) {
		buglog("changing wide...");
	}
	var r=randomInRange(0,imagesLoaded-1);
	if (r==currentWide || r==lastButOneWide) {
		changeWide();
		return 0;
	}
	lastButOneWide=currentWide;
	currentWide=r;
	document.getElementById("demoproj").style.backgroundImage="url('Images/wide"+r+".jpg')";
	if (DEBUG) {
		buglog("And starting fadeWide...");
	}
	fadeWide();
	return 0;
}
function fadeWide() {
	wideOpacity-=0.1;
	var theImg=document.getElementById("demoproj").getElementsByTagName("img")[0];
	if (wideOpacity<0.1) {
		if (DEBUG) {
			buglog("Faded away---swap over");
		}
		theImg.src="Images/wide"+currentWide+".jpg";
		theImg.title=imageDetails[currentWide][1]+" (click to view project)";
		theImg.parentNode.href="projects/"+imageDetails[currentWide][0];
		wideOpacity=1;
		setOpacity(wideOpacity,theImg);
		fadeTimer=setTimeout(changeWide,3000);
	} else {
		setOpacity(wideOpacity,theImg);
		fadeTimer=setTimeout(fadeWide,50);
	}
}

function wideImgMouseOver() {
	clearTimeout(fadeTimer);
	wideOpacity=1;
	setOpacity(wideOpacity,document.getElementById("demoproj").getElementsByTagName("img")[0]);
}

function wideImgMouseOut() {
	fadeWide();
}
	
function imgFadeInit() {
	if (DEBUG) {
		buglog("Starting up...");
	}
	document.getElementById("demoproj").getElementsByTagName("img")[0].onmouseover=wideImgMouseOver;
	document.getElementById("demoproj").getElementsByTagName("img")[0].onmouseout=wideImgMouseOut;
	changeWide();
}

// Add our inititalistion to the window load event
var oldOnLoad;
if (window.onload) {
	oldOnLoad=window.onload;
}
else {
	oldOnLoad=function(){};
}
window.onload=function() {
	preloadFirstWideImages(4);
	oldOnLoad();
}
//window.onload=imgFadeInit;