/*
	Synthetic Page and Event Reporting Framework (SPERF)
	----------------------------------------------------
	for use when reporting is desired for things that don't exist as/in a real web 
	page (e.g. HTML). this code makes use of Omniture's Site Catalyst reporting 
	product and creates 'page' or 'link' events for the requested event_id.

	use as follows:
		REPORTING.makeCall('simple_event');
		REPORTING.makeCall('complex_event', { key1 : 'value1', key2 : 'value2' });
*/

var REPORTING = {
	events : { },
	makeCall : function() { }
};


// called by the app
REPORTING.makeCall = function (event_id, data_object) {
	//alert("calling makecall with " +  event_id + " and " + data_object);
	// bail out if event_id is unknown
	if (typeof(this.events[event_id]) == "undefined") return false;

	// setup page variables
	this.events[event_id].populateData(data_object);
	this.events[event_id].setPageVariables();

	// send page event
	if (this.events[event_id].event_type == "PAGE") {
		// zero-out link event stuff
		s_linkType = "";
		s_lnk = "";

		// set pagename and URL stuff
		s_pageName = this.events[event_id].generatePageName();
		s_pageURL = this.events[event_id].generatePageURL();
		//alert(s_pageURL);
		s_hier2 = (s_pageURL.charAt(0) == "/") ? s_pageURL.substring(1, s_pageURL.length) : s_pageURL;
		s_hier2 = (s_hier2.charAt(s_hier2.length) == "/") ? s_hier2.substring(0, s_hier2.length - 1) : s_hier2;

		s_gs(s_account.toLowerCase());

	// send link event
	} else if (this.events[event_id].event_type == "LINK") {
		// this is temp
		//return;

		s_pageName = this.events[event_id].generatePageName();
		s_linkType = "o";
		s_lnk = s_co(s_wd); // this is no good
		s_hier2 = "";

		s_gs(s_account.toLowerCase());

	// unknown event type
	} else {
		return false;
	}

	// reset data for next call
	this.events[event_id].clearPageVariables();

	return true;
} // END: makeCall()


function ReportingEvent(event_type, page_name_function, page_url_function, page_variable_function) {
	this.event_type = event_type; // "PAGE" or "LINK"

	this.generatePageName = page_name_function;
	this.generatePageURL = page_url_function;
	this.setPageVariables = page_variable_function;

	this.data = {}; // holds a copy of the data passed in (for use by methods of this object)
} // END: ReportingEventType()


ReportingEvent.prototype.populateData = function(data) {
	for (var i in data) this.data[i] = data[i];
}; // END: populateData()


ReportingEvent.prototype.clearPageVariables = function() {
	this.data = { }; // reset data passed in by app

	s_pageName = s_pageURL = s_events = s_prop1 = s_prop21 = s_prop22 = "";
	s_linkTrackVars="None";

	// loop through and clear out all s_props, s_eVars, and s_heirs
/*
	for (var n=1; n < 26; n++) {
		s_wd['s_prop' + n] = '';
		s_wd['s_eVar' + n] = '';
		s_wd['s_hier' + n] = '';
	}
*/
}; // END: clearPageVariables()

// Test for mtvi_platform
function mtviPlatform() {
	try{
		if(document.mtvi_platform) return document.mtvi_platform;
		else return 'flashvideo';
	}
	catch(e){
	// report errors to r&d suite (unfinished)
	/*
		var er = new Array()
		for (var i in e){
			er.push( {i + " : '" + e[i] + "'"} );
		}
	*/
	}
};// END: mtviPlatform()

// utility method to test for 
function isDefined(v) {
	if(typeof v == 'undefined' || v == null || v == '' || v == 'undefined') return false;
	else return true;
}// END: isDefined()

// =============================================================================


