
// WMP functies

var playstateValues = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12); 
var ps 
// This function will run every time the Play State changes in Windows Media Player 
function wmpPlayStateChange(newState) { 

     // alert the play state value in plain text for convenience 
     ps = playstateValues[newState];
     //alert ( playstateValues[newState] ); 

} 




function wm_Play(){
	this.controls.play();
	return true;
}

function wm_Pause(){
	this.controls.pause();
	return true;
}

function wm_Stop(){
	this.controls.stop();
	return true;
}

function wm_getCurrentPosition(){

	return parseInt(this.controls.currentPosition);
}

function wm_setCurrentPosition(iPos){
	this.controls.currentPosition = iPos;
	return true;
}

function wm_getDuration(){
	return parseInt(this.currentMedia.duration);
}

function wm_getPlayState(){
	var tmp;
	if (browserName == 'Firefox'){
        	tmp =  ps;
    	}    
	else{
		tmp = this.PlayState;
	}
	if (tmp == 1 && ((parseInt(Math.floor(this.controls.currentPosition)) <= parseInt(Math.floor(this.currentMedia.duration))) && (parseInt(this.currentMedia.duration) >0))) {
		sPlayState = 8;
		tmp = 8;
	}
	
//	if(tmp != 8){
//		if ((parseInt(Math.ceil(this.controls.currentPosition)) == parseInt(Math.floor(this.currentMedia.duration))) && (parseInt(this.currentMedia.duration) >0))
//			{sPlayState = 8;tmp=8;return tmp;} //zorgt bij streaming wmv voor een Ended status bij einde stream.
//		if ((sPlayState == 8 && this.PlayState==1) || tmp==8) {// Ended wordt even afgegeven. Zorg dat ie blijft.
//		    tmp = 8;
//		}
//		if(this.PlayState==1 && (parseInt(Math.ceil(this.controls.currentPosition)) == parseInt(Math.floor(this.currentMedia.duration)) && (parseInt(this.currentMedia.duration) >0)))
//			tmp=8;
//	}
	return tmp;
	
}


function wm_getVolume(){
	return this.settings.volume;
}

function wm_setVolume(iVol){
	var newVol;
	newVol = iVol ;
	if(newVol<1) newVol = 1;
	if(newVol>100) newVol = 100;
	this.settings.volume = newVol;
	return true;
}

function wm_setURL(sURL){
    
	this.URL = sURL;
}

// Quicktime functies


function qt_Play(){
	this.Play();
	return true;
}

function qt_Pause(){
	this.Stop();
	return true;
}

function qt_Stop(){
	this.Stop();
	this.Rewind();
	return true;
}

function qt_getCurrentPosition(){
	return parseInt(this.GetTime()/this.GetTimeScale());
}

function qt_setCurrentPosition(iPos){
	this.SetTime(iPos * this.GetTimeScale())
	return true;
}

function qt_getDuration(){
	return parseInt(this.GetDuration()/this.GetTimeScale());
}


function qt_getPlayState(){

	
	var tmp;
	var sPS;
	tmp = 7;   
	sPS = this.GetPluginStatus();
		

	if(sPS.substring(0,7) == 'Waiting') tmp = 7; //Waiting (substring voor safari)
//	else if(sPS == 'Loading') tmp = 6; //Buffering
	else if(sPS.toLowerCase().indexOf('buffer') !=-1) tmp = 6; //Buffering

	else if((sPS == 'Complete' || sPS == 'Playable') && sPlayState != 6){
		if(this.GetRate() == 0){ //als we stilstaan
			if(this.GetTime() == 0){ //aan het begin
				if(sPlayState == 10) 
					tmp = 10; //Ready
				else{
					tmp = 1;
				}
			}	
			else{
				if (this.GetTime() == this.GetDuration()) //aan het eind
					tmp = 8; //Ended
				else
					tmp = 2; //Paused
			}
		

		}			
		else{  //speelt af
			tmp = 3;
		}
	}
	sPlayState = parseInt(tmp);	
	//if(document.getElementById('Fpos'))
	//	document.getElementById('Fpos').innerHTML = sPS + ' | ' + 'url:'+this.GetURL();
	return tmp;
}

function qt_getVolume(){
	return this.GetVolume()/2.56;
}

function qt_setVolume(iVol){
	var newVol;
	newVol = iVol * 2.56;
	if(newVol<0) newVol = 0;
	if(newVol>255) newVol = 255;
	this.SetVolume(newVol);
	return true;
}

function qt_setURL(sURL){    
	this.SetURL(sURL);
}

// Flash functies

var fl_currentPosition = 0;
var fl_currentVolume = 0;
var fl_duration = 0;
var fl_state = -1;
var sPlayState = 0;

function getUpdate(typ,pr1,pr2){
	if(typ == "time"){
		fl_currentPosition = parseInt(pr1); 
		if(fl_duration == 0 || fl_currentPosition == 0) fl_duration = parseInt(pr2)
	}
	else if(typ == "volume"){ 
		fl_currentVolume = pr1; 
	}
	else if(typ == "state"){
		fl_state = pr1;
	}
}

