var startSlide,stories,y,mainLink,rotationTimeout,fadeTimeout;
var opacity = 0;
var doRotate = true;

function validSlideValue(slide)
{
	if(slide >= imgLinkPairs.length)
		slide = slide - imgLinkPairs.length;
	else if(slide < 0)
		slide = imgLinkPairs.length + slide;

	return slide;
}

function beginStoryRotation(sentInterval,sentFadeSpeed,sentTarget,sentAlt)
{
	var imageRotation, storyRotation;
	var navRotation = "";
	//startSlide = Math.ceil(Math.random() * imgLinkPairs.length) - 1;
	startSlide = 0;
	y = validSlideValue(startSlide + 1);	
	stories = document.getElementById("storyItems").getElementsByTagName("div");	
	
	// Preload rotating images
	if(imgLinkPairs.length > 1)
	{	
		navRotation = '<div id="rotationNav" class="dlp_rotationNav">';
		navRotation += '<a href="javascript:moveToSlide(-1);" name="Feature Link Nav / Previous"><img src="' + getImageURL() + 'store/category/back.gif" class="dlp_navBack" alt="Back"/></a><img src="' + getImageURL() + 'store/category/whitePixel.gif" style="width:1px;height:14px;margin-right:5px" />';
		preloaded_images = new Image();
		
		for(i=0; i<imgLinkPairs.length; i++) 
		{
			preloaded_images.src = imgLinkPairs[i][0];

			var j = i+1;			
			var navImageSrc = startSlide == i ? getImageURL() + "store/category/fullSquare.gif" : getImageURL() + "store/category/emptySquare.gif";
			navRotation += '<a href="javascript:moveToSlide(' + j + ')" name="Feature Link Nav / Box ' + j + '"><img src="' + navImageSrc + '" class="dlp_navSquare" alt="Story ' + j + '" id="navItem' + j + '" /></a>';
		}		
		
		navRotation += '<img src="' + getImageURL() + 'store/category/whitePixel.gif" style="width:1px;height:14px;margin-left:5px" /><a href="javascript:moveToSlide(99);" name="Feature Link Nav / Next"><img src="' + getImageURL() + 'store/category/next.gif" class="dlp_navForward" alt="Next"/></a>';	
		navRotation += '</div>';
	}
	
	storyRotation =	'<div id="featureStory" class="dlp_storyContainer">';	
	imageRotation = '<div id="featureImage" style="width:212px;height:152px">';	
	imageRotation += '<a href="" id="mainLink" name="Feature Link 1 / ' + imgLinkPairs[startSlide][2] + '">';
	
	// Render appropriate image tag(s)
	if(sentFadeSpeed > 0)
	{
		imageRotation += '<img src="' + imgLinkPairs[startSlide][0] + '" alt="' + sentAlt + '" id="mainImage1" style="filter:progid:DXImageTransform.Microsoft.alpha(opacity=0);opacity:0;-moz-opacity:0;-khtml-opacity:0;z-index:20;position:absolute;" />';
		imageRotation += '<img src="' + imgLinkPairs[startSlide][0] + '" alt="' + sentAlt + '" id="mainImage2" style="filter:progid:DXImageTransform.Microsoft.alpha(opacity=100);opacity:1;-moz-opacity:1;-khtml-opacity:1;z-index:10;position:absolute;" />';
		
		storyRotation += '<div id="mainStory1" style="filter:progid:DXImageTransform.Microsoft.alpha(opacity=0);opacity:0;-moz-opacity:0;-khtml-opacity:0;z-index:20;position:absolute;">' + stories[startSlide].innerHTML + '</div>';
		storyRotation += '<div id="mainStory2" style="filter:progid:DXImageTransform.Microsoft.alpha(opacity=100);opacity:1;-moz-opacity:1;-khtml-opacity:1;z-index:10;position:absolute;">' + stories[startSlide].innerHTML + '</div>';
	}
	else
	{  
		imageRotation += '<img src="' + imgLinkPairs[startSlide][0] + '" alt="' + sentAlt + '" id="mainImage1" />';  
		storyRotation += '<div id="mainStory1">' + stories[startSlide].innerHTML + '</div>';
	}
	
	imageRotation += "</a></div>";
	storyRotation += "</div>";
	
	document.write(imageRotation);
	document.write(navRotation);
	document.write(storyRotation);
	
	frontImage = document.getElementById("mainImage1");
	backImage = document.getElementById("mainImage2");
	frontStory = document.getElementById("mainStory1");
	backStory = document.getElementById("mainStory2");
	mainLink = document.getElementById("mainLink");	
	interval = sentInterval;
	fadeSpeed = sentFadeSpeed / 2;
	target = sentTarget;
	
	mainLink.name = "Feature Link 1 / " + imgLinkPairs[startSlide][2];
	
	// Set inital link path
	if(imgLinkPairs[0][1] != "")
		mainLink.href = imgLinkPairs[startSlide][1] != "" ? imgLinkPairs[startSlide][1] : imgLinkPairs[0][1];

	if(imgLinkPairs.length > 1 && doRotate)		
		rotationTimeout = setTimeout(rotateImages, interval);
}

