
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);
//		this.setVolume(5);
	}
}
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();
		this._ytplayer.stopVideo();
		
		document.getElementById("p2"+this._index).width="362";
		document.getElementById("p0"+this._index).width="1";
		document.getElementById("p1"+this._index).width="3";
		document.getElementById("p3"+this._index).width="0";
		document.getElementById("p3"+this._index).style.width="0";
	}
}

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()/35), true);
		}
	}
}*/

/*
videoAPI.prototype.doseek = function(position) {
	//mousePosition(e);
	var thisLeft = position.offsetLeft;
 	alert(thisLeft);
 	alert(posx);
//	var x = seconds.pageX - thisLeft;
//	
//	alert(x);
	
	
}
*/
//jQuery(document).ready(function(){
//	jQuery('.doSeek').click(function(e){
//		var xPosition = parseInt(jQuery(this).offset().left);
//		obj0.doseek(e.pageX,xPosition);
//	});
//});
window.captureEvents(Event.CLICK);

videoAPI.prototype.doseek = function(px,xPosition) {
	   // alert(px);
	   // alert(xPosition);
	    var x = px - xPosition;
	   // alert(x);
	    
	    if (this._ytplayer) {
	    	if(x == 0) {
	    		this._ytplayer.seekTo(x, true);
	    	}
	    	else {
	    	this._ytplayer.seekTo(Math.round((x-0.5)*this.getDuration()/365), true);
	    	}
	    }
	
}

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();
    	//console.log("Get Vol: "+this._ytplayer.getVolume());
    	this.setplaypause(pstate);
    	if (pstate==1){this._ytplayer.pauseVideo();}
    	else{ 
//    		console.log('from else');
    		this._ytplayer.playVideo(); }
	}
}

videoAPI.prototype.setplaypause = function(pstate){
	if (pstate==1){
		if(this._homepage) {
			document.getElementById('pplay'+this._index).src = W3FE_BASE_URL+'/public/_images/lcs/youtube-player-images/ppause.gif';
		}
		else {
			document.getElementById('pplay'+this._index).src = W3FE_BASE_URL+'/public/_images/lcs/youtube-player-images/ppause.gif';
		}
	}	    
	else{
		if(this._homepage) {
			document.getElementById('pplay'+this._index).src = W3FE_BASE_URL+'/public/_images/lcs/youtube-player-images/pplay.gif';
		}
		else {
			document.getElementById('pplay'+this._index).src = W3FE_BASE_URL+'/public/_images/lcs/youtube-player-images/pplay.gif';
		}
	}
	document.getElementById('pplay'+this._index).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._ytplayer.setVolume(100);
	setInterval(this.updateytplayerInfo, 1000, this);
	this.updateytplayerInfo(this);
}


function num_format(num)
{

	min = parseInt(num) / 60;	
	min = parseInt(min);
	sec = parseInt(num) % 60;
	
	if ( sec == 0 ) 
		sec="00";
	else if(sec<10)
		sec="0"+sec;
	
	return min+":"+sec;
}
videoAPI.prototype.getVideoBytesLoaded = function() {
	if (this._ytplayer) {
//		this._ytplayer = document.getElementById("video0");
		return this._ytplayer.getVideoBytesLoaded();
	}
}

videoAPI.prototype.getVideoBytesTotal = function() {
	if (this._ytplayer) {
		return this._ytplayer.getVideoBytesTotal();
		
	}
}
videoAPI.prototype.getVideoStartBytes = function() {
	if (this._ytplayer) {
		return this._ytplayer.getVideoStartBytes();
		
	}
}
videoAPI.prototype.updateytplayerInfo = function(obj) {
//	console.log('from info'+obj._index);
//
//	if(obj!=null && this._index!=undefined)
		if(obj!=null)
	{
		 document.getElementById("videoDuration"+obj._index).innerHTML=num_format(parseInt(obj.getDuration()));
         document.getElementById("videoCurrentTime"+obj._index).innerHTML=num_format(parseInt(obj.getCurrentTime()));
         str_bytes = parseInt(obj.getVideoStartBytes());
         
         byt_load = obj.getVideoBytesLoaded();
        
         byt_load = parseInt(byt_load)+parseInt(str_bytes);
         tot_bytes = obj.getVideoBytesTotal();
//         console.log("bytlod=="+byt_load+"tot_bytes=="+tot_bytes);
		 parts = tot_bytes / 365;
		 parts = parseInt(parts);
		 loaded_parts = parseInt(byt_load) / parts;
//		 console.log(byt_load+" "+parts+" "+loaded_parts);
		 document.getElementById('p3'+obj._index).style.width=loaded_parts+"px";
//         this._index = 0;
		var p0w=Math.round(obj.getCurrentTime()/obj.getDuration()*363);
		//console.log("p0w: "+p0w);
		//console.log("currTime / Duration: "+Math.round(obj.getCurrentTime())+" / "+Math.round(obj.getDuration()));
	 	if (p0w>362 || p0w<0)
	 	{
	 	p0w=362;
	 	//alert("adding "+p0w);
	 	}
	 //	alert(this._ytplayer.getVolume());
		var p2w=363-p0w;
		//console.log(this._index);
		var p0=document.getElementById("p0"+obj._index);
		var p2=document.getElementById("p2"+obj._index);
		// console.log("p0: "+p0);
		if(p0!=null) {
			//console.log("p0.width / p0w: "+p0.width+" / "+p0w);
			
			if(loaded_parts!="Infinity")
			if(p0.width!=p0w){
				p0.width=p0w;
				p2.width=p2w;
			}
		}
	}
}


/*

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.playstart = function (){
	this.loadNewVideo();			
}




