var currentIter = 0;
var lastIter = 0;
var maxIter = 0;
var slideShowElement = "";
var slideShowData = new Array();
var slideShowInit = 0;

function initSlideShow(element, data) {
	slideShowElement = element;
	slideShowData = data;
	element.style.display="block";
	maxIter = data.length;
	for(i=0;i<data.length;i++)
	{
		var currentImg = document.createElement('img');
		currentImg.setAttribute('id','slideElement' + parseInt(i));
		currentImg.style.position="absolute";
		currentImg.style.left="0px";
		currentImg.style.bottom="0px";
		currentImg.style.margin="0px";
		currentImg.src=data[i][0];

		element.appendChild(currentImg);
		currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
		currentImg.currentOpacity.setOpacity(0);
	}

	var leftArrow = document.createElement('a');
	leftArrow.className = 'left';
	leftArrow.onclick = function () { pushPrevSlideShow(); };
	element.appendChild(leftArrow);

	var rightArrow = document.createElement('a');
	rightArrow.className = 'right';
	rightArrow.onclick = function () { pushNextSlideShow(); };
	element.appendChild(rightArrow);

	//currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
	//currentImg.currentOpacity.setOpacity(0);

	var slideInfoZone = document.createElement('div');
	slideInfoZone.setAttribute('id','slideInfoZone');
	element.appendChild(slideInfoZone);

	// Fade in the first image
	$('slideElement' + parseInt(currentIter)).currentOpacity.custom(0, 1);
	// Show the info for the first image
	setTimeout(showInfoSlideShow,500);

	//doSlideShow(1);
//	lastIter = maxIter-1;
//	gotoSlide(0);
}

function destroySlideShow(element) {
	var myClassName = element.className;
	var newElement = document.createElement('div');
	newElement.className = myClassName;
	element.parentNode.replaceChild(newElement, element);
}

function pushNextSlideShow () {
	setTimeout(hideInfoSlideShow,10);
	setTimeout(nextSlideShow,500);
}

function pushPrevSlideShow () {
	setTimeout(hideInfoSlideShow,10);
	setTimeout(prevSlideShow,500);
}

function nextSlideShow() {
	lastIter = currentIter;
	currentIter++;
	if (currentIter >= maxIter)
	{
		currentIter = 0;
		lastIter = maxIter - 1;
	}
	slideShowInit = 0;
	showSlide();
	//doSlideShow(1);
}

function prevSlideShow() {
	lastIter = currentIter;
	currentIter--;
	if (currentIter <= -1)
	{
		currentIter = maxIter - 1;
		lastIter = 0;
	}
	slideShowInit = 0;
	showSlide();
	//doSlideShow(2);
}

function gotoSlide(position) {
	if (position != currentIter)
	{
		lastIter = currentIter;
		currentIter = position;
		setTimeout(hideInfoSlideShow,10);
		setTimeout(showSlide,500);
	}
}

function showSlide()
{
	if (currentIter > lastIter)
	{
		$('slideElement' + parseInt(currentIter)).currentOpacity.options.onComplete = function() {
			$('slideElement' + parseInt(lastIter)).currentOpacity.setOpacity(0);
		}
		$('slideElement' + parseInt(currentIter)).currentOpacity.custom(0, 1);
	}
	else
	{
		$('slideElement' + parseInt(currentIter)).currentOpacity.setOpacity(1);
		$('slideElement' + parseInt(lastIter)).currentOpacity.custom(1, 0);
	}

	setTimeout(showInfoSlideShow,500);
}

function showInfoSlideShow() {
	slideShowElement.removeChild($('slideInfoZone'));
	var slideInfoZone = document.createElement('div');
	slideInfoZone.setAttribute('id','slideInfoZone');
	slideInfoZone.combo = new fx.Combo(slideInfoZone);
	slideInfoZone.combo.o.setOpacity(0);

	// Hide the slide title
	//var slideInfoZoneTitle = document.createElement('h2');
	//slideInfoZoneTitle.innerHTML = slideShowData[currentIter][2]
	//slideInfoZone.appendChild(slideInfoZoneTitle);

	var slideInfoZoneDescription = document.createElement('p');
	slideInfoZoneDescription.innerHTML = slideShowData[currentIter][3]
	slideInfoZone.appendChild(slideInfoZoneDescription);
	slideShowElement.appendChild(slideInfoZone);
	slideInfoZone.combo.o.custom(0, 0.8);
	slideInfoZone.combo.h.custom(0, slideInfoZone.combo.h.el.offsetHeight);
}

function hideInfoSlideShow() {
	$('slideInfoZone').combo.o.custom(0.8, 0);
	//$('slideInfoZone').combo.h.custom(slideInfoZone.combo.h.el.offsetHeight, 0);
}