/***********************************************
* Ultimate Fade-In Slideshow (v1.51): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

// Heavily modified and simplified to support only modern browsers.
 
function fadeshow (slideshowid, theimages, fadewidth, fadeheight, delay, pause, fadebgcolor, randomorder)
{
	this.fadeclear = 0;
	this.mouseovercheck = 0;
	this.degree = 100; //initial opacity degree (%)
	this.curimageindex = 0;
	this.nextimageindex = 1;

	this.pausecheck = pause;
	this.delay = delay;
	this.slideshowid = slideshowid;
	if (randomorder == 1)
		theimages.sort (function() { return 0.5 - Math.random(); });
	this.theimages = theimages;

	this.canvasbase = "canvas" + this.slideshowid;
	this.curcanvas = 0;
	this.postimages = new Array();
	for (p = 0; p < theimages.length; p++)
	{
		this.postimages[p] = new Image();
		this.postimages[p].src = theimages[p][0];
	}
 
	var fadewidth = fadewidth;
	var fadeheight = fadeheight;
	
	var slideshowMaster = document.getElementById(this.slideshowid);
	if (slideshowMaster == null)
		return;
		
	slideshowMaster.style.position = "relative";
	slideshowMaster.style.width = fadewidth + "px";
	slideshowMaster.style.height = fadeheight + "px";
	slideshowMaster.style.overflow = "hidden";
 
	slideshowMaster.innerHTML =
		'<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>';
		
	this.canvases = new Array();
	this.canvases[0] = document.getElementById (this.canvasbase + "_0");
	this.canvases[1] = document.getElementById (this.canvasbase + "_1");
	var crossobj = this.canvases[0];
	this.populateslide (crossobj, this.curimageindex);
	this.settransparency (crossobj, this.delay);
	if (this.pausecheck == 1)
	{	//IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
		var cacheobj = this;
		slideshowMaster.onmouseover = function() { cacheobj.mouseovercheck = 1 };
		slideshowMaster.onmouseout = function() { cacheobj.mouseovercheck = 0 };
	}
	this.rotateimage();
}

fadeshow.prototype.fadepic = function ()
{
	if (this.degree < 100)
	{
		this.degree += 15;
		if (this.degree > 100)
			this.degree = 100;
		this.settransparency(this.tempobj, this.degree);
	}
	else
	{
		clearInterval (this.fadeclear);
		var thisObj = this;
		this.nextcanvas = this.curcanvas;
		this.tempobj = this.canvases[this.nextcanvas];
		this.populateslide (this.tempobj, this.nextimageindex);
		this.nextimageindex = (this.nextimageindex < this.postimages.length - 1) ? this.nextimageindex + 1 : 0;
		setTimeout (function() { thisObj.rotateimage(); }, this.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 + '" alt="" />';
	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() }, 500);
	}
	else
	{
		this.resetit();
		var crossobj = this.tempobj = this.canvases[this.curcanvas];
		crossobj.style.zIndex++;
		var thisObj = this;
		this.fadeclear = setInterval(function () { thisObj.fadepic(); }, 75);
		this.curcanvas = (this.curcanvas == 0) ? 1 : 0;
	}
	this.curimageindex = (this.curimageindex < this.postimages.length - 1) ? this.curimageindex + 1 : 0;
}

fadeshow.prototype.resetit = function()
{
	this.degree = 10;
	var crossobj = this.canvases[this.curcanvas];
	this.settransparency(crossobj, this.degree);
}

fadeshow.prototype.settransparency = function(object, transparency)
{
	if (object.style.opacity && !object.filters)
	{
		object.style.opacity = transparency / 101;
		return;
	}
	else if (object.filters && object.filters[0])
	{
		if (typeof object.filters[0].opacity == "number") //if IE6+
			object.filters(0).opacity = transparency;
		else //else if IE5.5-
			object.style.filter = "alpha(opacity=" + transparency + ")";
		return;
	}
	else if (object.style.MozOpacity)
		object.style.MozOpacity = transparency / 101;
	else if (object.style.KhtmlOpacity)
		object.style.KhtmlOpacity = transparency / 100;
}

