var startSlide,stories,y,mainLink,rotationTimeout,fadeTimeout,swfCode;
var mainImages = new Array();
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 initMainImages()
{
	for(i=0; i<imgLinkPairs.length; i++)
	{
		var linkOpen,linkClose;
		if(imgLinkPairs[i][1] != "")
		{
			linkOpen = imgLinkPairs[i][1] != "" ? '<a href="' + imgLinkPairs[i][1] + '" name="Feature Link 1 / ' + imgLinkPairs[i][2] + '">' : '';
			linkClose = imgLinkPairs[i][1] != "" ? '</a>' : '';
		}
		else if(imgLinkPairs[0][1] != "")
		{
			linkOpen = imgLinkPairs[0][1] != "" ? '<a href="' + imgLinkPairs[0][1] + '" name="Feature Link 1 / ' + imgLinkPairs[0][2] + '">' : '';
			linkClose = imgLinkPairs[0][1] != "" ? '</a>' : '';
		}
		else
		{
			linkOpen = "";
			linkClose = "";
		}
		
		if(imgLinkPairs[i][3] != "" && imgLinkPairs[i][3] != null && imgLinkPairs[i][4] != "" && imgLinkPairs[i][4] != null) // if a swf
		{
			var swfPath = imgLinkPairs[i][3];
			var swfName = "dlpFlash" + (i + 1);
			var swfWidth = imgLinkPairs[i][4][0];
			var swfHeight = imgLinkPairs[i][4][1];
			var swfVersion = imgLinkPairs[i][4][2];
			var swfBGcolor = imgLinkPairs[i][4][3];
			var wmode = imgLinkPairs[i][4][4];
			var moreVars = imgLinkPairs[i][4][5];
			var noflashHTML = linkOpen + '<img src="' + imgLinkPairs[i][0] + '" alt="Feature Story"/>' + linkClose;
			var noflashRedirect = imgLinkPairs[i][4][6];
			
			mainImages[i] = renderDLPSWF(swfPath,swfName,swfWidth,swfHeight,swfVersion,swfBGcolor,wmode,moreVars,noflashHTML,noflashRedirect);			
		}
		else			
			mainImages[i] = linkOpen + '<img src="' + imgLinkPairs[i][0] + '" alt="Feature Story"/>' + linkClose;		
	}
}


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");	
	
	initMainImages();
	
	// 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/previous.gif" class="dlp_navBack" alt="Previous"/></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/fullSquare3.gif" : getImageURL() + "store/category/emptySquare3.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/next3.gif" class="dlp_navForward" alt="Next"/></a>';	
		navRotation += '</div>';
	}
	
	storyRotation =	'<div id="featureStory" class="dlp_storyContainer">';	
	imageRotation = '<div id="featureImage" style="width:188px;height:160px">';	
		
	// Render appropriate image tag(s)
	if(sentFadeSpeed > 0)
	{
		imageRotation += '<div id="mainImageDiv1" style="filter:progid:DXImageTransform.Microsoft.alpha(opacity=0);opacity:0;-moz-opacity:0;-khtml-opacity:0;z-index:20;position:absolute;display:none">';
		imageRotation += mainImages[startSlide];
		imageRotation += '</div>';	
		
		imageRotation += '<div id="mainImageDiv2" style="filter:progid:DXImageTransform.Microsoft.alpha(opacity=100);opacity:1;-moz-opacity:1;-khtml-opacity:1;z-index:10;position:absolute;">';
		imageRotation += mainImages[startSlide];
		imageRotation += '</div>';
		
		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 += '<div id="mainImageDiv1">' + mainImages[startSlide] + '</div>';  
		storyRotation += '<div id="mainStory1">' + stories[startSlide].innerHTML + '</div>';
	}
	
	imageRotation += "</div>";
	storyRotation += "</div>";
	
	document.write(imageRotation);
	document.write(navRotation);
	document.write(storyRotation);
	
	frontImage = document.getElementById("mainImageDiv1");
	backImage = document.getElementById("mainImageDiv2");
	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 cancelRotation()
{
	clearTimeout(rotationTimeout);
	clearTimeout(fadeTimeout);
}
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.style.display = "none";
	}
		
	frontImage.innerHTML = mainImages[y];
	frontStory.innerHTML = stories[y].innerHTML;
	
	for(k=0; k<imgLinkPairs.length; k++) 
	{
		var l = k + 1;
		var navImageSrc = y == k ? getImageURL() + "store/category/fullSquare3.gif" : getImageURL() + "store/category/emptySquare3.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)
	{
		frontImage.style.display = "block";
		fade();
	}
	else 
	{
		cX("onload");
		if(doRotate)
			rotationTimeout = setTimeout(rotateImages,interval);				
	}
}

