/******************************************************************/
/* � DMC 01 Consulting & Development GmbH - gerald.urbas@dmc01.at */
/******************************************************************/
/* Player Prototype                                               */
/******************************************************************/
/* is used by player plugins to save general funcitons and vars   */
/* provides replace funtion for finishing HTML strings            */
/******************************************************************/

/*
Prototype for different Player objects
*/

function playercode() {
	// define Object vars
	this.name = "";
	this.typ = "";
	this.detectionplugin = "";
	this.objid = "";
	this.globid = false;
	this.playerstring = "";
	this.playercontrols="";
	this.playertype=false;
	this.version = false;
	this.livestream = false;
	this.mp3stream = false;
	this.enabled = false;
	this.vsizes=false;
	this.autostart=false;
	this.enablebuffering=true;
	this.is_scriptable = false;
	this.flashstreamingserver=false;
	this.FLASHEMBEDW="";
	this.FLASHEMBEDH="";
	this.FLASHEMBEDHQW="";
	this.FLASHEMBEDHQH="";
	// define Object vars

}
function cutflv(tfilename) {
					var streamingurl = new RegExp('.flv$','g');
					var su = streamingurl.exec(tfilename);
					if (su=='.flv') {
						filename = tfilename.substr(0,(tfilename.length-4));
						this.debug("Streaming URL cut .flv on End of stream");
					} else {
						filename = tfilename
						this.debug("Streaming URL .flv isnt provided");
					}
					return filename;
	}
	
function cutmp3(tfilename) {
					var streamingurl = new RegExp('.mp3$','g');
					var su = streamingurl.exec(tfilename);
					if (su=='.mp3') {
						filename = tfilename.substr(0,(tfilename.length-4));
						this.debug("Streaming URL cut .mp3 on End of stream");
					} else {
						filename = tfilename
						this.debug("Streaming URL .mp3 isnt provided");
					}
					var streamingurl = new RegExp('.MP3$','g');
					var su = streamingurl.exec(tfilename);
					if (su=='.MP3') {
						filename = tfilename.substr(0,(tfilename.length-4));
						this.debug("Streaming URL cut .MP3 on End of stream");
					} else {
						filename = tfilename
						this.debug("Streaming URL .MP3 isnt provided");
					}
					return filename;
	}
