var currentIter = 0;
var lastIter = 0;
var maxIter = 0;
var slideShowElement = "";
var slideShowData = new Array();
var slideShowInit = 1;
var slideShowDelay = 15000;
var articleLink = "";
var winLink = "";

function initSlideShow(element, data) {
	slideShowElement = element;
	slideShowData = data;
	element.style.display="block";
	
	articleLink = document.createElement('a');
	articleLink.className = 'global';
	element.appendChild(articleLink);
	articleLink.href = "";

        winLink = document.createElement('a');
        element.appendChild(winLink);
        winLink.href = "";
	
	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.top="0px";
		currentImg.style.margin="0px";
		currentImg.style.border="0px";
		currentImg.src=data[i][0];

                if ( slideShowData[i][1].indexOf('.mpg') == -1 )
		articleLink.appendChild(currentImg);
                else
                winLink.appendChild(currentImg);
		currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
		currentImg.currentOpacity.setOpacity(0);
	}
	
	currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 400});
	currentImg.currentOpacity.setOpacity(0);
	
	var slideInfoZone = document.createElement('div');
	slideInfoZone.setAttribute('id','slideInfoZone');
	slideInfoZone.combo = new fx.Combo(slideInfoZone);
	slideInfoZone.combo.o.setOpacity(0);
	articleLink.appendChild(slideInfoZone);

	doSlideShow();
}

function nextSlideShow() {
	//soundManager.play('select');
	lastIter = currentIter;
	currentIter ++;
	if (currentIter >= maxIter)
	{
		currentIter = 0;
		lastIter = maxIter - 1;
	}
	doSlideShow();
	slideShowInit = 0;
}

function doSlideShow() {
	//alert(currentIter);
	if (slideShowInit == 1)
	{
		lastIter = maxIter - 2;
		currentIter = maxIter - 1;
		setTimeout(nextSlideShow,10);
	} else { 
		if (currentIter != 0) {
			$('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,1000);
		setTimeout(hideInfoSlideShow,slideShowDelay-1000);
		setTimeout(nextSlideShow,slideShowDelay);
	}	
}

function showInfoSlideShow() {
        var linkp = null;
        var list = articleLink.childNodes;
        for( var i=0; i<list.length; i++ )
               if ( list[i].id == 'slideInfoZone' )
                   linkp = list[i];
        if ( linkp != null )
	articleLink.removeChild($('slideInfoZone'));
        else
        winLink.removeChild($('slideInfoZone'));
	var slideInfoZone = document.createElement('div');
	slideInfoZone.setAttribute('id','slideInfoZone');
	slideInfoZone.combo = new fx.Combo(slideInfoZone);
	slideInfoZone.combo.o.setOpacity(0);
	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);
	
	if ( slideShowData[currentIter][1].indexOf('.mpg') == -1 )
        {
            articleLink.appendChild(slideInfoZone);
            articleLink.href = slideShowData[currentIter][1];
	    articleLink.setAttribute( 'rel', 'lightbox' );
            articleLink.target='_self';
	    initLightbox();
        }
        else
        {
            winLink.appendChild(slideInfoZone);
            winLink.href = slideShowData[currentIter][1];
            winLink.target='_blank';
        }
	slideInfoZone.combo.o.custom(0, 0.7);
	slideInfoZone.combo.h.custom(0, slideInfoZone.combo.h.el.offsetHeight);
}

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