/******************************************************************************

    Component for asynchronously tracking downloads.

    Classes:
        DownloadTracker

    $Revision: 6$
    $Date: 2007-09-05 10:41:34 AM$
    $NoKeywords$

******************************************************************************/

function DownloadTracker( )
{
    this.m_yahooRequestTimer = null;
    this.m_timerId = null;

    var self = this;

    this.m_httpClient  = new HttpClient( );
    this.m_httpClient.onError = function( )
    {
        self.HandleError( );
    };
    this.m_httpClient.onSuccess = function( )
    {
        self.HandleSuccess( );
    };

    this.TrackDownload( );
}
DownloadTracker.prototype =
{
    URL_PATH          : '/widgets/downloadtracker.aspx',
    RETRY_INTERVAL_MS : 20000,

    DOWNLOAD_TRACKER_KEY_PREFIX : 'DOWNLOAD_TRACKED_',

    /******************************************************************************
    PRIVATE METHODS
    ******************************************************************************/

    /******************************************************************************
      TrackDownload

        Issues a download-tracking request if necessary.

    ******************************************************************************/
    TrackDownload : function( )
    {
        try
        {
            var downloadTracked = VM_GetPreference( this.DOWNLOAD_TRACKER_KEY_PREFIX + VM_WIDGET_VERSION );

            if ( ! downloadTracked )
            {
                var url = this.GetURL( );
                this.m_httpClient.sendRequest( HTTP_VERB_GET,
                                               url,
                                               url,
                                               "text/html",
                                               '' ); // requestData
            }
        }
        catch ( e )
        {
            EX_Log( "DownloadTracker::TrackDownload( )\n" + e.message );
        }
    },

    /******************************************************************************
      HandleSuccess

        Records a successful download tracking

    ******************************************************************************/
    HandleSuccess : function( )
    {
        try
        {
            VM_SetPreference( this.DOWNLOAD_TRACKER_KEY_PREFIX + VM_WIDGET_VERSION, 1 );
        }
        catch ( e )
        {
            EX_Log( "DownloadTracker::HandleSuccess( )" );
        }
    },

    /******************************************************************************
      HandleError

        Handler for errors in tracking the download. Retries.

    ******************************************************************************/
    HandleError : function( )
    {
        try
        {
            this.Retry( );
        }
        catch ( e )
        {
            EX_Log( "DownloadTracker::HandleError( )\n" + e.message );
        }
    },

    /******************************************************************************
      Retry

        Sets a timeout for retrying the download-track request.

    ******************************************************************************/
    Retry : function( )
    {
        try
        {
            var self = this;

            function HandleTimeout( )
            {
                if ( typeof VM_WIDGET_SOURCE != 'undefined' &&
                     ( VM_WIDGET_SOURCE == "Y" || VM_WIDGET_SOURCE == "SY" || VM_WIDGET_SOURCE == "VY" ) )
                {
                    self.m_yahooRequestTimer.ticking      = false;
                    self.m_yahooRequestTimer.onTimerFired = "";
                    self.m_yahooRequestTimer              = null;
                }

                self.TrackDownload( );
            }

            if ( typeof VM_WIDGET_SOURCE != 'undefined' &&
                 ( VM_WIDGET_SOURCE == "Y" || VM_WIDGET_SOURCE == "SY" || VM_WIDGET_SOURCE == "VY" ) )
            {
                this.m_yahooRequestTimer = new Timer( );
                this.m_yahooRequestTimer.interval     = this.RETRY_INTERVAL_MS;
                this.m_yahooRequestTimer.onTimerFired = HandleTimeout;
                this.m_yahooRequestTimer.ticking      = true;
            }
            else
            {
                this.m_timerId = setTimeout( HandleTimeout, this.RETRY_INTERVAL_MS );
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "DownloadTracker::Retry( )" );
        }
    },

    /******************************************************************************
      GetURL

        Creates the URL to use in a download track request.

    ******************************************************************************/
    GetURL : function( )
    {
        try
        {
            var queryParameterHash =
            {
                src : VM_WIDGET_SOURCE,
                ver : VM_WIDGET_VERSION,
                os  : VM_WIDGET_OS_VERSION
            };

            var uString = VM_GetPreference( PREF_KEY_U_STRING );

            if ( uString )
            {
                queryParameterHash[ "u" ] = uString;
            }

            var queryParameters = '';

            var param = null;
            for ( param in queryParameterHash )
            {
                queryParameters += '&' + param + '=' + queryParameterHash[ param ];
            }

            // use the web service URL to thwart same-domain restrictions.
            var url = VM_URL.GetInternalWebPageURLBase( ) + this.URL_PATH;

            if ( queryParameters.length != 0 )
            {
                // append the parameters to the URL, skipping the first
                // character of queryParameters since it's an ampersand.
                url += '?' + queryParameters.substr( 1 );
            }

            return url;
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "DownloadTracker::GetURL( )" );
        }
    }
};
