/* extra JS functions for pro7, sat1, kabel1, 7Games
** mediacenter videoplayer20
** Omniture Tracking only 
** $Revision: 1257 $ from $Date: 2009-05-25 16:00:43 +0200 (Mon, 25 May 2009) $ 
*/

function VP20Tracker() {	
	// keep some values
	this.updatePosition = -1;
	this.updateAdPosition = -1;
	this.updateAdEvent = "";
	this.updateAdName = "";
	this.viewTime = 0;
	this.viewTimeDelta = 0; // prepare for zoomIn/Out
		
	/**
	* Overwrite the data of the s object and call s.t() or s.tl() for Omniture tracking
	*/
	this.track = function(evt, obj) {	

		var mVideoEventNameList = {
			/* 'loadplayer': 'event16',  'play': 'event25',  'pause': 'event26',  'setBytesPerSecond': 'videoBytesPerSecond', */
			'PageViews': 'event2', 'PageViewsVideo': 'event6',
			'prerollAd_start': 'event17,event28', 'prerollAd_completed': 'event18',
			'contentClip_start': 'event19,event29', 'contentClip_completed': 'event20,event66', 
			'midrollAd1_start': 'event30,event28', 'midrollAd1_completed': 'event31', 
			'midrollAd2_start': 'event32,event28', 'midrollAd2_completed': 'event33', 
			'midrollAd3_start': 'event34,event28', 'midrollAd3_completed': 'event35', 
			'midrollAd4_start': 'event36,event28', 'midrollAd4_completed': 'event37',
			'midrollAd5_start': 'event38,event28', 'midrollAd5_completed': 'event39',
			'updateAdPosition': 'event21',  'updatePosition': 'event22,event66',  
			'updateMidroll1Position': 'event40', 'updateMidroll2Position': 'event41', 'updateMidroll3Position': 'event42',
			'updateMidroll4Position': 'event43', 'updateMidroll5Position': 'event44',
			'adClicked': 'event27', 'fullscreenOn': 'event23', 'fullscreenOff': 'event24',
			'viewTimeXplus': 'event65', 'videoBytesPerSecond': 'event67'
		};
		
		// check if s is already created and s_code.js is loaded
		if(!window.s) return;
		
		// handle the s object
		s = s_gi(s_account);
		
		// reset some values
		s.eVar13 = 0; // videoBytesPerSecond
		s.eVar14 = 0; // viewTime
		s.eVar28 = 0; // old one; this.getEventValue('videoBytesPerSecond');
		
		//special handling for PageView event
		if(evt.evtname == "PageViews") evt.evtname = "PageViewsVideo";
		
		// overwrite or set the Omniture values on-the-fly before calling s.t() or s.tl()
		s.linkTrackVars = "events,eVar13,eVar14,eVar16,eVar17,products"; // what variables are send
		s.linkTrackEvents = (mVideoEventNameList[evt.evtname]) ? mVideoEventNameList[evt.evtname] : ""; // define the events to send
		
		s.pageName = "/" + obj.ivwpath + "/" + obj.title;
		s.prop1 = obj.title;
		s.prop2 = obj.ivw_content_type ? obj.ivw_content_type : "video"; // IVW content type, "video" for VP and "content" for MC
		s.prop3 = "/" + obj.ivwpath + "/";
		s.prop4 = obj.agof;
		s.prop6 = ""; // Search string
		s.prop7 = ""; // Search Result
		s.hier1 = obj.ivwpath + "/" +obj.title;
		s.hier2 = s.server + "/" + obj.ivwpath + "/" +obj.title;
		
		var linkURL_tmp = obj.link_url.split("/");
		
		s.channel = linkURL_tmp[0]; //obj.link_url.split("/") >> channel
		s.prop8 = (linkURL_tmp.length > 1) ? linkURL_tmp[1] : ""; // obj.link_url.split("/") >> format = hierarchie1
		s.prop9 = (obj.hierarchy2) ? obj.hierarchy2 : ""; // vplayer = hierarchie2
		s.prop10 = (obj.video_type) ? obj.video_type : ""; // full, short = hierarchie3
		s.prop22 = (obj.playback_duration) ? obj.playback_duration : 0; // =video length [sec]
		s.prop23 = (obj.id) ? obj.id : ""; // =videoID
		s.prop24 = (obj.playertype) ? obj.playertype : ""; // =videoPlayerType [pageplayer, mediaplayer]

		//s_browserInfo should be provided by tools.js
		if (typeof s_browserInfo != "undefined") {
			s.prop25 = s_browserInfo.getViewportWidth(); //viewport width
			s.prop26 = s_browserInfo.getViewportHeight(); //viewport height
			s.prop27 = s_browserInfo.getFlashVersion(); // flash version
		}

		s.eVar8 = obj.ivw_content_type; // IVW content type
		s.eVar16 = s.prop3 + obj.title; // VideoName
		s.eVar17 = evt.evtname; // EventName
		
		/**
		* define the s.products and prepare for later accumulation, see Omniture doc p.115
		* multiple event values separated with pipes "|"
		* s.events="purchase,event1,event2"
		* s.products="Category;Product;Quantity;Price;eventN=X[|eventN=X][,Category;Product;Quantity;Price;eventN=X]"
		*/
		s.products = (evt.evtname=="PageViewsVideo") ? "" : ";;;;";
		
		// attach position to fullscreenOn and fullscreenOff event
		// if(evt.evtname == "fullscreenOn" || evt.evtname == "fullscreenOff") s.eVar17 += " " + Math.round(evt.param1); // evt.param1=position [sec]
		
		// add viewtime to contentClip_completed
		if(evt.evtname == "contentClip_completed") {
			s.products += "event66=" + this.viewTime + "|";
			s.eVar14 = "viewTime " + this.viewTime;
		}
		
		// attach the data to the videoBytesPerSecond event
		if(evt.evtname == "videoBytesPerSecond") {
			s.products += mVideoEventNameList[evt.evtname] + "=" + evt.param1 + "|";
			s.eVar13 = "videoBytesPerSecond " + evt.param1;
		}
		// set the event list
		s.events = (mVideoEventNameList[evt.evtname]) ? mVideoEventNameList[evt.evtname] : ""; // put the events here, such as event17, ..
		if(s.events == "") return; // exit if event is not known
		else if(s.events == "event2") s.events = ""; // event2 is always added in s_code.js, so avoid double naming
		
		// set and reset updatePosition on contentClip_start/completed
		if(evt.evtname == "contentClip_start") {
			this.updatePosition = 0;
			this.viewTime = 0;	
		}
		if(evt.evtname == "contentClip_completed") {
			this.updatePosition = -1;
			this.viewTime = 0;
		}
		
		// save update of AdPositions and abort tracking, as position updates are collected
		if(evt.evtname == "updateAdPosition" || evt.evtname == "updateMidroll1Position" || evt.evtname == "updateMidroll2Position" || evt.evtname == "updateMidroll3Position" || evt.evtname == "updateMidroll4Position" || evt.evtname == "updateMidroll5Position")
		{
			this.updateAdPosition = evt.param1;
			this.updateAdEvent = s.events; 
			this.updateAdName = evt.evtname;
			return;
		}
		
		// save update of VideoPositions and abort tracking, as position updates are collected
		if(evt.evtname == "updatePosition")
		{
			this.updatePosition = evt.param1;
			this.viewTime += 5; // sum up the 5 seconds
			return;
		}
		
		// set and reset updateAdPosition on AdClip_start/completed
		if(evt.evtname == "prerollAd_start" || evt.evtname == "midrollAd1_start" || evt.evtname == "midrollAd2_start" || evt.evtname == "midrollAd3_start" || evt.evtname == "midrollAd4_start" || evt.evtname == "midrollAd5_start")
		{
			this.updateAdPosition = 0;
			switch (evt.evtname) {
				case "prerollAd_start":
					this.updateAdName = "updateAdPosition";
					break;
				case "midrollAd1_start":
					this.updateAdName = "updateMidroll1Position";
					break;
				case "midrollAd2_start":
					this.updateAdName = "updateMidroll2Position";
					break;
				case "midrollAd3_start":
					this.updateAdName = "updateMidroll3Position";
					break;
				case "midrollAd4_start":
					this.updateAdName = "updateMidroll4Position";
					break;
				case "midrollAd5_start":
					this.updateAdName = "updateMidroll5Position";
					break;
				default: 
					this.updateAdName = "";
			}
			
			this.updateAdEvent = (mVideoEventNameList[this.updateAdName]) ? mVideoEventNameList[this.updateAdName] : "";; 
		}
		if(evt.evtname == "prerollAd_completed" || evt.evtname == "midrollAd1_completed" || evt.evtname == "midrollAd2_completed" || evt.evtname == "midrollAd3_completed" || evt.evtname == "midrollAd4_completed" || evt.evtname == "midrollAd5_completed")
		{
			this.updateAdPosition = -1;
			this.updateAdEvent = "";
			this.updateAdName = "";
		}
		
		/*
		* invoke a complete new path set for the "PageViews" event with s.t()
		* for all other events just do the smaller s.tl()
		*/
		if(evt.evtname == "PageViewsVideo") s.t(); // s.t() just as IVW and updateAds()
		else s.tl(this, 'o', obj.title); // s.tl(this, 'o', s_eVar16);
	}
	
	/**
	* Call this method when user aborts the playing of a clip to send the viewTime, updatePosition and updateAdPosition data
	*/
	this.sendFinal = function() {
		
		// check if s is already created and s_code.js is loaded
		if(!window.s) return;
		
		try {
			// s.products="Category;Product;Quantity;Price;eventN=X[|eventN=X][,Category;Product;Quantity;Price;eventN=X]"
			s.products = ";;;;";
		
			if(this.updatePosition != -1) {
				// create the updatePos event here
				s.linkTrackEvents = "event22,event66" + ",";
				s.events = "event22,event66" + ",";
				s.products += "event22=" + this.updatePosition + "|" + "event66=" + this.viewTime + "|"; // TODO recheck with reports, p.115 documentation
				s.eVar17= "updatePosition " + this.updatePosition;
				s.eVar14 = "viewTime " + this.viewTime; // add viewtime to updatePosition
				// fire the event
				s.tl(this, 'o', s.prop1);
				// reset the saved data
				this.viewTime = 0;
				this.updatePosition = -1;
			}
			
			if(this.updateAdPosition != -1) {
				// create the updateAdPos event  here
				s.linkTrackEvents += this.updateAdEvent;
				s.events += this.updateAdEvent;
				s.products += "" + this.updateAdEvent + "=" + this.updateAdPosition;
				s.eVar17= this.updateAdName + " " + this.updateAdPosition;
				// fire the event
				s.tl(this, 'o', s.prop1);
				// reset the saved data
				this.updateAdPosition = -1;
				this.updateAdEvent = "";
				this.updateAdName = "";
			}
		}
		catch(e) {
			void(0);
		}
	}
	
	/**
	* attach sendFinal to unload event to send 
	* last updatePos and updateAdPos to Omniture
	*/
	this.setUnloader = function(fn) {
		if(window.addEventListener) window.addEventListener("unload", fn, false);
		else if(window.attachEvent) window.attachEvent("onunload", fn);
	}
}

var vp20Tracker = new VP20Tracker();
vp20Tracker.setUnloader(doTrackerUnload);

function doTrackerUnload() {
	vp20Tracker.sendFinal();
}
