
var player = null;

function playerReady(obj){
	var id = obj['id'];
	player = document.getElementById(id);
	setEvent();
//	setModel();
};

var opened=false;
var curS=0;

window.onload=startEm;

function startEm(){
	if(pesta==1){
		$('#tab img').attr('src','img/gran-leftarrow.gif');
		$('#pcont').animate({left:'0px'},500,function(){
			$('#tab img').attr('src','img/gran-rightarrow.gif');
			$('#lista').animate({top:'200px'});
			$('#pcont').animate({left:'-500px'},500);
		});
	}
}

$(function(){
	// settings
	if(pesta!=1){$('#play').find('img').attr('src','img/play.gif');}

	$('#slidercont').slider({min:0, max:100, startValue:50, slide: function(e,ui){sendEvent('volume',(100-ui.value));}});
	
	var ig=$('.rest-pres').height()-$('.rest-pres').parent('div').height();
	if(ig>0){
		$('#drag').slider({min:0, max:ig+5,slide: function (e,u){$('.rest-pres').css('top',(u.value*-1)+'px');}});
		$(".rest-pres").mousewheel(function(e,d){
			(d<0)?k=-5:k=5;
			we=parseInt($(".rest-pres").css("top"))+k;
			wo=$(".rest-pres").height()-$(".resp-pres").parent("div").height()+5;
			if(we<1 && we>(wo*-1)){$(".rest-pres").css("top", we+"px");$("#drag").slider("moveTo",Math.abs(we),0);}
		});

	}else{$('#drag').hide();}
	
	$('.rest-pres span:eq(0)').html('<img style="padding-top:1px;" src="img/flecha-player.gif" alt=""\/>');

	// funcs		
	$('#tab').click(function(){
		if(parseInt($('#pcont').css('left'))==0){
			$('#tab img').attr('src','img/gran-rightarrow.gif');
			$('#lista').animate({top:'200px'});
			$('#pcont').animate({left:'-500px'},300);
		}else{
			$('#tab img').attr('src','img/gran-leftarrow.gif');
			$('#pcont').animate({left:'0px'},300);
			if(opened){$('#lista').animate({top:'0px'},300);}
		}
	});

	$('#prev,#next,#play,#lista a,#playlist').click(function(){return false;});
	$('#prev').click(function(){
		if(player.getConfig().item!=0){
			curS=player.getConfig().item-1;
			setInfo(curS);
			sendEvent('PREV',null);
		}
	});
	$('#next').click(function(){
		if($('.playitem').length>player.getConfig().item+1){
			curS=player.getConfig().item+1;
			setInfo(curS);
			sendEvent('NEXT',null);
		}
	});
	$('#play').click(function(){
		z=$(this).find('img');
		(z.attr('src').indexOf('play')!=-1)?z.attr('src','img/pause.gif'):z.attr('src','img/play.gif');
		sendEvent('PLAY',null);
	});
	$('#lista a').click(function(){
		sendEvent('ITEM',parseInt($(this).attr('rel')));
		curS=parseInt($(this).attr('rel'));
		setInfo(curS);
	});
	$('#playlist').click(function(){
		(parseInt($('#lista').css('top'))==0)?g=200:g=0;
		$('#lista').animate({top:g+'px'},300);
		setOpen();
	});

	$('.sendsong').click(function(){
		t=$(this).attr('rel');
		if(t!=''){
			tm=parseInt($('.rest-pres a[href$='+t+']').attr('rel'));
			if(tm.length!=0){
				curS=tm;
				sendEvent('ITEM',curS);
				setInfo(curS);
			}
		}
		return false;
	});

	if($('.playitem').length){$('#infos').html($('.playitem').eq(0).html().replace(' - ','<br/>'));}

});

function setOpen(){(opened)?opened=false:opened=true;}

function sendEvent(typ,val){thisMovie('mpl').sendEvent(typ,val);};
function thisMovie(swf){if($.browser.msie){return window[swf]}else{return document[swf];}}
function setEvent(){thisMovie('mpl').addControllerListener('ITEM','playback');}
function playback(){setInfo(player.getConfig().item);}

function setModel(){
	thisMovie('mpl').addModelListener('LOADED','debug');
	thisMovie('mpl').addModelListener('ERROR','pstate');
};

function debug(obj){$('.containerTitulon h3').eq(0).html(obj.loaded+'->'+obj.total);}

function pstate(obj){alert(obj.message);}

function setInfo(curS){
	if(!isNaN(curS)){
		$('.rest-pres span').html('&nbsp;');
		$('.rest-pres span').eq(curS).html('<img style="padding-top:1px;" src="img/flecha-player.gif" alt=""\/>');
		$('#infos').html($('.playitem').eq(curS).html().replace(' - ','<br/>'));
		z=$('#play').find('img');
		if(z.attr('src').indexOf('play')!=-1){z.attr('src','img/pause.gif');}
	}
}
