var currentIter = 0;
var lastIter = 0;
var maxIter = 0;
var productSlideShowElement = "";
var productLink = "";
var productSlideShowData = new Array();
var productSlideShowInit = 1;

function initProductSlideShow(element, data, productImageUrl) {
	productSlideShowElement = element;
	productSlideShowData = data;
	element.style.display="block";
	
	productLink = document.createElement('a');
	productLink.className = 'global';
	element.appendChild(productLink);
	
	var productImage = document.createElement('img');
	productImage.className="productImage";
	productImage.style.position="absolute";
	productImage.style.top="0px";
	productImage.style.right="0px";
	productImage.style.margin="0px";
	productImage.src=productImageUrl;
	productLink.appendChild(productImage);
	//alert(productImageUrl);
	
	maxIter = data.length;
	for(i=0;i<data.length;i++)
	{
		var currentImg = document.createElement('img');
		currentImg.setAttribute('id','productSlideElement' + parseInt(i));
		currentImg.style.position="absolute";
		currentImg.style.left="0px";
		currentImg.style.top="0px";
		currentImg.style.margin="0px";
		currentImg.src=data[i][0];
		currentImg.className="slideelement";
	
		productLink.appendChild(currentImg);
		currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
		currentImg.currentOpacity.setOpacity(0);
	}
	
	var productBannerOpacityLink = document.createElement('div');
	productBannerOpacityLink.setAttribute('id','productBannerOpacityLink');
	productLink.appendChild(productBannerOpacityLink);
	
	
	
	currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
	currentImg.currentOpacity.setOpacity(0);
	
	var productSlideInfoZone = document.createElement('div');
	productSlideInfoZone.setAttribute('id','productSlideInfoZone');
	productSlideInfoZone.combo = new fx.Combo(productSlideInfoZone);
	productSlideInfoZone.combo.o.setOpacity(0);
	productLink.appendChild(productSlideInfoZone);
	
	doProductSlideShow(1);
}

function pushnextProductSlideShow () {
	setTimeout(hideProductInfoSlideShow,10);
	setTimeout(nextProductSlideShow,500);
}

function pushprevProductSlideShow () {
	setTimeout(hideProductInfoSlideShow,10);
	setTimeout(prevProductSlideShow,500);
}

function nextProductSlideShow() {
	lastIter = currentIter;
	currentIter++;
	if (currentIter >= maxIter)
	{
		currentIter = 0;
		lastIter = maxIter - 1;
	}
	productSlideShowInit = 0;
	doProductSlideShow(1);
}

function prevProductSlideShow() {
	lastIter = currentIter;
	currentIter--;
	if (currentIter <= -1)
	{
		currentIter = maxIter - 1;
		lastIter = 0;
	}
	productSlideShowInit = 0;	
	doProductSlideShow(2);
}

function doProductSlideShow(position) {
	//alert(currentIter);
	if (productSlideShowInit == 1)
	{
		setTimeout(nextProductSlideShow,10);
	} else {
		if (position == 1)
		{
			if (currentIter != 0) {
				$('productSlideElement' + parseInt(currentIter)).currentOpacity.options.onComplete = function() {
					$('productSlideElement' + parseInt(lastIter)).currentOpacity.setOpacity(0);
				}
				$('productSlideElement' + parseInt(currentIter)).currentOpacity.custom(0, 1);
			} else {
				$('productSlideElement' + parseInt(currentIter)).currentOpacity.setOpacity(1);
				$('productSlideElement' + parseInt(lastIter)).currentOpacity.custom(1, 0);
			}
		} else {
			if (currentIter != maxIter - 1) {
				$('productSlideElement' + parseInt(currentIter)).currentOpacity.setOpacity(1);
				$('productSlideElement' + parseInt(lastIter)).currentOpacity.custom(1, 0);
			} else {
				$('productSlideElement' + parseInt(currentIter)).currentOpacity.options.onComplete = function() {
					$('productSlideElement' + parseInt(lastIter)).currentOpacity.setOpacity(0);
				}
				$('productSlideElement' + parseInt(currentIter)).currentOpacity.custom(0, 1);	
			}
		}
		setTimeout(showProductInfoSlideShow,1000);
		setTimeout(pushnextProductSlideShow,8000); 
		//setTimeout(hideProductInfoSlideShow,8000);
		//setTimeout(nextProductSlideShow,9000);
	}	
}

function showProductInfoSlideShow() {
	productLink.removeChild($('productSlideInfoZone'));
	var productSlideInfoZone = document.createElement('div');
	productSlideInfoZone.setAttribute('id','productSlideInfoZone');
	productSlideInfoZone.combo = new fx.Combo(productSlideInfoZone);
	productSlideInfoZone.combo.o.setOpacity(0);
	var productSlideInfoZoneTitle = document.createElement('h2');
	productSlideInfoZoneTitle.innerHTML = productSlideShowData[currentIter][2];
	productSlideInfoZone.appendChild(productSlideInfoZoneTitle);
	var productSlideInfoZoneDescription = document.createElement('p');
	productSlideInfoZoneDescription.innerHTML = productSlideShowData[currentIter][3];
	productSlideInfoZone.appendChild(productSlideInfoZoneDescription);
	productLink.appendChild(productSlideInfoZone);
	productLink.href = productSlideShowData[currentIter][1];
	productSlideInfoZone.combo.o.custom(0, 0.7);
	productSlideInfoZone.combo.h.custom(0, productSlideInfoZone.combo.h.el.offsetHeight);
}

function hideProductInfoSlideShow() {
	$('productSlideInfoZone').combo.o.custom(0.7, 0);
	//$('productSlideInfoZone').combo.h.custom(productSlideInfoZone.combo.h.el.offsetHeight, 0);
}