// JavaScript Document
$(document).ready(function()
    	{			
				/****  The Player  ****/
				// Local copy of jQuery selectors, for performance.
				var jpPlayTime  = $("#jplayer_play_time");
				var jpTotalTime = $("#jplayer_total_time");
				var $list 		= $("#jplayer_playlist ol");
				var playing 	= false;


				var $coverflow = $('#flip').jcoverflip({
					change: function(event, ui){
						playSong(ui.to+1);
						return false;
					 } 
				});

				/**
				* Innitialize the Player 
				* (see jPlayer documentation for other options)
				*/
				$("#jquery_jplayer").jPlayer({
					//oggSupport	: true,
					preload		:"none",
					swfPath: "/scripts/",
					graphicsFix: true,
					supplied: 'mp3, ogg',
					cssSelectorAncestor: '#jp_interface',
					ready: function () {
						// Bind current songs
						$('#jplayer_playlist').find('a').bind('click',clickSong);
						$('#jplayer_playlist').find('span').bind('click',removeSong);
						var mp3file			= '/stream/' + $('#jplayer_playlist').first().find('.mp_mp3').html() + '.mp3';
						var oggfile			= '/stream/' + $('#jplayer_playlist').first().find('.mp_mp3').html() + '.ogg';
						$('#jplayer_playlist li').first().addClass("jplayer_playlist_current");
						jpTotalTime.show();
						jpPlayTime.show();
						$(this).jPlayer("setMedia", {mp3: mp3file, ogg: oggfile});
					},
					play: function() {
						playing = true;
					},
					ended: function() {
						playing = true;
						playListNext();
					}
				})
				.jPlayer("onProgressChange", 
					function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
					jpPlayTime.text($.jPlayer.convertTime(playedTime));
					jpTotalTime.text($.jPlayer.convertTime(totalTime));
				});

				/**
				* click previous button, plays previous song
				*/
				$("#jplayer_previous").bind('click', function(){
					playListPrev();
					$(this).blur();
					return false;
				});

				/**
				* click next button, plays next song
				*/
				$("#jplayer_next").bind('click', function() {
					playListNext();
					$(this).blur();
					return false;
				});

				/**
				* User clicks on a song in the playlist. Plays it
				*/
				function clickSong() {
						var index = $(this).parent().index();
						playing = true;
						playSong(index+1);
						return false;
				}

				/**
				* plays song in position i of the list of songs (playlist)
				*/
				function playSong(i){
					
					var $next_song 		= $list.find('li:nth-child('+ i +')');
					var mp3file				= '/stream/' + $next_song.find('.mp_mp3').html() + '.mp3';
					var oggfile				= '/stream/' + $next_song.find('.mp_mp3').html() + '.ogg';
					$list.find('.jplayer_playlist_current').removeClass("jplayer_playlist_current");
					$next_song.addClass("jplayer_playlist_current");
					
					if ($('#flip').length) {
						$('#flip').jcoverflip( 'current', i-1 );
					}
					if ($('.nocoverflow').length) {
						$('.nocoverflow img').attr('src', $next_song.find('.mp_image').html());
						$('.nocoverflow img').attr('alt', $next_song.find('a').html());
						$('.nocoverflow span.title').html($next_song.find('.mp_artist').html() + "<br />" + $next_song.find('.mp_title').html());
						$('.nocoverflow span.album').html("Taken from <em>" + $next_song.find('.mp_album').html() + "</em>");
					}
					
					if (playing) {
						$("#jquery_jplayer").jPlayer("setMedia", {mp3: mp3file, ogg: oggfile}).jPlayer("play");
					} else {
						$("#jquery_jplayer").jPlayer("setMedia", {mp3: mp3file, ogg: oggfile});
					}
				}
				
				/**
				* plays the next song in the playlist
				*/
				function playListNext() {
					var $list_nmb_elems = $list.children().length;
					var $curr 			= $list.find('.jplayer_playlist_current');
					var idx				= $curr.index();
					var index 			= (idx < $list_nmb_elems-1) ? idx+1 : 0;
					
					// Stop songs looping.
					if (index == 0) {
						playing = false;
					}
					playSong(index+1);
				}

				/**
				* plays the previous song in the playlist
				*/
				function playListPrev() {
					var $list_nmb_elems = $list.children().length;
					var $curr 			= $list.find('.jplayer_playlist_current');
					var idx				= $curr.index();
					var index 			= (idx-1 >= 0) ? idx-1 : $list_nmb_elems-1;
					playSong(index+1);
				}
				
				/**
				* User clicks the play icon on one of the songs listed for an band.
				* Adds it to the Playlist, and plays it
				*/
				function addFirst(song_idx,band,name,mp3,ogg) {
					$list.empty();
					/**
					* each song element in the playlist has:
					* - span for the close / remove action
					* - the mp3 path
					* - the ogg path
					*/
					var song_html = "<h3><a href='#' tabindex='1'>" + band + "</a></h3><p><a href='#' tabindex='1'>" + name + "</a></p>";
					song_html 	 += "<span></span>";
					song_html 	 += "<div class='mp_mp3' style='display:none;'>"+mp3+"</div>";
					var $listItem = $("<li/>",{
						id			: song_idx,	
						className	: 'jplayer_playlist_current',
						html 		: song_html
					});
					$list.append($listItem);
					//event to play the song when User clicks on it
					$listItem.find('a').bind('click',clickSong);
					//event to remove the song from the playlist when User clicks the remove button
					$listItem.find('span').bind('click',removeSong);
					//start playing it
					$("#jquery_jplayer").jPlayer("setFile", mp3, ogg).jPlayer("play");
					jpTotalTime.show();
					jpPlayTime.show();
				}
				
				/**
				* adds a song to the playlist, if not there already.
				* if it is the only one, start playing it
				*/
				function addToPlayList(song_idx,band,name,mp3,ogg) {
					var $list_nmb_elems = $list.children().length;
					//if already in the list return
					if($list.find('#'+song_idx).length)
						return;
					//class for the current song being played
					var c = '';
					if($list_nmb_elems == 0)
						c = 'jplayer_playlist_current';
					var song_html = "<h3><a href='#' tabindex='1'>" + band + "</h3><p><a href='#' tabindex='1'>" + name + "</a></p>";
					song_html 	 += "<span></span>";
					song_html 	 += "<div class='mp_mp3' style='display:none;'>"+mp3+"</div>";
					var $listItem = $("<li/>",{
						id			: song_idx,	
						className	: c,
						html 		: song_html
					});
					$list.append($listItem);
					//if its the only song play it
					if($list_nmb_elems == 0){
						$("#jquery_jplayer").jPlayer("setFile", mp3, ogg).jPlayer("play");
						jpTotalTime.show();
						jpPlayTime.show();
					}
					$listItem.find('a').bind('click',clickSong);
					$listItem.find('span').bind('click',removeSong);
				}
				
				/**
				* removes a song from the playlist.
				* if the song was the current one, and there are more songs 
				* in the playlist, then plays the next one.
				* if there are no more, stops the player, and removes the status bar
				* otherwise keeps playing whatever song was being played
				*/
				function removeSong() {
					var $this 	= $(this); 
					var current = $this.parent().parent().find('li.jplayer_playlist_current').index();
					$this.parent().remove();
					/*
					if ($('#flip').length) {
						$('#flip li').eq(current+1).remove();
						$('#flip').jcoverflip().remove();
					}*/
					var $list_nmb_elems = $list.children().length;
					if($list_nmb_elems > 0 && current) {
						playListNext();
					} else if($list_nmb_elems == 0){
						$("#jquery_jplayer").jPlayer("clearFile");
						jpTotalTime.hide();
						jpPlayTime.hide();
					}	
					return false;
				}
				

				
				/**
				* The next are the events for clicking on both "play" and "add to playlist" icons
				*/
				$('#mp_content_wrapper').find('.mp_play').bind('click',function(){
					var $this 		= $(this);
					var band   		= $this.closest('li').find('h3').html();
					var $paths		= $this.parent().siblings('.mp_song_info');
					var title   	= $paths.find('.mp_song_title').html();
					var mp3			= $paths.find('.mp_mp3').html();
					var band_id 	= $this.closest('.mp_content').attr('id');
					var song_index	= $this.parent().parent().index();
					var song_idx	= band_id + '_' + song_index;
					//add to playlist and play the song
					addFirst(song_idx,band,title,mp3,ogg);
				});
				$('#mp_content_wrapper').find('.mp_addpl').bind('click',function(){
					var $this 		= $(this);
					var band   		= $this.closest('li').find('h3').html();
					var $paths		= $this.parent().siblings('.mp_song_info');
					var title   	= $paths.find('.mp_song_title').html();
					var mp3			= $paths.find('.mp_mp3').html();
					var ogg			= $paths.find('.mp_ogg').html();
					var band_id 	= $this.closest('.mp_content').attr('id');
					var song_index	= $this.parent().parent().index();
					var song_idx	= band_id + '_' + song_index;
					//add to playlist and play the song if none is there
					addToPlayList(song_idx,band,title,mp3,ogg);
				});
				
				/**
				* we want to show on the band image, the play button for playing the whole band
				*/
				$('#mp_content_wrapper').find('.bandpic').bind('mouseenter',function(){
					var $this 		= $(this);
					$this.find('.mp_playall').show();
				}).bind('mouseleave',function(){
					var $this 		= $(this);
					$this.find('.mp_playall').hide();
				});
				
				/**
				* to play the whole band, we play the first song and add all the others to the playlist.
				* playing the first one, guarantees us that the playlist is set to empty before
				*/
				$('#mp_content_wrapper').find('.bandpic').bind('click',function(){
					var $this 		= $(this);
					var $band		= $this.parent();
					$band.find('.mp_play:first').trigger('click');
					$band.find('.mp_addpl').trigger('click');
				})
				
				/**
				* playlist songs can be reordered
				*/
				if ($('.nocoverflow').length) {
					$list.sortable();
				}

				$list.disableSelection();
				
				/* Scollbar */
				$('#jplayer_playlist').jScrollPane(
					{
						showArrows: false,
						horizontalGutter: 30,
						verticalGutter: 5,
						verticalDragMaxHeight: 40
					}
				);
				
				/*
				$('#cal_events').jScrollPane(
					{
						showArrows: false,
						horizontalGutter: 30,
						verticalGutter: 5,
						verticalDragMaxHeight: 40
					}
				);
				*/
				

				
			});
