var _fadeArray = new Array(); //array to cache fadeshow instances
var _fadeClear = new Array(); //array to cache corresponding clearinterval pointers
 
var _dom = (document.getElementById); //modern _dom browsers
var _isIE = document.all;
 
function fadeshow(theImages, fadeWidth, fadeHeight, fadeBgColor, borderWidth, delay, pause, displayOrder){
	this.pauseCheck = pause;
	this.mouseOverCheck = 0;
	this.delay = delay;
	this.degree = 10;
	this.currentImageIndex = 0;
	this.nextImageIndex = 1;
	_fadeArray[_fadeArray.length] = this;
	this.slideShowId = _fadeArray.length - 1;
	this.canvasBase = "canvas" + this.slideShowId;
	this.currentCanvas = this.canvasBase + "_0";
	if (typeof displayOrder != "undefined") {
		theImages.sort(function() {return 0.5 - Math.ran_dom();});
	}
	this.theImages = theImages;
	this.imageBorder = parseInt(borderWidth);
	this.postImages = new Array(); //preload images
	for (p = 0; p < theImages.length; p++) {
		this.postImages[p] = new Image();
		this.postImages[p].src = theImages[p][0];
	}
 
	var fadeWidth = fadeWidth + this.imageBorder * 2;
	var fadeHeight = fadeHeight + this.imageBorder * 2;
 
	if (_isIE && _dom || _dom) { //if IE5+ or modern browsers (ie: Firefox)
		document.write('<div id = "master'+this.slideShowId+'" style = "position:relative;width:'+fadeWidth+'px;height:'+fadeHeight+'px;overflow:hidden;"><div id = "'+this.canvasBase+'_0" style = "position:absolute;width:'+fadeWidth+'px;height:'+fadeHeight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity = 10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadeBgColor+'"></div><div id = "'+this.canvasBase+'_1" style = "position:absolute;width:'+fadeWidth+'px;height:'+fadeHeight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity = 10);opacity:0.1;-moz-opacity:0.1;-khtml-opacity:0.1;background-color:'+fadeBgColor+'"></div></div>');
	}
	else {
		document.write('<div><img name = "defaultslide' + this.slideShowId + '" src = "'+this.postImages[0].src+'"></div>');
	}
 
	if (_isIE && _dom || _dom) { //if IE5+ or modern browsers such as Firefox
		this.startit();
	}
	else {
		this.currentImageIndex++;
		setInterval("_fadeArray[" + this.slideShowId + "].rotateimage()", this.delay);
	}
}

function fadepic(obj) {
	if (obj.degree < 100) {
		obj.degree += 10;
		if (obj.tempobj.filters&&obj.tempobj.filters[0]) {
			if (typeof obj.tempobj.filters[0].opacity == "number") {//if IE6+
				obj.tempobj.filters[0].opacity = obj.degree;
			}
			else { //else if IE5.5-
				obj.tempobj.style.filter = "alpha(opacity = " + obj.degree + ")";
			}
		}
		else if (obj.tempobj.style.MozOpacity) {
			obj.tempobj.style.MozOpacity = obj.degree/101;
		}
		else if (obj.tempobj.style.KhtmlOpacity) {
			obj.tempobj.style.KhtmlOpacity = obj.degree/100;
		}
		else if (obj.tempobj.style.opacity && !obj.tempobj.filters) {
			obj.tempobj.style.opacity = obj.degree/101;
		}
	}
	else {
		clearInterval(_fadeClear[obj.slideShowId]);
		obj.nextcanvas = (obj.currentCanvas == obj.canvasBase+"_0") ? obj.canvasBase+"_0" : obj.canvasBase+"_1";
		obj.tempobj = _isIE? _isIE[obj.nextcanvas] : document.getElementById(obj.nextcanvas);
		obj.populateslide(obj.tempobj, obj.nextImageIndex);
		obj.nextImageIndex = (obj.nextImageIndex < obj.postImages.length-1) ? obj.nextImageIndex+1 : 0;
		setTimeout("_fadeArray[" + obj.slideShowId + "].rotateimage()", obj.delay);
	}
}
 
fadeshow.prototype.populateslide = function(picobj, picindex) {
	var slideHTML = "";
	if (this.theImages[picindex][1] != "") { //if associated link exists for image
		slideHTML = '<a href = "' + this.theImages[picindex][1] + '" target = "' + this.theImages[picindex][2] + '">';
	}
	slideHTML += '<img src = "' + this.postImages[picindex].src + '" border = "' + this.imageBorder+'px">';
	if (this.theImages[picindex][1] != "") { //if associated link exists for image
		slideHTML += '</a>';
	}
	picobj.innerHTML = slideHTML;
}
 
fadeshow.prototype.rotateimage = function() {
	if (this.pauseCheck == 1) { //if pause onMouseover enabled, cache object
		var cacheobj = this;
	}
	if (this.mouseOverCheck == 1) {
		setTimeout(function() { cacheobj.rotateimage() }, 100);
	}
	else if (_isIE&&_dom||_dom) {
		this.resetit();
		var crossobj = this.tempobj = _isIE ? _isIE[this.currentCanvas] : document.getElementById(this.currentCanvas);
		crossobj.style.zIndex++;
		_fadeClear[this.slideShowId] = setInterval("fadepic(_fadeArray[" + this.slideShowId + "])", 50);
		this.currentCanvas = (this.currentCanvas == this.canvasBase+"_0") ? this.canvasBase+"_1" : this.canvasBase+"_0";
	}
	else {
		var ns4imgobj = document.images['defaultslide' + this.slideShowId];
		ns4imgobj.src = this.postImages[this.currentImageIndex].src;
	}
	this.currentImageIndex = (this.currentImageIndex<this.postImages.length - 1) ? this.currentImageIndex+1 : 0;
}
 
fadeshow.prototype.resetit = function() {
	this.degree = 10;
	var crossobj = _isIE ? _isIE[this.currentCanvas] : document.getElementById(this.currentCanvas);
	if (crossobj.filters && crossobj.filters[0]) {
		if (typeof crossobj.filters[0].opacity == "number") { //if IE6+
			crossobj.filters(0).opacity = this.degree;
		}
		else { //else if IE5.5-
			crossobj.style.filter = "alpha(opacity = " + this.degree + ")";
		}
	}
	else if (crossobj.style.MozOpacity) {
		crossobj.style.MozOpacity = this.degree/101;
	}
	else if (crossobj.style.KhtmlOpacity) {
		crossobj.style.KhtmlOpacity = this.degree/100;
	}
	else if (crossobj.style.opacity&&!crossobj.filters) {
		crossobj.style.opacity = this.degree/101;
	}
}
 
fadeshow.prototype.startit = function() {
	var crossobj = _isIE ? _isIE[this.currentCanvas] : document.getElementById(this.currentCanvas);
	this.populateslide(crossobj, this.currentImageIndex);
	if (this.pauseCheck == 1) { //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
		var cacheobj = this;
		var crossobjcontainer = _isIE ? _isIE["master" + this.slideShowId] : document.getElementById("master" + this.slideShowId);
		crossobjcontainer.onmouseover = function(){ cacheobj.mouseOverCheck = 1 };
		crossobjcontainer.onmouseout = function(){ cacheobj.mouseOverCheck = 0 };
	}
	this.rotateimage();
}
