
/*
Slideshow, yeah
*/

var SS = {};

SS.isIE = ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera);
SS.imgs = new Array();		// images for slideshow
SS.container = null;		// 
SS.img1 = null;				// top image elemenet
SS.img2 = null;				// bottom image element

// swapper params
SS.swap = {};
SS.swap.currIndex = null;
SS.swap.tHdlSwap = null;
SS.swap.tIncSwap = 1500;

// fader params
SS.fade = {};
SS.fade.tHdlFade = null;
SS.fade.tIncFade = 25;
SS.fade.opInc = 0.01;
SS.fade.opacity = 1;

SS.init = function(container, img1, img2, imgRoot) {
	SS.container = id(container);
	SS.img1 = id(img1);
	SS.img2 = id(img2);
	SS.loadImages(imgRoot);
	SS.start();
}

SS.start = function() {
	if (SS.imgs.length == 0) return;
	
	// images
	SS.img1.src = SS.imgs[0];
	SS.img2.src = SS.imgs[0];
	SS.swap.currIndex = 0;
	SS.fade.opacity = 1;
	SS.applyOpacity();
	
	// start fader
	SS.swapImg();
}

SS.swapImg = function() {
	var newIndex = SS.swap.currIndex + 1;
	if (newIndex >= SS.imgs.length) newIndex = 0;
	SS.swap.currIndex = newIndex;
	
	SS.img1.src = SS.img2.src;
	/*
	SS.fade.opacity = 1;
	SS.applyOpacity();
	
	SS.img2.src = SS.imgs[newIndex];
	*/
	
	SS.swap.tHdlSwap = setTimeout(SS.startFade, SS.swap.tIncSwap);
	
}

SS.startFade = function() {
	/* Had to move these lines here because of lag caused by image.php */
	SS.fade.opacity = 1;
	SS.applyOpacity();
	SS.img2.src = SS.imgs[SS.swap.currIndex];
	
	SS.fade.tHdlFade = setInterval(SS.fadeImg, SS.fade.tIncFade);
}

SS.fadeImg = function() {
	SS.fade.opacity -= SS.fade.opInc;
	SS.applyOpacity();
	if (SS.fade.opacity < 0.03) {
		clearInterval(SS.fade.tHdlFade);
		SS.swapImg();
	}
}

SS.loadImages = function(imgRoot) {
	imgRoot = id(imgRoot);
	var imgList = imgRoot.getElementsByTagName("img");
	for (var i=0; i<imgList.length; i++) {
		SS.imgs[i] = imgList[i].src;
	}
}

SS.applyOpacity = function () {
	var opacity = SS.fade.opacity;
	if (opacity < 0) opacity = 0;
	var img = SS.img1;
	if (SS.isIE) {
		img.style.filter = "alpha(opacity=" + opacity*100 + ")";
	}
	else {
		// code for normal correctly programmed browsers
		img.style.opacity = opacity;
	}
}

/*
DOM
*/
function id(e) {
	if (typeof e == "string") return document.getElementById(e);
	return e;
}

