$j(document).ready(function(){
	var dsmd_home ='http://dsmd.lt/wp-content/themes/dsmd/lib/jplayer/';
	var MP3 = dsmd_home+'dsmd_beat/';
	var OGG = dsmd_home+'dsmd_beat/ogg/';
	
	var playItem = 0;

	var myPlayList = [
		{name:"Song 1",	mp3: MP3+"lerkini.mp3",ogg: OGG+"lerkini.ogg"},
		{name:"Song 2",	mp3: MP3+"rytas.mp3",ogg: OGG+"rytas.ogg"},
		{name:"Song 3",	mp3: MP3+"skit.mp3",ogg: OGG+"skit.ogg"},
		{name:"Song 4",	mp3: MP3+"sugrk.mp3",ogg: OGG+"sugrk.ogg"},
		{name:"Song 5",	mp3: MP3+"traveller.mp3",ogg: OGG+"traveller.ogg"}
	];

	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $j("#jplayer_play_time");
	var jpTotalTime = $j("#jplayer_total_time");

	$j("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
		},
		oggSupport: true
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($j.jPlayer.convertTime(playedTime));
		jpTotalTime.text($j.jPlayer.convertTime(totalTime));
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	$j("#jplayer_previous").click( function() {
		playListPrev();
		$j(this).blur();
		return false;
	});

	$j("#jplayer_next").click( function() {
		playListNext();
		$j(this).blur();
		return false;
	});

	function displayPlayList() {
		$j("#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></li>";
			$j("#jplayer_playlist ul").append(listItem);
			$j("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $j(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$j("#jquery_jplayer").jPlayer("play");
				}
				$j(this).blur();
				return false;
			});
		}
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$j("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current");
		$j("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current");
		playItem = index;
		$j("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg);
	}

	function playListChange( index ) {
		playListConfig( index );
		$j("#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 );
	}
});
