//dynamic color container
var playList=new Array();
var currentMediaPlaying=new Object();
var lastPlayedID;
var mediaCounter;
var nextPlayerItem=new Object();
var previousPlayerItem=new Object();

function writeDynamicStyle(color){
			var style='a.bodyLink:hover {text-decoration: none;color:'+color+';}\n\n a.footerItemLink:hover {text-decoration: none;color:'+color+';}\n\n a.appearanceButtonLink:hover {text-decoration: none;color:'+color+';}\n\n .buttonMouseOver {color:'+color+';}\n\n .pageNameBorder {border-top-color:'+color+';}\n\n.iconMouseOver {background-color:'+color+';}\n\n.headerColor {color:'+color+'};\n\n .startingLetter {color:'+color+';}\n\n .changeBackground {background-color:'+color+';}\n\n .albumName {color:'+color+';}\n\n  .activeOption {color:'+color+';}\n\n  a.slidingMenuItem:l {color:'+color+';}\n\n  .articleName {color:'+color+';}\n\n   .slidingMenuHandlerLeftBorder {border-left-color:'+color+';}\n\n .activePage {color:'+color+';}\n\n a.modalTextLink:hover {text-decoration: none;color:'+color+';}\n\n a.modalButtonsLink:hover {text-decoration: none;color:'+color+';}\n\n a.appearancesButtonLink:hover {text-decoration: none;color:'+color+';}\n\n  .justDynamicColor {color:'+color+';}\n\n  .headerAudio {color:'+color+';}\n\n .videoName {color:'+color+';}\n\n .galleryEvent {color:'+color+';}\n\n';
			
// ========================================================== 
// ! load the dynamic style into the main document's header   
// ========================================================== 
	$('#DynamicStyle').html(style);
}

function setPicture(data){
	writeDynamicStyle(data.color);
	$('#playerContent').hide();
	$('#playerContent').html('<img  id="playerPicture" width="'+data.width+'px" height="'+data.height+'px" style="'+data.style+'" src="/images/'+data.name+'" alt="angela gheorghiu picture - official website"/>');
	$('#playerContent').fadeIn('slow');
	
	//dynamicColor=data.color;
	setDynamicColor(data.color);
	//
	dynamicColor=data.color;
	//
	//$('#playerPicture').onLoad(center())
	adjustLayout();
}



function mediaPlayerStop(callback){	
	lastPlayedID=currentMediaPlaying.currentID;
		currentMediaPlaying.pause();	
		//currentMediaPlaying.currentTime=0;
		clearInterval(mediaCounter)
		currentMediaPlaying=new Object();
/*
	if(mediaPlaying()){
		currentMediaPlaying.pause();	
		currentMediaPlaying.currentTime=0;
		clearInterval(mediaCounter)
		currentMediaPlaying=new Object();
	}else{
		clearInterval(mediaCounter)
		currentMediaPlaying=new Object();
	}
*/
	if(callback){
		callback('stopped');
	}
	//delete local storage
	if(Modernizr.localstorage){
		localStorage.removeItem('currentMedia');
		localStorage.removeItem('currentMediaCurrentTime');
	}
	//currentMediaPlaying=null;
	$('#iconStop','#controls').css('background-color',siteColor.lightGrey);
	$('#iconPlay','#controls').css('background-color','transparent');
	$('#iconPause','#controls').css('background-color','transparent');

}

