$(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: "jquery_jplayer_",
			interface: "jp_interface_",
			playlist: "jp_playlist_"
		};
		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(e) {
			e.preventDefault();
			
			self.playlistPrev();
			$(this).blur();
			return false;
		});

		$(this.cssSelector.interface + " .jp-next").click(function(e) {
		e.preventDefault();
			self.playlistNext();
			$(this).blur();
			return false;
		});
		$(this.cssSelector.interface + " .jp-play").click(function(e) {
		e.preventDefault();
			self.playlistPlay();
			$(this).blur();
			return false;
		});
	};

	Playlist.prototype = {
		displayPlaylist: function() {
			var self = this;
			$(this.cssSelector.playlist + " ul").empty();
			
			for (i=0; i < this.playlist.length; i++) {
				var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
				listItem += "<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>"+ this.playlist[i].name +"</a>";

				listItem += "</li>";

				// Associate playlist items with their media
				$(this.cssSelector.playlist + " ul").append(listItem);
				$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
					var index = $(this).data("index");
					
					if(self.current !== index) {
						self.playlistChange(index);
					} else {
						$(self.cssSelector.jPlayer).jPlayer("play");
					}
					
					$(this).blur();
					return false;
				});

				
			}
			
		},
		playlistInit: function(autoplay) {
			if(autoplay) {
				this.playlistChange(this.current);
			} else {
				//this.playlistConfig(this.current);
				var song_title = $("#jp_playlist_2_item_0").text();
				$("#song_title").text(song_title);
			}
			
		},
		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.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
			var song_title = $("#jp_playlist_2_item_"+index).text();
			$("#song_title").text(song_title);
		},
		playlistChange: function(index) {
			this.playlistConfig(index);
			$(this.cssSelector.jPlayer).jPlayer("play");
			
		},
		playlistPlay: function(index) {
			if(this.current == 0){
			this.playlistConfig(this.current);
			$(this.cssSelector.jPlayer).jPlayer("play");
			}
			
		},
		playlistNext: function() {
			var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
			this.playlistChange(index);
			var song_title = $("#jp_playlist_2_item_"+index).text();
			$("#song_title").text(song_title);
			if(song_title == "02 - Ditch")
			{
				$("#song_title").append('&nbsp;&nbsp;<a href="http://www.youtube.com/watch?v=WeWzqEvNAkk" target="_blank" title="Ditch Video"><img style="width:12px; height: 12px; border: none;" src="http://www.irishstew.net/repository/images/design/social/20x20/video.png" alt="Ditch - Video"></a>');
				
			}
		},
		playlistPrev: function() {
			var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
			this.playlistChange(index);
			var song_title = $("#jp_playlist_2_item_"+index).text();
			$("#song_title").text(song_title);
			if(song_title == "02 - Ditch")
			{
				$("#song_title").append('&nbsp;&nbsp;<a href="http://www.youtube.com/watch?v=WeWzqEvNAkk" target="_blank" title="Ditch Video"><img style="width:12px; height: 12px; border: none;" src="http://www.irishstew.net/repository/images/design/social/20x20/video.png" alt="Ditch - Video"></a>');
				
			}
		}
	};

	var audioPlaylist = new Playlist("2", [
		{
			name:"01 - From Ashes To The 7th Sky",
			mp3:"http://www.irishstew.net/repository/player/01-From-Ashes-To-The-7th-Sky.mp3"
		},
		{
			name:"02 - Lady Of New Tomorrow",
			mp3:"http://www.irishstew.net/repository/player/02-Lady-Of-New-Tomorrow.mp3"
		},
		{
			name:"03 - Dream Shelf",
			mp3:"http://www.irishstew.net/repository/player/03-Dream-Shelf.mp3"
		},
		{
			name:"04 - When Day Is Over",
			mp3:"http://www.irishstew.net/repository/player/04-When-Day-Is-Over.mp3"
		},
		{
			name:"05 - Home Is Where Your Heart Is",
			mp3:"http://www.irishstew.net/repository/player/05-Home-Is-Where-Your-Heart-Is.mp3"
		},
		{
			name:"06 - Take Me High",
			mp3:"http://www.irishstew.net/repository/player/06-Take-Me-High.mp3"
		},
		{
			name:"07 - No Surrender",
			mp3:"http://www.irishstew.net/repository/player/07-No-Surrender.mp3"
		},
		{
			name:"08 - Prison",
			mp3:"http://www.irishstew.net/repository/player/08-Prison.mp3"
		},
		{
			name:"09 - One Way Ticket",
			mp3:"http://www.irishstew.net/repository/player/09-One-Way-Ticket.mp3"
		},
		{
			name:"10 - So In Love",
			mp3:"http://www.irishstew.net/repository/player/10-So-In-Love.mp3"
		},
		{
			name:"11 - One For The Road",
			mp3:"http://www.irishstew.net/repository/player/11-One-For-The-Road.mp3"
		},
		{
			name:"12 - Heather",
			mp3:"http://www.irishstew.net/repository/player/12-Heather.mp3"
		}

	], {
		ready: function() {
			audioPlaylist.displayPlaylist();
			audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.		
		},
		ended: function() {
			audioPlaylist.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
		},
		supplied: "mp3",
		swf: "js"
		
	});
	
});
