//*****
// qos.js
//***** 

tpQOSTypeArray = new Array();
var tpQOScurrentPL;
var tpQOSunloading;
function tpRegisterQOSType(eventType, mediaType, callback) {
	for (var i = 1; i < tpQOSTypeArray.length; i++) {if (tpQOSTypeArray[i].eventType == eventType && tpQOSTypeArray[i].mediaType == mediaType && tpQOSTypeArray[i].callback == callback){return;}}	
	var qosTypeObj = {mediaType:mediaType, eventType:eventType, callback:callback}
	tpQOSTypeArray.push(qosTypeObj);
}
function tpUnregisterQOSType(type, callback) {
	for (var i = 1; i < tpQOSTypeArray.length; i++) {
		if (tpQOSTypeArray[i].eventType == eventType && tpQOSTypeArray[i].mediaType == mediaType && tpQOSTypeArray[i].callback == callback) {tpQOSTypeArray.splice(i, 1);break;}
	}
}
function tpReceiveQOSEvent(eventType, mediaType, obj) {
	if (obj == undefined) return;	
	if (eventType == "onClipTime" || eventType == "onPlaylistBegin"){tpQOScurrentPL = obj;}	
	for (var i = 0; i < tpQOSTypeArray.length; i++) {
		if (tpQOSTypeArray[i].eventType == eventType && (mediaType == "ALL" || tpQOSTypeArray[i].mediaType == mediaType || tpQOSTypeArray[i].mediaType == "ALL")) {eval(tpQOSTypeArray[i].callback)(obj);}
	}
}
window.onunload = function() {tpQOSUnload();}
function tpQOSUnload() {if(tpQOScurrentPL == undefined) return;	tpQOSunloading = true;tpReceiveQOSEvent("onPlaylistEnd", "ALL", tpQOScurrentPL);}
function tpQOSSendURL(url) {var connection = new Image();connection.src = url;if (tpQOSunloading) {for (i = 0; ((!connection.complete) && (i < 100000)); i++) {}}}
// qos.js - end 

