var flashPlayer;
var wmvPlayer;
var urlToLoad
var wmvReq;
var isPlayerStopped=new Boolean(false);



function PlayCh(ch, type)
{
    //Stop The WMV first
    wmvReq = true;
    urlToLoad =  ch;
    PlayWmv();
    
    if (type == 'guest')
       setTimeout('wmvPlayer.sendEvent(\'STOP\')', '70000');    
}

function PlayWmv()
{
   isPlayerStopped = false; 
   wmvPlayer.sendEvent('LOAD',urlToLoad);
   wmvPlayer.sendEvent('PLAY');
   
   //setTimeout('wmvPlayer.sendEvent(\'STOP\')', '60000');
} 

function stopWmv()
{
    if (!isPlayerStopped){
        isPlayerStopped = 'true';
        wmvPlayer.sendEvent('STOP');
        setTimeout('stopWmv', '150');
    }
}

function PlayVod(ch)
{
   window.scrollTo(0,0);
   var urlToLoad =  "../Lib/GetLink.aspx?id=" + encodeURIComponent(ch) + "&type=ar";
   wmvPlayer.sendEvent('LOAD',urlToLoad);
   wmvPlayer.sendEvent('PLAY');
}

function playerReady(obj) {
	wmvPlayer = document.getElementById(obj['id']);
	addListeners();
};

function addListeners() {
    if (wmvPlayer) { 
		wmvPlayer.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}


function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
		if (wmvReq){
			wmvReq = false;
		    PlayWmv();
		    //wmvPlayer.sendEvent('PLAY');
		}
	}
}


function CallCLiveAPI(fetchUrl, currentCh, mediaType)
{
    xmlDoc = new XMLHttpRequest();
    currentCh = encodeURIComponent(currentCh);
    if (mediaType == 'ar') xmlDoc.open( "GET", fetchUrl + "?id=" + currentCh + '&type=ar', true );
    else xmlDoc.open( "GET", fetchUrl + "?id=" + currentCh, true );
    xmlDoc.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
    xmlDoc.onreadystatechange = playChannel;
    xmlDoc.send( null );
}
  
function playChannel()
{
        if ( xmlDoc.readyState != 4 ) return ;
        
        if (xmlDoc.responseText != '')
        {
            urlToLoad = xmlDoc.responseText;
            //PlayWmv();
            
        }
        else
        {
            alert("Unable to retreive video content. Please contact support.");
        }
}
