// JavaScript Document
// MainMenu Static API
//<![CDATA[
var self = null;

/***** Ctor *****/
/*	duration : the duration of the transition
	pauseTime : the interval between 2 transition
	startPause : if the component stay on the first tab or rotate between tabs*/
WidgetFade = function(duration, pauseTime, startPause)
{
	self = this;
	this.mActiveImage = null;
	this.mActiveTab = null;
	//this.mActiveInfo = null;
	this.mDuration = duration;
	this.mPauseTime = pauseTime;
	this.mPause = startPause;
	this.mCurrentIndex = null;
	this.mAnimation = setInterval(runAnimation, this.mPauseTime, this);
	this.mButtonEnable = true;
}

/*	Manage the tabClick
	index : wich tab has ben clicked*/
WidgetFade.prototype.tabClick = function(index)
{
	if(this.mButtonEnable)
	{
		if(index != this.mCurrentIndex)
		{
			if( this.mPause == false)
			{
				clearInterval(this.mAnimation);
				this.mAnimation = setInterval(runAnimation, this.mPauseTime);
			}
			
			this.showImage(index);
		}
	}
}

/*	Manage the transition between elements of the component
	index : the element to switch to*/
WidgetFade.prototype.showImage = function(index)
{
	this.mCurrentIndex = eval(index);
	var tab = document.getElementById("wf_tab" + index);
	//var info = document.getElementById("wf_info" + index);
	var image = document.getElementById("wf_image" + index);

	if(index==1) {
		document.getElementById("wf_image1").style.zIndex=5;
		document.getElementById("wf_image2").style.zIndex=4;
		document.getElementById("wf_image3").style.zIndex=3;
		document.getElementById("wf_image4").style.zIndex=2;
		document.getElementById("wf_image5").style.zIndex=1;
	}
	if(index==2) {
		document.getElementById("wf_image1").style.zIndex=1;
		document.getElementById("wf_image2").style.zIndex=5;
		document.getElementById("wf_image3").style.zIndex=4;
		document.getElementById("wf_image4").style.zIndex=3;
		document.getElementById("wf_image5").style.zIndex=2;
	}
	if(index==3) {
		document.getElementById("wf_image1").style.zIndex=2;
		document.getElementById("wf_image2").style.zIndex=1;
		document.getElementById("wf_image3").style.zIndex=5;
		document.getElementById("wf_image4").style.zIndex=4;
		document.getElementById("wf_image5").style.zIndex=3;
	}
	if(index==4) {
		document.getElementById("wf_image1").style.zIndex=3;
		document.getElementById("wf_image2").style.zIndex=2;
		document.getElementById("wf_image3").style.zIndex=1;
		document.getElementById("wf_image4").style.zIndex=5;
		document.getElementById("wf_image5").style.zIndex=4;
	}
	if(index==5) {
		document.getElementById("wf_image1").style.zIndex=4;
		document.getElementById("wf_image2").style.zIndex=3;
		document.getElementById("wf_image3").style.zIndex=2;
		document.getElementById("wf_image4").style.zIndex=1;
		document.getElementById("wf_image5").style.zIndex=5;
	}
	
	tab.className+="_enable";
	//info.className+=" wf_active";
	
	if( this.mActiveTab != null )
	{
		this.mActiveTab.className = this.mActiveTab.className.replace("_enable","");
	}

	/*
	if( this.mActiveInfo != null )
	{
		this.mActiveInfo.className = this.mActiveInfo.className.replace(" wf_active","");
	}
	*/

	if( this.mActiveImage != null )
	{
		this.mButtonEnable = false;
		
		var fadeIn = new Spry.Effect.Fade(image.id, {duration: this.mDuration, from: 0, to: 100, toggle: false});
		var fadeOut = new Spry.Effect.Fade(this.mActiveImage.id, {duration: this.mDuration, from: 100, to: 0, toggle: false});
		
		var obs = new Object;
		var self = this;
		obs.onPostEffect  = function(effect, data)
		{
			self.onAnimationComplete();
		}
		fadeIn.addObserver(obs);
		
		fadeIn.start();
		fadeOut.start();
	}
	else
	{
		image.style.opacity="1";
		image.style.filter="alpha(opacity=100)";
	}

	this.mActiveTab = tab;
	//this.mActiveInfo = info;
	this.mActiveImage = image;
}

/* Is Called on the post effect of the animation */
WidgetFade.prototype.onAnimationComplete = function()
{
	this.mButtonEnable = true;
}

/* is called by the setInterval passing by runAnimation() */
WidgetFade.prototype.onRunAnimation = function()
{
	var nextIndex = 1;
	
	if( this.mCurrentIndex >= 5 )
		nextIndex = 1;
	else
		nextIndex = this.mCurrentIndex + 1;
	
	this.showImage(nextIndex);
}


/* is called by the setInterval passing by runAnimation() */
WidgetFade.prototype.onRunAnimationBckwrd = function()
{
	var nextIndex = 1;
	
	if( this.mCurrentIndex == 1 )
		nextIndex = 5;
	else
		nextIndex = this.mCurrentIndex - 1;
	
	this.showImage(nextIndex);
}
/* is called on the pause button click */
WidgetFade.prototype.pauseAnimation = function(button)
{
	if( this.mPause && this.mButtonEnable )
	{
		button.className = button.className.replace("wf_play","wf_pause");
		this.mPause = false;
		this.onRunAnimation();
		this.mAnimation = setInterval(runAnimation, this.mPauseTime);
	}
	else
	{
		button.className = button.className.replace("wf_pause","wf_play");
		this.mPause = true;
		clearInterval(this.mAnimation);
	}
}

/* is called by the setInterval function */
runAnimation = function()
{
	self.onRunAnimation();
}
//]]>