﻿/*****************************************************************************************
Greenstreet Slide Show Javascript Engine
(C)2009 Greenstreet Software Limited, All Rights Reserved.
http://www.greenstreetsoftware.com

Written by Steve J. Riggall.
******************************************************************************************/

function GreenstreetSlideShow(RotationTime, RotationSpeed, SlideContainerDiv, PrimarySlideDiv)
{
	if(arguments.length != arguments.callee.length) throw new Error("Invalid constructor arguments..");

	this.currentSlide = null;
	this.rotTime = RotationTime * 1000;
	this.rotSpeed = RotationSpeed * 1000;
	this.divContainer = SlideContainerDiv;
	this.divPrimary = PrimarySlideDiv;
	this.idInterval = null;
	this.inTrans = false;
}

GreenstreetSlideShow.prototype.Start = function()
{
	var _self = this;
	this.idInterval = setInterval(function(){_self.ShowSlide()}, this.rotTime);
}

GreenstreetSlideShow.prototype.Stop = function()
{
	clearInterval(this.idInterval);
}

GreenstreetSlideShow.prototype.ShowSlide = function(id)
{
	if(this.inTrans) return;
	if(this.currentSlide==null) this.currentSlide = this.divPrimary;

	if(!id)
	{
		var bAssignNext = false;
		var ob = document.getElementById(this.divContainer);
		if(ob!=null)
		{
			var obArray = ob.getElementsByTagName('div');
			if(obArray != null)
			{
				for(var i=0; i<obArray.length; i++)			
				{
					var chld = obArray[i];
		
					if(bAssignNext && chld.id && chld.id.length>0)
					{
						id = chld.id;
						break;
					}
		
					if(chld.id == this.currentSlide)
					{
						bAssignNext = true; 
					}
				}
			}
		}
	}
	
	if(!id) id = this.divPrimary;

	this.Stop();
	this.Start();

	var nextSlide = id;
	var lastSlide = this.currentSlide;
	if(nextSlide !== lastSlide) this.Present(lastSlide, nextSlide);
	this.currentSlide = id;
}

GreenstreetSlideShow.prototype.Present = function(id1,id2)
{
	setOpacity(0, id2);
	setOpacity(100, id1);
	document.getElementById(id1).style.display = 'block';	
	document.getElementById(id2).style.display = 'block';
	document.getElementById(id1+'_thumb').className = 'navslide';
	document.getElementById(id2+'_thumb').className = 'navslide_focus';
	
	var speed = Math.round(this.rotSpeed / 100);
	var timer = 0;
	this.inTrans = true;

	for(i=0; i<100; i++) 
	{
		setTimeout("setOpacity(" + i + ",'" + id2 + "')",(timer * speed));
		setTimeout("setOpacity(" + (100-i) + ",'" + id1 + "')",(timer * speed));
		timer++;
	}
	
	var _self = this;
	setTimeout(function(){_self.inTrans = false;}, timer * speed);
	setTimeout("document.getElementById('" + id1 + "').style.display = 'none'", timer * speed);	
}

function setOpacity(opacity, id) 
{
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}
