//Previw Clip page prefix
var previewPrefix = masterPageInputPrefix + "ucPreview_";

//CoBrand player user control prefix for the Cobrand player and more
var CoBrandPrefix = masterPageInputPrefix + "ucCoBrandPlayer_ucPreview_";

//"constants" used in working with the page
var REAL = "RM";
var QT = "FL";
var WM = "WM";
var FL = "FL";
var TAB_PURCHASE = 0;
var TAB_INFO = 1;
var TAB_EMAIL = 2;
var TAB_MESSAGE = 3;

//Used to resize a popup window. 
function ResizePopup(width,height)
{
    if (window.screen) 
    {
        var x = screen.availWidth - width;
        var y = screen.availHeight - height; 
        self.resizeTo(screen.availWidth-x,screen.availHeight-y);
    }
}

//needs to fire whenever a user clicks a tab
//the selected tab displays while all other tabs hide
function tabsClick(tabIndex)
{
    for (var i = 0; i <= 3; i++)
    {
        var tabContent = $("tab" + i);
        var tabMenu = $("menu" + i);
        
        if (i == tabIndex)
        {
            if (tabMenu) tabMenu.className = "tabMenuSelected";
            showElement(tabContent);
            hideCC(false);
        }
        else
        {
            if (tabMenu) tabMenu.className = "tabMenu";
            hideElement(tabContent);
        }
    }         
}

//This will hide all the tabs and menus
function hideTabs()
{
    for (var i = 0; i <= 3; i++)
    {
       var menu = $('menu' + i);
       var tab = $('tab' + i);
       if (menu != null) hideElement(menu);
       if (tab != null)  hideElement(tab);
    }
}

function hideCC(checkTab)
{
    var objCC = $("closedCaptioningText");        
    if (checkTab && objCC.style.visibility == "visible")
    {
        tabsClick(0);        
    }
    if (objCC != null) hideElement(objCC);
}
                        
function toggleCC()
{
    var mediaPlayer = $('divMedia');
    var objCC = $("closedCaptioningText"); 
    var playerMessage = $('tab3');
                         
    //a Boolean variable to get the visiblity flag for TabMessage
    var isTabMessageVisible = false;
    if (playerMessage != null)
    {
        if (playerMessage.style.visibility == "visible")
        {
            isTabMessageVisible = true; 
        }
    }
        
    //if QuickTime
    if (mediaPlayer.innerHTML.indexOf('QTPlayer') != -1)
    {
        displayMessage("Closed Captioning is only available when using Windows Media Player");
        return;
    }
        
    //for non-IE browsers            
    if (!IsBrowserIE())
    {   
        var message = "Your browser only supports closed captioning for Windows Media Player in full-screen mode.<br><br>" + fullScreenMessage.replace(/\n/g, "<br>").replace("FAQs", "<a href=\"#\" onclick=\"loadUrlInMainWindow('/FAQ.aspx', true);\">FAQs</a>");
        displayMessage(message);               
        return;
    }
    
    if (objCC != null)
    {
        if (objCC.style.visibility == "visible")
        {
            tabsClick(0);            
        }
        else
        {
            tabsClick(-1);
            showElement(objCC);
        }
    }               
}

function setupPreviewClip(mediaType)
{
    if (mediaType == null) 
    {
        //if it's null, mediaType is an optional param (like an overload...)
        mediaType = $F(previewPrefix + 'hdnMediaType');         
    }    

    //this will display the flash loader before calling the play video function
    displayFlashLoad();
    setTimeout("playPreviewClip(" + mediaType + ");", 3000);
}
        
