/****************************************************
Version: 1.1
Author: Tim Saunders
Title: SlideShow
Date Last Revised:06/10/06

Please feel free to use this script, but please keep this
header with the file.

Also it's always nice to get feedback so why not drop
me an email at tsaunders@thecogworks.co.uk, if you 
like this script.
*****************************************************/


var _slideObject;
var oldIE = document.all;

function SlideShow()
{
	//Config Properties and Control methods
	this.Images = new Array();
	this.Pause = 1000;
	this.FadeSpeed = 50;
	this.OpacityInterval = 3;
	this.Loop;
	this.PreCacheImages = _preCacheImages;
	this.Height = 150;
	this.Width = 200;
	this.StartShow = _startShow;

	//No need to access these direct
	//Unless you want to do something fancy
	this.Advance = _advance;
	this.Fade = _fade;
	this.NextImageIndx = "";
	this.CurrentImageCount = 0;
	this.Front = "SlideContainer0";
	this.Back = "SlideContainer1";
	this.SetOpacity = _setItemOpacity_func;
	this.GetOpacity = _getItemOpacity_func;
	this.CurrentSlideContainer = "SlideContainer1";
	this.CreateImageHTML = _createImageHTML;
	this.DefaultImage = true;
	this.CreateContainer = _createContainer;
	
	//init
	this.CreateContainer();
}

function _preCacheImages()
{
	if(this.Images.length > 0)
	{
		if (document.images)
		{
			var imgCache = new Array();
			for (i=0;i<this.Images.length.length;i++)
			{
				imgCache[i] = new Image();
				imgCache[i].src = this.Images.length[i];}
			}
	}
}

function _startShow()
{
	_slideObject = this;
	
	if(this.DefaultImage)
	{
		var objElemCurrent = (oldIE)?eval("document.all." + this.Back):document.getElementById(this.Back);
		objElemCurrent.innerHTML = this.CreateImageHTML(0);
	}
	
	this.Advance();
}

function _advance()
{
	if(this.CurrentImageCount < this.Images.length-1)
	{	
		this.NextImageIndx = this.CurrentImageCount+1
		
		this.CurrentSlideContainer = (this.CurrentSlideContainer == this.Front)?this.Back:this.Front;
		
		var objElemCurrent = (oldIE)?eval("document.all." + this.CurrentSlideContainer):document.getElementById(this.CurrentSlideContainer);
		
		objElemCurrent.style.zIndex++;
		this.SetOpacity(this.CurrentSlideContainer,0);
		objElemCurrent.innerHTML = this.CreateImageHTML(this.NextImageIndx);
		
		this.CurrentImageCount++;

		if(this.Loop && this.CurrentImageCount == this.Images.length-1) //set it back to the beging
		{
			this.NextImageIndx = this.CurrentImageCount;
			this.CurrentImageCount = -1;
		}
		
		setTimeout("_slideObject.Fade();",this.Pause);
	}
}

function _fade()
{
	objOpacity = this.GetOpacity(this.CurrentSlideContainer);
	if(objOpacity < 99)
	{
		this.SetOpacity(this.CurrentSlideContainer, objOpacity+this.OpacityInterval);
		setTimeout("_slideObject.Fade();",this.FadeSpeed)
	}
	else
	{
		_slideObject.Advance();
	}
}

function _createImageHTML(indx)
{
	return '<img src="'+this.Images[indx]+'" />';
}

function _createContainer()
{
	document.write('<div id="SlideImageContainer" style="position:relative; height:192px; width:351px;">');
	document.write('<div id="SlideContainer0" style="position:absolute;top:0;left:0;width:'+this.Width+';height:'+this.Height+';filter:alpha(opacity=99);-moz-opacity:.99;opacity:.99;-khtml-opacity:.99;"></div>');
	document.write('<div id="SlideContainer1" style="position:absolute;top:0;left:0;width:'+this.Width+';height:'+this.Height+';filter:alpha(opacity=99);-moz-opacity:.99;opacity:.99;-khtml-opacity:.99;"></div>');
	document.write('</div>');
}

function _getItemOpacity_func(elem)
{
	var itm = (oldIE)?eval("document.all." + elem):document.getElementById(elem);

	//Get the Opacity based on availble property, as they are different for various browsers
	//Perhaps one day there will be just one ;)

	if (itm.filters)
	{
		return itm.filters.alpha.opacity;
	}

	if(itm.style.MozOpacity)
	{
		return itm.style.MozOpacity*100;
	}

	if(itm.style.opacity)
	{
		return itm.style.opacity*100;
	}

	if (itm.style.setProperty)
	{
		itm.style.getProperty('-khtml-opacity')*100;
	}

}

function _setItemOpacity_func(elem,num)
{
	var itm = (oldIE)?eval("document.all." + elem):document.getElementById(elem);

	//Set the Opacity based on availble property, as they are different for various browsers
	//Perhaps one day there will be just one ;)

	if (itm.filters)
	{
		itm.filters.alpha.opacity=num;
	}

	if(itm.style.MozOpacity)
	{
		itm.style.MozOpacity=num/101;
	}

	if(itm.style.opacity)
	{
		itm.style.opacity=num/100;
	}

	if (itm.style.setProperty)
	{
		itm.style.setProperty('-khtml-opacity',num/100,null);
	}
}