
// Create lp_utils namespace
if ( !window.lp_utils )
{
  window.lp_utils = {};
}

var isDOM = (typeof(window.event) == 'undefined');
var isIE = (typeof(window.event) != 'undefined');

/*
	Captures the fader items from the DOM
*/
lp_utils.fader = function (sId)
{
	lp_utils.fader(sId, false);	
}

lp_utils.fader = function (sId, randomStart)
{
	this.id = sId;
	this.container = document.getElementById(sId);
	
	this.opacityMax = 99;
	this.fadeSpeed = 5;
	this.fadeDirection = -1;
	this.fadeTimer = null; 
	
	this.items;			//Array of individual fader items (divs with class="fader_item")
	this.currentIndex;	//Current fader item
	
	this.init(randomStart);
}

lp_utils.fader.prototype.setMouseOver = function(oFunction)
{
	this.container.onmouseover = oFunction;	
}

lp_utils.fader.prototype.setMouseOut = function(oFunction)
{
	this.container.onmouseout = oFunction;	
}

lp_utils.fader.prototype.getItems = function () {
	var innerDivs = this.container.getElementsByTagName("div");
	var items = new Array();
	
	for(i=0;i<innerDivs.length;i++)
	{
		if(innerDivs[i].className == "fader_item")
		{
			items.push(innerDivs[i]);
		}
		if(innerDivs[i].className == "faderNextControl")
		{
			var that = this;
			innerDivs[i].onclick = function(){that.doFade(1)};
			innerDivs[i].style.visibility = "visible";
		}
		if(innerDivs[i].className == "faderPrevControl")
		{
			var that = this;
			innerDivs[i].onclick = function(){that.doFade(-1)};
			innerDivs[i].style.visibility = "visible";
		}
	}
	
	return items;
}


lp_utils.fader.prototype.init = function(randomStart) {
	
	this.items =  this.getItems();
	
	for(i=0;i<this.items.length;i++)
	{
		this.items[i].style.display = "none";	
		this.items[i].style.top = "0";
	}
	
	if(randomStart)
	{
		this.currentIndex = Math.round((this.items.length - 1) * Math.random());
	}
	else
	{
		this.currentIndex = 0;
	}
	
	this.items[this.currentIndex].style.display = "block";
	
	this.setOpacity(this.opacityMax);
}


lp_utils.fader.prototype.setOpacity = function(iValue){
	
	if(isIE) // For IE
	{
		this.items[this.currentIndex].style.filter = "alpha(opacity=" + iValue + ");";
	}
	
	if(isDOM) // For firefox / mozilla / W3C
	{
		this.items[this.currentIndex].style.opacity = iValue / 100;
	}
}

lp_utils.fader.prototype.getOpacity = function(){
	
	if(isIE) // For IE
	{
		return parseInt( this.items[this.currentIndex].style.filter.substring( this.items[this.currentIndex].style.filter.indexOf( "=" ) + 1 ), 10 );
	}
	
	if(isDOM) // For firefox / mozilla / W3C
	{
		return Math.round(parseFloat(this.items[this.currentIndex].style.opacity) * 100);
	}
}


lp_utils.fader.prototype.nextItem = function(){
	
	//fade out the current one
	
	//go to the next item
	
	//fade in the new one
	
}


lp_utils.fader.prototype.nextIndex = function(){
	
	this.items[this.currentIndex].style.display = "none";
	
	this.currentIndex++;
	
	if(this.currentIndex >= this.items.length)
	{
		this.currentIndex = 0;	
	}
	
	this.items[this.currentIndex].style.display = "block";
}

lp_utils.fader.prototype.prevIndex = function(){
	
	this.items[this.currentIndex].style.display = "none";
	
	this.currentIndex--;
	
	if(this.currentIndex < 0)
	{
		this.currentIndex = this.items.length - 1;	
	}
	
	this.items[this.currentIndex].style.display = "block";
}


lp_utils.fader.prototype.doFade = function(iDirection) {
	
	clearTimeout(this.fadeTimer);
	var that = this;
	
	var doAction = function(){
		
		that.doFadeStep();
		
		if(that.getOpacity() <= 0)
		{
			that.fadeDirection = 1;
			if(iDirection > 0) that.nextIndex();
			else that.prevIndex();
			that.setOpacity(0);
			that.fadeTimer = setTimeout(doAction, 10);
		}
		else if(that.getOpacity() >= that.opacityMax)
		{
			that.fadeDirection = -1;
			// Finished
		}
		else
		{
			that.fadeTimer = setTimeout(doAction, 10);
		}
	}
	
	this.fadeTimer = setTimeout(doAction, 10);
}

lp_utils.fader.prototype.doFadeStep = function (){
	var nextOpacity = this.getOpacity() + (this.fadeSpeed * this.fadeDirection);
	
	if(nextOpacity < 0)
	{
		nextOpacity = 0;
	}
	
	if(nextOpacity > this.opacityMax)
	{
		nextOpacity = this.opacityMax;	
	}
		
	this.setOpacity(nextOpacity);
}

var debugTick = 0;

lp_utils.fader.prototype.displayDebug = function(){
	
	var sDebugHTML = "debugtick = " + debugTick++;
	
	sDebugHTML += "<br/>opacity = " + this.getOpacity();
	sDebugHTML += "<br/>currentIndex = " + this.currentIndex;
	
	document.getElementById("pod_shop_debug").innerHTML = sDebugHTML;

}

lp_utils.faderControler = function ()
{
	this.faders = new Array();
	this.interval = 6000;
	this.timer = null;
}

lp_utils.faderControler.prototype.addFader = function(oFader){
	this.addFader(oFader, true);
}


lp_utils.faderControler.prototype.addFader = function(oFader, doAuto){
	var that = this;
	this.faders.push(oFader);	
	
	if(doAuto)
	{
		var doMouseOver = function(){that.stopAuto();};
		var doMouseOut = function(){that.startAuto();};
		oFader.setMouseOver(doMouseOver);
		oFader.setMouseOut(doMouseOut);
	}
}

lp_utils.faderControler.prototype.startAuto = function()
{	
	var that = this;
	
	var doAction = function(){
		for(i=0; i<that.faders.length; i++)
		{
			that.faders[i].doFade(1);
		}
	}
	
	clearInterval(this.timer);
	
	this.timer = setInterval(doAction, this.interval);
}

lp_utils.faderControler.prototype.stopAuto = function()
{
	clearInterval(this.timer);
}

