// JavaScript Document

//<![CDATA[
$(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() {
			self.playlistPrev();
			$(this).blur();
			return false;
		});

		$(this.cssSelector.interface + " .jp-next").click(function() {
			self.playlistNext();
			$(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>";

				// Create links to free media
				if(this.playlist[i].free) {
					var first = true;
					listItem += "<div class='jp-free-media'>(";
					$.each(this.playlist[i], function(property,value) {
						if($.jPlayer.prototype.format[property]) { // Check property is a media format.
							if(first) {
								first = false;
							} else {
								listItem += " | ";
							}
							listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
						}
					});
					listItem += ")</span>";
				}

				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;
				});

				// Disable free media links to force access via right click
				if(this.playlist[i].free) {
					$.each(this.playlist[i], function(property,value) {
						if($.jPlayer.prototype.format[property]) { // Check property is a media format.
							$(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function() {
								var index = $(this).data("index");
								$(self.cssSelector.playlist + "_item_" + index).click();
								$(this).blur();
								return false;
							});
						}
					});
				}
			}
		},
		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.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:"Just Dance",
			mp3:"http://www.roofraisers.net/audio/mp3/Just Dance CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Just Dance CG.ogg"
		},
		{
			name:"California Gurls",
			mp3:"http://www.roofraisers.net/audio/mp3/California Gurls TC.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/California Gurls TC.ogg"
		},
		{
			name:"Raise Your Glass",
			mp3:"http://www.roofraisers.net/audio/mp3/Raise Your Glass CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Raise Your Glass CG.ogg"
		},
		{
			name:"Billie Jean",
			mp3:"http://www.roofraisers.net/audio/mp3/Billie Jean TC.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Billie Jean TC.ogg"
		},
		{
			name:"Black Or White",
			mp3:"http://www.roofraisers.net/audio/mp3/Black or White TC.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Black or White TC.ogg"
		},
		{
			name:"Respect",
			mp3:"http://www.roofraisers.net/audio/mp3/Respect TC.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Respect TC.ogg"
		},
		{
			name:"Signed, Sealed, Delivered",
			mp3:"http://www.roofraisers.net/audio/mp3/Signed Sealed Delivered TC.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Signed Sealed Delivered TC.ogg"
		},
		{
			name:"Ain't Too Proud To Beg",
			mp3:"http://www.roofraisers.net/audio/mp3/Aint Too Proud To Beg.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Aint Too Proud To Beg.ogg"
		},
		{
			name:"All American Girl",
			mp3:"http://www.roofraisers.net/audio/mp3/All American Girl.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/All American Girl.ogg"
		},
		{
			name:"Don't Break My Heart Again",
			mp3:"http://www.roofraisers.net/audio/mp3/Don't Break My Heart Again.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Don't Break My Heart Again.ogg"
		},
		{
			name:"Famous In A Small Town",
			mp3:"http://www.roofraisers.net/audio/mp3/Famous in a Small Town.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Famous in a Small Town.ogg"
		},
		{
			name:"People Are Crazy",
			mp3:"http://www.roofraisers.net/audio/mp3/People Are Crazy.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/People Are Crazy.ogg"
		},
		{
			name:"Come Fly With Me",
			mp3:"http://www.roofraisers.net/audio/mp3/Come Fly With Me.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Come Fly With Me.ogg"
		},
		{
			name:"At Last",
			mp3:"http://www.roofraisers.net/audio/mp3/At Last CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/At Last CG.ogg"
		}

	], {
		ready: function() {
			audioPlaylist.displayPlaylist();
			audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
		},
		ended: function() {
			audioPlaylist.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
		},
		swfPath: "/js",
		supplied: "mp3, oga"
	});
	
var audioPlaylist2 = new Playlist("2", [
		{
			name:"Firework",
			mp3:"http://www.roofraisers.net/audio/mp3/Firework.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Firework.ogg"
		},
		{
			name:"Bad Romance",
			mp3:"http://www.roofraisers.net/audio/mp3/Bad Romance CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Bad Romance CG.ogg"
		},
		{
			name:"Poker Face",
			mp3:"http://www.roofraisers.net/audio/mp3/Poker Face.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Poker Face.ogg"
		},
		{
			name:"Rolling In The Deep",
			mp3:"http://www.roofraisers.net/audio/mp3/Rolling in the Deep.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Rolling in the Deep.ogg"
		},
		{
			name:"Party In The U.S.A.",
			mp3:"http://www.roofraisers.net/audio/mp3/Party in the USA CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Party in the USA CG.ogg"
		},
		{
			name:"You Belong To Me",
			mp3:"http://www.roofraisers.net/audio/mp3/You Belong to Me.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/You Belong to Me.ogg"
		},
		{
			name:"Cowboy Casanova",
			mp3:"http://www.roofraisers.net/audio/mp3/Cowboy Casanova CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Cowboy Casanova CG.ogg"
		},
		{
			name:"Running Down A Dream",
			mp3:"http://www.roofraisers.net/audio/mp3/Running Down a Dream.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Running Down A Dream.ogg"
		},
		{
			name:"Living On A Prayer",
			mp3:"http://www.roofraisers.net/audio/mp3/Living On A Prayer.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Living On A Prayer.ogg"
		},
		{
			name:"Pride And Joy",
			mp3:"http://www.roofraisers.net/audio/mp3/Pride & Joy.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Pride & Joy.ogg"
		},
		{
			name:"Got To Be Real",
			mp3:"http://www.roofraisers.net/audio/mp3/Got To Be Real.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Got To Be Real.ogg"
		},
		{
			name:"Come Away With Me",
			mp3:"http://www.roofraisers.net/audio/mp3/Come Away With Me CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Come Away With Me CG.ogg"
		},
		{
			name:"Let's Stay Together",
			mp3:"http://www.roofraisers.net/audio/mp3/Let's Stay Together CG.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Let's Stay Together CG.ogg"
		},
		{
			name:"When I Think About Cheating",
			mp3:"http://www.roofraisers.net/audio/mp3/When I Think About Cheating.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/When I Think About Cheating.ogg"
		},
		{
			name:"Redneck Woman",
			mp3:"http://www.roofraisers.net/audio/mp3/Redneck Woman.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Redneck Woman.ogg"
		},
		{
			name:"Perfect",
			mp3:"http://www.roofraisers.net/audio/mp3/Perfect.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Perfect.ogg"
		},
		{
			name:"Cheek To Cheek",
			mp3:"http://www.roofraisers.net/audio/mp3/Cheek To Cheek short edit.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Cheek To Cheek short edit.ogg"
		},
		{
			name:"Unforgettable",
			mp3:"http://www.roofraisers.net/audio/mp3/Unforgettable.mp3",
			oga:"http://www.roofraisers.net/audio/ogg/Unforgettable.ogg"
		}
		
	], {
		ready: function() {
			audioPlaylist2.displayPlaylist();
			audioPlaylist2.playlistInit(false); // Parameter is a boolean for autoplay.
		},
		ended: function() {
			audioPlaylist2.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
		},
		swfPath: "/js",
		supplied: "mp3, oga"
	});

});
//]]>
