

/*
 * WindowsMediaController: controls an embedded Windows Media Player video player
 */
function WindowsMediaController()
{
	this.Format = Format.WindowsMediaVideoDRM;
	
	this.PollInterval = 1; // in seconds
	
	this.CurrentVideoTimeWatched = 0;
	
	this.LoadHtml( );
	
	WindowsMediaController.__instance = this;
	
}



WindowsMediaController.GetInstance = function()
{
	if( ! WindowsMediaController.__instance )
	{
		WindowsMediaController.__instance = new WindowsMediaController();
	}
	
	return WindowsMediaController.__instance;
}


WindowsMediaController.WaitForEnd = function()
{
    var _WindowsMediaController = WindowsMediaController.GetInstance();


    _WindowsMediaController.CurrentVideoTimeWatched += _WindowsMediaController.PollInterval;
    
    if( _WindowsMediaController.IsScriptable() )
    {
        

	    try
	    {
            // Log( "WaitForEnd.PlayState " + _WindowsMediaController.GetPlayState() );
    	
		    if( 
			    _WindowsMediaController.GetPlayState() == 10 
			    || _WindowsMediaController.GetPlayState() == 0
			    || _WindowsMediaController.GetPlayState() == 1
			    )
		    {	
			    clearInterval( WindowsMediaController.IntervalId );
			    WindowsMediaController.IntervalId = null;
			    Player.OnClipEnded();
			    //Player.EndClip();
		    }
		    else
		    {
		        var Position = _WindowsMediaController.GetPosition();
		    
		        if( WindowsMediaController.Mod( Position, 10 ) )
		        {
		            Player.OnClipKeyPoint( Position, -1 );
		        }		    
		        
		    }
	    }
	    catch( Error )
	    {
		    clearInterval( WindowsMediaController.IntervalId );
		    WindowsMediaController.IntervalId = null;
	    }
    }
    else
    {
        if( WindowsMediaController.Mod( _WindowsMediaController.CurrentVideoTimeWatched, 10 ) )
        {
            Player.OnClipKeyPoint( _WindowsMediaController.CurrentVideoTimeWatched, -1 );
        }
    }
    
    
}

WindowsMediaController.Mod = function( a, b)
{
    return ( Math.round( a ) / b ) == Math.round( Math.round( a ) / b );
}


WindowsMediaController.prototype.GetPlayState = function()
{
	try
	{
		return this.GetPlayerElement().playState; // 3 playing, 1 stopped, 2 paused
	}
	catch( Error )
	{
		return 1;
	}
}

WindowsMediaController.prototype.GetPosition = function()
{
	try
	{
		return this.GetPlayerElement().controls.currentPosition;
	}
	catch( Error )
	{
		return -1;
	}
}

WindowsMediaController.prototype.IsScriptable = function()
{
	if( this.GetPlayerElement() )
	{
		return ( this.GetPlayerElement().playState != null && this.GetPlayerElement().playState != undefined );
	}
	
	return false;
}

WindowsMediaController.prototype.GetPlayerElement = function()
{
	return document.getElementById("WindowsMediaControllerHtmlObject");
}

