var tmp49int = 0;
var tmp49intTime = null;
var tmp49intMultiplier = 1;
var startSeekTime;
var tempTime;
var tempVolume;
var player;
var clipURL = null;
var tempTime = null;
var plst = null;
var tempDescr = null;
var tempDescrLength = null;
var tempTitle = null;
var tempVidtype = null;
var tempCategory = null;
var tempSubcategory = null;
var tempThumb = null;
var tmpXMLinfo = null;
var plstItem = null;
var tempSeekBoolean = false;
var tempSeekto = null;
var tempBuffBoolean = null;

function plstTracker(obj){
	plstItem = obj.index;
	tempDescr = plst[plstItem].description;
	tempDescrLength = tempDescr.length;
	tempVidtype = tempDescr.substr(0, tempDescr.indexOf(","));
	tempCategory = tempDescr.substr(tempDescr.indexOf(",") + 1, (tempDescr.lastIndexOf(",") - tempDescr.indexOf(",") - 1));
	tempSubcategory = tempDescr.substr(tempDescr.lastIndexOf(",") + 1, (tempDescrLength - tempDescr.lastIndexOf(",")));
	tempTitle = plst[plstItem].title;
	clipURL = plst[plstItem].File;
	tempThumb = plst[plstItem].image
	tmpXMLinfo = '<uurl>' + top.location.href + '</uurl>' + '<length>' + Math.round(plst[plstItem].duration) + '</length>';
	tmpXMLinfo += '<title>' + tempTitle + '</title>';
	if (tempVidtype == '') {tempVidtype = "content";}
	tmpXMLinfo += '<vidtype>' + tempVidtype + '</vidtype>';
	if (tempCategory != '') {tmpXMLinfo += '<category>' + tempCategory + '</category>';};
	if (tempSubcategory != ''){tmpXMLinfo += '<subcategory>' + tempSubcategory + '</subcategory>';};
	if (tempThumb != ''){tmpXMLinfo += '<imgurl>' + tempThumb + '</imgurl>';};
	tmp49intTime = Math.round(plst[plstItem].duration / tmp49int);
};

function timeTracker(obj) {
	tempTime = obj.position;
	if (tmp49int > 1) {
		if ((tempTime > (tmp49intTime * tmp49intMultiplier))){
			gg1.ggPM(49, Math.round(tempTime));		
			tmp49intMultiplier+=1;
		};
	};
	if (tempSeekBoolean == true){
		if (tempSeekto > tempTime){
			gg1.ggPM(5, Math.round(tempTime));
		}
		tempSeekto = null;
		tempSeekBoolean = false;
	};
};

function playTracker(obj) {
	if (obj.newstate == "PLAYING" && obj.oldstate == "PAUSED") {
		gg1.ggPM(5, Math.round(tempTime));
		 
	};
	
	if ((obj.newstate == "PLAYING") && (obj.oldstate != "PAUSED") && (tempBuffBoolean != true))
	{
		gg1.ggPM(15, plst[plstItem].file, tempVidtype, tmpXMLinfo);
//		gg1.ggPM(5, Math.round(tempTime));
	};
	tempBuffBoolean = false;
	if (obj.newstate == "BUFFERING" && obj.oldstate == "PLAYING") {
		tempBuffBoolean = true;
		};
	if (obj.newstate == "PAUSED") {
		gg1.ggPM(6, Math.round(tempTime));
		};
	if (obj.newstate == "IDLE") {
		gg1.ggPM(7, Math.round(tempTime));
		};
	};

function seekTracker(obj) {
	tempSeekto = Math.round(obj.position);
	gg1.ggPM(8, Math.round(tempTime), tempSeekto);
	if (tmp49int > 1) { tmp49intMultiplier = Math.round(tempSeekto / tmp49int)};
	tempSeekBoolean = true;
};

function muteTracker(obj) {
	gg1.ggPM(9, obj.state);
};

function fscreenTracker(obj) {
	gg1.ggPM(10, obj.fullscreen);
};
function volumeTracker(obj){
	gg1.ggPM(11, obj.percentage);
};

function errTracker(obj){
	alert(obj.message);
};

function playerReady(obj) {
		
	gg1.ggPM(51, "http://www.test.com");
	player = document.getElementById(obj['id']);
	player.addModelListener("STATE","playTracker");
	player.addModelListener("TIME","timeTracker");
	player.addViewListener("SEEK","seekTracker");
	player.addControllerListener("RESIZE","fscreenTracker");
	player.addControllerListener("ITEM","plstTracker");
	player.addControllerListener("VOLUME","volumeTracker");
	player.addViewListener("MUTE","muteTracker");
//	player.addModelListener("ERROR","errTracker"); // DEBUG ONLY!
	plst = player.getPlaylist();
};
