/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/


/*window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);*/

var d=document/*, imgs = new Array(), zInterval = null, current=0, pause=false*/;
var cssDone = false;

var gallery = {
	initialize: function(id) {
		if(!d || !d.getElementById || !d.createElement) return;

		this.gallery = d.getElementById(id);
		if(!this.gallery) return;

		// DON'T FORGET TO GRAB THIS FILE AND PLACE IT ON YOUR SERVER IN THE SAME DIRECTORY AS THE JAVASCRIPT!
		// http://slayeroffice.com/code/imageCrossFade/xfade2.css
		if (!cssDone) {
			css = d.createElement("link");
			css.setAttribute("href","/components/xfade/xfade2.css");
			css.setAttribute("rel","stylesheet");
			css.setAttribute("type","text/css");
			d.getElementsByTagName("head")[0].appendChild(css);
			cssDone = true;
		}
		
		this.sIndex = 0;
		this.cIndex = -1;
		this.nIndex = -1;

		this.imgs = this.gallery.getElementsByTagName("img");
		for(i=0;i<this.imgs.length;i++) {
			this.imgs[i].style.display = "block";
			this.imgs[i].rWidth = 0;
			this.imgs[i].xOpacity = 0;
			this.setOpacity(this.imgs[i]);
		}
		
		setTimeout(this.start.bind(this),1000*Math.random());
	},
	start: function() {
		
		if(this.imgs.length <= 0)
			return;
		
		if (this.cIndex == -1) {
			if (this.positionImage(this.sIndex)) {
				this.imgs[this.sIndex].style.display = "block";
				this.cIndex = this.sIndex;
				setTimeout(this.start.bind(this),50);
			}
			else {
				// Skip image
				this.sIndex = this.imgs[this.sIndex+1]?this.sIndex+1:0;
				setTimeout(this.start.bind(this),1000);
			}
			return;
		}
		nOpacity = this.imgs[this.cIndex].xOpacity;
		nOpacity+=.05;
		if (nOpacity > 1) {
			setTimeout(this.xfade.bind(this),4000+2000*Math.random());
			return;
		}
		this.imgs[this.cIndex].xOpacity = nOpacity;
		this.setOpacity(this.imgs[this.cIndex]);
		setTimeout(this.start.bind(this),50);
	},
	xfade: function() {
        
   		if(this.imgs.length <= 0)
			return;

		if (this.nIndex == -1) {
			this.nIndex = this.imgs[this.sIndex+1]?this.sIndex+1:0;
			if (this.positionImage(this.nIndex)) {
				this.imgs[this.nIndex].style.display = "block";
			}
			else {
				// Skip image
				this.sIndex = this.nIndex;
				this.nIndex = -1;
				setTimeout(this.xfade.bind(this),1000);
				return;
			}
		}
	
		cOpacity = this.imgs[this.cIndex].xOpacity;
		nOpacity = this.imgs[this.nIndex].xOpacity;
	
		cOpacity-=.05; 
		nOpacity+=.05;

		this.imgs[this.cIndex].xOpacity = cOpacity;
		this.imgs[this.nIndex].xOpacity = nOpacity;
		
		this.setOpacity(this.imgs[this.cIndex]); 
		this.setOpacity(this.imgs[this.nIndex]);
		
		if(cOpacity<=0) {
			// Hide old picture
			this.imgs[this.cIndex].style.display = "none";
			this.sIndex = this.cIndex = this.nIndex;
			this.nIndex = -1;
			setTimeout(this.xfade.bind(this),4000+2000*Math.random());
		} else {
			setTimeout(this.xfade.bind(this),50);
		}
	},
	positionImage: function(i) {
		if (this.imgs && this.imgs[i] && this.imgs[i].complete && this.imgs[i].height > 0 && this.imgs[i].width > 0)
		{ 	
			// Resize new picture
			if (this.imgs[i].width > this.gallery.offsetWidth) {
				var scale = this.gallery.offsetWidth/this.imgs[i].width;
				this.imgs[i].height = this.imgs[i].height * scale;
				this.imgs[i].width = this.gallery.offsetWidth; //Math.floor(this.imgs[i].height * scale);
			}

			if (this.imgs[i].height > this.gallery.offsetHeight) {
				var scale = this.gallery.offsetHeight/this.imgs[i].height;
				this.imgs[i].rWidth = this.imgs[i].width * scale;
				this.imgs[i].width = this.imgs[i].width * scale;
				this.imgs[i].height = this.gallery.offsetHeight; //Math.floor(this.imgs[i].height * scale);
			}
		
			// Position new picture
			var x = (this.gallery.offsetWidth - (this.imgs[i].rWidth>0?this.imgs[i].rWidth:this.imgs[i].width))/2;
			this.imgs[i].style.left = x + "px";
			return true;
		}
		return false;
	},
	setOpacity: function(obj) {
		if(obj.xOpacity>=1.0) {
			obj.xOpacity = 1.0;
//			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
};
gallery = Class.create(gallery);
