var currentIter = 0;
var lastIter = 0;
var maxIter = 0;
var slideShowElement = "";
var slideShowData = new Array();
var slideShowInit = 1;
var slideShowDelay = 11000; // was 9000. This is the time between slides, not the fade time.
var articleLink = "";
var tailoredInit = 1;

function initSlideShow(element, data) {

	slideShowElement = element;
	slideShowData = data;
	element.style.display="block";
	
	articleLink = document.createElement('a');
	articleLink.className = 'global';
	element.appendChild(articleLink);

	// !!! jdueck usually you specify the URL in each individual side.
	// We hard-coded it instead.
	articleLink.href = "http://www.tailoredmusic.com/testimonials/";
	
	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];
	
		articleLink.appendChild(currentImg);
		// !!! jdueck change the duration here
		currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 5500}); // was 5500
		currentImg.currentOpacity.setOpacity(0);
	}

	// !!! jdueck change the duration here also
	currentImg.currentOpacity = new fx.Opacity(currentImg, {duration: 5500}); // was 5500

	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)
	{
		setTimeout(nextSlideShow,10);
	}
	else
	{ 
		if (currentIter != 0)
		{
			$('slideElement' + parseInt(currentIter)).currentOpacity.options.onComplete = function()
			{
				$('slideElement' + parseInt(lastIter)).currentOpacity.setOpacity(0);
			}
			// !!! jdueck Make the first frame appear as quickly as possible with no fading.
			if ((currentIter == 2) && (tailoredInit == 1))
			{
				$('slideElement' + parseInt(currentIter)).currentOpacity.custom(1, 1);
				tailoredInit = 0;
			}
			else
			{
				$('slideElement' + parseInt(currentIter)).currentOpacity.custom(0, 1);
			}
		}
		else
		{
			$('slideElement' + parseInt(currentIter)).currentOpacity.setOpacity(1);
			$('slideElement' + parseInt(lastIter)).currentOpacity.custom(1, 0);
		}
		// !!! jdueck commented out to hide caption.
		//setTimeout(showInfoSlideShow,1000);
		//setTimeout(hideInfoSlideShow,slideShowDelay-1000);

		setTimeout(nextSlideShow,slideShowDelay);
	}	
}

function showInfoSlideShow() {
	articleLink.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);
	articleLink.appendChild(slideInfoZone);
	
	articleLink.href = slideShowData[currentIter][1];
	
	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);
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

function randOrd()
{
	return (Math.round(Math.random())-0.5);
}

/*
function startSlideshow()
{
	countArticle = 0;
	var mySlideData = new Array();

	while (countArticle <= 23)
	{
		eval ("mySlideData[countArticle++] = new Array(\'images/slides/slide" + countArticle + ".jpg\',\'destinationurl\',\'Item Title\',\'Item Description\')");
	}

	mySlideData.sort(randOrd);

	initSlideShow($('mySlideshow'), mySlideData);
}
*/