var wt_PlayerStatus;
var wt_PlayerStatusMsg;
var wt_ClipPosition;
var	wt_ClipLength;
var wt_stVersion = 178;

function wt_sendinfo_media(wt_mi,mk,mt1,mt2,mg){
	if (typeof(mg)!='undefined'){ 
		mg = wt_stEscape(mg);
	} else {
		mg='';
	}
	wt_stQuicksend('st&mi=' + wt_stEscape(wt_mi) + '&mk=' + mk + '&mt1=' + mt1 + '&mt2=' + mt2 + '&mg=' + mg + wt_stEncoding() );
}

function wt_stQuicksend (params, script) {
	if (!wt_dm || !wt_ci) return;
	if (!script) script="wt.pl";
	var wt_url=(location.protocol=="https:"?"https:":"http:")+"//"+wt_dm+"/"+wt_ci+"/"+script+"?p="+wt_stVersion+","+params;
	if (document.images) {
		if (typeof(wt_i) == "undefined") wt_i = new Array ();
		var ii = wt_i.length;
		wt_i[ii] = new Image ();
		wt_i[ii].src = wt_url;
		
	} else {
		document.write("<img src='"+wt_url+"' height='1' width='1'>");
	}
}
function wt_stEscape(u) {
	if (typeof(encodeURIComponent) == 'function') return encodeURIComponent(u);
	return escape(u);
}
function wt_stEncoding () {
	var tmp = "";
	if (typeof(encodeURIComponent) != 'function') tmp += "&enc0=old";
	tmp += "&enc1=ü&enc2=" 
	if (document.characterSet) tmp += document.characterSet.toLowerCase();
	if (document.charset) tmp += document.charset.toLowerCase();
	return tmp;
}

wtStreamPlayer = [
	// Windows Media Player
	{
		name: 'Windows Media Player',
		shortname: 'wm',
		actX: [
			'WMPlayer.OCX'
		],
		mimes: [
			'application/asx',
			'application/x-mplayer2',
			'video/x-ms-asf',
			'video/x-ms-asf-plugin',
			'audio/x-ms-wax',
			'video/x-ms-wm',
			'audio/x-ms-wma',
			'video/x-ms-wmv',
			'video/x-ms-wvx'
		],
		msgs: {
			"1": "stop",
			"2": "pause",
			"3": "play",
			"8": "eof"
		},
		getStatus: function () {
			//return this.msgs[wtStreamTrekk.player.PlayState];
			x = 'document.'+wtStreamTrekk.player+'.playState';
			return eval(x);
		},
		getPosition: function () {
			// current position in seconds
			x = 'document.'+wtStreamTrekk.player+'.controls.currentPosition';
			return Math.floor(eval(x));
		},
		getClipLength: function () {
			//return wtStreamTrekk.player.duration; // seconden
			if (!wtStreamTrekk.player){
				window.setTimeout("wtStreamPlayer[wtStreamTrekk.wtspid].getClipLength()",10);
			} else {
				// Realplayer returns millisec, so we have to convert into seconds
				x = 'document.'+wtStreamTrekk.player+'.currentMedia.duration';
				return Math.floor(eval(x));
			}
		}
	},
	// Real Player
	{
		name: 'RealPlayer',
		shortname: 'rp',
		actX: [
			'rmocx.RealPlayer G2 Control'
		],
		mimes: [
			'audio/x-pn-realaudio',
			'audio/x-pn-realaudio-plugin'
		],
		msgs: {
			"0": "stop",
			"3": "play",
			"4": "pause"
		},
		getStatus: function () {
			//return this.statusMsgs[wtStreamTrekk.player.GetPlayState()];
			x = 'document.'+wtStreamTrekk.player+'.GetPlayState()';
			return eval(x);
		},
		getPosition: function () {
			// Realplayer returns millisec, so we have to convert into seconds
			x = 'document.'+wtStreamTrekk.player+'.GetPosition()';
			return Math.floor(eval(x)/1000);
		},
		getClipLength: function () {
			if (!wtStreamTrekk.player){
				window.setTimeout("wtStreamPlayer[wtStreamTrekk.wtspid].getClipLength()",10);
			} else {
				// Realplayer returns millisec, so we have to convert into seconds
				x = 'document.'+wtStreamTrekk.player+'.GetLength()';
				return Math.floor(eval(x)/1000);
			}
		}
	},
	// Apple Quicktime
	{
		name: 'QuickTime',
		shortname: 'qt',
		actX: [
			'QuickTime.QuickTime'
		],
		mimes: [
			'video/quicktime',
			'video/x-m4v'			
		],
		msgs: {
			"0": "pause",
			"1": "play"
		},
		getStatus: function () {
			//return this.statusMsgs[wtStreamTrekk.player.GetRate()];
			x = 'document.'+wtStreamTrekk.player+'.GetRate()';
			return eval(x);
		},
		getPosition: function () {
			//return wtStreamTrekk.player.GetTime();
			x = 'document.'+wtStreamTrekk.player+'.GetTime()';
			return Math.floor(eval(x)/1000);
		},
		getClipLength: function () {
			//return wtStreamTrekk.player.GetEndTime();
			if (!wtStreamTrekk.player){
				window.setTimeout("wtStreamPlayer[wtStreamTrekk.wtspid].getClipLength()",10);
			} else {
				// Realplayer returns millisec, so we have to convert into seconds
				x = 'document.'+wtStreamTrekk.player+'.GetEndTime()';
				return Math.floor(eval(x)/1000);
			}
		}
	}
];