function fl_Play(){
	if(fl_state == 0) this.sendEvent('playpause');
	return true;
}

function fl_Pause(){

	if(fl_state == 2) this.sendEvent('playpause');
	
	return true;
}

function fl_Stop(){
	this.sendEvent('stop');
	return true;
}

function fl_getCurrentPosition(){
	return parseInt(fl_currentPosition);
}

function fl_setCurrentPosition(iPos){
	this.sendEvent("scrub",iPos)
	return true;
}

function fl_getDuration(){
	return parseInt(fl_duration);
}

function fl_getPlayState(){
	var tmp;
	tmp = 0;   
	if(fl_state == 0) tmp = 2; //Paused
	if(fl_state == 1) tmp = 6; //Buffering
	if(fl_state == 2) tmp = 3; //Playing
	if(fl_state == 3) tmp = 8; //Ended
	//document.getElementById('sub_header').innerHTML = fl_state;

	if(fl_state == 0 && fl_currentPosition == 0) 
		tmp = 10; //ready
	else if((fl_state > 1 || fl_state==0) && parseInt(Math.ceil(fl_currentPosition)) >= parseInt(Math.floor(fl_duration)) & fl_currentPosition > 0) {
		sPlayState = 8;
		}

	if(sPlayState==8 && fl_state==2){
	 	tmp =8;
		sPlayState=0;
	}
	return tmp;
}

function fl_getVolume(){
	return parseInt(fl_currentVolume);
}

function fl_setVolume(iVol){
	this.sendEvent('volume',iVol);
	return true;
}

function fl_setURL(sURL){   
	this.loadFile(sURL);
}



// Flashv4 functies

var fl4_currentPosition = 0;
var fl4_currentVolume = 0;
var fl4_duration = 0;
var fl4_state = -1;
var sPlayState = 0;
var fl4_ready = false;
var fl4_firstload = true;

function playerReady(obj) {
	//setTimeout('setReady()',3000);
	fl4_ready = true;
	
}
function setReady(){
	//alert('ready');
	fl4_ready = true;
}
function addListeners() {
	if (fl4_ready) {
	    objPlayer.fsetVolume(1);
	    //alert('ja');
		objPlayer.addModelListener("TIME", "fl4_getCurrentPositionEvt");
		objPlayer.addControllerListener("VOLUME", "fl4_getCurrentVolumeEvt");
		
	} else {
		setTimeout("addListeners()",200);
	}
}


function fl4_Play(){
	if(fl4_ready){
		if (fl4_state != 'PLAYING'){
			this.sendEvent('PLAY');
			if(fl4_firstload){
			   this.sendEvent('STOP');
			    //alert('ja2');
				
				fl4_firstload=false;
			}
			objPlayer.fsetVolume(100);
		}
	}
	return true;
}

function fl4_Pause(){
	
		if (fl4_state == 'PLAYING') this.sendEvent('PLAY');
	
		return true;

}

function fl4_Stop(){
	this.sendEvent('STOP');
	return true;
}

function fl4_getCurrentPosition(){
	return parseInt(fl4_currentPosition);
}

function fl4_getCurrentVolumeEvt(obj){
	fl4_currentVolume = parseInt(obj.percentage);
}

function fl4_getCurrentPositionEvt(obj){
	fl4_currentPosition = parseInt(obj.position);
	fl4_duration = parseInt(obj.duration);
}

function fl4_setCurrentPosition(iPos){
	this.sendEvent('SEEK',iPos);
	return true;
}

function fl4_getDuration(){
	return parseInt(fl4_duration);
}

function fl4_getPlayState(){

	var tmp;
	tmp = 0;   
	
	if(fl4_ready){
		if (fl4_firstload && fl4_currentPosition > 0) {	
			
			this.sendEvent('STOP');
			fl4_firstload = false;
			this.sendEvent('PLAY');
		}
		//alert('test1');
		fl4_state = objPlayer.getConfig().state;

		if(fl4_state == 'PAUSED') tmp = 2; //Paused
		if(fl4_state == 'BUFFERING') tmp = 6; //Buffering
		if(fl4_state == 'PLAYING') tmp = 3; //Playing
		if(fl4_state == 'COMPLETED') tmp = 8; //Ended
		if(fl4_state == 'PAUSED' && fl4_currentPosition == 0) 
			tmp = 10; //ready
		else if((fl4_state != 'BUFFERING' || fl4_state=='PAUSED') && parseInt(Math.ceil(fl4_currentPosition)) >= parseInt(Math.floor(fl4_duration)) & fl4_currentPosition > 0) {
			sPlayState = 8;
		}
		if(sPlayState==8 && fl4_state== 'PLAYING'){
		 	tmp =8;
			sPlayState=0;
		}
	}
	return tmp;
}

function fl4_getVolume(){
	return parseInt(fl_currentVolume);
}

function fl4_setVolume(iVol){
    var newVol;
    //alert('a' + iVol);
	newVol = iVol ;
	if(newVol<1) newVol = 1;
	if(newVol>100) newVol = 100;
	this.sendEvent('VOLUME',newVol);
	return true;
}

function fl4_setURL(sURL){   
	this.loadFile(sURL);
}