/* handle omniture code */

function ReportManager(){
   this.self = this;
   intCmp = null;
   this.myvar = "foobar";
   this.last_gateway = "";
   playCountTimer=null;

}

ReportManager.prototype.init = function(){

	// insert div + image into doc
   s_dc(s_account);  // necessary for dynamic reporting. 
   
   s_channel = pageConfig.section; // filter out player.jhtml from site sections report
   REPORTING.makeCall('generic_page' );
   
   s_channel = pageConfig.section;// reassign s_channel for subsequent calls
   
   try {
	       //call straight from fscommand for firefox
	       // document.getElementById('bbFlash').attachEvent("FSCommand",reporting_manager.handle_fscommand);
		  
   } catch(e){ 
      printStatus("reportingManger init: "+e.description) 
   }
}


ReportManager.prototype.handle_fscommand = function(cmd,args){
   try {
      reporting_manager.dispatchReport(cmd,args);
   } catch(e){ 
   }
}

ReportManager.prototype.dispatchReport = function(cmd,args){
 

   if(cmd == "getPosition"){
    // window.status=args;
     return;
   }


   try {
      if(cmd == "playVideo"){
    	  	 eval("this.reportPlayVideo("+args+")");
	
      } 
   }catch(e){ 
     // alert('error reporting video' + e.message);
   }

   try {
      if(cmd == "sendBrowseEvent"){
					     eval("this.reportBrowseEvent("+args+")");

      }
   }catch(e){ 
	printStatus("error reporting browse event: "+ e.message) 						
   }

   try {
      if(cmd == "addToPlaylist"){
										eval("this.reportAddToPlaylist("+args+")");
      }
   }catch(e){ 
      printStatus("error reporting addToPlaylist: "+e.description) 
   }

}

// label gov agreement id should be passed as another param here
// and presumably added to SPERF so that s_prop13 does get populated 
//ReportManager.prototype.reportPlayVideo = function(url, gate, cont, cont_t, video_t, video_id, artist_name, artist_id, buff_url, buff_gate, buff_cont, buff_cont_t , buff_video_t, buff_video_id, buff_artist_name, buff_artist_id){

ReportManager.prototype.reportPlayVideo = function(current, buff){

   /* 
     if(typeof arguments[16] != "undefined"){
     	var label_gov_id = arguments[16];
     }
    */
	//alert('reportplayvideo called');
	try {
		if (current.content.length==0) { cont = "playlist"; }
		thevideo_event = { gateway: current.gateway.name };
		if (current.url){           thevideo_event.url = current.url;
		var isLocal = false;
		if (isLocal) current.url = isLocal;
         thevideo_event.location = isLocal ? "od_vidsrc_local" : "od_vidsrc_stream";
		} 
		if (current.content){
		        thevideo_event.content_record = unescape(current.content.type); 
		        thevideo_event.content_title = unescape(current.content.title);
		}		
		if (current.title)        thevideo_event.video_title = unescape(current.title);
		if (current.id)           thevideo_event.video_id = current.id;
		if (current.artistName)   thevideo_event.artist_name = unescape(current.artistName);
		if (current.artistId)     thevideo_event.artist_id = current.artistId;
		if (current.label)        thevideo_event.label = unescape(current.label);
		if (current.agreement)    thevideo_event.label_gov_id = current.agreement;
		if (current.vidtype)      thevideo_event.vidtype = current.vidtype;
		if (current.vidtypename)  thevideo_event.vidtypename = unescape(current.vidtypename);
		if(top.location.search.indexOf("jb_test")!=-1) alert(current.vidtypename);

 
		//clearTimeout(playCountTimer);
		//playCountTimer=setTimeout("reporting_manager.makeTheCall(thevideo_event)",2000);

		 REPORTING.makeCall("video_play", thevideo_event);
		
	} catch(e) {
	 // var msg = 'error reporting play ' + e.message; 
      //window.status = msg;  
	//alert("error reporting play: "+e.description);
	}
}

