//class Player FL
//public
function PlayerFL(onChangeState,onCommand){
this.playerType="FL";
this.onChangeState=onChangeState;
this.onCommand=onCommand;
this.myState="null";
//this.myLastTime=-2;
this.myEndofclip=false;
this.myMediatrack=new Array();
this.myPlayer=null;
this.Starttime=0;
this.myPulse;
this.myURL="";
this.myCurrentindex=-1;
this.myTotalDuration=gUnknownDuration;
this.myscheduleClip=null;
this.mySkinName = "clearSkin_3";//DEPRECATED
this.myVolume=100;
this.myPos=0;
}//end function

//public
PlayerFL.prototype.hook=function Player_hook(func){
this.onChangeState=func;
}//end function

//public
PlayerFL.prototype.init=function Player_init(q_URL,q_SSIZE,q_INSTREAM,q_CONTROL,q_ALLOWMENU,q_AUTOSTART,q_eventlist,q_STARTTIME){
this.myFormat="FL";
this.myTag=(WIN()&&IE())?"OBJECT":"EMBED";
this.myURL=q_URL;
this.myLive=(URL2protocol(this.myURL).toLowerCase()=="rtmplive://");
this.myDisplaysize=q_SSIZE;
this.mySevent=true;
this.myInstream=q_INSTREAM;
this.myAllowmenu=q_ALLOWMENU=="1";
this.myScriptable=true;//until proven otherwise
this.myAutostart=q_AUTOSTART;//||FX();
this.myShowcontrols=q_CONTROL;//This flash player always have internal controls
this.setMediatrack(q_eventlist);
q_STARTTIME=0;//This flash player always starts from beginning of clip
this.setTime(q_STARTTIME);
this.setMyURL(q_STARTTIME);
this.myConfigfile="config1_9109.xml";//DEPRECATED
this.myUseConfigFile=false;//DEPRECATED
}//end function

//public
PlayerFL.prototype.paint=function Player_paint(q_obj,q_OVERWRITE){
this.myContainer=q_obj;
this.myPlayername=q_obj.id+"img";
var q_WIDTH=parseInt(this.myContainer.style.width);
var q_HEIGHT=parseInt(this.myContainer.style.height);
this.myOverwrite=q_OVERWRITE;
if(q_OVERWRITE || !div(this.myPlayername)){
	var q_str=this.embedUrl();
	var so = new SWFObject(q_str, this.myPlayername, q_WIDTH, q_HEIGHT, '7', '#000000');
  so.addParam('allowscriptaccess','always');
  so.addParam('allowfullscreen','true');
//	var so = new SWFObject('FLVPlayer_Progressive.swf?'+this.flashVars2(), this.myPlayername, q_WIDTH, q_HEIGHT, '7', '#000000');
	so.write(this.myContainer.id);
	this.myPlayer=document.getElementById(this.myPlayername);
}else{
	this.myPlayer=document.getElementById(this.myPlayername);
}//end if
this.startClock(now());
}//end function

//public 
PlayerFL.prototype.startClock=function Player_startClock(q_time){
this.Starttime=q_time;

}//end function

//public 
PlayerFL.prototype.embedUrl=function Player_embedUrl(){
if(URL2protocol(this.myURL).toLowerCase()=="http://"){
	//ondemandProgressive
	return "/ability/program/livepro/admin/mediaplayer.swf?"+this.flashVars5();
	//return "mediaplayer.swf?"+this.flashVars5();
	//return "FLVPlayer_Progressive.swf?"+this.flashVars2();
}else if(this.isLive()){
	//liveStreaming
	return "/ability/program/livepro/admin/liveplayer.swf?"+this.flashVars4();
	//return "liveplayer.swf?"+this.flashVars4();
}else{
	//ondemandStreaming
	return "/ability/program/livepro/admin/mediaplayer.swf?"+this.flashVars4();
	//return "mediaplayer.swf?"+this.flashVars4();
	//return "FLVPlayer.swf?"+this.flashVars3();
}//end if
}//end function