function fade()
{
	if(opacity >= 100 - fadeSpeed)
	{
		backImage.innerHTML = frontImage.innerHTML;
		
		backStory.innerHTML = frontStory.innerHTML;
		if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) 
		{ 
			var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			if (ffversion<3)
  
			//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);
				frontImage.style.display = "none";
			}
		}
		cX("onload");
		//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;
}





function renderDLPSWF(swfPath,swfName,swfWidth,swfHeight,swfVersion,swfBGcolor,wmode,moreVars,noflashHTML,noflashRedirect){
	if (flashversion >= swfVersion) {
		var strPreviouslyViewed;
		var swfViewedCookie = readCookie(swfName + "Viewed");
		if (swfViewedCookie != null){
			strPreviouslyViewed = swfViewedCookie;
		} else {
			createCookie(swfName + "Viewed",1);
		}
		if(!wmode){ wmode = "Opaque" };
		swfCode = '<div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + swfVersion + ',0,0,0" width="' + swfWidth + '" height="' + swfHeight + '" id="' + swfName + '" align="middle" onmouseup="javscript:cancelRotation()">' +
		'<param name="allowScriptAccess" value="always" />' +
		'<param name="FlashVars" value="imageURL=' + getImageURL() + '&storeURL=' + getStoreURL() + '&contentURL=' + getContentURL() + '&platform=' + platform2 + '&viewed=' + strPreviouslyViewed + moreVars +'">' +
		'<param name="movie" value="' + swfPath + '" />' +
		'<param name="quality" value="high" />' +
		'<param name="bgcolor" value="' + swfBGcolor + '" />' +
		'<param name="wmode" value="' + wmode + '" />' +
		'<embed src="' + swfPath + '" FlashVars="imageURL=' + getImageURL() + '&storeURL=' + getStoreURL() + '&contentURL=' + getContentURL() + '&platform=' + platform2 + '&viewed=' + strPreviouslyViewed + moreVars +'" quality="high" wmode="' + wmode + '" bgcolor="' + swfBGcolor + '" width="' + swfWidth + '" height="' + swfHeight + '" name="' + swfName + '" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
		'</object></div>'
		
		// Handler for javascript commands from from Flash
		window[swfName + "_DoFSCommand"] = function(command, args) {
			var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
			var flashVideoObj = isInternetExplorer ? document.all.shippingFlash : document.shippingFlash;
			var myArray = args.split(",");
			if (command == "cmCreatePageviewTag") {
			  cmCreatePageviewTag(myArray[0], null, myArray[1]); //(linkName,null,catString)
			} else if (command == "cmCreateManualLinkClickTag") {
			  cmCreateManualLinkClickTag(myArray[0],myArray[1]); //(link path,link name)
			}
		}
		
		// Hook for Internet Explorer.
		if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
			document.write('<script language=\"VBScript\"\>\n');
			document.write('On Error Resume Next\n');
			document.write('Sub ' + swfName + '_FSCommand(ByVal command, ByVal args)\n');
			document.write('	Call ' + swfName + '_DoFSCommand(command, args)\n');
			document.write('End Sub\n');
			document.write('</script\>\n');
		}
		
	} else {
		if (noflashRedirect){
			window.location = noflashRedirect;
		} else {
			swfCode = noflashHTML;
		}
	}
	
	return swfCode;
}