function mediaPlayerPlay(element,callback){
	if(currentMediaPlaying.paused){
		currentMediaPlaying.play();
	}
	if(!currentMediaPlaying.src){
		element.addEventListener('error',function(){
			if(element.error.code==4){
				alert('File not supported: '+element.src);
				callback('error',element.src)
			}
			mediaPlayer('reset');
		},false)
		
		element.addEventListener('canplay',function(){
			if(Modernizr.localstorage && localStorage['currentMediaCurrentTime']){
/*
				element.currentTime=Number(localStorage['currentMediaCurrentTime']);
				if(callback){
					callback('seeking');
				}
*/
				element.play();
				if(callback){
					callback('playing');
				}
			}else{
				element.play();
				if(callback){
					callback('playing');
				}
			}			
			//workaround for google chrome
			
			//detect next/previous
			var playlist=new goto(JSON.parse(localStorage['playList']),1);	
			for(i=0;i<playlist.gotoContent.length;i++){			
				if(currentMediaPlaying.currentID==playlist.gotoContent[i].id){
					//set current media info
					currentMediaPlaying.trackName=playlist.gotoContent[i].trackName;
					currentMediaPlaying.composer=playlist.gotoContent[i].composer;
					currentMediaPlaying.albumName=playlist.gotoContent[i].albumName;
					
					if(playlist.gotoContent[i+1]){
						nextPlayerItem.id=playlist.gotoContent[i+1].id;										
						nextPlayerItem.src='/tracks/'+playlist.gotoContent[i+1].id+browserCap.audioExtension;		
						nextPlayerItem.trackName=playlist.gotoContent[i+1].trackName;
						nextPlayerItem.composer=playlist.gotoContent[i+1].composer;
						nextPlayerItem.albumName=playlist.gotoContent[i+1].albumName;								
					}else{
						nextPlayerItem.id=false;
						nextPlayerItem.src=false;
					}
					if(playlist.gotoContent[i-1]){
						previousPlayerItem.id=playlist.gotoContent[i-1].id;										
						previousPlayerItem.src='/tracks/'+playlist.gotoContent[i-1].id+browserCap.audioExtension;	
						previousPlayerItem.trackName=playlist.gotoContent[i-1].trackName;
						previousPlayerItem.composer=playlist.gotoContent[i-1].composer;
						previousPlayerItem.albumName=playlist.gotoContent[i-1].albumName;	
														
					}else{
						previousPlayerItem.id=false;
						previousPlayerItem.src=false;
					}					
					break;
				}
			}	
			//write file to localStorage
			if(Modernizr.localstorage){
				var storeMedia=new Object();
				storeMedia.id=currentMediaPlaying.currentID;
				storeMedia.src=currentMediaPlaying.src;
								
				localStorage['currentMedia']=JSON.stringify(storeMedia)
			}
				
		},true)	
	}

	
	$('#iconPlay','#controls').css('background-color',siteColor.lightGrey);
	$('#iconPause','#controls').css('background-color','transparent');
	$('#iconStop','#controls').css('background-color','transparent');
	
}

function mediaPlayerPause(element,callback){
	if(mediaPlaying()){
		currentMediaPlaying.pause();
			if(callback){
			callback('paused');
		}
		$('#iconPlay','#controls').css('background-color','transparent');
		$('#iconStop','#controls').css('background-color','transparent');
		$('#iconPause','#controls').css('background-color',siteColor.lightGrey);
	}

}

function mediaPlayerCounter(display,limit){
	//var mediaCounter;
	if(currentMediaPlaying.src && !currentMediaPlaying.paused){
		mediaCounter=setInterval(function(){
			var remainingTime			
			if(limit){
				remainingTime=((limit-currentMediaPlaying.currentTime)).toFixed(0);
				if(currentMediaPlaying.currentTime>limit){
					mediaPlayer('stop');
				}				
			}else{
				remainingTime=((currentMediaPlaying.duration-currentMediaPlaying.currentTime)/60).toFixed(2);
			}
			if(remainingTime>=0){
				if(display){
					display.text(' ('+remainingTime+')');
				}else{
					return remainingTime;
				}
				
			}
			//write localStorage with the current file values
			if(Modernizr.localstorage){
				localStorage['currentMediaCurrentTime']=currentMediaPlaying.currentTime;
			}
			
		},1000)	
			
	}
}

function mediaPlaying(){
	if(currentMediaPlaying.src && currentMediaPlaying.paused){
		return false;
	}else if(currentMediaPlaying.src && !currentMediaPlaying.paused) {
		return true;
	}else if(!currentMediaPlaying.src){
		return false;
	}
}

function getVideoBackground(){

	$('#playerContent').html('<video id="videoBackground" width="640px" height="360px"></video>');	
	mediaPlayer('play',function(){},null,"/movies/vissidarte"+browserCap.videoExtension,'video','videoBackground')
	$('#playerContent').css('left','-100px')
	
	color='#af381d';	
	writeDynamicStyle(color);
	//center controls
	$('#controls').show();
}

function mediaPlayer(action,callback,id,src,mediaType,container){	
	var mediaFile;
	var fullSRC
	if(action=="play"){
		//the source path returned by the browser for the media playing
		if(src.split('/'[0]=='http:')){
			fullSRC=src;
		}else{
			fullSRC='http://'+document.location.hostname+src;
		}
		if(container){
			mediaFile=document.getElementById(container);
		}else{
			mediaFile=document.createElement(mediaType);		
		}
		mediaFile.src=fullSRC;
		//detect if any media is playing
		if(mediaPlaying()){
			//same src -> same file playing so we have to delete currentMediaPlaying
			if(fullSRC==currentMediaPlaying.src){
				mediaPlayerStop(callback);
			}else{
				mediaPlayerStop(callback);
				mediaPlayerPlay(mediaFile,callback);
				currentMediaPlaying=mediaFile;
				currentMediaPlaying.currentID=id;
				currentMediaPlaying.mediaType=mediaType;
			}
		}else{
			//no file playing -> play passed one
			mediaPlayerPlay(mediaFile,callback);
			currentMediaPlaying=mediaFile;
			currentMediaPlaying.currentID=id;
			currentMediaPlaying.mediaType=mediaType;
		}
	}else if (action=="pause"){
		mediaPlayerPause(callback);
	}else if(action=="stop"){
		mediaPlayerStop(callback);
	}else if(action=='reset'){
		if(mediaPlaying()){
			mediaPlayerStop();
		}
	}
}