REPORTING.events['video_play'] = new ReportingEvent(
	"PAGE",

	function() { // generatePageName()
		
		// fix for page name length issue (NOT FINISHED YET)
		var name = this.generatePageURL();
		var truncated_name;
		var max_name_length = 100;
		//alert(name);
		// var max_id_length = 8;
		var pos_hash;

		if (name.length > 100) {
			pos_hash = name.lastIndexOf('#');
         if(pos_hash != -1){
            id = name.substring(pos_hash);
            truncated_name = (name.substring(0,100-id.length  )) + id; 
            return truncated_name;
         }
		}  else {
         	return name;
      }

	},

	function() { // generatePageURL()
		// /<gateway>/<franchise>/<content title (parent)>/<video title>


      if(isDefined(this.data['errorstate'])){
         if(this.data['errorstate'] == true){
            var pagebase = "/overdrive/error/error_";

            if(isDefined(this.data['errortype'])) {
               pagebase += this.data['errortype']; 
            }

            if(isDefined(this.data['error_country'])) {
               pagebase += "_" + this.data['error_country'];
            }

            return pagebase;
         }
      }

//for (i in this.data['content_record']){alert(i + "= " + this.data['content_record'][i])}

		//alert("the artist name = " + this.data['artistName']);
		// var url_components = ['/overdrive'];
		var url_components = ['/'+ mtviPlatform()];

		if (this.data['gateway'])        url_components.push(this.data['gateway']);
		if (this.data['content_record']) url_components.push(this.data['content_record']);
		if (this.data['content_title'])  url_components.push(this.data['content_title']);
		if (this.data['video_title'])    url_components.push(this.data['video_title'] + ((this.data['video_id']) ? '#' + this.data['video_id'] : "") );

		return (url_components.join('/'));
	},

	function() { // setPageVariables()
	
		var mtvi_vid = isDefined(this.data['video_id']) ? this.data['video_id'] : "";
		var mtvi_vtitle = isDefined(this.data['video_title']) ? this.data['video_title'] : "";
	

      if(isDefined(this.data['errorstate'])){
         if(this.data['errorstate'] == true){
			s_channel = "";
			s_prop10 = ""; 
			s_prop13 = "";
			s_prop14 = "";        
			s_prop27 = "";
			s_prop28 = "";

            if(isDefined(this.data['errortype'])) {
               s_prop22 = "VID_ERROR: " + this.data['errortype']; 
            }
         }

      } else {


         // build label variable - e.g. 2-BMG : Kelly Clarkson : 62586 - Because of You
         if (isDefined(this.data['label_gov_id'])) {
            var mtvi_labelid = typeof this.data['label_gov_id'] != 'undefined' ? this.data['label_gov_id'] : "0";
         //	var mtvi_label = this.data['label']; // not getting passed this in 1.2
            var mtvi_artist = typeof this.data['artist_name'] != 'undefined' ? this.data['artist_name'] : "MTVI-NO_ARTIST"; 
            mtvi_vid = typeof this.data['video_id'] != 'undefined' ? this.data['video_id'] : "MTVI-NO_VID";
            mtvi_vtitle = typeof this.data['video_title'] != 'undefined' ? this.data['video_title'] : "MTVI-NO_VTITLE";
            s_prop13 = mtvi_labelid + "-" + getVidGovLabelName(mtvi_labelid) + " : " + mtvi_artist + " : " + mtvi_vid + " - " + mtvi_vtitle;
         }
         

         s_channel = mtviPlatform();
         s_prop1 = isDefined(this.data['artist_name']) ? this.data['artist_name'] : "";
         s_prop10 = typeof this.data['content_record'] != 'undefined' ? this.data['content_record'] : "MTVI-NO_RECORD";
         s_prop12 = typeof this.data['location'] != 'undefined' ? this.data['location'] : "";
		 /*if(isDefined(this.data['video_content_type'])){
			 s_prop14 = this.data['video_content_type'];
		 }*/
		 var s_hier3Suffix= isDefined(this.data['vidtypename']) ? this.data['vidtypename'] : "";
		 s_prop14 = s_hier3Suffix;
		 var s_hier3_MUSIC_Array=new Array("MUSIC","Music Video","Interview","Concert","Promotional","Show Performance",".com Live Performance","Exclusive Audio Track","Video Bio");
		 var s_hier3_NEWS_Array=new Array("NEWS","Overdrive News","News");
		 var s_hier3_TV_Array=new Array("TV","Show Clip");
		 var s_hier3_MOVIE_Array=new Array("MOVIE","Movie Clip","Movie Trailer");
		 var s_hier3_GAMES_Array=new Array("GAMES","Video Game Trailer","Gameplay Footage");
		 //var s_prop14_UNDEFINED_Array=new Array("UNDEFINED","Non-channel Live Performances","Behind the Scenes");
		 var s_hier3_ALL_Array=new Array(s_hier3_MUSIC_Array,s_hier3_NEWS_Array,s_hier3_TV_Array,s_hier3_MOVIE_Array,s_hier3_GAMES_Array);
		 var s_hier3Prefix="";
		 outerLoop:
		 for(var i=0;i<s_hier3_ALL_Array.length;i++){
		 	var currArray=s_hier3_ALL_Array[i];
		 	for(var j=1;j<currArray.length;j++){
				if(s_hier3Suffix==currArray[j]){
					s_hier3Prefix=currArray[0]+",";
					break outerLoop;		
				}
			}
		 }
		 s_hier3=s_hier3Prefix+s_hier3Suffix;
         s_prop21 = mtvi_vtitle + "#" + mtvi_vid;
         s_prop22 = "OD_USER:" + window.screen.width + "x" + window.screen.height;
         s_prop27 = typeof this.data['gateway'] != 'undefined' ? this.data['gateway'] : "MTVI-NO_GATE";
         s_prop28 = typeof this.data['content_title'] != 'undefined' ? this.data['content_title'] : "MTVI-NO_TITLE";


      }



		return;
	
		// Report internal campaign for target video...
		if(typeof mtvnParams.source != null && typeof mtvnParams.source != 'undefined'){
			s_prop17 =  intCmp == null?unescape( mtvnParams.source ):"";
			var h1String = unescape( (mtvnParams.source + this.generatePageURL()).split("/").join(",") );
			s_hier1 = intCmp == null?h1String:"";
		}
	
		// ...and set a flag in Player.js to supress expression in subsequent page calls
		intCmp = 1;
		
		return;
	}
); // END: video_play


