var myIntervalId;
var myListOfTemplateIds;

function autoRunDesignPreviews( aListOfTemplateIds )
{
	myListOfTemplateIds = aListOfTemplateIds;
	if( canPlayMovies() )
	{
		playNextDesignPreview();
	}
	else
	{
		showNextDesignScreenshot();
	}
	myIntervalId = setInterval( "showNextDesignWhenReady()", 5000);
}

function showNextDesignWhenReady()
{
	if ( !canPlayMovies() )
	{
		showNextDesignScreenshot();
	}
	else if( isFinishedPlayingMovieWithSound() )
	{
		playNextDesignPreview();
	}
}

function canPlayMovies()
{
	return swfobject.getFlashPlayerVersion().major >= 8;
}

function isFinishedPlayingMovieWithSound()
{
	var theFlashMovie = document.getElementById("DesignPreviewSwf");
	return !theFlashMovie.IsPlaying() && mySoundOnFlag && canPlayMovies();
}

// Use function pointer to combine this and next, genius.
function playNextDesignPreview()
{
	if ( myListOfTemplateIds.length > 0 )	
	{
		playDesignPreview( myListOfTemplateIds.shift() ); 
	}
	else
	{
		clearInterval( myIntervalId );		
	}
}

function showNextDesignScreenshot()
{
	if ( myListOfTemplateIds.length > 0 )	
	{
		showDesignScreenshot( myListOfTemplateIds.shift() ); 
	}
	else
	{
		clearInterval( myIntervalId );		
	}
}

function loadDesignPreview( aTemplateId )
{
	playDesignPreview( aTemplateId );
}

function stopAutoPlayUnmuteAndLoadDesignPreview( aTemplateId )
{
	if ( !canPlayMovies() )
	{
		showDesignScreenshot( aTemplateId );
	}
	else
	{
		setAudibleAndGraphics(true);
		playDesignPreview( aTemplateId );
	}

	clearInterval( myIntervalId );
}

function showDesignScreenshot( aTemplateId )
{
	var theFeaturedDesignDiv = document.getElementById( "featuredDesign" );
	var theDesignPreviewSwfDiv = theFeaturedDesignDiv.firstChild;
	var theScreenshotImg = theDesignPreviewSwfDiv.firstChild;
	theScreenshotImg.src = "/templates/" + aTemplateId + "/screenshot.jpg" ;
	setCurrentTemplate( aTemplateId );
}

function playDesignPreview( aTemplateId )
{
	if ( isUpgradeableFlashVersion() )
    {
			var flashvars = { };
			var params = { allowScriptAccess:"always", allowNetworking:"all", swliveconnect:"true",
							salign:"TL", scale:"noscale", bgcolor:"#FFFFFF", wmode:"transparent"  };
			var attributes = { id:"DesignPreviewSwf" };
			swfobject.embedSWF("/templates/" + aTemplateId + "/previewVideo.swf", 
							   "DesignPreviewSwf",
							   "360",
							   "270",
							   "8.0.0",
							   false,
							   flashvars, params, attributes);
		
		setCurrentTemplate( aTemplateId );
    }
}

function setCurrentTemplate( aTemplateId )
{
    var theElementNodeType = 1;
    var theDesignSection = document.getElementById("designs");
	if (theDesignSection)
	{
		for (var i = 0; i < theDesignSection.childNodes.length; i++)
		{
			if ( theDesignSection.childNodes[i].nodeType == theElementNodeType )
			{
				theDesignSection.childNodes[i].className = "unselectedTemplate";
			}
		}
	
		var theTemplateIconImageElement = document.getElementById(aTemplateId);
		theTemplateIconImageElement.className="selectedTemplate";
	}
			
	var thePartnerPageDesignSection = document.getElementById("design_icons");
	if (thePartnerPageDesignSection)
	{
		for (var i = 0; i < thePartnerPageDesignSection.childNodes.length; i++)
		{
			if ( thePartnerPageDesignSection.childNodes[i].nodeType == theElementNodeType )
			{
				var thePartnerPageDesignIconDiv = thePartnerPageDesignSection.childNodes[i];
				var thePartnerPageDesignIconImageDiv = getFirstChildThatIsAnElement(thePartnerPageDesignIconDiv);
				var thePartnerPageDesignLink = getFirstChildThatIsAnElement(thePartnerPageDesignIconImageDiv);
				thePartnerPageDesignLink.className = "unselectedTemplate";
			}
		}
	
		var theTemplateIconImageElement = document.getElementById(aTemplateId);
		theTemplateIconImageElement.className="selectedTemplate";
	}
}

function getFirstChildThatIsAnElement( aParent )
{
    var theElementNodeType = 1;

	for (var i = 0; i < aParent.childNodes.length; i++)
	{
		if ( aParent.childNodes[i].nodeType == theElementNodeType )
		{
			return aParent.childNodes[i];
		}
	}
}