function playerAPI(){
	this.player = new Array();
	this.mediaid = new Array();
	this.playerPath = "/mods/collections/scripts/player.swf";
	this.activePlayer = 0;
	this.uniqueUserId = 0;
	this.listener = new Array();
	this.sendLock = true;
	this.sendQueue = false;
	this.autoplay = "false";
	this.logo = null;
	this.sendURL = "/mods/collections/scripts/mediaStatistics.php";
	this.sendTriggers = "PLAY,LOAD,UNLOAD";//accepted params(comma seperated, ALL CAPS): ALL,NONE,FULLSCREEN,MUTE,PLAY,STOP,SEEK,VOLUME,LOAD,UNLOAD
	this.timestamp = function(){
		var date = new Date();
		var time = "";
		time += date.getFullYear()+"-";
		if((date.getMonth()+1)<10)
			time += "0"+(date.getMonth()+1)+"-";
		else
			time += (date.getMonth()+1)+"-";
		if(date.getDate()<10)
			time += "0"+date.getDate()+" ";
		else
			time += date.getDate()+" ";
		if(date.getHours()<10)
			time += "0"+date.getHours()+":";
		else
			time += date.getHours()+":";
		if(date.getMinutes()<10)
			time += "0"+date.getMinutes()+":";
		else
			time += date.getMinutes()+":";
		if(date.getSeconds()<10)
			time += "0"+date.getSeconds()+"\t";
		else
			time += date.getSeconds()+"\t";
		return time;
	};
	this.getTrigger = function(str){
		if(this.sendTriggers=="ALL") return true;
		var triggers = this.sendTriggers.split(",");
		for(var i=0;i<triggers.length;i++){
			if(triggers[i]==str)
				return true;
		}
		return false;
	};
	this.generateUniqueUserId = function(){
		var date = new Date();
		this.uniqueUserId = date.getTime()+"_"+(Math.floor(Math.random()*9999+1)+1000);
	};
	this.playerReady = function(obj){
		var id = this.player.length;
		this.player[id] = document.getElementById(obj['id']);
		this.viewEvent(id);
		return id;
	};
	this.load = function(id,str,media_id){
		this.mediaid[id] = media_id;
		this.listener = new Array();
		this.player[id].sendEvent("LOAD",str);
		this.listener[this.listener.length] = this.timestamp()+"loaded";
		if(this.getTrigger("LOAD"))
			this.send();
	};
	this.play = function(id){
		this.player[id].sendEvent("PLAY","true");
	};
	this.pause = function(id){
		this.player[id].sendEvent("PLAY","false");
	};
	this.stop = function(id){
		this.player[id].sendEvent("STOP","true");
	};
	this.viewEvent = function(id){
		this.player[id].addViewListener("FULLSCREEN","longTail.viewEvent_FULLSCREEN");
		this.player[id].addViewListener("MUTE","longTail.viewEvent_MUTE");
		this.player[id].addViewListener("PLAY","longTail.viewEvent_PLAY");
//		this.player[id].addControllerListener("STOP","longTail.controllerEvent_STOP");
		this.player[id].addViewListener("LOAD","longTail.viewEvent_LOAD");
		this.player[id].addViewListener("SEEK","longTail.viewEvent_SEEK");
		this.player[id].addViewListener("VOLUME","longTail.viewEvent_VOLUME");
	};
	this.controllerEvent_STOP = function(){
		this.listener[this.listener.length] = this.timestamp()+"end of stream";
		if(this.getTrigger("STOP"))
			this.send();
	};
	this.viewEvent_FULLSCREEN = function(obj){
		if(obj['state'])
			this.listener[this.listener.length] = this.timestamp()+"fullscreen on";
		else
			this.listener[this.listener.length] = this.timestamp()+"fullscreen off";
		if(this.getTrigger("FULLSCREEN"))
			this.send();
	};
	this.viewEvent_MUTE = function(obj){
		if(obj['state'])
			this.listener[this.listener.length] = this.timestamp()+"mute on";
		else
			this.listener[this.listener.length] = this.timestamp()+"mute off";
		if(this.getTrigger("MUTE"))
			this.send();
	};
	this.viewEvent_PLAY = function(obj){
		if(obj['state'])
			this.listener[this.listener.length] = this.timestamp()+"play";
		else
			this.listener[this.listener.length] = this.timestamp()+"pause";
		if(this.getTrigger("PLAY"))
			this.send();
	};
	this.viewEvent_STOP = function(){
		this.listener[this.listener.length] = this.timestamp()+"stop";
		if(this.getTrigger("STOP"))
			this.send();
	};
	this.viewEvent_LOAD = function(){
		this.listener[this.listener.length] = this.timestamp()+"loaded";
		if(this.getTrigger("LOAD"))
			this.send();
	};
	this.viewEvent_SEEK = function(obj){
		this.listener[this.listener.length] = this.timestamp()+"jump to position "+obj['position']+" seconds";
		if(this.getTrigger("SEEK"))
			this.send();
	};
	this.viewEvent_VOLUME = function(obj){
		this.listener[this.listener.length] = this.timestamp()+"volume set at "+obj['percentage']+"%";
		if(this.getTrigger("VOLUME"))
			this.send();
	};
	this.init = function(div,w,h,file,img,mediaid){
		var flashvars = {
			playerready:"longTail.playerReady",
			autostart:this.autoplay,
			logo:this.logo
		}
		var params = {
			allowfullscreen:"true", 
			allowscriptaccess:"always"
		}
		var attributes = {
			id:"mpl"+this.player.length,  
			name:"mpl"+this.player.length,
			wmode:"transparent"
		}
		if(typeof(div)=="undefined" || div==false){
			div = "mplWrapper"+this.player.length;
			document.write("<div id=\""+div+"\"></div>");
		}
		if(typeof(w)=="undefined" || w==false) w = 400;
		if(typeof(h)=="undefined" || h==false) h = 200;
		if(typeof(file)!="undefined" && file!=false) flashvars.file = file;
		if(typeof(img)!="undefined" && img!=false) flashvars.image = img;
		if(typeof(mediaid)=="undefined" && mediaid==false) mediaid = 0;
		this.mediaid[this.player.length] = mediaid;
//		swfobject.embedSWF(this.playerPath, div, w, h, "9.0.115", false, flashvars, params, attributes);
		jwplayer(div).setup({
			flashplayer: this.playerPath,
			file: flashvars.file,
			height: h,
			width: w
		});
		return this.player.length;
	};
	this.send = function(){
		if(this.sendLock && this.listener.length>0){
			this.sendLock = false;
			this.sendQueue = false;
			var content = "&user_id="+this.uniqueUserId+"&media_id="+this.mediaid[this.activePlayer]+"&log=";
			for(var i=0;i<this.listener.length;i++)
				content += this.listener[i]+"\n";
			if(window.XMLHttpRequest)
				a=new XMLHttpRequest();
			else if(window.ActiveXObject)
				a=new ActiveXObject("Microsoft.XMLHTTP");
			a.open("post",this.sendURL,true);
			a.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			a.setRequestHeader("Content-length", content.length);
			a.setRequestHeader("Connection", "close");
			a.onreadystatechange=function(){
				if(a.readyState==4){
					longTail.sendLock = true;
					if(longTail.sendQueue)
						longTail.send();
				}
			};
			a.send(content);
		}
		else
			this.sendQueue = true;
	};
	this.unload = function(){
		this.listener[this.listener.length] = this.timestamp()+"left the page";
		if(this.getTrigger("UNLOAD"))
			this.send();
	};
}
var longTail = new playerAPI();			//initiate API
longTail.generateUniqueUserId();		//generates unique session id
unload[unload.length] = "longTail.unload();";	//enables the unload event

