
var g_dWmpControlHeight = 64;
var g_dWmpAspectRatio = (4/3);

// wrapper for the main video player object (upper left corner)
function VideoPlayer(el, id, nocontrols)
{
    var m_el = el;
    var m_contentEl = CreateChildElement( m_el, "div", "wmpVideoPlayerContent" );
    var m_pVideo = null;
    var m_pInfo = null;
    var m_dVideoHeight = 0;
    
    var m_sliderChangedCallback = null;
    var m_playstateChangedCallback = null;
    
    var m_pIssueStatusOnPlaying = null;
    
    if( !id )
        id = "VideoPlayer";
        
    var width = m_contentEl.offsetWidth;
    var height = width / g_dWmpAspectRatio + g_dWmpControlHeight;
    
    var m_pVideo = CreateChildElement( m_contentEl, "object", "", id );
    if (m_pVideo)
    {
        m_pVideo.width = width;
        m_pVideo.height = height;
        m_pVideo.classid= "clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6"; 
        m_pVideo.stretchToFit = "true";
        m_pVideo.invokeURLs = "false";
        
        // add in passed params
        if( nocontrols )
        {
            m_pVideo.uiMode = "none";
        }
        
        function OnPositionChange(oldPosition, newPosition)
        {
            if( m_sliderChangedCallback != null )
            {
                m_sliderChangedCallback( newPosition, true );
            }
        }
        
        function OnPlayStateChange(newState)
        {
            var playState = TranslatePlayState( newState );
            if( playState != null && m_playstateChangedCallback != null )
            {
                m_playstateChangedCallback( playState );
            }
            
            if( m_pIssueStatusOnPlaying != null && newState == "Playing" )
            {
                this.UpdateStatus( m_pIssueStatusOnPlaying, true );
                m_pIssueStatusOnPlaying = null;
            }
        }        
        
        // set up our event handlers
        m_pVideo.attachEvent("PositionChange", OnPositionChange);
        m_pVideo.attachEvent("PlaystateChange", OnPlayStateChange);
    }
	            
	// this is needed by TabViewer for layout
	this.AspectRatio = g_dWmpAspectRatio;
    
    // info is a stream definition - create in DeliveryInfo in Viewer.js
    this.Initialize = function(info, sliderChangedCallback, playStateChangedCallback)
    {  
        m_pInfo = info;
        m_pVideo.URL = info.Url;
        
        m_sliderChangedCallback = sliderChangedCallback;
        m_playstateChangedCallback = playStateChangedCallback;
    }
    
    this.GetVideoPlayer = function()
    {
        return m_pVideo;
    }
    
    this.SetVideoPosition = function(dTime)
    {
        m_pVideo.controls.currentPosition = dTime;
    }
    
    this.GetVideoPosition = function()
    {
        return m_pVideo.controls.currentPosition;
    }
    
    this.GetPlayState = function()
    {
        return TranslatePlayState( m_pVideo.playState );
    }
    
    function TranslatePlayState( playState )
    {
        switch( playState )
        {
            case 1:     return "Stopped";
            case 2:     return "Paused";
            case 3:     return "Playing";
            case 6:     return "Buffering";
            case 7:     return "Buffering";
            default:    return null;
        }
    }
    
    this.SetPlayState = function( playState )
    {
        if( playState == this.GetPlayState() )
            return;
            
        if( playState == "Playing" )
            m_pVideo.controls.play();
        else if ( playState == "Paused" )
            m_pVideo.controls.pause();
        else if ( playState == "Stopped" )
            m_pVideo.controls.stop();
    }
        
    // this is only in the interface for the wmp video player
    this.OnResize = function(maxHeight, maxWidth)
    {
        var desiredWidth = maxWidth == undefined ? m_contentEl.clientWidth : maxWidth;
        
        // change our height based on our width
        this.Height = desiredWidth / g_dWmpAspectRatio + g_dWmpControlHeight;        
        if( this.Height > maxHeight )
        {
            this.Height = maxHeight;
        }
        this.Width = (this.Height - g_dWmpControlHeight) * g_dWmpAspectRatio;
        
        if( m_pInfo != null && m_pInfo.Tag == "AUDIO" )
        {
            this.Height = g_dWmpControlHeight;
        }
        
        m_el.style.height = this.Height + "px";
        m_contentEl.style.height = this.Height + "px";
                
        if( !m_pVideo )
            return;
                
        m_pVideo.style.width = this.Width + "px";
        m_pVideo.style.height = this.Height + "px"; 
        m_dVideoHeight = this.Height;
    }
    
    this.SetSize = function(width, height)
    {
        m_el.style.width = width + "px";
        m_el.style.height = height + "px";
        m_contentEl.style.height = height + "px";
                
        if( !m_pVideo )
            return;
                
        m_pVideo.style.width = width + "px";
        m_pVideo.style.height = height + "px"; 
        m_dVideoHeight = height;
    }
    
    this.UpdateStatus = function(pEvent, bUserInitiated)
    {
        if( !m_pVideo )
            return;
              
        if( this.GetPlayState() != "Playing" && bUserInitiated == true )
        {
            m_pIssueStatusOnPlaying = pEvent;
        }
        
        // synchronize positions - if we are more than the threshold away from the
        // archival video then synchronize
        var fTargetObjPos;
        if( m_pInfo.Segments )
        {
            var iTargetSeg = 0;
            while( iTargetSeg < m_pInfo.Segments.length && m_pInfo.Segments[iTargetSeg].RelativeStart <= pEvent.Time )
            {
                iTargetSeg++;
            }
            iTargetSeg--;
            var pTargetSeg = m_pInfo.Segments[iTargetSeg];
            fTargetObjPos = Math.max( pTargetSeg.Offset + pEvent.Time - pTargetSeg.RelativeStart, 0 ); 
        }
        else
        {
            // our target with respect to the archival video
            fTargetObjPos = Math.max( pEvent.Time - m_pInfo.RelativeStart, 0 );    
        }
            
        // only update the position if we are way off or this update was initiated by a user action   
        var fCurPos = this.GetVideoPosition();
        if( bUserInitiated || (Math.abs(fCurPos - fTargetObjPos) > g_fOffsetThreshold) )
        {
            this.SetVideoPosition( fTargetObjPos );
        }
    }
    
    this.SetVisible = function(bVisible)
    {
        SetVisible(m_el, bVisible);
    }  
    
    this.GetHeight = function()
    {
        return m_dVideoHeight;
    }

    this.GetWidth = function()
    {
        return this.Width;
    }
}

