/*********************************************/
/*********************************************/
/** COPYRIGHTED - COPYRIGHTED - COPYRIGHTED **/
/** THIS FILE IS STRICTLY COPYRIGHTED       **/
/** OWNER:                                  **/
/** David Lorenz                            **/
/** Schloßstraße 3                          **/
/** 72351 Geislingen                        **/
/** Country: GER                            **/
/**                                         **/
/** www.vaib.de                             **/
/** VAIB INTERNET TECHNOLOGIES              **/
/*********************************************/
/*********************************************/

var _PLAYER				=	new Object();
_PLAYER.Sound			=	new Object();
_PLAYER.Video			=	new Object();
_PLAYER.Video.currentId	=	null;
_PLAYER.loop			=	new Object();
_PLAYER.loop.setting	=	false;

_PLAYER.loop.toggleLoop	=	function (bb){
	if(_PLAYER.loop.setting==false){
		//loop all
		_PLAYER.loop.setting	=	'all';
		bb.style.background		=	'url(img/loop_clrd.gif)';
		base.tooltipShow('Repeat <strong>all</strong>')
	}else if(_PLAYER.loop.setting=='all'){
		//loop one
		_PLAYER.loop.setting	=	'one';
		bb.style.background		=	'url(img/loopone.gif)';
		base.tooltipShow('Repeat <strong>one</strong>')
	}else if(_PLAYER.loop.setting=='one'){
		//loop none
		_PLAYER.loop.setting	=	false;
		bb.style.background		=	'url(img/loop_sw.gif)';
		base.tooltipShow('Repeat <strong>off</strong>')
	}
};

_PLAYER.loop.handle				=	function(){
	if(_PLAYER.loop.setting != false){
		if(_PLAYER.loop.setting=='one'){
			_YTPlayer.seekAndPlayBeginning();
		}else if(_PLAYER.loop.setting=='all'){
			if(_PLAYLIST.db.objects.length > 0){
				//play next playlist-track
				_PLAYLIST.playNext();
			}else{
				_YTPlayer.seekAndPlayBeginning();
			}
		}
	}
};

_PLAYER.toggleControls	=	function(){
	var JQObject	=	$('#PL_underd');
	if(JQObject.css('bottom')=='9px'){
		//close
		_PLAYER.closeControls();
	}else if(JQObject.css('bottom')=='54px'){
		_PLAYER.dropControls();
	}		
};

_PLAYER.dropControls	=	function(){
	$('#PL_underd').animate({
		bottom: '9px'
	},300,'linear',function(){
		if($('#openclose').css('display')=='block'){
			//do nothing, is already opened
		}else{
			$('#openclose').show('fast',function(){
				$('#openclose').css('display','block');
			});
		}
	});
	
	$('#isContenting').animate({
		top: '114px'
	},300);
};

_PLAYER.closeControls	=	function(){
	$('#PL_underd').animate({
		bottom: '54px'
	},300);
	
	$('#isContenting').animate({
		top: '65px'
	},300);
};




_PLAYER.Sound.setVol	=	function(bb,vol){
	vol	=	(typeof(vol)=='number' && vol < 101 && vol > 0) ? vol : 40;
	var bars;
	var i;
	var barElms	=	bb.getElementsByTagName('a');
	
	for(i=0; i < 5; i++){
		barElms[i].style.background	=	'#525252';
	}
	
	if(vol < 40){
		bars	=	1;
		vol		=	20;
	}else if(vol < 60){
		bars	=	2;
		vol		=	40;
	}else if(vol < 80){
		bars	=	3;
		vol		=	60;
	}else if(vol < 100){
		bars	=	4;
		vol		=	80;
	}else{
		bars	=	5;
		vol		=	100;
	}
	
	for(i=0; i < bars; i++){
		barElms[i].style.background	=	'#909090';
	}
	
	_YTPlayer.setVol(vol);
};

_PLAYER.Sound.toggleMute	=	function(bb){
	if(_YTPlayer.isMuted()=="true"){
		//unmute
		bb.style.background	=	'url(img/muting_speaker_clrd.gif)';
		_YTPlayer.unMute();
	}else{
		//mute
		bb.style.background	=	'url(img/muting_speaker_sw.gif)';
		_YTPlayer.mute();
	}
};




