$(document).ready(function(){

	var Playlist = function(instance, playlist, options) {
		var self = this;

		this.instance = instance; // String: To associate specific HTML with this playlist
		this.playlist = playlist; // Array of Objects: The playlist
		this.options = options; // Object: The jPlayer constructor options for this playlist

		this.current = 0;

		this.cssId = {
			jPlayer: "jplayer_",
			interface: "jp_interface_",
			playlist: "jp_playlist_",
			playState: "play_state_",
			nowPlaying: "now_playing_",
			artist: "artist_",
			cdCover: "cd_cover_"
		};
		this.cssSelector = {};

		$.each(this.cssId, function(entity, id) {
			self.cssSelector[entity] = "#" + id + self.instance;
		});

		if(!this.options.cssSelectorAncestor) {
			this.options.cssSelectorAncestor = this.cssSelector.interface;
		}

		$(this.cssSelector.jPlayer).jPlayer(this.options);

		$(this.cssSelector.interface + " .jp-previous").click(function() {
			self.playlistPrev();
			$(this).blur();
			return false;
		});

		$(this.cssSelector.interface + " .jp-next").click(function() {
			self.playlistNext();
			$(this).blur();
			return false;
		});
	};

	Playlist.prototype = {
		playlistInit: function(autoplay) {
			if(autoplay) {
				this.playlistChange(this.current);
			} else {
				this.playlistConfig(this.current);
			}
		},
		playlistConfig: function(index) {
			$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
			$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
			this.current = index;
			$(this.cssSelector.playState).html("Now Playing:");
			$(this.cssSelector.cdCover).html('<img src="/pics/cd_' + this.playlist[this.current].albumid + '.jpg" width="75" height="75" alt="" />');
			$(this.cssSelector.artist).html("by " + this.playlist[this.current].artist);			
			$(this.cssSelector.nowPlaying).html("&quot;" + this.playlist[this.current].name + "&quot;");
			$(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
		},
		playlistChange: function(index) {
			this.playlistConfig(index);
			$(this.cssSelector.jPlayer).jPlayer("play");
			
		},
		playlistNext: function() {
			var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
			this.playlistChange(index);
		},
		playlistPrev: function() {
			var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
			this.playlistChange(index);
		}
	};

	var audioPlaylist = new Playlist("1", [
{
	name: "Sai Dessa",
albumid: "38",
artist: "Luciana Souza",
mp3: "tracks/ls_SaiDessa.mp3"
},
{
	name: "Crepuscule",
albumid: "78",
artist: "Gregoire Maret",
mp3: "tracks/gm_crepuscule.mp3"
},
{
	name: "Human Nature",
albumid: "73",
artist: "Vijay Iyer",
mp3: "tracks/vi_humannature.mp3"
},
{
	name: "It Ain't Necessarily So",
albumid: "82",
artist: "Tierney Sutton Band",
mp3: "tracks/ts_aintso.mp3"
},
{
	name: "Tribal Wisdom",
albumid: "76",
artist: "Vijay Iyer",
mp3: "tracks/vi_tribalwisdom.mp3"
},
{
	name: "Circling",
albumid: "74",
artist: "Gretchen Parlato",
mp3: "tracks/gp_circling.mp3"
},
{
	name: "A Shine On Your Shoes",
albumid: "70",
artist: "Jane Monheit",
mp3: "tracks/jm_shineonyourshoes.mp3"
},
{
	name: "Shazoo",
albumid: "62",
artist: "Lionel Loueke",
mp3: "tracks/ll_shazoo.mp3"
},
{
	name: "It's Alright With Me",
albumid: "80",
artist: "Dominick Farinacci",
mp3: "tracks/df_itsalrightwithme.mp3"
},
{
	name: "Juguete",
albumid: "81",
artist: "Miguel Zenon",
mp3: "tracks/mz_juguete.mp3"
},
{
	name: "Tanktified",
albumid: "54",
artist: "Stefon Harris",
mp3: "tracks/sh_tanktified.mp3"
},
{
	name: "This Is Always",
albumid: "70",
artist: "Jane Monheit",
mp3: "tracks/jm_thisisalways.mp3"
},
{
	name: "Full Time",
albumid: "64",
artist: "Regina Carter",
mp3: "tracks/rc_fulltime.mp3"
},
{
	name: "Brown Belle Blues",
albumid: "83",
artist: "Stefon Harris",
mp3: "tracks/sh_brownbelleblues.mp3"
},
{
	name: "Waltz For Debby",
albumid: "66",
artist: "Billy Childs",
mp3: "tracks/bc_waltzfordebby.mp3"
},
{
	name: "Decisive Steps",
albumid: "67",
artist: "Tia Fuller",
mp3: "tracks/tf_decisivesteps.mp3"
},
{
	name: "Memories of You",
albumid: "31",
artist: "Kenny Barron",
mp3: "tracks/kb_MemoriesOfYou.mp3"
},
{
	name: "Dawn of Goodbye",
albumid: "80",
artist: "Dominick Farinacci",
mp3: "tracks/df_dawnofgoodbye.mp3"
},
{
	name: "The Very Thought of You",
albumid: "77",
artist: "Sachal Vasandani",
mp3: "tracks/sv_theverythoughtofyou.mp3"
}
	], {
		ready: function() {
			audioPlaylist.playlistInit(true); // Parameter is a boolean for autoplay.
		},
		ended: function() {
			audioPlaylist.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
			$("#play_state_1").html("Now Playing:");
		},
		swfPath: "/js",		
		solution: "html, flash",
		supplied: "mp3"
	});

});

