var mousepos = {x: 0, y: 0};

plus.player.setDisplay("playlist", function(){
	var radioPlayerList = $("#radioPlayerList");
	radioPlayerList.html("");
	
	for (var i in this.prop.list){
		var item = this.prop.list[i];
		var a = $("<a>").html(item.title);
		a.attr('index', i);
		a.attr('href', 'javascript:plus.player.ctrl.setItem('+i+')');
		radioPlayerList.append(a);
	}
});

plus.player.setDisplay("state", function(){
	var playerButtonPlay = $("#playerButtonPlay");
	playerButtonPlay.attr('class','');
	playerButtonPlay.addClass("playerBt");
	playerButtonPlay.addClass("player-state-"+this.prop.state);
});

plus.player.setDisplay("time", function(){
	if (controlSeek == false){
		var playerTime = $("#playerTime");
		var playerSeekP = $("#playerSeekP");
		playerTime.html(this.formatTime(this.prop.position)+"<br />"+this.formatTime(this.prop.duration));
		if (this.prop.duration == 0){
			playerSeekP.css("width", "0%");
		}else{
			playerSeekP.css("width", (this.prop.position/this.prop.duration*100)+"%");
		}
	}
});

plus.player.setDisplay("item", function(){
	var radioPlayerList = $("#radioPlayerList");
	var radioPlayerBtPlaylist = $("#radioPlayerBtPlaylist");
	var radioPlayerFooter = $("#radioPlayerFooter");
	var a = radioPlayerList.children("a[index="+this.prop.current+"]");
	var tempo = 150;
	var colorTo = "#E6EFB3";
	var colorFrom = "#FFFFFF";
	
	radioPlayerList.children().attr("class","");
	if (this.prop.single){
		a
		.animate({backgroundColor: colorTo}, tempo)
		.animate({backgroundColor: colorFrom}, tempo)
		.animate({backgroundColor: colorTo}, tempo)
		.animate({backgroundColor: colorFrom}, tempo)
		.animate({backgroundColor: colorTo}, tempo, "linear", function(){
			$(this).css("background-color", "");
			$(this).addClass("playing");
			radioPlayerList.children().not(a).attr("class","");
		});
		radioPlayerFooter.show();
	}else{
		a.animate({backgroundColor: colorTo}, tempo, "linear", function(){
			$(this).css("background-color", "");
			$(this).addClass("playing");
			radioPlayerList.children().not(a).attr("class","");
		});
		radioPlayerFooter.hide();
	}
});

plus.player.setDisplay("volume", function(){
	var playerVolP = $("#playerVolP");
	playerVolP.css("width", this.prop.volume+"%");
});

function loadPlayerMusic(file, title, obj){
	plus.player.ctrl.loadURL(file);
	
	var overMusic = $("#overMusic");
	var radioPlayer = $("#radioPlayer");
	var obj = $(obj);
	
	var posini = obj.offset();
	var posfim = radioPlayer.offset();
	
	overMusic.css("top", posini.top+obj.height()/2);
	overMusic.css("left", posini.left+obj.width()/2);
	overMusic.css("display", "block");
	
	overMusic.fadeTo(60, 1);
	overMusic.animate({top: posfim.top+60, left: posfim.left+80}, {queue: false, duration: 1000, easing: "swing", complete: function(){
		overMusic.fadeTo(100, 0).css("display", "none");
	}});
}

var $playerVolBar, $playerVolC, $playerVolP, $playerSeekBar, $playerSeekC, $playerSeekP;
var controlVol = false;
var controlSeek = false;
var seekPercentage;

function radioControlVolDown(e){
	e.preventDefault();
	controlVol = true;
}
function radioControlSeekDown(e){
	e.preventDefault();
	controlSeek = true;
}
function radioControlsUp(e){
	if (controlSeek){
		plus.player.ctrl.seek(seekPercentage);
	}
	controlVol = false;
	controlSeek = false;
}
function radioMouseControls(e){
	if (controlVol){
		var posBar = $playerVolBar.offset().left;
		var barW = mousepos.x-posBar-5;
		var totalW = $playerVolBar.innerWidth()-10;
		
		if (barW < 0) barW = 0;
		else if(barW > totalW) barW = totalW;
		
		$playerVolP.css("width", barW+"px");
		plus.player.ctrl.volume(Math.round(barW/totalW*100));
	}
	if (controlSeek){
		var posBar = $playerSeekBar.offset().left;
		var barW = mousepos.x-posBar-5;
		var totalW = $playerSeekBar.innerWidth()-10;
		
		if (barW < 0) barW = 0;
		else if(barW > totalW) barW = totalW;
		
		seekPercentage = barW/totalW;
		$playerSeekP.css("width", barW+"px");
	}
}

$(document).ready(function(){
	$playerVolBar = $("#playerVolBar");
	$playerVolC = $("#playerVolC");
	$playerVolP = $("#playerVolP");
	$playerSeekBar = $("#playerSeekBar");
	$playerSeekC = $("#playerSeekC");
	$playerSeekP = $("#playerSeekP");
	
	$playerVolC.mousedown(radioControlVolDown);
	$playerSeekC.mousedown(radioControlSeekDown);
	$("body").mousemove(function(e){
		mousepos.x = e.pageX;
		mousepos.y = e.pageY;
		radioMouseControls();
	}).mouseup(radioControlsUp);
});
