// JavaScript source code

if (typeof(ISL) ==="undefined")
	var ISL = { };
ISL.Splash = {
	initialTimeout: 2000,			// delay before beginning image preload
	subsequentTimeout: 3000,		// delay before beginning image preload
	loadingCheckTimeout: 500,		// delay after checking a current preload status
	iP: "/site-isl/images/",		// image path
	loadingImages: [],
	loadedImages: [],
	currentStyle: 4
};
ISL.Splash.reqImages = [
							[ISL.Splash.iP+"bg_home_13.jpg",ISL.Splash.iP+"bg_homebox1_13.jpg",ISL.Splash.iP+"bg_homebox2_13.jpg"],
							[ISL.Splash.iP+"bg_home_14.jpg",ISL.Splash.iP+"bg_homebox1_14.jpg",ISL.Splash.iP+"bg_homebox2_14.jpg"],
							[ISL.Splash.iP+"bg_home_15.jpg",ISL.Splash.iP+"bg_homebox1_15.jpg",ISL.Splash.iP+"bg_homebox2_15.jpg"],
							[ISL.Splash.iP+"bg_home_16.jpg",ISL.Splash.iP+"bg_homebox1_16.jpg",ISL.Splash.iP+"bg_homebox2_16.jpg"],
							[ISL.Splash.iP+"bg_home_17.jpg",ISL.Splash.iP+"bg_homebox1_17.jpg",ISL.Splash.iP+"bg_homebox2_17.jpg"]
						];


// add an indexOf function to the javascript Array object, then make it even simpler to use
function isArray(v) {
     return v && typeof v === "object" && typeof v.length === "number" && !(v.propertyIsEnumerable("length"));
}
Array.prototype.indexOf = function (item) {
	if (isArray(item))
		for (var j=0;j<item.length;j++) {
			var k=this.indexOf(item[j]);
			if (k>-1) return k;
		}
	else
		for (var i=0;i<this.length;i++)
			if (this[i]==item) return i;
	return -1;
}
Array.prototype.iO = Array.prototype.indexOf;

ISL.Splash.toggleStyle = function (styleId) {
	var me = ISL.Splash;
	if (me.loadedImages.iO(me.reqImages[styleId-1])==-1 && me.loadingImages.iO(me.reqImages[styleId-1])==-1)
		me.preloadImageSet(styleId-1);		// load the background image first

	var styleselector = document.getElementById("styleselector");
	if (styleselector != null) {
		var links = styleselector.getElementsByTagName("a");
		for (var i=0; i<links.length; i++) {
			text = links[i].childNodes[0].nodeValue;
			if (parseInt(text)==styleId)
				links[i].className="on";
			else
				links[i].className="";
		}
	}
	
	me.currentStyle = styleId;
	if (me.loadingImages.iO(me.reqImages[styleId-1])>-1)
		window.setTimeout(me.toggleSplash, me.loadingCheckTimeout);	// come back in half a second to see if the image is loaded yet
	else
		me.toggleSplash();			// toggle the screen immediately
	
	// stop click event propagation
	return false;
}

ISL.Splash.toggleSplash = function () {
	var home_main = document.getElementById("home_main");
	if (home_main != null) home_main.className = "home_" + ISL.Splash.currentStyle;
}

ISL.Splash.initialize = function () {
	var me = ISL.Splash;
	var home_main = document.getElementById("home_main");
	if (home_main != null) 
		me.currentStyle = parseInt(home_main.className.replace("home_", ""));
	else
		me.currentStyle = 1;
	
	var styleselector = document.getElementById("styleselector");
	if (styleselector != null) {
		var links = styleselector.getElementsByTagName("a");
		var a = null;
		for (var i=0; i<links.length; i++) {
			a = links[i];
			if (a != null) {
				a.setAttribute("styleid", i+1);
				a.href = "javascript:void(0);";
				a.onclick = function () { ISL.Splash.toggleStyle(this.getAttribute("styleid")); this.blur(); return false; }
				if (parseInt(a.childNodes[0].nodeValue)==me.currentStyle)
					a.className = "on";
			}
		}
	}

	// preload the remaining images (the rollovers) from the initial set	
	me.preloadImageSet(me.currentStyle-1);
	if (me.currentStyle!=1) me.toggleStyle(me.currentStyle);
	
	// start the timeout to preload the remaining image sets
	window.setTimeout(me.initImages, me.initialTimeout);
}

ISL.Splash.initImages = function () {
	var me = ISL.Splash;
	if (me.loadingImages.iO(me.reqImages[me.currentStyle-1])>-1) {
		window.setTimeout(me.initImages, me.loadingCheckTimeout);	// still loading the current images
		return;
	}

	var imageSets = me.reqImages.length;
	for (var i=me.currentStyle%imageSets;i!=me.currentStyle-1;i=(i+1)%imageSets) {	
		if (me.loadedImages.iO(me.reqImages[i])==-1 && me.loadingImages.iO(me.reqImages[i])==-1) {
			me.preloadImageSet(i);
			window.setTimeout(me.initImages, me.subsequentTimeout);	// come back and check in 3 seconds to see if we can load another
			return;
		}
	}
//	window.status="finished loading";
}

ISL.Splash.preloadImageSet = function (id) {
	var me = ISL.Splash;
	for (var i=0;i<me.reqImages[id].length;i++)
		me.preloadImage(me.reqImages[id][i]);
}

ISL.Splash.preloadImage = function (src) {
	var me = ISL.Splash;
	if (me.loadedImages.iO(src)>-1||me.loadingImages.iO(src)>-1) return;
	var img = new Image()
	img.onload = function () { me.loadedImages=me.loadedImages.concat(me.loadingImages.splice(me.loadingImages.iO(src),1));/*window.status="loaded "+src;*/ };
	img.onerror = function () { me.loadedImages=me.loadedImages.concat(me.loadingImages.splice(me.loadingImages.iO(src),1)); };
	img.onabort = function () { me.loadingImages.splice(me.loadingImages.iO(src),1); };
	me.loadingImages.push(src);
	img.src = src;
}

ow_f_AppendLoadEvent(ISL.Splash.initialize);