ctv.ca.TrackingController = function() {
    this.hbxAccounts = new Hashtable(); 
    this.hbxAccounts.put('hbx_account_2', "DM5610304FME;DM561030KMRC"); //discovery
    this.hbxAccounts.put('hbx_account_3', "DM551230O5NE;DM561030KMRC");  //primetime
    this.hbxAccounts.put('hbx_account_4', "DM551230O5NE;DM561030KMRC");  //etalk
    this.hbxAccounts.put('hbx_account_7', "DM561030INRA;DM561030KMRC");  //mtv
    this.hbxAccounts.put('hbx_account_8', "DM561030BMAN;DM561030KMRC");  //comedy
    this.hbxAccounts.put('hbx_account_9', "DM56032977WE;DM561030KMRC");  //tsn
    this.hbxAccounts.put('hbx_account_20', "DM551230O5NE;DM561030KMRC");  //idol
    this.hbxAccounts.put('hbx_account_22', "DM561030HNDE;DM561030KMRC");  //bnn
    this.hbxAccounts.put('hbx_account_5', "DM551230O5NE;DM561030KMRC");  //ctv evevt
    this.hbxAccounts.put('hbx_account_28', "DM551230O5NE;DM561030KMRC");  //achannel evevt
    
	this.hbxPopGroups = new Hashtable();
    this.hbxPopGroups.put('hbx_popgroup_2', "1;1");
    this.hbxPopGroups.put('hbx_popgroup_3', "3;1");
    this.hbxPopGroups.put('hbx_popgroup_4', "2;1");
    this.hbxPopGroups.put('hbx_popgroup_7', "1;1");
    this.hbxPopGroups.put('hbx_popgroup_8', "1;1");
    this.hbxPopGroups.put('hbx_popgroup_9', "1;1");
    this.hbxPopGroups.put('hbx_popgroup_20',"4;1");
    this.hbxPopGroups.put('hbx_popgroup_22',"1;1");
    this.hbxPopGroups.put('hbx_popgroup_5', "3;1");
 
    this.hbxCats = new Hashtable();
    this.hbxCats.put('hbx_cat_2', "/discovery/discovery/bbVideo"); //discovery
    this.hbxCats.put('hbx_cat_3', "/ctv/ctventertainment/bbVideo/primetime");  //primetime
    this.hbxCats.put('hbx_cat_4', "/ctv/ctventertainment/bbVideo/etalk");  //etalk
    this.hbxCats.put('hbx_cat_7', "/mtv/bbVideo");  //mtv
    this.hbxCats.put('hbx_cat_8', "/comedy/bbVideo");  //comedy
    this.hbxCats.put('hbx_cat_9', "/tsn/bbVideo/wj08");  //tsn
    this.hbxCats.put('hbx_cat_20', "/ctv/ctvshows/idol/bbVideo");  //idol
    this.hbxCats.put('hbx_cat_22', "/bnn/bbVideo");  //bnn 
    this.hbxCats.put('hbx_cat_3', "/ctv/ctventertainment/bbVideo/dodge");  //ctv event
    this.hbxCats.put('hbx_cat_28', "/ctv/achannel/bbVideo");  //achannel event
    this.hbxCats.put('hbx_cat_5', "/ctv/ctventertainment/oscars/");  //CTVEvents event
    
    this.currentPlayListIndex = -1;
    this.currentMedia = null;
    this.trackingClip = false;
    this.mediaLength = 0;
    
    this.lastPos = 0;	
    this.duration = 0;
    this.dragTime = 0;
    this.dragThresh = 10;
	this.lastTrackPos = 0;
   
    this.mark25 = false;
    this.mark50 = false;
    this.mark75 = false;
    this.mark100 = false;
    this.trackDuration = false;
    this.blocking = false;
}

