/********************************
********** Videotracker **********
********************************/

function VideoTracker() {
	this.mPluginList = new Array();
	this.mDebugMode = 0;

	this.init = function() {
		this.setUnloadHandler();
	}

	this.setUnloadHandler = function() {
		this.debug('VideoTracker.setUnloadHandler');
		if(window.addEventListener) {
			window.addEventListener("unload", videoTrackerUnloadCallback, false);
		}
		else if(window.attachEvent) {
			window.attachEvent("onunload", videoTrackerUnloadCallback);
		}
	}

	this.trackVideoPlayerEvent = function(pArgs) {
		this.debug('VideoTracker.trackVideoPlayerEvent(' + pArgs.join(', ') + ')');
		for(var i = 0; i < this.mPluginList.length; i++) {
			var plugin = this.mPluginList[i];
			plugin.trackVideoPlayerEvent(pArgs);
		}
	}

	this.processUnload = function() {
		this.debug('VideoTracker.processUnload');
		for(var i = 0; i < this.mPluginList.length; i++) {
			var plugin = this.mPluginList[i];
			plugin.processUnload();
		}
	}

	this.setDebugMode = function(pMode) {
		this.mDebugMode = pMode;
		for(var i = 0; i < this.mPluginList.length; i++) {
			var plugin = this.mPluginList[i];
			plugin.setDebugMode(pMode);
		}
	}

	this.addPlugin = function(pPluginInstance) {
		this.debug('VideoTracker.addPlugin(' + pPluginInstance + ')');
		this.mPluginList.push(pPluginInstance);
		pPluginInstance.init();
		//pPluginInstance.setDebugMode(this.mDebugMode);
	}

	this.debug = function(pText) {
		if(this.mDebugMode) {
			window.alert(pText);
		}
	}
}

function VideoTrackerPlugin() {
	// debug mode
	// 0: no debugging
	// 1: alerts
	this.mDebugMode = 0;

	/**
	* Callback method for video player.
	* pArgs: array of strings
	**/
	this.trackVideoPlayerEvent = function(pArgs) {
	}

	/**
	* Initializes plugin.
	**/
	this.init = function() {
	}

	/**
	* Callback method for tracking on unload.
	**/
	this.processUnload = function() {
	}

	this.setDebugMode = function(pMode) {
		this.mDebugMode = pMode;
	}

	this.debug = function(pText) {
		if(this.mDebugMode) {
			window.alert(pText);
		}
	}
}

// callback: unload
function videoTrackerUnloadCallback()
{
	videoTracker.processUnload();
}

// callback: videoplayer event
function trackExtended(pArgs) {
	videoTracker.trackVideoPlayerEvent(pArgs.split(","));
}

var videoTracker = new VideoTracker();
videoTracker.init();
videoTracker.setDebugMode(0);