// public replace funtion
function replaceVars(pstring,conn) {
		var filename="";
		var server = "";
		var myurl = this.connurls[conn];
		if (this.typ=='flashmedia') {
			dbg("Flash Streaming URL cuttin started url:("+myurl+")");
			if (this.flashstreamingserver) {
				dbg("Streamingserver info is avaible!")
				server = this.flashstreamingserver;
				var re = new RegExp(this.flashstreamingserver,'g');
				var m = re.exec(myurl);
				if (m == null) {
					dbg("Perfect streaming Url combination provided")
					filename = this.cutflv(myurl);
					filename = this.cutmp3(filename);
					this.debug("Streaming URL OHNE Server"+ server+" "+filename);
				} else {
					dbg("Streaming server provided in URL, cut the Serverstring")
					var serverindex = myurl.indexOf('://');
					var tserver = myurl.substr(serverindex+3);
					filename =  this.cutflv(tserver.replace(re,""));
					filename =  this.cutmp3(filename);
					this.debug("Streaming URL mit Server"+ server+" "+filename);
				}
			} else {
				dbg("No streamingserver provided")
				var serverindex = myurl.indexOf('://');			
				var filenameindex = myurl.lastIndexOf('/');
				var tfilename = myurl.substr(filenameindex+1)
				var tserver = myurl.substr(serverindex+3);
				var t2server = tserver.substr(0,(tserver.length-tfilename.length));
				var re = new RegExp("http://",'g');
				var m = re.exec(myurl);
				if (m == null) {
					server = t2server;
					filename = this.cutflv(tfilename);
					this.debug("Streaming URL Ohne benannten FMS "+ server+" "+filename);
				}else{
					server = "";
					filename = myurl
					this.debug("HTTP File DL "+ server+" "+filename);
				}
			}
			if (filename=='' && server=='') {
				this.debug("ALERT: FMS Streaming URLS failed to parse!");
			}
		}

		// !! hack, added on 5.6.2008 !!
		if (myurl.indexOf('.asx') != -1) {
					
			filename = myurl;
		} else {
		}

		var m3uindex = myurl.lastIndexOf('.m3u');
		var mp3index = myurl.lastIndexOf('.mp3');
		var mp3index_upp = myurl.lastIndexOf('.MP3');
		var isaudio = "0";
		var audiomode = 3;
		var ismp3 = "0";
		var islive = "0";
		
		if (mp3index!=-1 || mp3index_upp!=-1 || m3uindex!=-1) {
			ismp3 = "1";
			isaudio = "1";
			audiomode = 3;
		}

		if (this.flashstreamingserver && (mp3index!=-1 || mp3index_upp!=-1)) {
			ismp3 = "0";
			alert(mp3index+'??'+mp3index_upp)
		}

		if (this.livestream == true) {
			islive = "1";
		}
		if (this.mp3stream == true) {
			isaudio = "1";
			if (this.livestream == false) {
				audiomode = 0;
				islive="0";
			}
			if (this.livestream == false) {
				audiomode = 3;
				islive="0";
			}
			if (this.livestream == true) {
				audiomode = 3;
				islive="1";
			}
			if (this.playertype == 'simple') {
				audiomode = 3;
			}
		}
		this.previewimage = this.getPreviewimage();
		//alert("params: this.playertype "+this.playertype+" this.livestream "+this.livestream+" this.mp3stream"+this.mp3stream);
		pstring=pstring.replace(/%FLASHEMBEDW%/g,this.FLASHEMBEDW); // special for flashplayer
		pstring=pstring.replace(/%FLASHEMBEDH%/g,this.FLASHEMBEDH); // special for flashplayer
		pstring=pstring.replace(/%FLASHEMBEDHQW%/g,this.FLASHEMBEDHQW); // special for flashplayer
		pstring=pstring.replace(/%FLASHEMBEDHQH%/g,this.FLASHEMBEDHQH); // special for flashplayer
		pstring=pstring.replace(/%FLASHEMBEDSIMPLEW%/g,this.FLASHEMBEDSIMPLEW); // special for flashplayer
		pstring=pstring.replace(/%FLASHEMBEDSIMPLEH%/g,this.FLASHEMBEDSIMPLEH); // special for flashplayer
		pstring=pstring.replace(/%FLASHEMBEDAUDIOW%/g,this.FLASHEMBEDAUDIOW); // special for flashplayer
		pstring=pstring.replace(/%FLASHEMBEDAUDIOH%/g,this.FLASHEMBEDAUDIOH); // special for flashplayer
		pstring=pstring.replace(/%PREIMG%/g,this.previewimage); // special for flashplayer
		pstring=pstring.replace(/%AUTOSTART%/g,this.autostart); // special for flashplayer
		pstring=pstring.replace(/%W%/g,this.vsizes[conn]['w']); // movie size W
		pstring=pstring.replace(/%H%/g,this.vsizes[conn]['h']); // movie size H
		pstring=pstring.replace(/%EMBEDH%/g,(this.vsizes[conn]['h']+this.playercontrolsheight[this.playertype]['embed'])); // RP + MMP needs some correction of the output size
		pstring=pstring.replace(/%OBJECTH%/g,(this.vsizes[conn]['h']+this.playercontrolsheight[this.playertype]['object'])); // RP + MMP needs some correction of the output size
		pstring=pstring.replace(/%SERVER%/g,server);
		pstring=pstring.replace(/%FILENAME%/g,filename);
		pstring=pstring.replace(/%URL%/g,myurl);
		pstring=pstring.replace(/%ISLIVE%/g,islive);
		pstring=pstring.replace(/%AUDIO%/g,isaudio);
		pstring=pstring.replace(/%ISMP3%/g,ismp3);
		pstring=pstring.replace(/%MODE%/g,audiomode);
		pstring=pstring.replace(/%BUFFER%/g,this.enablebuffering);
		pstring=pstring.replace(/%VERSION%/g,this.version);
		pstring=pstring.replace(/%EMBEDCONTROLS%/g,this.playercontrols[this.playertype]['embed']); // 4 embeds replace special player setteing (window, controls etc)
		pstring=pstring.replace(/%OBJECTCONTROLS%/g,this.playercontrols[this.playertype]['object']); // 4 objects replace special player setteing (window, controls etc)
		//alert(pstring+" OURL:"+this.connurls[conn]);
		//this.debug(escape(myurl));
		//this.debug(pstring)
		return pstring; // return finished string...
}

