//================================
//Consolidate Player js - 1-29-07
//================================

//Const
var bLaunchStandAloneForMACWM=false;

/////////////////////////////////////////////////////////////////////////////////////
// All Dependencies
/////////////////////////////////////////////////////////////////////////////////////

function fnMCast(szName, szMsg, szURL, szType, sUniqueID, sGID)
{
//What initiates a multicast
//      Arguments:
//          szName  - program name
//          szMsg   - message for message window
//          szURL   - URL to be sent to player
//          szType  - video type (MP1, MP2...)
//          sUniqueID   - What uniquely identifies this asset in the SAP (ip, port...)
//          sGID        - GID of asset to be played
//
//  Called from:
//      PC Live
//      Linx Live
//      MAC Live
//
//      1. Sets the message in message
//      2. Sets properties in postback object
//      3. Verifies correct player
//Notes:
//  1. Invoked for Live
 
    if (szName == null)
        return;	


    if ("STB" == viewer)
        setTitleAndChannelProperties(szName);   // located in MCSPlayerCtrlSTB

    // If no URL ...
    if (szURL == "")
    {
        document.getElementById("lblMsg").innerHTML = "Cannot be viewed.";

        //No need to move this to the individual player js file
        if ("MSIE" == viewer)
	        alert(szName + "\r\rCannot be viewed.");
        return;
    }

    // Postback is the iFrame that sets the appropriate player
    // The page_load event processes conditionaly on the IsPostBack flag.  
    //    When is the page ever posted back?
    var obj;
    if(opener){
        obj=opener.postBack;
    }else{
        obj = ("MAC"==viewer)?postBack:document.getElementById("postBack").contentWindow;
    }
    
    //
    // This is a terrible solution but the only one that works without removing all
    // of the archaic iframes that are currently in use for status determination.
    var recState = null;
    for(var i = 0; i < 1000; ++i)
    {
        if ((recState = _getRecordStatus(obj)) != null)
            break;
    }
     
    if(recState != null)
    {
        var szRec = getRecordState("Action", recState.value);
        updateMessage(szRec, szMsg);
    }
    
    // reset cc
    szURL = resetCC(szURL);
    // set current url, and record status:
    obj.document.getElementById("selURL").value = szURL;
    obj.document.getElementById("selProg").value = szName;
    obj.document.getElementById("progMsg").value = szMsg;
    obj.document.getElementById("progType").value = szType;
    obj.document.getElementById("hidGID").value = sGID;
    if(sUniqueID!=null)
	    obj.document.getElementById("SAPUniqueID").value=unescape(sUniqueID);

    // for vbrick player
    if ("MSIE" == viewer)
	    setupPlayer(szURL, szType, szRec);          // located in MCSPlayerCtrlPC.js
	else if ("linux" == viewer)
	    setupPlayerLinux(szURL, szType, szRec);     // located in MCSPlayerCtrl_PC_Mac_Linx.js     
    // MAC
    else if ("MAC" == viewer)
        MAC_openVBrickPlayer(szURL,szType,szRec);   // located in Main.js
    // for STB
    else if ("STB" == viewer)
	    setupPlayer(szURL, szType, szRec);          // located in MCSPlayerCtrlSTB
}		


