/* handle omniture code */
var testRepImage_3=new Image();
testRepImage_3.src='http://www.mtv.com/images/test_image_3.gif';

function ReportManager(){
   this.self = this;
   intCmp = null;
   this.myvar = "foobar";
   this.last_gateway = "";
   playCountTimer=null;

}

ReportManager.prototype.init = function(){
	// insert div + image into doc
   try{
      s_dc(s_account);  // necessary for dynamic reporting. 
   }catch(e){ 
   }

   s_channel = ""; // filter out player.jhtml from site sections report
   REPORTING.makeCall('overdrive_pv' );
   
   s_channel = configManager.channelName;// 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) 
   }

}
//if(top.location.search.indexOf("jb_test")!=-1) alert("in RM.1.6");
// 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){
	//alert("called by flash!");
	try {
		if(top.location.search.indexOf("jb_test")!=-1){
			var t='';
			for(var i in current){
				t+=i;
				t+='=';
				t+=current[i];
				t+='\n';
			}
			alert('in reporting_manager.reportPlayVideo(\n'+t+')');
		}
	}
	 catch(e) { 
		printStatus("ReportManager.reportPlayVideo error: "+e.description); 
	}
	
   /* 
     if(typeof arguments[16] != "undefined"){
     	var label_gov_id = arguments[16];
     }
    */
	try {
		if (current.content.length==0) { cont = "playlist"; }
		thevideo_event = { gateway: unescape(current.gateway.name) };
		if (current.url){           thevideo_event.url = current.url;
		var isLocal = (configManager.useOptimizer) ? appManager.isLocalFile(current.url) : 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 (current.parenttypename)  thevideo_event.parenttypename = unescape(current.parenttypename);
		if (current.parenttitle)  thevideo_event.parenttitle = unescape(current.parenttitle);
		////ok","blocked" or "error
		if(current.status && current.status.toLowerCase()!='ok'){
			thevideo_event.errorstate=true;
			if(current.status.toLowerCase()=='blocked'){
				thevideo_event.errortype="country_block";
			}
			else{
				thevideo_event.errortype=unescape(current.status);
			}
		}
			
		clearTimeout(playCountTimer);
		playCountTimer=setTimeout("reporting_manager.delayedCall(thevideo_event)",2000);

		 //REPORTING.makeCall("video_play", thevideo_event);
		
	} catch(e) {
	 // var msg = 'error reporting play ' + e.message; 
      //window.status = msg;  
		printStatus("error reporting play: "+e.description); 
	}
}

ReportManager.prototype.delayedCall=function(vidE){
	try{
		//if(top.location.search.indexOf("jb_test")!=-1)alert("in delayedCall");
		var testRepImage_1=new Image();
		var testRepImage_2=new Image();
		theVid=vidE;
		flvPlayer=document.getElementById('bbFlash');
		try{
			if(top.location.search.indexOf("jb_test")!=-1) {
				alert("flvPlayer="+document.getElementById('bbFlash'));
			}
		} catch(e){
			printStatus("error -> ReportManager.prototype.delayedCall: "+e.description); 
		}
		if(isFirefox===true) flvPlayer=document.getElementById('bbFlash_moz');
		try{
			if(top.location.search.indexOf("jb_test")!=-1){
				alert("flvPlayer.getVideoTime="+flvPlayer.getVideoTime);
			}
		} catch(e){
			printStatus("error -> ReportManager.prototype.delayedCall: "+e.description); 
		}
		testRepImage_2.src='http://www.mtv.com/images/test_image_2.gif';
		if(reporting_manager.isDefined(flvPlayer)){
			var currentVideoTime=flvPlayer.getVideoTime();
			testRepImage_1.src='http://www.mtv.com/images/test_image_1.gif';		
			if(currentVideoTime>2){
				REPORTING.makeCall("video_play", theVid);
				try{
					if(top.location.search.indexOf("jb_test")!=-1) {
						alert("testRepImage_1="+testRepImage_1.src);
					}
				} catch(e){
					printStatus("error -> ReportManager.prototype.delayedCall: "+e.description); 
				}
			}
			else{
				playCountTimer=setTimeout("reporting_manager.delayedCall(theVid)",1000);
			}
		}
		//if(top.location.search.indexOf("jb_test")!=-1) alert(flvPlayer);		
	}
	catch(e){
		printStatus("error delay reporting play: "+e.description)
	}
}

ReportManager.prototype.makeTheCall= function(vidE){//deprecated for flv players 1.6 and above
//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(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);


}

ReportManager.prototype.reportEmbedEvent = function(videoInfo) {
	switch((typeof videoInfo).toLowerCase()){
		case  "object":
			REPORTING.makeCall("embed", videoInfo);
			//if(top.location.search.indexOf("jb_test")!=-1) alert("Embed event: "+ videoInfo.video_id + " | "+ videoInfo.video_title);
			break;
		case "string":
			REPORTING.makeCall("embed", {video_id:videoInfo,video_title:""});
			//if(top.location.search.indexOf("jb_test")!=-1) alert("Embed event: "+ videoInfo);
			break;
		default: 
			return; 
			break;
	}
}

ReportManager.prototype.reportSnagEvent = function(snagURL) {
	REPORTING.makeCall("snag", {url:snagURL});
	//if(top.location.search.indexOf("jb_test")!=-1) alert("Snag event: "+ snagURL);
}

ReportManager.prototype.reportSearchSuccess = function(terms) {
	REPORTING.makeCall("searchResult", {searchSuccess:1,searchTerm:terms});
	//if(top.location.search.indexOf("jb_test")!=-1) alert("Search event: "+ terms);
}

ReportManager.prototype.reportSearchFailed = function(terms) {
	REPORTING.makeCall("searchResult", {searchSuccess:0,searchTerm:terms});
	//if(top.location.search.indexOf("jb_test")!=-1) alert("Search event: "+ terms);
}

ReportManager.prototype.reportHpTabEvent = function(tab) {
	REPORTING.makeCall("hp_tab", {tabName:tab});
	if(top.location.search.indexOf("hn_test")!=-1) alert("Tab: "+ tab);
}

ReportManager.prototype.reportHpToutEvent = function(toutObj) {
	REPORTING.makeCall("hp_tout", toutObj);
	if(top.location.search.indexOf("hn_test")!=-1) alert("Tab: "+ toutObj.tabName + "\nTout: "+toutObj.toutName);
}

ReportManager.prototype.reportMiniMuteEvent = function(state) {
	REPORTING.makeCall("mini_mute", {muteState:state});
	if(top.location.search.indexOf("hn_test")!=-1) alert("State: "+ muteState);
}

ReportManager.prototype.reportMiniScrollEvent = function() {
	REPORTING.makeCall("mini_scroll", {});
	if(top.location.search.indexOf("hn_test")!=-1) alert("Scroll");
}

ReportManager.prototype.reportPlayerResize = function(size) {
	if(!this.isDefined(size)) size=1;
   REPORTING.makeCall("player_resize", {'playerSize':size});
}

// 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;
}
