/*****************************************
*************** Omniture *****************
*****************************************/
function OmniturePlugin() {
	this.constructor();

	this.mProductList = new Array();
	this.mEventList = new Array();
	this.mSentEventList = new Array();
	this.mEventValues = new Object();
	this.mVideoEventName = '';
	this.mEventRules = new Object();
	this.mVideoEventNameList = {
		'loadplayer': 'event16', 'prerollAd_start': 'event17', 'prerollAd_completed': 'event18',
		'contentClip_start': 'event19', 'contentClip_completed': 'event20', 'updateAdPosition': 'event21',
		'updatePosition': 'event22', 'zoomIn': 'event23', 'zoomOut': 'event24', 'play': 'event25', 'pause': 'event26',
		'adClicked': 'event27', 'setBytesPerSecond': 'videoBytesPerSecond'
		};

	/********* inherited methods **********/

	this.init = function() {
		this.buildEventRules();
		this.setEventValue('event21', -1);
		this.setEventValue('event22', -1);
	}

	this.processUnload = function() {
		this.sendTracking(true);
	}

	this.trackVideoPlayerEvent = function(pArgs) {
		this.debug('OmniturePlugin.trackVideoPlayerEvent: ' + pArgs.join(', '));

		var eventName = pArgs[0];
		var eventRule = this.mEventRules[eventName];
		var trackingEventName = eventRule['trackingEventName'];
		var eventValue = pArgs[1];
		var videoEventName = eventRule['videoEventName']
		// store event
		if(typeof(eventRule['store']) != 'undefined' && eventRule['store'] == 1 && (typeof(eventRule['storeIfHasEvent']) == 'undefined' || this.isEventSent(this.mVideoEventNameList[eventRule['storeIfHasEvent']]) || this.hasEvent(this.mVideoEventNameList[eventRule['storeIfHasEvent']]))) {
			if((typeof(eventRule['multiple']) != 'undefined' && eventRule['multiple'] == 1) || !this.isEventSent(trackingEventName)) {
				this.addEventOnce(trackingEventName);
				this.setVideoEventName(videoEventName == null ? '' : videoEventName);
			}
		}
		// store value
		if(typeof(eventRule['storeValue']) != 'undefined' && eventRule['storeValue'] == 1) {
			var value = pArgs[1] * 1;
			if(!this.hasEventValue(trackingEventName) || (value > this.getEventValue(trackingEventName))) {
				this.setEventValue(trackingEventName, value);
			}
		}
		// send collected data
		if(typeof(eventRule['send']) != 'undefined' && eventRule['send'] == 1) {
			if(typeof(eventRule['sendArgs']) != 'undefined' && eventRule['sendArgs'] == 1) {
				this.setVideoEventName(this.getVideoEventName() + pArgs[2]);
			}
			this.sendTracking(false);
		}
		// pretend other event
		if(typeof(eventRule['trackEventAfter']) != 'undefined') {
			this.trackVideoPlayerEvent((new Array(eventRule['trackEventAfter'])).concat(eventRule['trackEventAfterArgs']));
		}
	}

	/********* specific methods **********/

	this.addEvent = function(pEventName) {
		this.addEventOnce(pEventName);
	}

	this.addEventOnce = function(pEventName) {
		if(this.hasEvent(pEventName)) {
			return;
		}
		this.mEventList[this.mEventList.length] = pEventName;
	}

	this.addEventIfHasOtherEvent = function(pEvent, pOtherEvent, pOnlyOnce) {
		if(!this.hasEvent(pOtherEvent) || pOnlyOnce && this.isEventSent(pEvent)) {
			return false;
		}
		this.addEvent(pEvent);
		return true;
	}

	this.addProduct = function(pProduct) {
		this.mProductList[this.mProductList.length] = pProduct;
	}

	this.getEventsAsString = function(pDelimiter) {
		return this.mEventList.join(pDelimiter);
	}

	this.getProductsAsString = function(pDelimiter) {
		return this.mProductList.join(pDelimiter);
	}

	this.isEventSent = function(pEventName) {
		for(var i = 0; i < this.mSentEventList.length; i++) {
			if(pEventName == this.mSentEventList[i]) {
				return true;
			}
		}
		return false;
	}

	this.hasEvents = function() {
		return this.mEventList.length > 0;
	}

	this.hasEvent = function(pEventName) {
		for(var i = 0; i < this.mEventList.length; i++) {
			if(pEventName == this.mEventList[i]) {
				return true;
			}
		}
		return false;
	}

	this.clear = function() {
		this.mSentEventList = this.mSentEventList.concat(this.mEventList);
		this.mProductList = new Array();
		this.mEventList = new Array();
	}

	this.setEventValue = function(pEventName, pEventValue) {
		this.mEventValues[pEventName] = pEventValue;
	}
	this.getEventValue = function(pEventName) {
		return this.mEventValues[pEventName];
	}
	this.hasEventValue = function(pEventName) {
		return typeof(this.mEventValues[pEventName]) != 'undefined';
	}

	this.setVideoEventName = function(pName) {
		this.mVideoEventName = pName;
	}

	this.getVideoEventName = function() {
		return this.mVideoEventName;
	}

	this.buildEventRules = function() {
		var eventRules = this.mEventRules;

		// set defaults for all events
		for(var videoEventName in this.mVideoEventNameList) {
			var mappedEventName = this.mVideoEventNameList[videoEventName];
			eventRules[videoEventName] = new Object();
			eventRules[videoEventName]['store'] = 1;
			eventRules[videoEventName]['trackingEventName'] = mappedEventName;
			eventRules[videoEventName]['videoEventName'] = videoEventName;
			eventRules[videoEventName]['multiple'] = 0; // may not be sent multiple times
		}
		
		// prerollAd_start
		eventRules['prerollAd_start']['send'] = 1;
		//eventRules['prerollAd_start']['videoEventName'] = null; // clear eVar17
		eventRules['prerollAd_start']['trackEventAfter'] = 'updateAdPosition';
		eventRules['prerollAd_start']['trackEventAfterArgs'] = new Array('1');
		// prerollAd completed
		eventRules['prerollAd_completed']['storeIfHasEvent'] = 'prerollAd_start'; // only store if the given event is sent or to be sent
		// contentClip_start
		eventRules['contentClip_start']['send'] = 1;
		//eventRules['contentClip_start']['videoEventName'] = null; // clear eVar17
		eventRules['contentClip_start']['trackEventAfter'] = 'updatePosition';
		eventRules['contentClip_start']['trackEventAfterArgs'] = new Array('1');
		// contentClip_completed
		eventRules['contentClip_completed']['send'] = 1;
		eventRules['contentClip_completed']['sendArgs'] = 1; // append seconds to video event name
		eventRules['contentClip_completed']['storeIfHasEvent'] = 'contentClip_start'; // only store if the given event is sent or to be sent
		// updateAdPosition (don't store)
		eventRules['updateAdPosition']['store'] = 0;
		eventRules['updateAdPosition']['storeValue'] = 1;
		// updatePosition (don't store)
		eventRules['updatePosition']['store'] = 0;
		eventRules['updatePosition']['storeValue'] = 1;
		// videoBytesPerSecond
		eventRules['setBytesPerSecond']['store'] = 0;
	}

	this.sendTracking = function(pIsUnload) {
		// do omniture tracking
		this.sendOmnitureTracking(pIsUnload);
	}

	this.sendOmnitureTrackingStructure = function(pOTS) {
		if(!this.hasEvents()) {
			return;
		}
		pOTS.events = this.getEventsAsString(',');
		pOTS.linkTrackEvents = pOTS.events;
		pOTS.products += this.getProductsAsString('|');
		// debug
		if(this.mDebugMode) {
			this.alertDebugInfo();
		}
		if(s_eVar16) {
			pOTS.tl(this,'o',s_eVar16);
			if(this.mDebugMode > 1) {
				this.sendDebugTracking(pOTS);
			}
			this.clear();
		}
	}

	this.sendOmnitureTracking = function(pIsUnload) {
		var structure = this.buildOmnitureTracking(pIsUnload);
		this.sendOmnitureTrackingStructure(structure);
	}

	this.buildOmnitureTracking = function(pIsUnload) {
		var s=s_gi(s_account);
		s.linkTrackVars="events,eVar16,eVar17,eVar28,products";
		s.eVar16=s.prop3 + s_eVar16;			// VideoName
		//s.eVar28 = videoBytesPerSecond;
		s.eVar28 = this.getEventValue('videoBytesPerSecond');
		s.products=";;;;";
		s.events = '';

		/*** The following does the logic of adding events and products ***/

		// send event28 with event17
		this.addEventIfHasOtherEvent('event28', 'event17', true);
		// send event29 with event19
		this.addEventIfHasOtherEvent('event29', 'event19', true);
		// send event18 with event19 if prerollAdStart is sent or to be sent
		if(this.isEventSent('event17') || this.hasEvent('event17')) {
			this.addEventIfHasOtherEvent('event18', 'event19', true);
		}
		// on unload send event21 and event22
		if(pIsUnload) {
			// add event21 if event18 has not been sent
			if(!this.isEventSent('event18') && !this.isEventSent('event21') && this.hasEventValue('event21') && this.getEventValue('event21') >= 0) {
				this.addEvent('event21');
				this.addProduct('event21=' + this.getEventValue('event21'));
				this.setVideoEventName('updateAdPosition ' + this.getEventValue('event21'));
			}
			// add event22 if event20 has not been sent
			if(!this.isEventSent('event20') && !this.isEventSent('event22') && this.hasEventValue('event22') && this.getEventValue('event22') >= 0) {
				this.addEvent('event22');
				this.addProduct('event22=' + this.getEventValue('event22'));
				this.setVideoEventName('updatePosition ' + this.getEventValue('event22'));
			}
		}
		// send prerollAdStart with prerollAdCompleted
		this.addEventIfHasOtherEvent('event17', 'event18', true);
		// send contentClipStart with contentClipCompleted
		this.addEventIfHasOtherEvent('event19', 'event20', true);
		// set video event name on content clip completed
		s.eVar17 = this.getVideoEventName(); // EventName

		// add user agent once
		//this.addUserAgentEvent();

		return s;
	}

	this.alertDebugInfo = function() {
		var propertyArray = new Array('OmniturePlugin.alertDebugInfo()');
		//propertyArray = getObjectProperties(userTracker, 0, 10);
		propertyArray.push('events: ' + this.getEventsAsString(', '));
		propertyArray.push('products: ' + this.getProductsAsString(', '));
		propertyArray.push('videoEventName (eVar17): ' + this.getVideoEventName());
		window.alert(propertyArray.join("\n"));
	}

	this.sendDebugTracking = function(pStructure)
	{
		var params = new Array();
		params.push('products='+pStructure.products);
		params.push('events='+pStructure.events);
		params.push('useragent=' + navigator.appName);
		params.push('saccount=' + s_account);
		params.push('random=' + Math.random() * 100000);
		this.makeDebugTrackingRequest(params);
	}

	this.makeDebugTrackingRequest = function(pParams) { 
		var xmlhttp = null;
		// Mozilla
		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open("GET", '/serien_shows/best_of_formel_eins/videotest/tracker.php?' + pParams.join('&'), true);
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				//document.getElementById('debug').innerHTML = xmlhttp.responseText;
				//window.alert(xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);
	}

	this.addUserAgentEvent = function()
	{
		var eventNumber = 0;
		if (navigator.appName.indexOf("Opera") != -1) {
			eventNumber = 31;
		}
		else if(navigator.appName.indexOf("Explorer") != -1) {
			eventNumber = 32;
		}
		else if(navigator.appName.indexOf("Mozilla") != -1) {
			eventNumber = 34;
		}
		else if(navigator.appName.indexOf("Netscape") != -1) {
			eventNumber = 33;
		}
		else if(navigator.appName.indexOf("Safari") != -1) {
			eventNumber = 35;
		}
		if(eventNumber > 0) {
			eventName = 'event' + eventNumber;
			if(!this.isEventSent(eventName)) {
				this.addEvent('event' + eventNumber);
			}
		}
	}

}
OmniturePlugin.prototype = new VideoTrackerPlugin();

// add to tracker
videoTracker.addPlugin(new OmniturePlugin());