var gLogtimechange=false;
function getUpdate(typ,pr1,pr2,swf) { 
if(typ == 'time') { 
  gPlayer.myPos=pr1; 
  if(gLogtimechange){
		logga("getUpdate time "+pr1);
		gLogtimechange=false;
  }//end if
}else if(typ == 'state') { 
  gPlayer.updateState(pr1); 
}//end if
}//end function

//private
PlayerFL.prototype.isLive=function Player_isLive(){
return this.myLive;
}//end function

//private
PlayerFL.prototype.flashVars2=function Player_flashVars2(){
return "&MM_ComponentVersion=1&skinName="+ this.mySkinName + "&streamName="+ this.myURL + "&autoPlay=true&autoRewind=false";
}//end function

//private 
PlayerFL.prototype.flashVars3=function Player_flashVars3(){
var str;
str="serverName=z_SERVERNAME&fallbackServerName=z_SERVERNAME&skinName=clearSkin_3&appName=z_FOLDER&streamName=z_NAMEPART&isLive=false&bufferTime=2&autoPlay=true&autoRewind=true&isFullScreen=false&bgColor=0x000000&";
return this.setFlashVarParameters(str);
}//end function

//private 
PlayerFL.prototype.flashVars4=function Player_flashVars4(){
var str;
str="mediaplayer.swf?&displayheight=z_HEIGHT&file=rtmp://z_SERVERNAME/z_FOLDER&id=z_NAMEPART&height=z_HEIGHT&width=z_WIDTH&bufferlength=0.1&autostart="+this.myAutostart+"&allowfullscreen=z_ALLOWFULLSCREEN&overstretch=true&enablejs=true&shownavigation=z_SHOWCONTROL";
return this.setFlashVarParameters(str);
}//end function

//private 
PlayerFL.prototype.flashVars5=function Player_flashVars5(){
var str;
str="&displayheight=z_HEIGHT&file=z_URL&id=z_NAMEPART&height=z_HEIGHT&width=z_WIDTH&bufferlength=0.1&autostart="+this.myAutostart+"&allowfullscreen=z_ALLOWFULLSCREEN&overstretch=true&enablejs=true&shownavigation=z_SHOWCONTROL";
return this.setFlashVarParameters(str);
}//end function

//private 
PlayerFL.prototype.setFlashVarParameters=function Player_setFlashVarParameters(str2){
var str=str2;
str=replace(str,"z_URL",this.myURL);
str=replace(str,"z_SERVERNAME",URL2servername(this.myURL));
str=replace(str,"z_FOLDER",URL2folder(this.myURL));
str=replace(str,"z_NAMEPART",URL2name(this.myURL));
str=replace(str,"z_HEIGHT",""+parseInt(this.myContainer.style.height));
str=replace(str,"z_WIDTH",""+parseInt(this.myContainer.style.width));
str=replace(str,"z_SHOWCONTROL",""+this.myShowcontrols);
str=replace(str,"z_ALLOWFULLSCREEN",""+this.myAllowmenu);
return str;
}//end function


//public
PlayerFL.prototype.dispose=function Player_dispose(){
if(!this.noPlayer()){
	this.stop();
	this.myPlayer=null;
	this.myContainer.innerHTML="";
}//end if
}//end function

//public
PlayerFL.prototype.initandpaint=function Player_paint(q_obj,q_URL,q_SSIZE,q_INSTREAM,q_CONTROL,q_ALLOWMENU,q_AUTOSTART,q_OVERWRITE){
this.init(q_URL,q_SSIZE,q_INSTREAM,q_CONTROL,q_ALLOWMENU,q_AUTOSTART,eventlist,0);
this.paint(q_obj,q_OVERWRITE);
return true;
}//end function

//public
PlayerFL.prototype.setPlayer=function Player_setPlayer(q_name,format){
//try{
this.myFormat=format;
this.myPlayer=document.getElementById(q_name);
this.myContainer=this.myPlayer.parentNode;
//}catch(errObj){
//}//end catch
}//end function

