jQuery(document).ready(function(){

	var playItem = 0;

	var myPlayList = [
		{name:"Traxx Motos - Ivete",mp3:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Jingle_Ivete_60seg.mp3",ogg:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Jingle_Ivete_60seg.ogg"},
		//{name:"Traxx Motos - Sky",mp3:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos -  SKY.mp3",ogg:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos -  SKY.ogg"},
		{name:"Traxx Motos - Star",mp3:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos -  Star 50.mp3",ogg:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos -  Star 50.ogg"},
		{name:"Traxx Motos - Work",mp3:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos - Work 125.mp3",ogg:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos - Work 125.ogg"},
		//{name:"Traxx Motos - Fly",mp3:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos - Fly 125.mp3",ogg:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos - Fly 125.ogg"},
		//{name:"Traxx Motos - Joto",mp3:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos - Joto 125.mp3",ogg:"http://www.traxx.com.br/novotraxx/flashmp3player/mp3/Traxx Motos - Joto 125.ogg"}
	];

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = jQuery("#jplayer_play_time");
	var jpTotalTime = jQuery("#jplayer_total_time");

	jQuery("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(true); // Parameter is a boolean for autoplay.
		},
		oggSupport: true
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text(jQuery.jPlayer.convertTime(playedTime));
		jpTotalTime.text(jQuery.jPlayer.convertTime(totalTime));
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	jQuery("#jplayer_previous").click( function() {
		playListPrev();
		jQuery(this).blur();
		return false;
	});

	jQuery("#jplayer_next").click( function() {
		playListNext();
		jQuery(this).blur();
		return false;
	});

	function displayPlayList() {
		jQuery("#jplayer_playlist ul").empty();
		for (i=0; i < myPlayList.length; i++) {
			var listItem = (i == myPlayList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>";
			listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a> (<a href='" + myPlayList[i].mp3 + "' tabindex='1'>download</a>)</li>";
			jQuery("#jplayer_playlist ul").append(listItem);
			jQuery("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = jQuery(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					jQuery("#jquery_jplayer").jPlayer("play");
				}
				jQuery(this).blur();
				return false;
			});
			jQuery("#jplayer_playlist_get_mp3_"+i).data( "index", i ).click( function() {
				var index = jQuery(this).data("index");
				jQuery("#jplayer_playlist_item_"+index).trigger("click");
				jQuery(this).blur();
				return false;
			});
		}
	}

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

	function playListConfig( index ) {
		jQuery("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
		jQuery("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
		playItem = index;
		jQuery("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
	}

	function playListChange( index ) {
		playListConfig( index );
		jQuery("#jquery_jplayer").jPlayer("play");
	}

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

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