function playPreviewClip(mediaType)
{  
    if (mediaType == null || mediaType == "")
        mediaType = WM;

    //pluginlist is defined in /common/js/detect.js
    if (mediaType == WM && pluginlist.indexOf("Windows Media Player") == -1)
    {
        displayPlayerMessage(WM);
        return;            
    }			    
    
    else if (mediaType == QT && pluginlist.indexOf("QuickTime") == -1)
    //else if (mediaType == FL && DetectFlashVer(12, 0))
    {
       // displayPlayerMessage(QT);
       // return;
    }
    
    //check to see which control we're on.  if we're not on the preview control,
    //we need to refer to all these fields using the combination cobrand/preview control id's.
    if ($(previewPrefix + 'hdnAccessMethod') == null)
    {
        previewPrefix = CoBrandPrefix;
    }
                                    
    var mediaUrl = $F(previewPrefix + 'hdnMediaID');

    //although we now have CC for QT, we haven't edited the look and feel of the
    //preview player to support it.  so disable the option for QuickTime.
    if (mediaType == QT)
    {
        hideCC(true);   
    }
     
    var accessMethod = $F(previewPrefix + 'hdnAccessMethod');        
    var contentType = $F(previewPrefix + 'hdnContentType');        
    var contentID = $F(previewPrefix + 'hdnContentID');                        
    var referer = $F(previewPrefix + 'hdnReferer');
    var ip = $F(previewPrefix + 'hdnIP');
    var operatingSystem = $F(previewPrefix + 'hdnOS');
    var browser = $F(previewPrefix + 'hdnBrowser');
    var browserVersion = $F(previewPrefix + 'hdnBrowserVersion');
    var eID = $F(previewPrefix + 'hdnEID');
    
    var strMediaURL = "/PlayMedia.aspx?MediaID=" + escape(mediaUrl) 
        + "&AccessMethod=" + accessMethod + "&contentType=" + contentType + "&contentID=" + contentID
        + "&Format=" + mediaType + "&BitRate=300Kbps&IP=" + escape(ip)
        + "&Referer=" + escape(referer) + "&OS=" + escape(operatingSystem) 
        + "&Browser=" + escape(browser) + "&BrowserVersion=" + escape(browserVersion)
        + "&EID=" + escape(eID);
                                   
 var flashfile = document.getElementById(previewPrefix +'hdnFlashRelease700').value

    BuildPlayerAndPlayVideo(strMediaURL, mediaType, '1', '','','','','','',flashfile,'');
}

function setupRealVideo(url)
{
    //this will display the flash loader before calling the play video function
    displayFlashLoad();
    setTimeout("playRealVideo('" + url + "');", 3000);
}

function playRealVideo(url)
{            
    //pluginlist is defined in /common/js/detect.js
	if (pluginlist.indexOf("RealPlayer") == -1)
	{
	    displayPlayerMessage(REAL);
	    return;
	}
	
	//real player is installed, proceed.        
    var mediaPlayer = $('divMedia');

    if (mediaPlayer == null)
        return;
    
    //copying the code and comments from the old preview clip player
	//Window
    var real = '<OBJECT ID="RVOCX" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" WIDTH="320" HEIGHT="220"><PARAM NAME="SRC" VALUE="' + url + '"><PARAM NAME="CONTROLS" VALUE="ImageWindow"><PARAM NAME="CONSOLE" VALUE="previewClipConsole"><PARAM NAME="AUTOSTART" VALUE="true"> <EMBED TYPE="audio/x-pn-realaudio-plugin" SRC="' + url + '" WIDTH="320" HEIGHT="220" NOJAVA="true" CONTROLS="ImageWindow" CONSOLE="previewClipConsole" AUTOSTART="true" \/><\/OBJECT>';
	
    //Controls
    real += '<OBJECT ID="RVOCXCTL" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" WIDTH="320" HEIGHT="60"><PARAM NAME="SRC" VALUE="' + url + '"><PARAM NAME="CONTROLS" VALUE="All"><PARAM NAME="CONSOLE" VALUE="previewClipConsole"><PARAM NAME="AUTOSTART" VALUE="true"> <EMBED ID="RVOCXCTL" TYPE="audio/x-pn-realaudio-plugin" SRC="' + url + '" WIDTH="320" HEIGHT="60" CONTROLS="All" NOJAVA="true" CONSOLE="previewClipConsole" AUTOSTART="true"></OBJECT>';
	    		
	mediaPlayer.innerHTML = real;	
}

