/* Author: 

*/

var playItem = 0;
var tempoAntigo = 0;
var soundM = false;

$(document).ready(function(){
   
   //player
   $('#hh_player').jPlayer({
       ready: function(){
           playListInit(true);
       },
       swfPath: baseUrl + ASSETS + "/swf",
       preload:'none'
        
   }).jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime){

           $('#jplayer_total_time').html($.jPlayer.convertTime(playedTime));
   
        //cookie
        // if(playedTime > (tempoAntigo + 2000)){
        // 
        //     $.cookie("disco", $('#currentdisc .disco').text(), {path: '/'});
        //     $.cookie("faixa", playItem, {path: '/'});
        //     $.cookie("tempo", playedTime, {path: '/'});
        // 
        //     tempoAntigo =   playedTime;
        // }
   }).jPlayer("onSoundComplete",function(){
      setTimeout('playListNext()', 1000);
   });


   $("#jplayer_play").click( function() {
       if( soundM )
            soundManager.stopAll();
   }) 
    $("#jplayer_previous").click( function() {
		playListPrev();
		$(this).blur();
		return false;
	});

	$("#jplayer_next").click( function() {
		playListNext();
		$(this).blur();
		return false;
	});
        
    $("#jplayer_pause").click( function() {
            resetPlayer();
    });
    
    $("select[name=faixas]").change(function(){
    	playListChange( $(this).val() );
    });

/* 
	$(".tracks-list a").click(function() {
		playListChange( $(this).parent().parent().index() );
	});
 */

        
        // if( $.cookie("tempo") != null ){
        //     
        //         if( $("#currentdisc .disco").text() != $.cookie("disco") )
        //             $("#discs .disco").each(function(i,val){
        //                 if( $(val).text() == $.cookie("disco"))
        //                     $(this).parent().parent().parent().find('a').click();
        //             });
        //             $("#hh_player").jPlayer("stop");
        //         setTimeout( function() { 
        //             playListConfig( $.cookie("faixa") );
        //             $("#hh_player").jPlayer("playHeadTime", $.cookie("tempo"));
        //         }, 1000);
        //         
        // }
        
        
});

function playListInit(autoplay) {
        if(autoplay) {
                playListChange( playItem );
        } else {
                playListConfig( playItem );
        }
}


function playListConfig( index ) {
        playItem = index;
        $("#hh_player").jPlayer("setFile", playList[playItem].mp3 );
        $('#currentdisc .musicname a').text(playList[playItem].name);

}


function playListChange( index ) {
        playListConfig( index );
        $("#hh_player").jPlayer("play");
        $("select[name=faixas] option").each(function(){
        	if( $(this).index() == index )
        		$(this).attr("selected","selected");
        });
}

function playListNext() {
        var index = (playItem+1 < playList.length) ? playItem+1 : 0;
        playListChange( index );
        resetPlayer();
        tempoAntigo = 0;
}

function playListPrev() {
        var index = (playItem-1 >= 0) ? playItem-1 : playList.length-1;
        playListChange( index );
        resetPlayer();
        tempoAntigo = 0;
}

function resetPlayer() {
        
        $.cookie("disco",null, {path: '/'});
        $.cookie("faixa",null, {path: '/'});
        $.cookie("tempo",null, {path: '/'});
}