//public
PlayerFL.prototype.play=function Player_play(){
this.myPlayer.sendEvent("playpause");
}//end function

//public
PlayerFL.prototype.pause=function Player_pause(){
this.myPlayer.sendEvent("playpause");
}//end function

//public
PlayerFL.prototype.stop=function Player_stop(){
this.myPlayer.sendEvent("stop");
}//end function

//public
PlayerFL.prototype.up=function Player_volumeUp(){
if(this.myVolume<=90){
	this.myVolume=this.myVolume+10;
	this.myPlayer.sendEvent("volume",this.myVolume);
}//end if
}//end function

//public
PlayerFL.prototype.down=function Player_volumeDown(){
if(this.myVolume>=10){
	this.myVolume=this.myVolume-10;
	this.myPlayer.sendEvent("volume",this.myVolume);
}//end if
}//end function

//public
PlayerFL.prototype.mute=function Player_mute(q_muted){
if(q_muted==false){
	this.myVolume=50;
	this.myPlayer.sendEvent("volume",this.myVolume);
}else{
	if(this.myVolume!=0){
		this.myVolume=0;
		this.myPlayer.sendEvent("volume",this.myVolume);
	}//end if
}//end if
}//end function

//public
PlayerFL.prototype.updateEvent=function Player_updateEvent(){
if(!this.noPlayer()){
	if(this.isMulticlip()){
		if(this.isLastclip()){
			if(this.getTime()>this.myTotalDuration){
				logga("pause at end");
				gPlayer.pause();
			}//end if
		}else{
			var q_nextClipstart=this.myMediatrack[this.myCurrentindex+1].myClipstart;
			var q_clipTimeLeft=q_nextClipstart-this.getTime();
			if(q_clipTimeLeft<-0.1&&q_clipTimeLeft>-1.9){//we passed a clip end
				logga("we passed a clip end "+q_clipTimeLeft);
				//logga("enter new clip");
				//gPlayer.setTime(q_nextClipstart);
			}else if(q_clipTimeLeft>0&&q_clipTimeLeft<1.5){
				this.scheduleClip(q_nextClipstart,q_clipTimeLeft*1000-100);
			}//end if
		}//end if
	}else{
		if(this.getTime()>this.myTotalDuration){
			logga("pause at end");
			gPlayer.pause();
		}//end if
	}//end if
}//end if
}//end function

//TODO: Force setTime to truncate to the beginning of the selected clip. This flash player always starts playing at the beginning of a clip
//public
PlayerFL.prototype.setTime=function Player_setTime(q_playtime){
if(this.isLive()){
}else{
	this.unScheduleClip();
	logga("setTime("+q_playtime+")");
	if(this.noPlayer()){
		return this.setStarttime(q_playtime);
	}else{
		if(this.isMulticlip()){
			return this.setMediatrackPos(q_playtime);
		}else{
			return this.setPos(q_playtime);
		}//end if
	}//end if
}//end if
}//end function

//public
PlayerFL.prototype.getTime=function Player_getTime(){
if(this.noPlayer()){
	return this.myStarttime;
}else{
	if(this.isMulticlip()){
		return this.getMediatrackPos();
	}else{
		return this.getPos();
	}//end if
}//end if
}//end function

//public
PlayerFL.prototype.getDuration=function Player_getDuration(){
	return gUnknownDuration;
}//end function

//public
PlayerFL.prototype.getSrc=function Player_getSrc(){
	return this.myURL;
}//end function

//public
PlayerFL.prototype.setSrc=function Player_setSrc(src){
var cmp=src.replace("file://","");
if(this.getSrc()!=cmp){
	this.myURL=cmp;
	if(this.myPlayer){
		this.paint(this.myContainer,true);//reinitialize player and start playing file src
	}//end if
}else{
	return false;
}//end if
}//end function

