$(document).ready(function(){

// Jquery Player --------------------------------------------->

	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("2", [
		{
			name:"LOVERS, SINNERS AND REDEEMERS (2011)"
		},
		{
			name:"01. Long Way To Heaven",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/01-long-way-to-heaven.mp3"
		},
		{
			name:"02. Don't Need A Reason",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/02-don't-need-a-reason.mp3"
		},
		{
			name:"03. Arrows Of Desire",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/03-arrows-of-desire.mp3"
		},
		{
			name:"04. Contradictions",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/04-contradictions.mp3"
		},
		{
			name:"05. Landmines",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/05-landmines.mp3"
		},
		{
			name:"06. Brilliantly Shining",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/06-brilliantly-shining.mp3"
		},
		{
			name:"07. Forever And A Day",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/07-forever-and-a-day.mp3"
		},
		{
			name:"08. Get That Feeling",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/08-get-that-feeling.mp3"
		},
		{
			name:"09. Assassins Bullet",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/09-assassins-bullet.mp3"
		},
		{
			name:"10. Mary, Mirror Of Light",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/10-mary,-mirror-of-light.mp3"
		},
		{
			name:"11. Last Will And Testament",
			mp3:"../audio/mp3/lovers-sinners-and-redeemers/11-last-will-and-testament.mp3"
		},
		{
			name:"COMPASSION AND CRUELTY (2007)"
		},
		{
			name:"01. I've Still Got Music",
			mp3:"../audio/mp3/compassion-and-cruelty/01-i've-still-got-music.mp3"
		},
		{
			name:"02. Jump In My Face",
			mp3:"../audio/mp3/compassion-and-cruelty/02-jump-in-my-face.mp3"
		},
		{
			name:"03. You Are Revolution",
			mp3:"../audio/mp3/compassion-and-cruelty/03-you-are-revolution.mp3"
		},
		{
			name:"04. Rock Bottom Blues",
			mp3:"../audio/mp3/compassion-and-cruelty/04-rock-bottom-blues.mp3"
		},
		{
			name:"05. Source Of Strength",
			mp3:"../audio/mp3/compassion-and-cruelty/05-source-of-strength.mp3"
		},
		{
			name:"06. Diamonds In The Rough",
			mp3:"../audio/mp3/compassion-and-cruelty/06-diamonds-in-the-rough.mp3"
		},
		{
			name:"07. Swan Song",
			mp3:"../audio/mp3/compassion-and-cruelty/07-swan-song.mp3"
		},
		{
			name:"08. Feeling Good, Feeling Fine",
			mp3:"../audio/mp3/compassion-and-cruelty/08-feeling-good,-feeling-fine.mp3"
		},
		{
			name:"09. Black Madonna",
			mp3:"../audio/mp3/compassion-and-cruelty/09-black-madonna.mp3"
		},
		{
			name:"10. Compassion And Cruelty",
			mp3:"../audio/mp3/compassion-and-cruelty/10-compassion-and-cruelty.mp3"
		},
		{
			name:"11. Sand Grains In Eternity",
			mp3:"../audio/mp3/compassion-and-cruelty/11-sand-grains-in-eternity.mp3"
		},
		{
			name:"BURNING DOVES (2004)"
		},
		{
			name:"01. Persephone",
			mp3:"../audio/mp3/burning-doves/01-persephone.mp3"
		},
		{
			name:"02. Hammerblows Of Thunder",
			mp3:"../audio/mp3/burning-doves/02-hammerblows-of-thunder.mp3"
		},
		{
			name:"03. Wheels In The Sky",
			mp3:"../audio/mp3/burning-doves/03-wheels-in-the-sky.mp3"
		},
		{
			name:"04. Burning Doves",
			mp3:"../audio/mp3/burning-doves/04-burning-doves.mp3"
		},
		{
			name:"05. Lonesome Highway",
			mp3:"../audio/mp3/burning-doves/05-lonesome-highway.mp3"
		},
		{
			name:"06. Sleeping, Waking",
			mp3:"../audio/mp3/burning-doves/06-sleeping,-waking.mp3"
		},
		{
			name:"07. A Drop In The Mirror Pool",
			mp3:"../audio/mp3/burning-doves/07-a-drop-in-the-mirror-pool.mp3"
		},
		{
			name:"08. Leaving My Cross",
			mp3:"../audio/mp3/burning-doves/08-leaving-my-cross.mp3"
		},
		{
			name:"09. Lillies Of Vice",
			mp3:"../audio/mp3/burning-doves/09-lillies-of-vice.mp3"
		},
		{
			name:"10. Temple In My Heart",
			mp3:"../audio/mp3/burning-doves/10-temple-in-my-heart.mp3"
		},
		{
			name:"11. Loaded (Straight Ahead)",
			mp3:"../audio/mp3/burning-doves/11-loaded-(straight-ahead).mp3"
		},
		{
			name:"12. Spanish Lullaby (Bonus)",
			mp3:"../audio/mp3/burning-doves/12-spanish-lullaby.mp3"
		},
		{
			name:"13. Roman Ashes (Bonus)",
			mp3:"../audio/mp3/burning-doves/13-roman-ashes.mp3"
		}
	], {
		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"
	});
	
// Jquery Scrollbars ---------------------------------------- >
	
	$(function()
	{
		$('#sp-player-playlist').jScrollPane(
		{
			showArrows: true,
			autoReinitialise: true,
			}
		);
	});
	
// Jquery Pop out ------------------------------------------- >

	$(function(){
        var childWin;
        $('.pop-out').click(function(){
    	    childWin = window.open('http://www.sunwheelpsychedelic.com/player.html','child', "width=700, height=180, scrollbars=no, toolbar=no, location=no, status=no, menubar=no");
        	if (window.focus) {childWin.focus()}
		});
	});  

			
});