_PLAYER.Video.dropped	=	false;
_PLAYER.Video.dropDown	=	function(){
	if(!_PLAYER.Video.dropped){
		_PLAYER.Video.dropped	=	true;
		
		$('#holds_YTappender').animate({
			marginTop: '65px'
		},800,'linear',function(){
			$('#holds_YTappender').animate({
				marginTop: '17px'
			},200,'linear',function(){
				$('#holds_YTappender').animate({
					marginTop: '57px'
				},200,'linear',function(){
					$('#holds_YTappender').animate({
						marginTop: '47px'
					},150);
				});
			});
		});
	}
};

_PLAYER.seekOnClick	=	function(){
	var getClickPercent_inBar	=	browser.getCursorLeftToLoaderBar();
	var getLoadedPercent		=	_YTPlayer.getPercentsLoaded();
	
	if(getLoadedPercent > getClickPercent_inBar || getLoadedPercent	==	getClickPercent_inBar){
		//seek, all ok
		var getTotalSeconds		=	_YTPlayer.getTotalTime();
		var seekingSeconds		=	Math.round((getClickPercent_inBar/100)*getTotalSeconds);
		var acPlayerState		=	_YTPlayer.State;
		_YTPlayer.play();
		_YTPlayer.pause();
		
		if(acPlayerState==1){ //play if it was playing before
			_YTPlayer.play();
		}
		
		_YTPlayer.seek(seekingSeconds);
	}else{
		alert('not yet loaded this far');
	}
};

_PLAYER.Video.manualStop	=	function(){
	if(_YTPlayer.State==3){
		//dont stop, avoid "streaming stop"-error
	}else{
		_YTPlayer.play();
		_YTPlayer.seek(0);
		_YTPlayer.pause();
		$('#plbtn_playpause').css('background','url(img/playbtn.gif)');
	}
};

_PLAYER.Video.PlayPause	=	function(bb){
	var acPlayerState	=	''+_YTPlayer.State;
	if(acPlayerState=='1'){
		//pause
		_PLAYER.Video.showPLAY();
		_YTPlayer.pause();
	}else if(acPlayerState != '3' && acPlayerState != '-1'){
		_PLAYER.Video.showPAUSE();
		_YTPlayer.play();
	}
};

_PLAYER.Video.showPLAY	=	function(){
	$('#plbtn_playpause').css('background','url(img/playbtn.gif)');
};

_PLAYER.Video.showPAUSE	=	function(){
	$('#plbtn_playpause').css('background','url(img/pausebtn.gif)');
};

_PLAYER.Video.Cue	=	function(vId,title){
	if(_PLAYER.Video.currentId == vId){
		if(_YTPlayer.State==0){ 
			//check if state is like "ended/stopped"
			_YTPlayer.seekAndPlayBeginning();
		}else if(_YTPlayer.getPercentsPlayed()>98){ 
			//or if percents played > 98 repeat!
			_YTPlayer.seekAndPlayBeginning();
		}else{ 
			//else just show, that track is already playing!
			base.tooltipShow('Track is already playing');
		}
	}else{
		_PLAYER.Video.dropDown(); //will only drop if not yet dropped
		_PLAYER.dropControls(); //will only drop if not yet dropped
		
		_INTV.stateIndic.stop(); //stop
		_INTV.stateIndic.init(); //refresh
		_INTV.loaderBars.stop(); //stop
		_INTV.loaderBars.init(); //refresh
		_INTV.seekerArrow.stop(); //stop
		_INTV.seekerArrow.init(); //refresh
		fl('cueVideoById',vId);
		$('#titlePlaying_name').html(Base64.decode(title));
		
		_PLAYER.Video.currentId		=	vId;
		_YTPlayer.play();
		_PLAYER.Video.showPAUSE();
		
		_PLAYLIST.playlist.refreshHTML();
	}
	
	/** -------------------------------- **/
	metaBox.track_meta_data(vId, title);
};