//public
PlayerFL.prototype.setSize=function Player_setSize(width,height){
if(width=="full"){
	this.registerOriginalSize();
	this.myContainer.style.left=0;
	this.myContainer.style.top=0;
	this.myContainer.style.zIndex=100;
	this.myContainer.style.width=div("master").offsetWidth;
	this.myContainer.style.height=div("master").offsetHeight;
	this.myPlayer.width=this.myContainer.offsetWidth;
	this.myPlayer.height=this.myContainer.offsetHeight;
}else if (width=="normal"){
	if(this.isFullscreen()){
		this.restoreOriginalSize();
	}//end if
	this.myPlayer.width=this.myContainer.offsetWidth;
	this.myPlayer.height=this.myContainer.offsetHeight;
}else if(isNumeric(width)){
	this.setSize("normal");
	this.myPlayer.width=width;
	this.myPlayer.height=height;
}//end if
}//end function

//public
PlayerFL.prototype.setShowcontrols=function Player_setShowcontrols(q_show){
if(this.myPlayer){
	if(this.myShowcontrols!=q_show){
			this.myShowcontrols=q_show;
			this.paint(this.myContainer,true);
	}//end if
}//end if
}//end function

//private
PlayerFL.prototype.isFullscreen=function Player_isFullscreen(){
return (this.myContainer.offsetWidth==div("master").offsetWidth);
}//end function

//private
PlayerFL.prototype.registerOriginalSize=function Player_registerOriginalSize(){
if(this.originalWidth){
}else{
		this.originalLeft=this.myContainer.offsetLeft;
		this.originalTop=this.myContainer.offsetTop;
		this.originalWidth=this.myContainer.offsetWidth;
		this.originalHeight=this.myContainer.offsetHeight;
}//end if
}//end function

//private
PlayerFL.prototype.restoreOriginalSize=function Player_restoreOriginalSize(){
if(this.originalWidth){
		this.myContainer.style.left=this.originalLeft;
		this.myContainer.style.top=this.originalTop;
		this.myContainer.style.width=this.originalWidth;
		this.myContainer.style.height=this.originalHeight;
}//end if
}//end function

//public
PlayerFL.prototype.canScript=function Player_canScript(){
return false;
}//end function

//public
PlayerFL.prototype.setState=function Player_setState(state){
if(("play,pause,stop,connect,buffer,idle,").indexOf(state+",")>-1){
	if(this.myState!=state){
		this.myState=state;
		this.onChangeState(this.myState);
	}//end if
}//end if
}//end function
//end class

//public
PlayerFL.prototype.setCmd=function Player_setCmd(q_cmd){
if(this.onCommand){
	this.onCommand(q_cmd,null,null);
}//end if
}//end function
//end class

//public
PlayerFL.prototype.getState=function Player_getState(){
try{
	if(this.myState){
		return this.myState;
	}else{
		return "null";
	}//end if
}catch(errObj){
	return "null";
}//end catch
}//end function

//private
PlayerFL.prototype.updateState=function Player_updateState(newstate){
try{
//	this.myState=newstate;
	this.myState=this.stateConstFL(newstate);
}catch(errObj){
}//end catch
}//end function


//public
PlayerFL.prototype.stateConstFL=function Player_stateConstWM(q_stateNum){
switch (q_stateNum){
case 0:return "pause";//Undefined
case 1:return "buffer";//Stopped
case 2:return "play";//Paused
default:return "idle";
}//end switch
return "null";
}//end function

//public
PlayerFL.prototype.getBuffering=function Player_getBuffering(){
	return 0;
}//end function

//public
PlayerFL.prototype.getBandwidth=function Player_getBandwidth(){
return 0;
}//end function

//public
PlayerFL.prototype.getFramerate=function Player_getFramerate(){
return 0;
}//end function

//public
PlayerFL.prototype.getVersion=function Player_getVersion(){
return this.myPlayer.versionInfo;
}//end function