function MCastMPEGSetup(szURL,szRec,szType,seekTime) 
{
//Called from fnMCast() for MSIE and MPEG streams
//      Arguments:
//          szURL   - URL to be sent to player
//          szRec   - ?
//          szType  - video type (MP1, MP2...)
//          seekTime - where to start playing asset
//
//      Opened from:
//          PC Live
//          Linx Live
//          Mac Live
//          Mac stored
//
//      1. Displays the record btn
//      2. ? Does something with sdp file 
//      3. calls openPlayer
 
    if (GetStreamTypeEnum(szType)==enumStreamType.WM && (Viewer==enumViewer.LinuxPC || Viewer==enumViewer.MacPC))
    {
        var ccOn = document.getElementById('ucEC_img_cc').src;
        
        if (Viewer==enumViewer.LinuxPC && ccOn.indexOf("ccOn.gif") > -1)
            qtplayer.document.getElementById('oVBPlayer').ClosedCaption = true;
        else if (Viewer==enumViewer.LinuxPC && ccOn.indexOf("ccOn.gif") < 0)
            qtplayer.document.getElementById('oVBPlayer').ClosedCaption = false;

        if (ccOn.indexOf("ccOn.gif") > -1)
        {
            //only show label if WM with CC enabled
	        parent.document.getElementById('lblCC').style.display='block';                                        
        }
        else
        {
            parent.document.getElementById('lblCC').style.display='none';                        
        }
    }
    //used if an mp1, mp2, or mp4 stream is selected after a WM that had CC
    else if (Viewer==enumViewer.LinuxPC || Viewer==enumViewer.MacPC)
    {
        parent.document.getElementById('lblCC').style.display='none';    
    }   

	if ("" != szRec)
	{
		var usable = (szRec == "Stop") ? 2 : 1;
		try{
		
		//Support for multiple ext players
        var sParent = window.parent.location.href;
        if ((bMultipleExtPlayer == "TRUE") && (sParent.indexOf("ExtPlayer4") > 0))
            qtplayer.document.getElementById("oVBPlayer").RecButtonVisible = 0;
        else
		qtplayer.document.getElementById("oVBPlayer").RecButtonVisible = 1;
		qtplayer.document.getElementById("oVBPlayer").RecButtonUsable = usable;
		}catch(e){}
	}
				
	if (szType == "MPEG4")
	{
		szURL = fnUnEscSDP(szURL);
    }

	openPlayer(szURL, seekTime,szRec);
}

	
//// open player
//var retry_player = true;
function openPlayer(stream, seekTime,szRec)
{
//      Arguments:
//          stream      - URL to be sent to player
//          seekTime    - where to start playing asset
//          szRec       - ?
//
//      Opened from:
//          PC Live
//          PC Stored
//          Linx Live
//          Linx Stored
//          Mac Live
//          Mac Stored
//
//      1. opens the player
//      2. ? Does something with the record button 
//      3. Sets cc on linux 

    var retry_player = true;
    try
    {
		// using preview window
		if (!extPlayer || extPlayer.closed || "Thumbnail" == streamType){
			try{
				SetPlayerButtons(szRec);
				qtplayer.document.getElementById("oVBPlayer").Open(stream, seekTime);
			}catch(ex){ 
				//sometimes on mac when switching back to vbrick player from quicktime there is an error thrown
				//because the browser is slow in re-rendering the vbrick plugin, so if there is an error wait a second and 
				//try again
				if(retry_player)
				{
					retry_player = false;
					window.setTimeout('openPlayer("'+stream+'","'+ seekTime+'","'+szRec+'")',1000);
				}
				else
					throw "error";
				return;
			}
			if ((szRec != null && szRec != "" && bMultipleExtPlayer == "TRUE" ) &&
			(!(bMultipleExtPlayer == "TRUE"  && window.parent.location.href.indexOf("ExtPlayer4") > 0)))
			{
				qtplayer.document.getElementById("oVBPlayer").RecButtonUsable = (szRec == "Stop") ? 2 : 1;;				
			}

			if(viewer=="linux" && stream.indexOf('cc=on_ovl')>-1){
				qtplayer.document.getElementById("oVBPlayer").ClosedCaption=true;
			}
			// reset the variable in case it retried and worked
			// the 2nd time in which case it would be set to false.
			retry_player = true;
		}
		// reopen external player
		else{
			SetPlayerButtons(szRec);
			externalPlay(1);	// reset seek time due to restart a new stream
			extPlayer.focus();
		}
    }catch (error){ 
		LogPlayerStop();
		alert("The Program is Currently Not Available or Not Accessible.");
	}
}

function _getRecordStatus(obj)
{
    var recState = obj.document.getElementById("recState");
    
    if(recState != null)
        return recState;
    else
        return null;
}