//*****
// qos_mps.js
//*****
tpRegisterQOSType("onPlaylistEnd", "ALL", "QOSMPS_handlePLEnd");
var QOSMPS_baseUrl = "http://release.theplatform.com/tracker.log"
var QOSMPS_qs = "";
var QOSMPS_baseCount = 0;
var QOSMPS_baseClips = new Array();
function QOSMPS_handlePLEnd(pl) {
	QOSMPS_baseCount = 0;
	QOSMPS_baseClips.length = 0;
	//the first 3 values are always the same
	QOSMPS_qs = "?type=qos&ver=1";
	//let's parse out the query string
	if (pl.currentDate != undefined) QOSMPS_appendStr_qs("d", pl.currentDate);
	if (pl.userName != undefined) QOSMPS_appendStr_qs("un", pl.userName);
	if (pl.countryCode != undefined) QOSMPS_appendStr_qs("cc", pl.countryCode);
	if (pl.regionCode != undefined) QOSMPS_appendStr_qs("rc", pl.regionCode);
	if (pl.browser != undefined) QOSMPS_appendStr_qs("br", pl.browser);
	if (pl.operatingSystem != undefined) QOSMPS_appendStr_qs("os", pl.operatingSystem);
	if (pl.feed != undefined) QOSMPS_appendStr_qs("p", pl.feed);
	if (pl.player != undefined) QOSMPS_appendStr_qs("pl", pl.player);
	if (pl.affiliate != undefined) QOSMPS_appendStr_qs("af", pl.affiliate);
	if (pl.playlistID != undefined) QOSMPS_appendStr_qs("prid", pl.playlistID);
	for (var i = 0;i < pl.clips.length;i++) {QOSMPS_parseClip(pl.clips[i]);}	
	if (QOSMPS_baseClips.length > 1) QOSMPS_parseAggregate(pl.playlistID);QOSMPS_sendData(QOSMPS_qs);
}
function QOSMPS_parseClip(c) {
	var bc = c.baseClip;
	for (var i = 0; i < QOSMPS_baseClips.length; i++) {
		if (QOSMPS_baseClips[i].releaseID == bc.releaseID) {return;}
	}
	QOSMPS_baseClips.push(bc);
	if (bc.releaseID != undefined)  QOSMPS_appendStr_qs("rid" + QOSMPS_baseCount, bc.releaseID);
	if (bc.title != undefined)  QOSMPS_appendStr_qs("t" + QOSMPS_baseCount, bc.title);
	if (bc.author != undefined)  QOSMPS_appendStr_qs("a" + QOSMPS_baseCount, bc.author);
	if (bc.releaseLength != undefined)  QOSMPS_appendStr_qs("l" + QOSMPS_baseCount, bc.releaseLength);
	if (bc.bitrate != undefined)  QOSMPS_appendStr_qs("b" + QOSMPS_baseCount, bc.bitrate);
	if (bc.loadTime != undefined)  QOSMPS_appendStr_qs("lt" + QOSMPS_baseCount, bc.loadTime);
	if (bc.trueLength == undefined && c.baseClip.releaseLength != undefined) bc.trueLength = bc.releaseLength;
	if (bc.lengthPlayed != undefined) {
		var percentPlayed = QOSMPS_round((bc.lengthPlayed / bc.trueLength) * 100, 2);
		if (!isNaN(percentPlayed)) {QOSMPS_appendStr_qs("pp" + QOSMPS_baseCount, percentPlayed);}
	}
	if (bc.rebufferingTime != undefined) {
		var percentRebuff = QOSMPS_round((bc.rebufferingTime / (bc.rebufferingTime + bc.lengthPlayed)) * 100, 2);
		QOSMPS_appendStr_qs("pr" + QOSMPS_baseCount, percentRebuff);
	}
	var percentBW;
	if (bc.connectionBitrate != undefined) {
		if (bc.trueBitrate == undefined && bc.bitrate != undefined) bc.trueBitrate == bc.bitrate / 1000;
		percentBW = QOSMPS_round((bc.connectionBitrate / bc.trueBitrate) * 100, 2);
		if (!isNaN(percentBW)) {if (percentBW > 100) percentBW = 100;}
		else {percentBW = 100;}
	}
	else {percentBW = 100;}
	if (bc.lengthPlayed != undefined && bc.lengthPlayed > 0) {
		bc.percentBW = percentBW;
		QOSMPS_appendStr_qs("pb" + QOSMPS_baseCount, percentBW)
	}
	else {bc.percentBW = undefined;}
	if (bc.lengthPlayed != undefined)  QOSMPS_appendStr_qs("lp" + QOSMPS_baseCount, bc.lengthPlayed);
	QOSMPS_baseCount++;
}
function QOSMPS_parseAggregate(plid) {
	if (plid != undefined) QOSMPS_appendStr_qs("rid" + QOSMPS_baseCount, plid);
	var loadTime = QOSMPS_baseClips[0].loadTime;
	var trueLength = 0;
	var lengthPlayed = 0;
	var percentBW = 100;
	var bwLength = 0;
	var rebuffTime = 0;
	var bwCalcArray = new Array();	
	for (var i = 0; i < QOSMPS_baseClips.length; i++) {
		var bc = QOSMPS_baseClips[i];
		if (bc.trueLength != undefined) trueLength += bc.trueLength;
		if (bc.lengthPlayed != undefined) lengthPlayed += bc.lengthPlayed;
		if (bc.rebufferingTime != undefined) rebuffTime += bc.rebufferingTime;
		if (bc.percentBW != undefined) {
			var bwObj = new Object();
			bwObj.percentBW = bc.percentBW;
			bwObj.trueLength = bc.trueLength;
			bwCalcArray.push(bwObj);
			bwLength += bc.trueLength;
		}
	}	
	var compositeBW = 0;
	if (bwLength > 0) {
		for (var i = 0; i < bwCalcArray.length; i++) {
			var bwObj = bwCalcArray[i];
			var percentTotal = bwObj.trueLength / bwLength;
			compositeBW += bwObj.percentBW * percentTotal
		}
	}
	if (compositeBW > 100) compositeBW = 100;
	if (trueLength > 0) QOSMPS_appendStr_qs("l" + QOSMPS_baseCount, trueLength);
	if (loadTime != undefined) QOSMPS_appendStr_qs("lt" + QOSMPS_baseCount, loadTime);
	if (lengthPlayed > 0 && trueLength > 0) {
		var percentPlayed = QOSMPS_round((lengthPlayed / trueLength) * 100, 2);
		if (!isNaN(percentPlayed)) QOSMPS_appendStr_qs("pp" + QOSMPS_baseCount, percentPlayed);
	}
	if (rebuffTime > 0 && trueLength > 0) {
		var percentRebuff = QOSMPS_round((rebuffTime / (rebuffTime + lengthPlayed))*100, 2);
		if (!isNaN(percentRebuff)) QOSMPS_appendStr_qs("pr" + QOSMPS_baseCount, percentRebuff);
	}
	if (compositeBW > 0) QOSMPS_appendStr_qs("pb" + QOSMPS_baseCount, QOSMPS_round(compositeBW, 2));
	if (lengthPlayed > 0) QOSMPS_appendStr_qs("lp" + QOSMPS_baseCount, lengthPlayed);
}
function QOSMPS_appendStr_qs(name, value) {QOSMPS_qs += "&" + name + "=" + value;}
function QOSMPS_sendData(qString) {
	var theUrl = QOSMPS_baseUrl + qString;
	tpQOSSendURL(theUrl);
	QOSMPS_qs = "";	
}
function QOSMPS_round(num, power) {
	var mult = Math.pow(10, power);
	var raw = num * mult;
	raw = Math.round(raw);
	return raw / mult;
}
// qos_mps.js - end

