﻿var _fadeinslideshowcount = 0;

function fadeinslideshow(imgarray, w, h, delay, tagid, makefit) {
//	imgarray.sort(function() {return 0.5 - Math.random();});
	this.id = "_fadeslide"+(++_fadeinslideshowcount); //Generate unique ID for this slideshow instance (automated)
	this.createcontainer(imgarray, parseInt(w), parseInt(h), tagid, makefit);
	if (typeof delay == "undefined") {
		delay = 0
	}
	this.delay = parseInt(delay);
	this.nextcanvasindex = -1;
	this.isMouseover = 0;
	this.init();
}

fadeinslideshow.prototype.createcontainer=function(imgarray, w, h, tagid, makefit) {
	//If there are comments, flow those below.
	var found = false;
	for (var i = 0; i < imgarray.length; i++) {
		if ((typeof imgarray[i][1] != "undefined") && (imgarray[i][1] != "")) {
			found = true;
			break;
		}
	}
	if (found) {
		found = 12 * 1.3333 * 3;
	} else {
		found = 0;
	}
	var inner = '<div id="'+this.id+'" style="position:relative;width:'+w+'px;height:'+(h+found)+'px;overflow:hidden;">';
	for (var i = 0; i < imgarray.length; i++) {
		var desc;
		if ((typeof imgarray[i][1] != "undefined") && (imgarray[i][1] != "")) {
			desc = '<p>'+imgarray[i][1]+'</p>';
		} else {
			desc = "";
		}
		//zoom:1 = IE6 hack to force hasLayout
		inner += '<div class="__fadeinslideshow" style="z-index:2;position:absolute;filter:progid:DXImageTransform.Microsoft.alpha(opacity=0);opacity:0.0;-moz-opacity:0.0;-khtml-opacity:0.0;">';
		if ((imgarray[i][0].length > 4) && (imgarray[i][0].substr(imgarray[i][0].length - 4, 4).toLowerCase() == ".avi")) {
			inner += "<object classid=\"CLSID:05589FA1-C356-11CE-BF01-00AA0055595A\"></object>";
		} else {
			inner += '<img src="'+imgarray[i][0]+'" style="border:0;"/>';
		}
		inner += desc+'</div>';
	}
	inner += '</div>';
	if (typeof tagid == "undefined") {
		document.write(inner);
	} else {
		document.getElementById(tagid).innerHTML = inner;
	}
	this.slideshowref = document.getElementById(this.id);
	if (typeof makefit == "undefined") {
		makefit = false;
	}
	this.canvases = [];
	for (var i = 0; i < imgarray.length; i++){
		this.canvases[i] = this.slideshowref.childNodes[i];
		var imgs = this.canvases[i].getElementsByTagName('img');
		if (imgs.length > 0) {
			//May need to shrink the image
			if (makefit) {
				var img = imgs[0];
				var x = 0;
				var y = 0;
				if (img.width > w) {
					x = w / img.width;
				}
				if (img.height > h) {
					y = h / img.height;
				}
				if (x || y) {
					var shrink = Math.min(x, y);
					img.width = Math.round(img.width * shrink);
				}
			}
		} else {
			this.canvases[i].getElementsByTagName('object')[0].title = imgarray[i][0];
		}
	}
}

fadeinslideshow.prototype.setopacity=function(canvasindex, degree) {
	var canvas = this.canvases[canvasindex];
	var objs = canvas.getElementsByTagName('object');
	if (objs.length > 0) {
		if (degree >= 100) {
			objs[0].FileName = objs[0].title;
		} else {
			objs[0].FileName = '';
		}
	}
	if (canvas.filters && canvas.filters[0]) {
		if (typeof canvas.filters[0].opacity == "number") { //if IE6+
			canvas.filters[0].opacity = degree;
		} else { //else if IE5.5-
			canvas.style.filter="alpha(opacity="+degree+")";
		}
	} else if (canvas.style.MozOpacity) {
		canvas.style.MozOpacity=degree/101;
	} else if (canvas.style.KhtmlOpacity) {
		canvas.style.KhtmlOpacity = degree/100;
	} else if (canvas.style.opacity && !canvas.filters) {
		canvas.style.opacity = degree/101;
	}
}

fadeinslideshow.prototype.fadeslide=function() {
	if (this.degree < 100) {
		this.degree += 10;
		this.setopacity(this.nextcanvasindex, this.degree);
		if (this.lastcanvasindex >= 0) {
			this.setopacity(this.lastcanvasindex, 100 - this.degree);
		}
	} else {
		clearInterval(this.animatetimer);
		var slideshow = this;
		setTimeout(function(){slideshow.rotateslide()}, this.delay);
	}
}

fadeinslideshow.prototype.setupnextslide=function() {
	this.lastcanvasindex = this.nextcanvasindex++;
	if (this.nextcanvasindex >= this.canvases.length) {
		this.nextcanvasindex = 0;
	}
}

fadeinslideshow.prototype.rotateslide=function() {
	var slideshow = this;
	if (this.isMouseover) {
		setTimeout(function(){slideshow.rotateslide()}, 50);
	} else {
		this.setupnextslide();
		this.degree = 0;
		this.animatetimer = setInterval(function(){slideshow.fadeslide()}, 100);
	}
}

fadeinslideshow.prototype.init=function(){
	var slideshow = this;
	this.slideshowref.onmouseover = function(){slideshow.isMouseover=1};
	this.slideshowref.onmouseout = function(){slideshow.isMouseover=0};
	this.rotateslide();
}