REPORTING.events['browse'] = new ReportingEvent(
	"LINK",

	function() { // generatePageName()
	/**/
		return (
			'/' + mtviPlatform() + '/' + this.data['gateway'] +
			( (typeof(this.data['content_record']) != "undefined") ? '/' + this.data['content_record'] : "") + 
			( (typeof(this.data['content_title']) != "undefined") ? '/' + this.data['content_title'] : "")
		);
	
	},

	function() { // generatePageURL()
		return;
	},

	function() { // setPageVariables()
	/**/
		s_linkName = this.generatePageName();
		s_prop22 = "OD_USER:" + window.screen.width + "x" + window.screen.height;
		return;
	
	}
); // END: browse


REPORTING.events['playlist_add'] = new ReportingEvent(
	"LINK",

	function() { // generatePageName()
		var artist_name_string = "";
		if ( (typeof(this.data['artist_name']) != 'undefined') && (typeof(this.data['artist_id']) != 'undefined')) {
			artist_name_string = ' by ' + this.data['artist_name'] + ' [' + this.data['artist_id'] + ']';
		}
		return ('Add to Playlist: ' + this.data['video_title'] + ' [' + this.data['video_id'] + ']' + artist_name_string);
	},

	function() { // generatePageURL()
		return;
	},

	function() { // setPageVariables()
		s_linkName = this.generatePageName();
		s_linkTrackVars="prop1";
		s_prop22 = "OD_USER:" + window.screen.width + "x" + window.screen.height;
		if (typeof(this.data['artist_name']) != 'undefined') s_prop1 = this.data['artist_name'];
		return;
	}
); // END: playlist_add


REPORTING.events['overdrive_exit'] = new ReportingEvent(
	"LINK",

	function() { // generatePageName()
		return "Exit Overdrive";
	},

	function() { // generatePageURL()
		return;
	},

	function() { // setPageVariables()
		s_linkName = this.generatePageName();
		return;
	}
); // END: overdrive_exit

REPORTING.events['overdrive_pv'] = new ReportingEvent(
	"PAGE",

	function() { // generatePageName()
		return "/" + mtviPlatform() + "/player.jhtml";
	},

	function() { // generatePageURL()
		return mtviPlatform() + "/player.jhtml";
	},

	function() { // setPageVariables()
		s_prop22 = "OD_USER:" + window.screen.width + "x" + window.screen.height;
		return;
	}
); // END: overdrive_pv

REPORTING.events['generic_page'] = new ReportingEvent(
	"PAGE",

	function() { // generatePageName()
		//return window.location.pathname; ---- replacing with text of article title if available
		if (pageConfig.section){
			return pageConfig.articleTitle;	
		} else {
			return window.location.pathname;
		}
	},

	function() { // generatePageURL()
		return window.location.pathname;
	},

	function() { // setPageVariables()
		s_prop22 = "OD_USER:" + window.screen.width + "x" + window.screen.height;
		return;
	}
); // END: generic_page

REPORTING.events['fullscreen'] = new ReportingEvent(
	"PAGE",

	function() { // generatePageName()
		return "fullscreen";
	},

	function() { // generatePageURL()
		return "broadband";
	},

	function() { // setPageVariables()
		s_prop22 = "OD_USER:" + window.screen.width + "x" + window.screen.height;
		return;
	}
); // END: generic_page

// ... add new reporting event types below
//REPORTING.makeCall('overdrive_pv' );
//REPORTING.makeCall('overdrive_pv', { error : 'upgrade' );
//REPORTING.makeCall('overdrive_pv', { error : 'platform' );



function getVidGovLabelName(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;
   }
}