WindowsMediaController.prototype.LoadHtml = function( pVideoUrl, pIsAd )
{
	var videoPlayerHTML = "";
		
	var ShowControls = 1;
	
	if( pVideoUrl )
	{
		if( pIsAd )
		{
			ShowControls = 0;
		}
	
		videoPlayerHTML = ''
		+ '<object id="WindowsMediaControllerHtmlObject" onClick="return false;" '
		+ ' width="416" height="356" '
		+ ' classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" '
		+ ' standby="Loading..." '
		+ ' type="application/x-oleobject"> '
		+ '<param name="Url" value="' + pVideoUrl + '"> '
		+ '<param name="autoStart" value="true"> '
		+ '<param name="enableContextMenu" value="true"> '
		+ '<param name="windowlessVideo" value="false"> '
		+ '<param name="stretchToFit" value="false"> '
		+ '<param name="ShowControls" value="' + ShowControls + '"> '
		+ '<param name="ShowStatusBar" value="false"> '
		+ '<param name="ShowDisplay" value="false"> '
		+ '  <embed id="WindowsMediaControllerHtmlObject" name="WindowsMediaControllerHtmlObject" type="application/x-mplayer2" '
		+ '  pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" '
		+ '  width="416" '
		+ '  height="356" '  
		+ '  src="' + pVideoUrl + '" '
		+ '  ShowControls="' + ShowControls + '" '
		+ '  showstatusbar="0" '
		+ '  AutoSize="true" '
		+ '  EnableContextMenu="1" '
		+ '  DisplaySize="0" '
		+ '  AutoStart="true" '
		+ '  stretchToFit="true" '
		+ '  > '
		+ '  </embed> '
		+ '</object> ';
	}
	else
	{
		videoPlayerHTML = ''
		+ '<object id="WindowsMediaControllerHtmlObject" onClick="return false;" '
		+ ' width="416" height="356" '
		+ ' classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" '
		+ ' standby="Loading..." '
		+ ' type="application/x-oleobject"> '
		+ '<param name="autoStart" value="true"> '
		+ '<param name="enableContextMenu" value="true"> '
		+ '<param name="windowlessVideo" value="false"> '
		+ '<param name="stretchToFit" value="false"> '
		+ '<param name="ShowControls" value="true"> '
		+ '<param name="ShowStatusBar" value="false"> '
		+ '<param name="ShowDisplay" value="false"> '
		+ '  <embed id="WindowsMediaControllerHtmlObject" name="WindowsMediaControllerHtmlObject" type="application/x-mplayer2" '
		+ '  pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" '
		+ '  width="416" '
		+ '  height="356" '  
		+ '  ShowControls="' + ShowControls + '" '
		+ '  showstatusbar="0" '
		+ '  AutoSize="true" '
		+ '  EnableContextMenu="1" '
		+ '  DisplaySize="0" '
		+ '  AutoStart="true" '
		+ '  stretchToFit="true" '
		+ '  > '
		+ '  </embed> '
		+ '</object>';
	}
	
	Interface.GetInstance().PlayerViewer.innerHTML = "";
	Interface.GetInstance().PlayerViewer.innerHTML = videoPlayerHTML;
}

WindowsMediaController.prototype.Play = function( pVideo )
{
    if( pVideo.Url )
    {
    
	    Log( "Passing in " + pVideo.Url + " to WindowsMediaController" );
    	
	    if( this.IsScriptable() )
	    {
		    this.GetPlayerElement().Url = pVideo.Url;
    		
		    if( pVideo.IsAd )
		    {
			    this.GetPlayerElement().ShowControls = false;
		    }
		    else if( ! this.GetPlayerElement().ShowControls )
		    {
			    this.GetPlayerElement().ShowControls = true;
		    }
    		
		    if( WindowsMediaController.IntervalId != null )
		    {
			    clearInterval( WindowsMediaController.IntervalId );
		    }
		    WindowsMediaController.IntervalId = setInterval( "WindowsMediaController.WaitForEnd()", this.PollInterval * 1000 );
	    }
	    else
	    {
		    // Stop the ad after it's duration is up.
		    if( pVideo.IsAd )
		    {
			    this.LoadHtml( pVideo.Url, pVideo.IsAd );
    			
			    var AdVideoDuration = 15;
			    if( pVideo.Duration > 0 )
			    {
				    AdVideoDuration = eval( pVideo.Duration );
			    }
    			
			    // Add 2 seconds to the ad runtime for buffering
			    AdVideoDuration += 2;
    		
    		    setTimeout( "Player.OnClipEnded(" + AdVideoDuration + ")", AdVideoDuration * 1000 );
		    }
		    else
		    {
			    Log( "Loading Not Scriptable Playlist" );
			    
			    this.LoadHtml( "http://watch.ctv.ca/news/NotScriptablePlaylist.aspx?videourl=" + escape( pVideo.Url ), pVideo.IsAd );
			    
			    if( WindowsMediaController.IntervalId != null )
		        {
			        clearInterval( WindowsMediaController.IntervalId );
		        }
		        WindowsMediaController.IntervalId = setInterval( "WindowsMediaController.WaitForEnd()", this.PollInterval * 1000 );
		    }
	    }
	}
	else
	{
	    throw "Video " + pVideo.ClipId + " passed to WindowsMediaController.prototype.Play without a Video.Url";
	}
	
	
}

WindowsMediaController.OnAdClipEnded = function( pDuration )
{
    Player.OnClipKeyPoint( pDuration, pDuration );
    Player.OnClipEnded();
}