//public
PlayerFL.prototype.showCurrent=function showCurrent(){
if(this.isMulticlip()){
	var q_media=this.getCurrentMedia();
	if(q_media){
		return "Multiclip,["+this.myCurrentIndex+"],"+q_media.myClipstart+","+q_media.mySrc+","+q_media.myMarkin+","+q_media.myDuration;
	}else{
		return "Multiclip,no current media";
	}//end if
}else{
		return "Singleclip,"+this.myURL;
}//end if
}//end function

//public
PlayerFL.prototype.player2Sync=function Player_player2Sync(q_offset){
return false;//Syncing two flash videos is not possible
}//end function
//---------------------------------------------------------------------------------------------------
//private
PlayerFL.prototype.setMyURL=function Player_setMyURL(q_STARTTIME){
this.myStarttime=q_STARTTIME;
this.myCurrentindex=this.getMediatrackIndex(this.myStarttime);
if(this.myURL==""&&this.myCurrentindex>-1){
	this.myURL=this.getCurrentMedia().mySrc;
}//end if
}//end function

//private
PlayerFL.prototype.noPlayer=function Player_noPlayer(){
if(this.myPlayer){
	return false;
}else{
	return true;
}//end if
}//end function

//private
PlayerFL.prototype.scheduleClip=function Player_scheduleClip(q_start,q_at){
if(this.myscheduleClip){
}else{
	logga("schedule new clip in "+q_at+"ms");
	this.myscheduleClip=setTimeout("gPlayer.setTime("+q_start+")",q_at);
}//end if
}//end function

//private
PlayerFL.prototype.unScheduleClip=function Player_unScheduleClip(){
if(this.myscheduleClip){
	clearTimeout(this.myscheduleClip);
	this.myscheduleClip=null;
}//end if
}//end function

//private
PlayerFL.prototype.isLastclip=function Player_isLastclip(){
return (this.myCurrentindex==this.myMediatrack.length-1);
}//end function

//private
PlayerFL.prototype.setStarttime=function Player_setStarttime(q_pos){
	this.myStarttime=q_pos;
	if(this.setMediatrackIndex(q_pos)>-1){
		this.myURL=this.getCurrentMedia().mySrc;
	}//end if
	return this.myStarttime;
}//end function

//private
PlayerFL.prototype.setPos=function Player_setPos(arg){
this.myPlayer.sendEvent("scrub",arg);
this.myPos=arg;
}//end function

//private
PlayerFL.prototype.getMediatrackIndex=function Player_getMediatrackIndex(q_pos){
var plist=this.myMediatrack;
for(var q_i=plist.length-1;q_i>=0;q_i--){
	if(q_pos>=plist[q_i].myClipstart && plist[q_i].myDuration==-1){
		return q_i;
	}else if(q_pos>=plist[q_i].myClipstart && q_pos<plist[q_i].myClipstart+plist[q_i].myDuration){
		return q_i;
	}//end if
}//next
return -1;
}//end function

//private
PlayerFL.prototype.time2pos=function Player_time2pos(q_time){
var q_i=this.getMediatrackIndex(q_time);
if(q_i>=0){
	var q_media=this.myMediatrack[q_i];
	return q_media.myMarkin+parseInt(q_time)-q_media.myClipstart;
}else{
	return 0;
}//end if
}//end function

//private
PlayerFL.prototype.setMediatrackIndex=function Player_setMediatrackIndex(q_pos){
var q_i=this.getMediatrackIndex(q_pos);
if(q_i>=0){
	this.myCurrentindex=q_i;
	return this.myCurrentindex;
}else{
	return -1;
}//end if
}//end function

//private
PlayerFL.prototype.getCurrentMedia=function Player_getCurrentMedia(){
	return this.myMediatrack[this.myCurrentindex];
}//end function