///Writes out the waiting flash element for the video wait function
function displayFlashLoad()
{
    var mediaPlayer = $('divMedia');

    if (mediaPlayer != null)
    {
	    var toWrite = '';
	    toWrite += ('<div id="spinner">');
	    toWrite += ('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
	    toWrite += ('  codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ');
	    toWrite += (' ID="FLASH_AD" WIDTH="320" HEIGHT="280">');
	    toWrite += (' <PARAM NAME="movie" VALUE="/common/flash/fmgodLoading.swf?2"> <PARAM NAME="quality" VALUE="high"> <PARAM NAME="bgcolor" VALUE="#FFFFFF"> '); 
	    toWrite += (' <PARAM NAME="wmode" VALUE="transparent"> ');
	    toWrite += (' <EMBED wmode="transparent" src="/common/flash/fmgodLoading.swf?1" quality=high ');
	    toWrite += (' swLiveConnect="FALSE" WIDTH="320" HEIGHT="280" bgcolor="#FFFFFF"');
	    toWrite += (' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">');
	    toWrite += (' </EMBED>');
	    toWrite += (' </OBJECT>');
	    toWrite += ('</div>');
	
	    mediaPlayer.innerHTML = toWrite;
    }
}

function displayPlayerMessage(mediaType)
{
    var playerName = "";
    var playerURL = "";
    
    switch (mediaType)
    {
	    case QT:
	        playerName = "QuickTime";
		    playerURL = '<p>To download the latest version of Flash for free, <a href="http://www.apple.com/quicktime/" target="_blank">click here<\/a>.<\/p>';
		    break;
			
	    case WM:
	        playerName = "Windows Media Player";
		    playerURL = '<p>To download Windows Media Player for free, <a href="http://www.microsoft.com/windows/windowsmedia/download/" target="_blank">click here<\/a>.<\/p>'
		    break;
			
	    case REAL:
	        playerName = "Real One Media";
		    playerURL = '<p>To download Real Player for free, <a href="http://www.real.com" target="_blank">click here<\/a>.<\/p>'
		    break;
    }
            
    var message = '<p>You do not have ' + playerName + ' installed.<\/p>'
    message += '<p>Either you do not have ' + playerName + ' installed, or your browser is unable to embed the player. ';
    message += 'Please install or reinstall ' + playerName + ' in order to view the preview clip.<\/p>';
    message += playerURL;  
    
    displayMessage(message);          
}

function displayMessage(message)
{
    //hide tabs, and show the error message tab
    tabsClick(TAB_MESSAGE);
    var playerMessage = $('tab3');
    playerMessage.innerHTML = message;
}

function addToCart(itemID)
{
    var tc = $F(previewPrefix + 'hdnTrackingID');
    var url = "/Cart.aspx?WT.si_n=ShoppingCart&WT.si_p=CartAdd&ItemId=" + itemID + "&TrackingId=" + tc;

    loadUrlInMainWindow(url, true);
    
    return false;
}

function updateDigitalPrice()
{
    //the digital format dropdown is delimited as "itemID|price"
    var lblDigitalPrice = $(previewPrefix + 'lblDigitalPrice');
    var price = $F(previewPrefix + 'ddlDigitalFormats').split('|');
    
    if (price.length > 1)
    {        
        lblDigitalPrice.innerHTML = price[1];
    }
    
    return false;
}

function addDigitalItemToCart()
{
    //the digital format dropdown is delimited as "itemID|price"
    var item = $F(previewPrefix + 'ddlDigitalFormats').split('|');   
    
    if (item.length > 0)
    {
        return addToCart(item[0]);
    } 
    
    return false;
}


/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/ 

var w=1
var h=1

if (document.getElementById || document.all)
document.write('<div id="trailimageid" style="position:absolute;visibility:hidden;left:0px;top:-1000px;width:1px;height:1px;"><img id="ttimg" /></div>')

function gettrailobj()
{
	if (document.getElementById) return document.getElementById("trailimageid").style
	else if (document.all) return document.all.trailimagid.style
}

function truebody()
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidetrail()
{
	document.onmousemove=""
	document.getElementById('ttimg').src='img/s.gif'
	gettrailobj().visibility="hidden"
	gettrailobj().left=-1000
	gettrailobj().top=0
}


function showtrail(width,height,file)
{
	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1 && navigator.userAgent.toLowerCase().indexOf('safari') == -1)
	{
		w=width
		h=height
		
		// followmouse()
	
		gettrailobj().visibility="visible"
		gettrailobj().width=w+"px"
		gettrailobj().height=h+"px"
		
		document.getElementById('ttimg').src=file
		document.onmousemove=followmouse
	}
}


function followmouse(e)
{

	if(navigator.userAgent.toLowerCase().indexOf('opera') == -1 && navigator.userAgent.toLowerCase().indexOf('safari') == -1)
	{

		var xcoord=20
		var ycoord=20

		if (typeof e != "undefined")
		{
			xcoord+=e.pageX
			ycoord+=e.pageY
		}
		else if (typeof window.event !="undefined")
		{
			xcoord+=truebody().scrollLeft+event.clientX
			ycoord+=truebody().scrollTop+event.clientY
		}

		var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
		var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)

		if (xcoord+w+3>docwidth)
		xcoord=xcoord-w-(20*2)

		if (ycoord-truebody().scrollTop+h>truebody().clientHeight)
		ycoord=ycoord-h-20;

		gettrailobj().left=xcoord+"px"
		gettrailobj().top=ycoord+"px"

	}

}

/* END IMAGE TRAIL SCRIPT */

