//
// track user time spent on each tv show (program guide); rely heavily on jw flash object 
// events triggered and user accepting cookies;
// save tracking info only in cookies then send all cookies to server when user leaves the page
//
function VIET_vnl_AgentTracking() {

    //
    // severe assumption: there is only one tv show is playing at one time
    // 
    this.iCurrGuideId = 0;
    this.dtCurrStartTime = null;
    this.dtStopWatchMark = new Date();
    this.iCurrDuration = 0;                 // all durations are in seconds
    this.bIsStopWatchRunning = false;

    this.iAgentId = $_COOKIES('ag');

    this.initRemainDuration();
}
VIET_vnl_AgentTracking.prototype = {

    init: function() {
        oUser.handle_SubscriptionRequired();
    
        // start recording last cookies 2 minutes into the program
        var me = this;
        setTimeout(function() { me.recordServer(); }, 60*1000);
    },
    
    //
    // remaining seconds to subscribe
    //
    initRemainDuration: function() {
        this.iRemainDuration = $_COOKIES('cs')==null ? iSubsTh : Math.round((iSubsTh - $_COOKIES('cs'))/1.5);
    },
    
    needSubscribe: function() {
        return bEnSubs && (this.iRemainDuration<=0 && this.isAgentIdAvail());
    },
    
    isAgentIdAvail: function() {
        return (this.iAgentId!=null && this.iAgentId!='');
    },
    
    //
    // this function is called each time the video player changes clips either by moving to next item
    // in playlist or user clicking on next/previous button
    //
    record: function(guideId) {
    
        // only do it when clientId available
        if( !this.isAgentIdAvail() ) return;

        // reset time and record duration
        this.reset();

        // recalculate remaining seconds - trigger event when it is time to subscribe
        this.iRemainDuration = this.iRemainDuration - this.iCurrDuration;
        if( this.iRemainDuration<=0 )
            oUser.handle_SubscriptionRequired();

        if( guideId==null ) { // force reset and record current guide
            if( this.iCurrGuideId>0 && this.iAgentId>0 )
                setCookie('al' + this.iCurrGuideId + '_' + Math.round(this.dtCurrStartTime.valueOf()/1000), this.iCurrDuration, 30);
        }
        else if( guideId!=this.iCurrGuideId ) { // change guide

            //
            // record last stats in cookies
            //
            if( this.iCurrGuideId>0 && this.iAgentId>0 ) {
                setCookie('al' + guideId + '_' + Math.round(this.dtCurrStartTime.valueOf()/1000), this.iCurrDuration, 30);
            }

            //
            // and reset time and guideId for the next guide
            //
            this.iCurrGuideId = guideId;
            this.dtCurrStartTime = getServerTime();
            this.dtStopWatchMark = new Date();
            this.iCurrDuration = 0;

        }

    },

    //
    // record elapsed time (only if stop watch is running) for the current guide and reset stop 
    // watch
    //
    reset: function() {

        if( this.bIsStopWatchRunning==true ) {
            var dtNow = new Date();
            var iElapsedTime = Math.round( (dtNow.valueOf() - this.dtStopWatchMark.valueOf())/1000 );
            this.iCurrDuration += iElapsedTime;
        }

        this.dtStopWatchMark = new Date();
    },

    //
    // stop playing video: reset stop watch, record elapsed time, and let others know that the
    // stop watch is not running
    //
    stop: function() {
        this.reset();
        this.bIsStopWatchRunning = false;
    },

    //
    // start playing video: reset stop watch, record elapsed time, and let others know that now
    // the stop watch is running
    //
    play: function() {
        this.reset();
        this.bIsStopWatchRunning = true;
    },
    
    recordServer: function() {

	// fixing database overload 01/2010 - don't do this anymore
	return;

        var me = this;
        YAHOO.util.Connect.asyncRequest('GET', '/includes/agent_cookies.inc.php', {success:function(o){
                me.iAgentId = $_COOKIES('ag');
                me.initRemainDuration();
                //
                // remove all agent-log cookies
                //
                var arCookies = getCookies();
                for(var k in arCookies) {
                    if( k.indexOf('al')==0 )
                        setCookie(k, '', -1);
                }
            }, failure:function(){}});
    },

    //
    // this function is called when user leaves the page
    //
    recordLast: function() {

        this.record();

        //
        // send out all cookies to server to have log data immediately; there won't be any guarantee if
        // the request get to the server or the server having enough time for server to response, that is
        // why in next visit, all cookies will be re-examined
        //
        //YAHOO.util.Connect.asyncRequest('GET', '/includes/agent_cookies.inc.php', {success:function(o){}, failure:function(){}});
    }
}
var oAgent = new VIET_vnl_AgentTracking();