ctv.ca.TrackingController.prototype = {
    replaceSpecials : function(oString)
    {
        var nString = unescape(oString);
        
        nString = nString.replace(/[^\S ]+/g, '_');
        nString = nString.replace(/&lt;/g, '');
        nString = nString.replace(/&gt;/g, '');
        nString = nString.replace(/&nbsp;/g, '');
        nString = nString.replace(/&quot;/g, '');
        nString = nString.replace(/&amp;/g, '');
        nString = nString.replace(/&#?\w+;/g, '');
        nString = nString.replace(/\t/g, '');
        nString = nString.replace(/'/g, '');
        nString = nString.replace(/"/g, '');
        nString = nString.replace(/&/g, '');
        nString = nString.replace(/|/g, '');
        nString = nString.replace(/#/g, '');
        nString = nString.replace(/$/g, '');
        nString = nString.replace(/%/g, '');
        nString = nString.replace(/^/g, '');
        nString = nString.replace(/\*/g, '');
        nString = nString.replace(/:/g, '');
        nString = nString.replace(/!/g, '');
        nString = nString.replace(/</g, '');
        nString = nString.replace(/>/g, '');
        nString = nString.replace(/~/g, '');
        nString = nString.replace(/;/g, '');
        nString = nString.replace(/\W+/gi,"_");
    	nString = nString.replace(/\s+/gi,"_");
    	nString = nString.replace(/_+/g,"_");


        return nString.toLowerCase();
    },
    
    hbxTrackMedia : function (hbxFileName, eventName, hbxCurrentPos, hbxEndPos, playerName, playerVersion)
	{
        //set up account, etc...
        var acct = this.hbxAccounts.get('hbx_account_' + ctvConfigController.orgId);    
        var contentCat = this.hbxCats.get('hbx_cat_' + ctvConfigController.orgId);
        var mlc = contentCat + ";" + "/ctvgm" + contentCat; 
       
        var filePath = hbxFileName;
        
        //debug("hbx: " + hbx.acct + " ** " + hbx.mlc + " ** " + fileName);
        
        var pos = Math.round(hbxCurrentPos * 1000) - 1;
			if (pos < 0) pos = 0;
							
		//_hbset('hb',acct);
        _hbSet('vcon',mlc); 
        _hbSet('m.f',contentCat + filePath);
		_hbSet('m.cp',pos);
		_hbSet('m.ep',Math.round(hbxEndPos * 1000));
		_hbSet('m.s',eventName);
		_hbSet('m.cl',playerName);
		_hbSet('m.cv',playerVersion);
		_hbSet('m.tt','e');
		_hbSend();
		
		//reset the account and cat back to page level
        _hbSet('vcon',hbx.mlc); 
	},
    
    getContentPath : function() {
        if (this.currentMedia == null)
            return "";
            
        var path = [''];
        path.push("s" + this.currentMedia.parentClipShowTypeId + "_" + this.currentMedia.parentClipShowTypeName);
        path.push("sn" + this.currentMedia.parentClipSeason);
        path.push("ep" + this.currentMedia.parentClipEpisode);
        path.push(this.currentMedia.parentClipCat);
        path.push('c' + this.currentMedia.vid);
        path.push(this.currentMedia.parentClipTitle);
        if (typeof (g_gateway) != 'undefined') path.push(g_gateway);
        
        return (path.join('/'));
    },
    
    trackProgress : function(pos) {
			
		if (this.currentMedia == null) return;
        if (this.currentMedia.isAd) return;

		if (this.mediaLength != 0) //not live
		{
			if (this.currentMedia.vid <= 0) return;  //skip stings, bumpers
			if (pos < 5) return; //only track 3s into the clip;
		}
        if (this.blocking) return;
        
        this.blocking = true;
        
        if (!this.trackingClip) {
            this.trackingClip = true;
            this.hbxTrackMedia(this.getContentPath(), 'play', 0, this.mediaLength, 'ctvBBP', '2.0');
            //debug("tracking new clip. - " + this.getContentPath());
        }

        var gap = pos - this.lastPos;
        if (gap > this.dragThresh)
        {
            this.dragTime += gap;
        }
        this.duration += Math.abs(gap);
        this.lastPos = pos;
		
        var doTrack = false;
		if (this.mediaLength == 0)
		{
		   //live
		   var delta = this.duration - this.lastTrackPos;
		   if (delta >= 20)
		   {
				doTrack = true;
				this.lastTrackPos = this.duration;
		   }
		}
		else
		{
	        var pcnt = (this.duration - this.dragTime)/this.mediaLength;

	        if ( pcnt >= 0.25 && !this.mark25)
	        {
	            this.mark25 = true;
	            doTrack = true;
	            //debug("tracking 25 pct.");
	        }
	        else if ( pcnt >= 0.5 && !this.mark50)
	        {
	            this.mark50 = true;
	            doTrack = true;
	            //debug("tracking 50 pct.");
	        }
	        else if ( pcnt >= 0.75 && !this.mark75)
	        {
	            this.mark75 = true;
	            doTrack = true;
	            //debug("tracking 75 pct.");
	        }
	        else if ( pcnt >= 0.95 && !this.mark100)
	        {
	            this.mark100 = true;
	            doTrack = true;
	            //debug("tracking 100 pct.");
	        }
		}
        
        if (doTrack)
            this.hbxTrackMedia(this.getContentPath(), 'playp', this.duration - this.dragTime, this.mediaLength, 'ctvBBP', '2.0');
								
        
        this.blocking = false;
    },
    
    trackEndMedia : function () {
        if (this.trackingClip) {
            this.hbxTrackMedia(this.getContentPath(), 'stop', this.duration - this.dragTime, this.mediaLength, 'ctvBBP', '2.0');
            this.trackingClip = false;
            //debug("tracking end clip. - " + this.getContentPath());
            this.currentMedia = null;
        }
    },
    
    onVideoWindowStateChange : function(sender, s) {
        if (s == 8) { //end
            this.trackEndMedia();
        }
    
    },
    
    onVideoWindowPositionChange : function(sender, pos) {
            this.trackProgress(pos);
    },
    
    onVideoWindowMediaLength : function(sender, length) {
        if (length > 30) ////skip stin, bumper
            this.mediaLength = length;
    },
    
    onNewMedia : function(sender, media) {
        this.currentMedia = media;
        this.mediaLength = 0;
        this.lastPos = 0;	
        this.duration = 0;
        this.dragTime = 0;
   
        this.mark25 = false;
        this.mark50 = false;
        this.mark75 = false;
        this.mark100 = false;
        
        this.trackingClip = false;
    }

}

ctv.ca.TrackingController.registerClass('ctv.ca.TrackingController', Sys.Component);

var ctvTrackingController = new ctv.ca.TrackingController();