ReportManager.prototype.makeTheCall= function(vidE){
try {
    	theVid=vidE;
		if(Player.controls.currentPosition>2){
		   if (theVid.video_id==videoEvent.id){

            var error_vid = Player.currentMedia.getItemInfo("ERROR_VID");
            var error_country = Player.currentMedia.getItemInfo("ERROR_COUNTRY");
            var error_auth = Player.currentMedia.getItemInfo("ERROR_AUTH");
            var error_expired = Player.currentMedia.getItemInfo("ERROR_EXPIRED");
            var error_badvid = Player.currentMedia.getItemInfo("ERROR_BADVID");
            var video_content_type = Player.currentMedia.getItemInfo("video_content_type");
			if(top.location.search.indexOf("jb_test")!=-1) alert(video_content_type);
			
            if(reporting_manager.isDefined(error_vid)){
               theVid.errorstate = true;
               theVid.error_vid = error_vid;
               if(reporting_manager.isDefined(error_auth)){
                  theVid.errortype = "no_auth";
               }
               if(reporting_manager.isDefined(error_country)){
                  theVid.errortype = "country_block";
                  theVid.error_country = error_country;
               }
               if(reporting_manager.isDefined(error_expired)){
                  theVid.errortype = "expired";
               }
               if(reporting_manager.isDefined(error_badvid)){
                  theVid.errortype = "badvid";
               }
            }     

            if(reporting_manager.isDefined(video_content_type)){
               theVid.video_content_type = video_content_type;
            }
			
/******************************************************/
			   // Temp fix in place for Intro video reporting as 1st video. 
//			   	if (!appManager.playIntro && !appManager.adPlaying){
//			 		REPORTING.makeCall("video_play", theVid);
//					if (window.location.search.indexOf('keepnew')>=0) {
//						var theVidO = "";
//						for (i in theVid) {
//							theVidO += "|" + i + ":" + theVid[i] + "|\n";
//						}
//						alert (theVidO);
//					}
//		   		}			
			   
			   /******************************************************/
			   // Temp fix in place for Intro video reporting as 1st video. 
			   	if (!appManager.playIntro && !appManager.adPlaying){
			 		if (typeof(theVid) != "undefined" && theVid.vidtype=='45'){
						REPORTING.makeCall("interstitial_play", theVid);
					}
					else{					
						REPORTING.makeCall("video_play", theVid);
					}	
		   		}
			   /******************************************************/
		   }	 
         } 
  		else if (Player.controls.currentPosition < 2) {
			playCountTimer=setTimeout("reporting_manager.makeTheCall(theVid)",1000);
 		}
	} catch(e) {
 	    //var msg = 'error  ' + e.message; 
            //window.status = msg;  
            //printStatus("error makeTHeCall: "+e.description)
			mtvi_catchError(e);
	}
}

ReportManager.prototype.reportAddToPlaylist = function(video_title, video_id, artist_name, artist_id, buff_url) {

	var playlist_event = { video_title: video_title, video_id: video_id };
	if (artist_name) playlist_event.artist_name = artist_name;
	if (artist_id)   playlist_event.artist_id = artist_id;
	REPORTING.makeCall("playlist_add", playlist_event);

}

ReportManager.prototype.reportBrowseEvent = function(gate, cont, cont_t, article_t) {

   orig_gate = gate;

   // force hierarchy (not checked everywhere yet) 
   if(typeof gate != 'undefined'){
      if(gate.indexOf("_archive") != -1 ) { 
         gate = this.last_gateway + "/" + "archive";
      } else {
         if(typeof cont == "undefined"){
         }
         this.last_gateway = orig_gate;
      }
   }



	var browse_event = { gateway: gate };
	if (cont)      browse_event.content_record = cont;
	if (cont_t)    browse_event.content_title = cont_t;
	if (article_t) browse_event.article_title = article_t;

   REPORTING.makeCall("browse", browse_event);


}

// return name for id 
ReportManager.prototype.getVidGovLabelId = function (id){

   id = parseInt(id);
   switch(id){
      case 1:
         return "Warner";
      break;
      case 2:
         return "BMG";
      break;

      case 3:
         return "Universal";
      break;

      case 4:
         return "Sony";
      break;

      case 5:
         return "EMI";
      break;

      case 6:
         return "Independent Label";
      break;

      default:
      return "Unknown";
      break;
   }
}

var reporting_manager = new ReportManager();
try { 
	if ((navigator.userAgent.toLowerCase().indexOf("firefox")>-1)){  
		window.addEventListener("DOMContentLoaded", reporting_manager.init, false); 
	} else {
      window.attachEvent("onload", reporting_manager.init);
   }
} catch(e) {

}

/**
function reporting_unload() {
   try {
      if (typeof (s_wd) != "undefined") REPORTING.makeCall('overdrive_exit');  
   } catch(e) {

   }
**/

ReportManager.prototype.isDefined = function(v){
   if(typeof v == 'undefined' || v == null || v == '' || v == 'undefined') return false;
   else return true;
}
