// Slideshow v1.1
// By Isaac Chua
// http://www.isaacchua.com/
// August 12, 2010

function Slideshow(container, fadeTime, displayTime, setCss) {
	this.container = container;
	this.fadeTime = fadeTime;
	this.displayTime = displayTime;
	this.setCss = setCss;

	this.elements = [];
	this.controls = [];
	this.length = 0;
	this.opacityStep = 50/this.fadeTime;

	this.timer = null;

	this.currentSlide = 0;
	this.fadingSlide = null;

	this.fadeNext = 1;
	this.fadeAnother = false;

	this.init = function() {
		if (!document.getElementById) return;
		var cont = document.getElementById(this.container);
		if (!cont) return;

		if (!cont.childNodes) return;
		var childNodes = cont.childNodes;
		for (var i=0;i<childNodes.length;i++) {
			if (childNodes[i].nodeName.toLowerCase()=='div') {
				this.elements.push(childNodes[i]);
			}
		}
		this.length = this.elements.length;
		if (this.length<=1) return; // make sure we have more than one slide

		if (this.setCss) {
			cont.style.position = "relative";
			var e;
			for (var i=1;i<this.length;i++) {
				e = this.elements[i];
				e.style.position = "absolute";
				e.style.top = 0;
				e.style.left = 0;
				e.style.display = "none";
				e.xOpacity = 0;
			}
			e = this.elements[0];
			e.style.position = "absolute";
			e.style.top = 0;
			e.style.left = 0;
			e.style.display = "block";
			e.xOpacity = 1;
		}
		else {
			for (var i=1;i<this.length;i++) {
				this.elements[i].xOpacity = 0;
			}
			this.elements[0].xOpacity = 1;
		}

		var controls = document.createElement('ul');
		controls.className = 'slideshow_controls';
		for (var i=0;i<this.length;i++) {
			var e_li = document.createElement('li');
			var e_a = document.createElement('a');
			e_a.setAttribute('href', '#');
			e_a.slideshowId = i;
			e_a.onclick = setCallbackContext(this, 'changeSlide');
			var e_txt = document.createTextNode(i+1);
			e_a.appendChild(e_txt);
			e_li.appendChild(e_a);
			controls.appendChild(e_li);
			this.controls.push(e_a);
		}
		this.controls[0].className = 'slideshow_selected';
		cont.appendChild(controls);

		this.setFadeTimer(this.displayTime);
	};
	
	this.changeSlide = function(e, o) {
		if (this.fadingSlide==null) { // is it not fading?
			if (this.currentSlide!=o.slideshowId) { // is the chosen slide different?
				this.fadeNext = o.slideshowId;
				clearTimeout(this.timer);
				this.setFadeTimer(50);
			}
		}
		else if (this.fadingSlide!=o.slideshowId) { // is the chosen slide different?
			this.fadeNext = o.slideshowId;
		}
		o.blur();
		return false;
	};
	
	this.fade = function() {
		if (this.fadingSlide==null) {
			this.fadingSlide = this.fadeNext;
			this.fadeNext = null;

			if (this.currentSlide > this.fadingSlide) {
				this.increaseOpacity(this.elements[this.fadingSlide], 1);
			}
			this.elements[this.fadingSlide].style.display = "block";
		}

		var currentSlide = this.elements[this.currentSlide];
		var fadingSlide = this.elements[this.fadingSlide];
		var fadeComplete = false;

		// do the fade according to the index of the fading and current slides
		if (this.currentSlide > this.fadingSlide) {
			fadeComplete = this.increaseOpacity(currentSlide, -this.opacityStep);
		}
		else {
			fadeComplete = this.increaseOpacity(fadingSlide, this.opacityStep);
		}

		if (fadeComplete) {
			currentSlide.style.display = "none";
			this.increaseOpacity(currentSlide, -1);
			this.controls[this.currentSlide].className = '';
			this.controls[this.fadingSlide].className = 'slideshow_selected';
			this.currentSlide = this.fadingSlide;
			this.fadingSlide = null;

			if (this.fadeNext==null) { // check if any other slide is in queue
				this.fadeNext = (this.currentSlide+1)%this.length;
				this.setFadeTimer(this.displayTime);
			}
			else {
				this.setFadeTimer(50);
			}
		}
		else {
			this.setFadeTimer(50);
		}
	};

	this.setFadeTimer = function(t) {
		this.timer = setTimeout(setCallbackContext(this, 'fade'), t);
	};
	
	this.increaseOpacity = function(obj, opac) {
		obj.xOpacity += opac;
		if (obj.xOpacity > 0.999) {
			obj.xOpacity = 1;
			var cOpac = 100;
			var dOpac = 1;
		}
		else if (obj.xOpacity < 0.001) {
			obj.xOpacity = 0;
			var cOpac = 0;
			var dOpac = 0;
		}
		else {
			var cOpac = Math.round(obj.xOpacity*100);
			var dOpac = cOpac/100;
		}
		obj.style.opacity = dOpac;
		obj.style.MozOpacity = dOpac;
		obj.style.KhtmlOpacity = dOpac;
		obj.style.filter = "alpha(opacity=" + cOpac + ")";
		return dOpac==0 || dOpac==1;
	};

	window.addEventListener ?
		window.addEventListener("load", setCallbackContext(this, 'init'), false) :
		window.attachEvent("onload", setCallbackContext(this, 'init'));

}

function setCallbackContext(object, method) {
	return (
		function(e) {
			e = e || window.event;
			return object[method](e, this);
		}
	);
}