function mediaPlayerNext(){
	if(nextPlayerItem.id){
		mediaPlayer('play',function (){
		},nextPlayerItem.id,nextPlayerItem.src,'audio');
	}
}

function mediaPlayerPrevious(){
	if(previousPlayerItem.id){
		mediaPlayer('play',function (){
		},previousPlayerItem.id,previousPlayerItem.src,'audio');
	}
}

function getPictureBackground(){
	$.post('scripts/getPlayerBackground.php',setPicture,'json');
}

function openPlayList(){
	var content={title:'Music player',playList:playList};
	createPopUpWindow('mediaplayer',content, 550, 360)
}

$(document).ready(function(){
	//enable stop
	$('#iconStop','#controls').css('background-color',siteColor.lightGrey);
	
	//add bubbles
	$('#iconPlay').CreateBubblePopup(optionsTooltips);
	$('#iconNext').CreateBubblePopup(optionsTooltips);
	$('#iconPrevious').CreateBubblePopup(optionsTooltips);
			
	//player buttons settings
	$('.playerButton').hover(
		function(){
			if(currentMediaPlaying.mediaType=='video'){
				if(mediaPlaying() || currentMediaPlaying.paused){
					setInnerHtmlTooltip($('#iconPlay'),'<b>Vissi D\'arte</b>');
				}else{
					setInnerHtmlTooltip($('#iconPlay'),'Press to see the play list');
				}
				
				if(nextPlayerItem){
						setInnerHtmlTooltip($('#iconNext'),'<b>No NEXT video file</b>');
				}	
					
				if(previousPlayerItem){
						setInnerHtmlTooltip($('#iconPrevious'),'<b>No PREVIOUS video file</b>');
				}			
			}else{
				if(mediaPlaying() || currentMediaPlaying.paused){
					setInnerHtmlTooltip($('#iconPlay'),'<b>'+currentMediaPlaying.trackName+'</b><br>by <b>'+currentMediaPlaying.composer+'</b><br>from the album <b>'+currentMediaPlaying.albumName+'</b>');
				}else{
					setInnerHtmlTooltip($('#iconPlay'),'Press to see the play list');
				}
				
				if(nextPlayerItem){
						setInnerHtmlTooltip($('#iconNext'),'<b>'+nextPlayerItem.trackName+'</b><br>by <b>'+nextPlayerItem.composer+'</b><br>from the album <b>'+nextPlayerItem.albumName+'</b>');
				}	
					
				if(previousPlayerItem){
						setInnerHtmlTooltip($('#iconPrevious'),'<b>'+previousPlayerItem.trackName+'</b><br>by <b>'+previousPlayerItem.composer+'</b><br>from the album <b>'+previousPlayerItem.albumName+'</b>');
				}			
			}
					
			$(this).ShowBubblePopup();
			BubbleObject=$(this);			
			$(this).css('cursor','pointer');
			if(rgb2hex($(this).css('background-color'))!=siteColor.lightGrey){
				$(this).css('background-color',dynamicColor);
			}			
		},
		function(){
			$(this).css('cursor','default');
			if(rgb2hex($(this).css('background-color'))!=siteColor.lightGrey){
				$(this).css('background-color','transparent');	
			}			
			$(this).HideBubblePopup();		
		}
	)
	

	
	$('.playerButton').mouseup(function(event){
		event.stopImmediatePropagation();
		if($(this).attr('id')=='iconPlay'){
			if(currentMediaPlaying.mediaType=='video'){
				return;
			}
			if(currentMediaPlaying.paused){
				mediaPlayerPlay(currentMediaPlaying,null);
			}else{
				if(browserCap.hasAudio){
					openPlayList();
					/*
					var content={title:'Music player',playList:playList};
					createPopUpWindow('mediaplayer',content, 550, 360);
					*/			
				}else{
					alert('Your browser cannot play audio files.\nPlease update it to a HTML5 compliant version.');
				}			
			}
			
		}else if($(this).attr('id')=='iconPause'){
			mediaPlayerPause();
		}else if($(this).attr('id')=='iconStop'){
			if(currentMediaPlaying.mediaType=='video'){
				mediaPlayerStop()
				$('#playerContent').html('');
				$('#playerContent').css('left','');
				getPictureBackground();
			}else{
				mediaPlayerStop()
			}	
		}else if($(this).attr('id')=='iconPrevious'){
			mediaPlayerPrevious()
		}else if($(this).attr('id')=='iconNext'){
			mediaPlayerNext()
		}	
		
	});

})