wtStreamTrekk = {
	player: null,
	wt_PlayerStatus: 0,
	wt_PlayerStatusMsg: null,
	wt_ClipPosition: 0,
	wt_ClipLength: 0,
	wtspid: null,
	isIE: navigator.userAgent.indexOf('MSIE') != -1,

	init: function () {
		this.player = wt_mpid; 
		if (typeof(wt_mp) != 'string' || wt_mp == ''){
			window.setTimeout("wtStreamTrekk.detectPlayer()",10);
		} else {
			switch (wt_mp){
				case 'rm': // realplayer
					this.wtspid = 1;
					break;
				case 'qt': // quicktime
					this.wtspid = 2;
					break;
				case 'wm': // windows mediaplayer
					this.wtspid = 0;
					break;
			}
		}
		if (this.wtspid != null && this.detectPlugin()){
			window.setTimeout("wtStreamTrekk.check()",100);
		}
	},
	check: function () {
		if (!this.wt_ClipLength || this.wt_ClipLength == 0){
			this.wt_ClipLength = wtStreamPlayer[this.wtspid].getClipLength();
			window.setTimeout("wtStreamTrekk.check()",10);
			return;
		}
		window.setInterval("wtStreamTrekk.checkPlayerStatus()",1000);
		window.setInterval("wtStreamTrekk.setCurrentPosition()",100);
		this.wtRegisterEvent (window, (this.isIE && window.onbeforeunload) ? "beforeunload" : "unload" , this.sendPixelOnUnload); 
	},
	checkPlayerStatus: function (){
		if( !wt_PlayerStatusMsg ){
			wt_PlayerStatusMsg = 'init';
			wt_ClipPosition = this.wt_ClipPosition;
			wt_ClipLength = this.wt_ClipLength;
			this.sendPixel();
		}
		if (this.wt_PlayerStatus != wtStreamPlayer[this.wtspid].getStatus()){
			this.wt_PlayerStatus = wtStreamPlayer[this.wtspid].getStatus();
			this.wt_PlayerStatusMsg = wtStreamPlayer[this.wtspid].msgs[this.wt_PlayerStatus];
			if ( (this.wt_PlayerStatusMsg == 'stop' || this.wt_PlayerStatusMsg == 'pause') && this.wt_ClipPosition == this.wt_ClipLength ) this.wt_PlayerStatusMsg = 'eof';
			wt_PlayerStatusMsg = this.wt_PlayerStatusMsg;
			wt_ClipPosition = this.wt_ClipPosition;
			wt_ClipLength = this.wt_ClipLength;
			this.sendPixel();
		} 
	},
	setCurrentPosition: function () {
		if (wtStreamPlayer[this.wtspid].getPosition() > 0){
			this.wt_ClipPosition = wtStreamPlayer[this.wtspid].getPosition();
			wt_ClipPosition = this.wt_ClipPosition;
		}
	},
	detectPlugin: function () {
		plug = false;
		if (this.isIE){ 
			 eval ("try { p = new ActiveXObject(wtStreamPlayer[this.wtspid].actX[0]) }catch(e){};");
			if (typeof(p) == 'object') plug = true;
		} else {
			if ((navigator.mimeTypes) && (navigator.mimeTypes.length > 0)) {
				for (a = 0; a < navigator.mimeTypes.length; a++) {
					for (aa = 0; aa < wtStreamPlayer[this.wtspid].mimes.length; aa++){
						m = wtStreamPlayer[this.wtspid].mimes[aa];
						if (navigator.mimeTypes[a].type.indexOf(m) >=0) {
							plug = true;
							break;
						}
					}
				}
			}
		}
		return plug;
  },
	detectPlayer: function () {
		p = false;
		for (z=0; z<wtStreamPlayer.length; z++){
			if (!document.layers && document.getElementById) {
				eval ("try { p = wtStreamPlayer[z].getStatus() }catch(e){};");
			}
			if (p){
				wt_mp = wtStreamPlayer[z].shortname;
				wtStreamTrekk.init();
				return;
			}
		}
		wtStreamTrekk.init();
	},
	wtRegisterEvent: function (obj, e, f) {
		if (obj.addEventListener) {
			obj.addEventListener (e, f, false);
		} else {
			if (obj.attachEvent) {
				obj.attachEvent ("on"+e, f);
			}
		}
	},
	sendPixel: function () {
		if (wt_PlayerStatusMsg != '' && typeof(wt_PlayerStatusMsg) != 'undefined'){
			// send only defined messages
			wt_stQuicksend('st&mi=' + wt_stEscape(wt_mi) + '&mk=' + wt_PlayerStatusMsg + '&mt1=' + wt_ClipPosition + '&mt2=' + wt_ClipLength + '&mg=' + wt_stEscape(wt_mg) + wt_stEncoding() );
			if (wt_PlayerStatusMsg == 'stop'){
				this.wt_ClipPosition = 1;
				wt_ClipPosition = 1;
			}
		}
	},
	sendPixelOnUnload: function () {
		if (wt_ClipPosition > 0 && wt_ClipPosition != wt_ClipLength){
			wt_stQuicksend('st&mi=' + wt_stEscape(wt_mi) + '&mk=stop&mt1=' + wt_ClipPosition + '&mt2=' + wt_ClipLength + '&mg=' + wt_stEscape(wt_mg) + wt_stEncoding() );
		}
	}
}	

if (typeof(wt_mpid) == 'string' && wt_mpid != '') {
	wtStreamTrekk.init();
}
