// File: /include/js/mediaPlayer.js
// Desc: manipulates a single instance of the media player
// $Revision: 18$
// $Date: 5/18/2007 12:27:36 AM$
// $Author: Donnie Tognazzini$
// $NoKeywords$

/*
VM_MediaPlayer
manages the Flash Control
*/
function VM_MediaPlayer( callViewDOMId, callViewJObject, messagePrimaryFetch, messageSecondaryFetch )
{
    try
    {
        this.m_messagePrimaryFetch      = messagePrimaryFetch;
        this.m_messageSecondaryFetch    = messageSecondaryFetch;
        this.m_mediaServerUsed          = this.PRIMARY_SERVER;

        // Wraps the flash object
        function FlashPlayer( )
        {
            this.STATUS_PLAYING     = "playing";
            this.STATUS_NOT_PLAYING = "not_playing";

            this.m_player = document.getElementById( "player_" + callViewDOMId );

            // set the player to object in the embed tag if
            // the browser is not safari
            if ( ! /webkit/.test( navigator.userAgent.toLowerCase( ) ) )
            {
                // set the player only if the embed tag is locatable
                var embedList = this.m_player.getElementsByTagName( "embed" );
                if ( embedList.length != 0 )
                {
                    this.m_player = embedList.item( 0 );
                }
            }

            var mySelf = this;

            function SetVariable( command, args )
            {
                // This is required on FF. Why? Who the fuck knows...
                setTimeout( function( )
                {
                    try
                    {
                        mySelf.m_player.SetVariable( command, args );
                    }
                    catch ( e )
                    {
                        // oh well.
                    }
                },
                10 );
            }

            this.Play = function( file )
            {
                SetVariable( "_root.browserCommand", "play " + file );
            };

            this.Stop = function( )
            {
                SetVariable( "_root.browserCommand", "stop" );
            };

            this.IsActive = function( )
            {
                var status = this.m_player.GetVariable( "_root.soundStatus" );
                return status == this.STATUS_PLAYING;
            };
        }

        this.m_flashPlayer = new FlashPlayer( );

        // Wraps the call action button
        function PlayControl( )
        {
            this.m_actionButtonImage = $( "img.actionButton", callViewJObject )[ 0 ];

            this.ShowPlay = function( )
            {
                this.m_actionButtonImage.src       = VM_RESOURCE_PLAY_IMAGE;
                this.m_actionButtonImage.useMap    = "#mapPlay_" + callViewDOMId;
            };

            this.ShowStop = function( )
            {
                this.m_actionButtonImage.src       = VM_RESOURCE_STOP_IMAGE;
                this.m_actionButtonImage.useMap    = "#mapStop_" + callViewDOMId;
            };
        }

        this.m_playControl = new PlayControl( );

        this.m_tryingSecondary  = false;
        this.m_playIsPending    = false;
    }
    catch ( e )
    {
        EX_ASSERT_NO_EXCEPTIONS( e, "VM_MediaPlayer::VM_MediaPlayer( )" );
    }
}

VM_MediaPlayer.prototype =
{
    PRIMARY_SERVER   : 0,
    SECONDARY_SERVER : 1,

    onFlashError: function( )
    {
        try
        {
            // try the secondary if we tried the primary
            if ( this.m_mediaServerUsed == this.PRIMARY_SERVER )
            {
                this.m_mediaServerUsed = this.SECONDARY_SERVER;

                this.m_flashPlayer.Play( this.m_messageSecondaryFetch );
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_MediaPlayer::onFlashError( )" );
        }
    },

    onFlashComplete: function( )
    {
        this.m_playControl.ShowPlay( );
    },

    onFlashSeekStart : function( )
    {
        this.m_playControl.ShowPlay( );
    },

    onFlashSeekComplete : function( )
    {
        this.m_playControl.ShowStop( );
    },

    play: function( )
    {
        try
        {
            this.m_mediaServerUsed = this.PRIMARY_SERVER;

            this.m_flashPlayer.Play( this.m_messagePrimaryFetch );

            this.m_playControl.ShowStop( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_MediaPlayer::play( )" );
        }
    },

    stop: function( )
    {
        try
        {
            if ( this.m_flashPlayer.IsActive( ) )
            {
                this.m_flashPlayer.Stop( );
            }

            this.m_playControl.ShowPlay( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_MediaPlayer::stop( )" );
        }
    },

    isInUse: function( )
    {
        return this.m_flashPlayer.IsActive( );
    }
};

