function videoAPI() {
	this._ytplayer = null;
	this._id = null;
	this._index = null;
	this._homepage=null;
}

videoAPI.prototype.loadNewVideo = function() {
	if (this._ytplayer) {
		this._ytplayer.cueVideoById(this._id, 0);
	}
}

videoAPI.prototype.play = function() {
	if (this._ytplayer) {
		this._ytplayer.playVideo();
	}
}

videoAPI.prototype.pause = function() {
	if (this._ytplayer) {
		this._ytplayer.pauseVideo();
	}
}

videoAPI.prototype.stop = function() {
	if (this._ytplayer) {
		this._ytplayer.stopVideo();
	}
}

videoAPI.prototype.getPlayerState = function() {
	if (this._ytplayer) {
		return this._ytplayer.getPlayerState();
	}
}

videoAPI.prototype.doseek = function(seconds) {
	if (this._ytplayer) {
		if(seconds == 0) {
			this._ytplayer.seekTo(seconds, true);
		}
		else {this._ytplayer.seekTo(Math.round((seconds-0.5)*this.getDuration()/10), true);}
	}
}

videoAPI.prototype.mute = function() {
	if (this._ytplayer) {
		this._ytplayer.mute();
	}
}

videoAPI.prototype.unMute = function() {
	if (this._ytplayer) {
		this._ytplayer.unMute();
	}
}
        
videoAPI.prototype.setVolume = function(newVolume) {
	if (this._ytplayer) {
		this._ytplayer.setVolume(newVolume);
	}
}

videoAPI.prototype.getVolume = function() {
	if (this._ytplayer) {
		return this._ytplayer.getVolume();
	}
}

videoAPI.prototype.clearVideo = function() {
	if (this._ytplayer) {
		this._ytplayer.clearVideo();
	}
}

videoAPI.prototype.getDuration = function () {
	if (this._ytplayer) {
		return this._ytplayer.getDuration();
	}
}

videoAPI.prototype.playpause = function(){       
	if (this._ytplayer) {
    	var pstate=this.getPlayerState();
    	this.setplaypause(pstate);
    	if (pstate==1){this._ytplayer.pauseVideo();}
    	else{ this._ytplayer.playVideo(); }
	}
}

videoAPI.prototype.setplaypause = function(pstate){
	if (pstate==1){
		if(this._homepage) {
			document.getElementById('pplay'+this._index).src = 'images/ppause_s.gif';
		}
		else {
			document.getElementById('pplay'+this._index).src = 'images/ppause.gif';
		}
	}	    
	else{
		if(this._homepage) {
			document.getElementById('pplay'+this._index).src = 'images/pplay_s.gif';
		}
		else {
			document.getElementById('pplay'+this._index).src = 'images/pplay.gif';
		}
	}
	document.getElementById('pplay'+this._index).blur();			
}

videoAPI.prototype.dovolume = function(inv) {
	if (this._ytplayer){
		if(this._ytplayer.getVolume()==inv*20 && inv != 0){inv=inv-1;}
		var mstate = this._ytplayer.isMuted();
		if (mstate){this._ytplayer.unMute();this.setmuteunmute(mstate);}           
		this._ytplayer.setVolume(inv*20);
		this.setdovolume(inv);				
	}
}

videoAPI.prototype.setdovolume = function(volumeid){
	var i=1;
	for (i=1;i<=volumeid;i++){
		var onid='av'+i+this._index;
		var offid='nv'+i+this._index;
		document.getElementById(onid).style.display = '';
		document.getElementById(offid).style.display = 'none';
		document.getElementById(onid).blur();			
	}
	for (i=volumeid+1;i<=5;i++){
		var onid='av'+i+this._index;
		var offid='nv'+i+this._index;
		document.getElementById(onid).style.display = 'none';
		document.getElementById(offid).style.display = '';
		document.getElementById(offid).blur();
	}
}

videoAPI.prototype.getCurrentTime = function() {
	if (this._ytplayer) {
		return this._ytplayer.getCurrentTime();
	}
}
 
videoAPI.prototype.onytplayerStateChange = function(newState) {
    this.setytplayerState(newState);		              
}

videoAPI.prototype.setytplayerState = function(newState) {
	this.setplaypause(newState);
	this.setmuteunmute(!this._ytplayer.isMuted());
	this.setdovolume(Math.round(this._ytplayer.getVolume()/20));
	setInterval(this.updateytplayerInfo, 10000, this);
	this.updateytplayerInfo(this);	
}

videoAPI.prototype.updateytplayerInfo = function(obj) {
	if(obj!=null)
	{
		var p0w=Math.round(obj.getCurrentTime()/obj.getDuration()*100);
	 	if (p0w>99 || p0w<0){p0w=99;} 
		var p2w=100-p0w;
		var p0=document.getElementById("p0"+this._index);
		var p2=document.getElementById("p2"+this._index);
		if(p0!=null) {
			if(p0.width!=p0w){
				p0.width=p0w;
				p2.width=p2w;
			}
		}
	}
}

videoAPI.prototype.muteunmute = function(){
	if (this._ytplayer){           
		var mstate = this._ytplayer.isMuted();
		this.setmuteunmute(mstate);
		if (mstate){this._ytplayer.unMute();}
		else{this._ytplayer.mute();}	
	}
}

videoAPI.prototype.setmuteunmute = function(mstate){
	if (mstate){
		if(this._homepage) {
			document.getElementById('pmute'+this._index).src = 'images/punmute_s.gif';
		}
		else {
			document.getElementById('pmute'+this._index).src = 'images/punmute.gif';
		}
	}
	else{
		if(this._homepage) {
			document.getElementById('pmute'+this._index).src = 'images/pmute_s.gif';
		}
		else {
			document.getElementById('pmute'+this._index).src = 'images/pmute.gif';
		}
	}
	document.getElementById('pmute'+this._index).blur();		 
}

videoAPI.prototype.playstart = function (){
	this.loadNewVideo();			
}