function moveToSlide(slide)
{
	clearTimeout(rotationTimeout);
	clearTimeout(fadeTimeout);
	doRotate = false;
	
	if(slide == -1) // Back Button
		y = validSlideValue(y-2);
	else if(slide < 99) // i.e. not the Next button; if Next button clicked (slide == 99), y already has correct value
		y = validSlideValue(slide - 1);

	resetOpacity();	
	rotateImages();	
}

function rotateImages()
{
	if(fadeSpeed > 0 && backImage) // <= IE6 requires more time to load back images, thus setting front opacity to 0 must wait as long as possible.  Contrast this to Firefox <= 2 (in fade() function) which requires more time to set opacity (but not to load images).
	{
		setOpacity(frontImage,0);
		setOpacity(frontStory,0);
	}
		
	frontImage.src = imgLinkPairs[y][0];
	frontStory.innerHTML = stories[y].innerHTML;
	
	for(k=0; k<imgLinkPairs.length; k++) 
	{
		var l = k + 1;
		var navImageSrc = y == k ? getImageURL() + "store/category/fullSquare.gif" : getImageURL() + "store/category/emptySquare.gif";
		document.getElementById("navItem" + l).src = navImageSrc;
	}
		
	if(imgLinkPairs[1][1] != "")  			// If more than one link specified, enable link rotation
	{
		mainLink.href = imgLinkPairs[y][1];
		mainLink.name = "Feature Link 1 / " + imgLinkPairs[y][2];
		if(target != "")					// If window target is supplied
			mainLink.target = target;
	}
	y = validSlideValue(y+1);
	
	if(fadeSpeed > 0 && backImage)
		fade();
	else if(doRotate)
		rotationTimeout = setTimeout(rotateImages,interval);
}

function fade()
{
	if(opacity >= 100 - fadeSpeed)
	{
		backImage.src = frontImage.src;
		backStory.innerHTML = frontStory.innerHTML;
		if(backImage.style.opacity&&!backImage.filters) // Firefox <=2 requires more lead time to set opacity.  For IE issue, see note in rotateImages() function.
		{
			setOpacity(frontImage,0);
			setOpacity(frontStory,0);
		}
		
		//setOpacity(frontStory,101);	// workaround for Firefox's text opacity bug
		opacity = 0;
		//setTimeout(resetOpacity,5);
		if(doRotate)
			rotationTimeout = setTimeout(rotateImages,interval);
	}
	else if(opacity < 100)
	{
		opacity += fadeSpeed;
		setOpacity(frontImage,opacity);
		setOpacity(frontStory,opacity);			
		fadeTimeout = setTimeout(fade,10);
	}
	/*else
	{ 
		backImage.src = frontImage.src;		
		backStory.innerHTML = frontStory.innerHTML;
		setOpacity(frontStory,101);	// workaround for Firefox's text opacity bug
		setTimeout(resetOpacity,5);
		setOpacity(frontImage,0);
		setOpacity(frontStory,0);
		if(doRotate)
			rotationTimeout = setTimeout(rotateImages,interval);
	}	*/
}

/*function setOpacity(theImage, theOpacity)
{
	// Apply correct opacity filter based on broswer
	if (theImage.filters)
		theImage.style.filter = "alpha(opacity="+theOpacity+")";
	else if (theImage.style.MozOpacity)
		theImage.style.MozOpacity=theOpacity/101;
	else if (theImage.style.KhtmlOpacity)
		theImage.style.KhtmlOpacity=theOpacity/100;
	else if (theImage.style.opacity&&!theImage.filters)
		theImage.style.opacity=theOpacity/101;
}*/

function resetOpacity()
{
	opacity = 0;
}