//private
PlayerFL.prototype.setMediatrackPos=function Player_setMediatrackPos(arg){
if(this.setMediatrackIndex(arg)>-1){
	q_media=this.getCurrentMedia();
	if(!this.isIdentical(q_media)){
		this.myPlayer.setSrc(q_media.mySrc);
	}//end if
	gLogtimechange=true;	
	this.setPos(q_media.myMarkin+parseInt(arg)-q_media.myClipstart);
	return (q_media.myMarkin+parseInt(arg)-q_media.myClipstart);
}else{
	return -1;
}//end if
}//end function

//private
PlayerFL.prototype.getPos=function Player_getPos(){
if(this.myPlayer){
	if(this.myPos>=0){
		return this.myPos;
	}else{
		return (now()-this.Starttime)/1000;
	}//end if
}else{
	return -1;
}//end if
}//end function

//private
PlayerFL.prototype.getMediatrackPos=function Player_getMediatrackPos(){
var q_i=this.myCurrentindex;
if(q_i>=0){
	q_media=this.getCurrentMedia();
	if(this.isIdentical(q_media)){
		var t= this.getPos()-q_media.myMarkin+q_media.myClipstart;
		return t;
	}else{
		return -1;
	}//end if
}else{
	return -1;
}//end if
}//end function

//private
PlayerFL.prototype.isIdentical=function Player_isIdentical(q_media){
return (this.getSrc().toLowerCase()==replace(q_media.mySrc.toLowerCase(),"file://",""));
}//end function

//private
PlayerFL.prototype.isValidpos=function Player_isValidpos(q_pos){
if(q_pos>=0 && this.getDuration()==-1){//duration undefined - time is presumably within the clip
	return true;
}else if(q_pos>=0 && q_pos <= this.getDuration()){
	return true;
}else{
	return false;
}//end if
}//end function

//private
PlayerFL.prototype.getMediatrackDuration=function Player_getMediatrackDuration(){
var plist=this.myMediatrack;
var q_dur=0;
for(var q_i=0;q_i<plist.length;q_i++){
	q_dur=q_dur+plist[q_i].myDuration;
}//next
return q_dur;
}//end function

//private
PlayerFL.prototype.verifyMediatrackDuration=function Player_verifyMediatrackDuration(){
var plist=this.myMediatrack;
var q_verified=true;
for(var q_i=0;q_i<plist.length;q_i++){
	q_verified=q_verified && isEqualDuration(plist[q_i].myDuration,this.getRealDuration(q_i));
}//next
return q_verified;
}//end function

//private
function isEqualDuration(a,b){
return almostEqual(a,b);
}//end function

//private
PlayerFL.prototype.fixMediatrackDuration=function Player_fixMediatrackDuration(){
var plist=this.myMediatrack;
for(var q_i=0;q_i<plist.length;q_i++){
	plist[q_i].myDuration=this.getRealDuration(q_i);
}//next
return true;
}//end function

//private
PlayerFL.prototype.getRealDuration=function Player_getRealDuration(q_i){
var plist=this.myMediatrack;
if(q_i<plist.length-1){
	return plist[q_i+1].myClipstart-plist[q_i].myClipstart;
}else{
	return this.myTotalDuration-plist[q_i].myClipstart;
}//end if
}//end function

//private
PlayerFL.prototype.isMulticlip=function Player_isMulticlip(){
if(this.myMediatrack.length>1){
	return true;
}else if(this.myMediatrack[0]){
	return (this.myMediatrack[0].myMarkin>0);
}else{
	return false;
}//end if
}//end function

//private
PlayerFL.prototype.setMediatrack=function Player_setMediatrack(q_ev){
this.myMediatrack=new Array();
for (q_i=0;q_i<q_ev.length;q_i++){
if(isVideocmd(q_ev[q_i][1])){
	var v=js2array(q_ev[q_i][1]);
	this.myMediatrack[this.myMediatrack.length]=new Mediatrackitem(v[1],v[2],v[3],q_ev[q_i][0]);
}else if(startswith(q_ev[q_i][1],"eventlist_endmark(")){
	this.myTotalDuration=parseInt(q_ev[q_i][0]);
}//end if
}//next
}//end function

//end class