//*****
// externalPlayerUtil.js 
//*****
var tpTimer;
var tpDuration = 0;
var tpPosition = 0;
var tpBackupUrl = "";
var tpPlayTO;
var tpStarting = true;
var tpIsHidden = false;
var tpClosed = true;
var tpMP;
window.onbeforeunload = tpCheckRefresh;
function tpCheckRefresh() {clearInterval(tpTimer);}
function tpGetTop(obj){result = 0;while (obj) {result += obj.offsetTop;obj = obj.offsetParent;}return result;}
function tpGetLeft(obj) {result = 0;while(obj) {result += obj.offsetLeft;obj = obj.offsetParent;} return result;}
function tpResizeMP(divID, height, width, topDelta, leftDelta) {var element = document.getElementById(divID);element.style.height = height + "px";element.style.width = width + "px";element.style.top = topDelta + "px";element.style.left = leftDelta + "px";element.style.position = "absolute";tpMP = element;}
function tpHideDIV() {tpIsHidden = true;	document.getElementById("externalDiv").style.visibility = "hidden";}
function tpShowDIV(){	tpIsHidden = false;	document.getElementById("externalDiv").style.visibility = "visible";}
var tpPlaying = false;
function tpWMPStateChanged(playerState) {
	if (tpClosed) return;
	switch(playerState) {
		case 0:
			tpCleartpTimer();break;
		case 1:
			tpPlaying = false; tpClearTimer();	break;
		case 2:
			tpPlaying = false; tpClearTimer();	break;
		case 3:
			if(tpStarting) {tpDelayMessage("mediaBegins");tpStarting = false;}
			else {tpDelayMessage("playing");}
			tpRunTimer(); tpPlaying = true; clearTimeout(tpPlayTO);	break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			tpPlaying = true; tpDelayMessage("buffering"); clearTimeout(tpPlayTO);	break;
		case 7:
			break;
		case 8:
			tpPlaying = false; tpClearTimer(); tpDelayMessage("setPlayerDone"); break;
		case 9:
			tpClearTimer(); break;
		case 10:
			tpPlayTO = setTimeout("tpTimedPlay()", 100); break;
		case 11:
			tpClearTimer(); break;
		default:
			tpClearTimer();
	}	
}
function tpDelayMessage(message) {setTimeout("tpSendDelayMessage('" + message + "')", 1);}
function tpSendDelayMessage(message) {tpController.thisMovie("playerwidget").receiveJSMessage(message);}
function tpTimedPlay(){if (tpMP.status != 3 || tpMP.status != 6 && tpBackupUrl != "") {tpMP.URL = tpBackupUrl;}}
function tpExtPlayURL(url) {tpClosed = false;tpBackupUrl = url;tpMP.URL = url;tpStarting = true;if (tpIsHidden) {tpShowDIV();}}
function tpExtSeek(seekPos) {if(!tpPlaying) {tpMP.controls.currentPosition = seekPos;tpMP.controls.play();tpPlaying = true;tpDelayMessage("togglePause");}else {tpMP.controls.currentPosition = seekPos;}}
function tpRunTimer() {tpClearTimer(); tpTimer = setInterval("tpReport()", 1000);}
function tpClearTimer() {clearInterval(tpTimer);}
function tpReport() {
	if(tpMP.currentMedia != null) {
		tpDuration = tpGetDuration(); tpPosition = tpGetPosition();
		if (tpPlaying) {tpController.thisMovie("playerwidget").receiveJSMessage("synchPosition",tpDuration,tpPosition);}
	}
}
function tpGetDuration(){if(tpMP.currentMedia.duration > 0) {return tpMP.currentMedia.duration;}}
function tpGetPosition(){return tpMP.controls.currentPosition;}
function tpExtClose() {tpClosed = true;tpMP.controls.close();tpBackupUrl = "";tpClearTimer();if (!tpIsHidden) {tpHideDIV();}}
function tpExtPauseMovie(state) {if(state){tpMP.controls.pause();}else {tpMP.controls.play();}}
function tpExtSetSoundLevel(level){tpMP.settings.volume = level;}
function tpExtMutePlayer(isMute){
	if(!tpPlaying){
		if(isMute == true){tpMP.settings.mute = true;tpMP.controls.play();tpMP.controls.pause();}
		else{tpMP.settings.mute = false;tpMP.controls.play();tpMP.controls.pause();}
	} else {
		if(isMute == true){tpMP.settings.mute = true;tpMP.controls.pause();tpMP.controls.play();}
		else{tpMP.settings.mute = false;tpMP.controls.pause();tpMP.controls.play();}	
	}
}
function tpExtFullScreen(){tpMP.fullScreen=true;}
// externalPlayerUtil.js - end 