// templatefunction for Player types
function writeplayercode(targetobjid,conn) {
		var pstring = this.playerstring;
		pstring=this.replace(pstring,conn);
		$j("#"+targetobjid).empty();
		$j("#"+targetobjid).append(pstring);
		//alert("auto"+ this.autostart+" eb"+this.enablebuffering+" pi"+this.previewimage);
		//alert("pt: "+ this.name+"auto"+ this.autostart+" eb"+this.enablebuffering+" pi"+this.previewimage);
		if (this.name!='Flash' && this.autostart==0 && this.enablebuffering==0 && this.previewimage!= false) {
			//alert('add Image')
			this.previewImage(targetobjid,conn);
		} else {
			this.playercall(targetobjid,conn);
		}
} 

function previewImagecall(targetobjid,conn) {
	var gid = this.getglobid();
	$j("#"+targetobjid).empty();
	
	$j("#"+targetobjid).append('<span id="genplayer'+gid+'" src="'+this.previewimage+'" />');
	$j("#genplayer"+gid).append('<IMG src="'+this.previewimage+'" /><br /><IMG src="/euro2008/layout/pics/gen/playvideo.gif" />');
	$j("#genplayer"+gid).attr('globid',gid);
	$j("#genplayer"+gid).attr('tgt',targetobjid);
	$j("#genplayer"+gid).attr('conn',conn);
	//alert($j("#"+targetobjid).attr('globid'))
	$j("#genplayer"+gid).click(function () {
		var mygid = $j(this).attr('globid');
		var mytgt = $j(this).attr('tgt');
		var myconn = $j(this).attr('conn');
		var myplayer = loaded_Players[mygid];
		myplayer.players[myplayer.currentplayer].autostart=true;
		myplayer.players[myplayer.currentplayer].playercall(mytgt,myconn);
	});
}

// templatefunction for setting sizes
function setsizes(arr) {
	s = Array();
	for (as in this.getconn()) {
		s.push(arr[as]);
	} 
	this.vsizes = arr;
}

function setconn(urls) {
		if (urls.length>0) {
			dbg(urls.length + "URL avaible:");
			this.connurls=urls;
		} else {
			dbg("no URL avaible:"+urls.length+" :"+this.name+" is disabled");
			this.enabled = false;
		}
}

function setplayertype(pt,v,l,a,fms,b) {
	this.playertype=pt;
	this.version = v;
	this.livestream = l;
	this.mp3stream = a;
	this.flashstreamingserver = fms;
	if (b==true)
	{
		this.enablebuffering = 1;
	} else {
		this.enablebuffering = 0;
	}

}

function setautostart(bool) {
	if (bool == true) {
		current_Player = this.globid;
		current_PlayerDomId = this.objid;
		this.autostart = "1";
	}else {
		this.autostart = "0";
	}
}

function setglobid(str) {
	this.globid = str;
}

function getglobid() {
	return this.globid;
}


function playerPopup() {
	this.debug("Open Popup");
	obj_f_popup = window.open('popup.html','fpopup','directories=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,location=no,hotkeys=no');
	//popup.player = player;
	obj_f_popup.focus();
}

function carrierPopup(h) {
	this.debug("Open Carrier");
	if (player.carrier_url!=false) {
		obj_f_carrier = window.open(player.carrier_url,'fcarrier','height='+h+',width=256,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,location=no,hotkeys=no');
		//popup.player = player;
		obj_f_carrier.focus();
	} else {
		this.debug("No Carrier Url defined!");
	}
}

function playerPopupClose() {
	this.debug("Close Popup");
	this.debug("Close Popup: "+obj_f_popup+" "+parent.videoopended+" "+videoopended);
	obj_f_popup=false;
	parent.videoopended=false;
	videoopended=false;
	this.debug("Close Popup: "+obj_f_popup+" "+parent.videoopended+" "+videoopended);
}

function setPreviewImage(img) {
	this.previewimage = img;
}

function getPreviewImage(img) {
	return this.previewimage;
}