$(document).ready(function(){

	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.7, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});


	var playItem = 0;

	var myPlayList = [
		{name:"Dusty - Separated Mind",filename:"http://dusty.melnicek.cz/WEB/music/dusty-separated-mind.mp3"},
		{name:"Nooly&Dusty - RiverTank",filename:"http://dusty.melnicek.cz/WEB/music/nooly-and-dusty-rivertank.mp3"},
		{name:"Dusty - OverTime",filename:"http://dusty.melnicek.cz/WEB/music/dusty-overtime.mp3"},
		{name:"Dusty - Political Machine",filename:"http://dusty.melnicek.cz/WEB/music/dusty-political-machine.mp3"},
		{name:"Dusty - Roses From Heaven",filename:"http://dusty.melnicek.cz/WEB/music/dusty-roses-from-heaven.mp3"},
		{name:"LowFuel - HoneyBlaster Soundtrack ukazky",filename:"http://dusty.melnicek.cz/WEB/music/lowfuel-hb-soundtrack.mp3"},
		{name:"Dusty - Hudba do vanocniho pranicka",filename:"http://dusty.melnicek.cz/WEB/music/dusty-xmas.mp3"},
		{name:"Dusty idea (nedokonceny motiv)",filename:"http://dusty.melnicek.cz/WEB/music/dusty-idea.mp3"},
		{name:"MoreOptions - Do Ticha (ze zkusebny)",filename:"http://dusty.melnicek.cz/WEB/music/moreoptions/MO-do-ticha.mp3"},
		{name:"MoreOptions - Speedarina (ze zkusebny)",filename:"http://dusty.melnicek.cz/WEB/music/moreoptions/MO-speedarina.mp3"},
		{name:"MoreOptions - Lies (ze zkusebny)",filename:"http://dusty.melnicek.cz/WEB/music/moreoptions/MO-lies.mp3"},
		{name:"Nooly&Dusty - Mlazice Fighter",filename:"http://dusty.melnicek.cz/WEB/music/nooly-and-dusty-mlazice-fighter-intro.mp3"},
		{name:"Dusty - Alien Buggy SoundTrack",filename:"http://dusty.melnicek.cz/WEB/music/dusty-alienbuggy.mp3"},
		{name:"Dusty - Nostalgia Time",filename:"http://dusty.melnicek.cz/WEB/music/dusty-nostalgia-time.mp3"},
		{name:"Dusty - Jarni romance",filename:"http://dusty.melnicek.cz/WEB/music/dusty-jarni-romance.mp3"},
		{name:"Dusty - ElectroMania",filename:"http://dusty.melnicek.cz/WEB/music/dusty-electromania.mp3"},
		{name:"Dusty - Fallacy (2000)",filename:"http://dusty.melnicek.cz/WEB/music/dusty-fallacy.mp3"},
		{name:"Dusty - Cradle Of Filth (1999)",filename:"http://dusty.melnicek.cz/WEB/music/dusty-cradle-of-filth.mp3"}
	];


	$("#jquery_jplayer").jPlayer({
		ready: function() {
			displayPlayList();
			playListInit(false); // Parameter is a boolean for autoplay.
		}
	})
	.jPlayerId("play", "player_play")
	.jPlayerId("pause", "player_pause")
	.jPlayerId("stop", "player_stop")
	.jPlayerId("loadBar", "player_progress_load_bar")
	.jPlayerId("playBar", "player_progress_play_bar")
	.jPlayerId("volumeMin", "player_volume_min")
	.jPlayerId("volumeMax", "player_volume_max")
	.jPlayerId("volumeBar", "player_volume_bar")
	.jPlayerId("volumeBarValue", "player_volume_bar_value")
	.onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		var myPlayedTime = new Date(playedTime);
		var ptMin = (myPlayedTime.getMinutes() < 10) ? "0" + myPlayedTime.getMinutes() : myPlayedTime.getMinutes();
		var ptSec = (myPlayedTime.getSeconds() < 10) ? "0" + myPlayedTime.getSeconds() : myPlayedTime.getSeconds();
		$("#play_time").text(ptMin+":"+ptSec);

		var myTotalTime = new Date(totalTime);
		var ttMin = (myTotalTime.getMinutes() < 10) ? "0" + myTotalTime.getMinutes() : myTotalTime.getMinutes();
		var ttSec = (myTotalTime.getSeconds() < 10) ? "0" + myTotalTime.getSeconds() : myTotalTime.getSeconds();
		$("#total_time").text(ttMin+":"+ttSec);
	})
	.onSoundComplete( function() {
		playListNext();
	});

	$("#ctrl_prev").click( function() {
		playListPrev();
		return false;
	});

	$("#ctrl_next").click( function() {
		playListNext();
		return false;
	});

	function displayPlayList() {
		$("#playlist_list ul").text('');
		for (i=0; i < myPlayList.length; i++) {

			$("#playlist_list ul").append("<li id='playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
			$("#playlist_item_"+i).data( "index", i ).hover(
				function() {
					if (playItem != $(this).data("index")) {
						$(this).addClass("playlist_hover");
					}
				},
				function() {
					$(this).removeClass("playlist_hover");
				}
			).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				}
			});
		}
		$('.scroll-pane').jScrollPane();
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$("#playlist_item_"+playItem).removeClass("playlist_current");
		$("#playlist_item_"+index).addClass("playlist_current");
		playItem = index;
  		$("#song_title").text(myPlayList[playItem].name);
		$("#jquery_jplayer").setFile(myPlayList[playItem].filename);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_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 );
	}
});
