/* ---- Global object for global variables ---------------------------------- */
function elAdminComponentSlideShow()
{
	this.index         = 0;
	this.timeout       = null;
	this.transTO       = null;
	this.transProgress = 0;
	//this.transSpeed    = 10;
	this.transSpeed    = 4;
	this.transDir      = 1;
	this.transFlag     = true;
	this.images        = Array();
	this.running       = false;
	this.delay         = 5000;
}

eacss = new elAdminComponentSlideShow();

/* ---- Preload&Launch functions -------------------------------------------- */
function preloadSlideShow(images)
{
	eacss.images = images;
	slideshow_preload = Array();
	for(var i=0;i<eacss.images.length;i++)
	{
		slideshow_preload[i] = new Image();
		slideshow_preload[i].src = eacss.images[i];
	}
}

function launchSlideShow(aDelay)
{
	stopSlideShow();

	eacss.index = 0;
	eacss.delay = aDelay;
	eacss.transFlag = true;
	eacss.transDir = 1;
	
	fContainer = document.getElementById('slideshow_container');
	sContainer = document.getElementById('slideshow_secondtainer');
	fContainer.src = eacss.images[0];
	sContainer.src = eacss.images[1];
	fContainer.style.zIndex = 1;
	sContainer.style.zIndex = 0;
	setOpacity(fContainer,1);
	setOpacity(sContainer,0);
	
	//if (eacss.running == false)
	//{
		//window.clearTimeout(eacss.timeout);
		//window.clearTimeout(eacss.transTO);
		eacss.running = true;
		eacss.timeout = window.setTimeout("refreshSlideShow()",eacss.delay);
		//refreshSlideShow();
	//}
}

/* ---- Transition related functions ---------------------------------------- */
function refreshSlideShow()
{
	if (eacss.running == true)
	{
		launchTransition();
		eacss.index++;
		if (eacss.index >= eacss.images.length)
		{
			eacss.index = 0;
		}
		eacss.timeout = window.setTimeout("refreshSlideShow()",eacss.delay);
	}
}

function launchTransition()
{
	var next = eacss.index + 1;
	if (next >= eacss.images.length)
	{
		next = 0;
	}
	
	try
	{
		prepareNextImageSet(eacss.images[eacss.index],eacss.images[next]);
		updateTransition();
	}
	catch (e)
	{
		stopSlideShow();
	}
}

function updateTransition()
{
	var stopLaunch = false;
	
	eacss.transProgress +=  eacss.transDir * eacss.transSpeed;
	
	if (eacss.transProgress <= 0)
	{
		eacss.transDir      = 1;
		eacss.transProgress = 0;
		stopLaunch = true;		
	}
	else if (eacss.transProgress > 100)
	{
		eacss.transDir      = -1;
		eacss.transProgress = 100;
		stopLaunch = true;
	}

	var trans = eacss.transProgress/100
	
	try
	{
		setOpacity(document.getElementById('slideshow_secondtainer'),trans);
		setOpacity(document.getElementById('slideshow_container'),1-trans);
		/*
		document.getElementById('slideshow_secondtainer').style.opacity = trans;
		document.getElementById('slideshow_container').style.opacity    = (1-trans);
		document.getElementById('slideshow_secondtainer').style.filter = "alpha(opacity=" + eacss.transProgress + ")";
		document.getElementById('slideshow_container').style.filter    = "alpha(opacity=" + (100-eacss.transProgress) + ")";
		* */
	
		if (stopLaunch == false)
		{
			eacss.transTO = window.setTimeout("updateTransition()", 30);
		}
	}
	catch (e)
	{
		stopSlideShow();
	}
}

function prepareNextImageSet(fImage,sImage)
{
	fContainer = document.getElementById('slideshow_container');
	sContainer = document.getElementById('slideshow_secondtainer');
	
	switch (eacss.transFlag)
	{
		case true:
			fContainer.src = fImage;
			sContainer.src = sImage;
			fContainer.style.zIndex = 1;
			sContainer.style.zIndex = 0;			
			eacss.transFlag = false;
		break;
		
		default:
			sContainer.src = fImage;
			fContainer.src = sImage;
			sContainer.style.zIndex = 1;
			fContainer.style.zIndex = 0;			
			eacss.transFlag = true;
		break;
	}	
}

function stopSlideShow()
{
	eacss.running = false;
	window.clearTimeout(eacss.timeout);
	window.clearTimeout(eacss.transTO);
}

function setOpacity(container,value)
{
	container.style.opacity = value;
	container.style.filter  = "alpha(opacity=" + Math.round(value*100) + ")";
}

/* ---- Asynchronous Refresher function called by framework ----------------- */
function receiveLoadedSlideShow(aResponse,aContainer)
{
	var aContent = decodeXmlResponse(aResponse,"content");
	
	if (aContent.length > 0)
	{
		stopSlideShow();
		
		bContent = "";
		aOffset  = 0;
		aIndex   = 0;
		new_images = Array();
		while (aContent.indexOf(";",aOffset) != -1)
		{
			aBase   = aOffset;
			aOffset = aContent.indexOf(";",aOffset);
			new_images[aIndex] = aContent.substring(aBase,aOffset);
			aOffset++;
			aIndex++;
		}

        preloadSlideShow(new_images);
		launchSlideShow(eacss.delay);
		aContainer.style.visibility = 'visible';
	}
	else
	{
		aContainer.style.visibility = 'hidden';
		stopSlideShow();
	}
}