//*******************************************************************
// Kappa Solutions Ltd. Javascript Library.
// (c) 2000-2009 Kappa Solutions Ltd. except where credited
// 
// Opacity and transition effects used in this class are credited to
// brainerror.net: 
//		http://brainerror.net/scripts/javascript/blendtrans/
//*******************************************************************

var KSL;
if (!KSL) KSL = {};
if (!KSL.Widget) KSL.Widget = {};

//*******************************************************************
// Arguments Class Def.
//
//	This class holds member variables corresponding to the
//	arguments in the query string of a document URL. The variables 
//	are created by the constructor.
//*******************************************************************
KSL.Widget.SlideShow = function(ele, opts) 
{
	this.element = this.getElement(ele);
	this.defaultPanel	= 0; 	// Show the first panel by default.
	this.showTime		= 7.5;	// Slide show time (secs)
	this.transTime = 0.5;			// Slide transition time (secs)
	this.transSmoothness = 5;	// Change in opacity per step (%)
	
	this.backZIndex		= 49;	// Z-index for back panels
	this.foreZIndex		= 50;	// Z index for foreground panels
	
	this.currentPanelIndex = 0;

	KSL.Widget.SlideShow.setOptions(this, opts);

    this.panels = this.getPanels();
    
	if (typeof (this.defaultPanel) == "number")
	{
		if (this.defaultPanel < 0)
		{
			this.defaultPanel = 0;
		}
		else if (this.defaultPanel >= this.panels.length)
		{
			this.defaultPanel = (this.panels.length -1);
		}
	}
	else
	{
		this.defaultPanel = 0;
	}

	// The defaultTab property is supposed to be the tab element for the tab content
	// to show by default. The caller is allowed to pass in the element itself or the
	// element's id, so we need to convert the current value to an element if necessary.

	this.initialisePanels(this.defaultPanel);
	
	setTimeout(this.me+".switchPanel();", this.showTime*1000);
};


KSL.Widget.SlideShow.prototype.initialisePanels = function(selectPanel)
{
	for (var i = 0; i < this.panels.length; i++)
	{
		this.setOpacity(i, 0);	
		this.panels[i].style.position="absolute";
		this.panels[i].style.left="0px";
		this.panels[i].style.top="0px";
		this.panels[i].style.zIndex = this.backZIndex;
	}
	this.setOpacity(selectPanel, 100);
	this.panels[selectPanel].style.zIndex = this.foreZIndex;
	this.selectedPanel = selectPanel;
}

KSL.Widget.SlideShow.prototype.getElement = function(ele)
{
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
}

KSL.Widget.SlideShow.prototype.switchPanel = function()
{
    var nextPanel = this.selectedPanel+1;
    if (nextPanel >= this.panels.length) nextPanel = 0;
     
    // Send active panel to back and fade out
    this.panels[this.selectedPanel].style.zIndex = this.backZIndex;
    this.changeOpacity(this.selectedPanel, 100, 0);
    
    // Send new panel to front and fade in
    this.panels[nextPanel].style.zIndex = this.foreZIndex;
    this.changeOpacity(nextPanel, 0, 100);
    
    this.selectedPanel = nextPanel;
	
	setTimeout(this.me+".switchPanel();", this.showTime*1000);

	return;
}

KSL.Widget.SlideShow.prototype.getElementChildren = function(element)
{
	var children = [];
	var child = element.firstChild;
	while (child)
	{
		if (child.nodeType == 1 /* Node.ELEMENT_NODE */)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};

KSL.Widget.SlideShow.prototype.getPanels = function()
{
	if (this.element)
	{
		return this.getElementChildren(this.element);
	}
	return null;
};

KSL.Widget.SlideShow.setOptions = function(obj, optionsObj, ignoreUndefinedProps)
{
	if (!optionsObj) return;
	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] == undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};

KSL.Widget.SlideShow.prototype.changeOpacity = function(panel, opacStart, opacEnd) 
{
//debugger;

	var speed = this.transTime*1000/this.transSmoothness;
    var time = speed; 
	var opacity = opacStart;
	
	// Determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart < opacEnd) 
	{
		while (opacity <= opacEnd)
		{
			setTimeout(this.me + ".setOpacity(" + panel + "," + opacity + ")", time);
			opacity += this.transSmoothness;
			time += speed;
		}
		//for(i = opacStart; i >= opacEnd; i--) {
		//	setTimeout(this.me + ".setOpacity(" + panel + "," + i + "')", speed);
		//	timer++;
		//}
	} 
	else if(opacStart > opacEnd) 
	{
		while (opacity >= opacEnd)
		{
		var s = this.me + ".setOpacity(" + panel + "," + opacity + ")";
			setTimeout(this.me + ".setOpacity(" + panel + "," + opacity + ")", time);
			opacity -= this.transSmoothness;
			time += speed;
		}
		//for(i = opacStart; i <= opacEnd; i++)
		//	{
		//	setTimeout(this.me + ".setOpacity(" + panel + "," + i + "')", speed);
		//	timer++;
		//}
	}
}

KSL.Widget.SlideShow.prototype.setOpacity = function(panel, opacity) 
{

	var style = this.panels[panel].style; 
	style.opacity = (opacity / 100);
	style.MozOpacity = (opacity / 100);
	style.KhtmlOpacity = (opacity / 100);
	style.filter = "alpha(opacity=" + opacity + ")";
	
//	var children = this.getElementChildren(this.panels[panel]);
//	if (children) {
//		for (var i = 0; i<children.length; i++)
//		{
//			var style = children[i].style; 
//			style.opacity = (opacity / 100);
//			style.MozOpacity = (opacity / 100);
//			style.KhtmlOpacity = (opacity / 100);
//			style.filter = "alpha(opacity=" + opacity + ")";
//	
//		}
//	}

}


