$(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>";


				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);
			}
		},
		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 jingles = [
		{"name":"4U2go","mp3":"\/download\/jingles\/4U2go.mp3"},
		{"name":"AceOfSpades","mp3":"\/download\/jingles\/AceOfSpades.mp3"},
		{"name":"Betray","mp3":"\/download\/jingles\/Betray.mp3"},
		{"name":"DefaultTheme","mp3":"\/download\/jingles\/DefaultTheme.mp3"},
		{"name":"Duduk","mp3":"\/download\/jingles\/Duduk.mp3"},
		{"name":"Flow","mp3":"\/download\/jingles\/Flow.mp3"},
		{"name":"GenX","mp3":"\/download\/jingles\/GenX.mp3"},
		{"name":"Guard1","mp3":"\/download\/jingles\/Guard1.mp3"},
		{"name":"Morning","mp3":"\/download\/jingles\/Morning.mp3"},
		{"name":"Proof of the Truth","mp3":"\/download\/jingles\/Proof-of-the-Truth.mp3"},
		{"name":"RisingTheme","mp3":"\/download\/jingles\/RisingTheme.mp3"},
		{"name":"Serafino","mp3":"\/download\/jingles\/Serafino.mp3"},
		{"name":"The Next One II","mp3":"\/download\/jingles\/The-Next-One-II.mp3"},
		{"name":"ThinkingTheme","mp3":"\/download\/jingles\/ThinkingTheme.mp3"},
		{"name":"Valuta","mp3":"\/download\/jingles\/Valuta.mp3"}
	];

	var audioPlaylist = new Playlist("2", [
			jingles[Math.floor(Math.random()*jingles.length)],
			{"name":"Afra","mp3":"\/download\/music\/Uli-Graner-Afra.mp3"},
			{"name":"After she’s gone","mp3":"\/download\/music\/Uli-Graner-After-she-s-gone.mp3"},
			{"name":"Automatic people","mp3":"\/download\/music\/Uli-Graner-Automatic-people.mp3"},
			{"name":"Bis ich zerbrech","mp3":"\/download\/music\/Uli-Graner-Bis-ich-zerbrech.mp3"},
			{"name":"Don Giovanni Overture","mp3":"\/download\/music\/Uli-Graner-Don-Giovanni-Overture.mp3"},
			{"name":"Flip Top Head Remix","mp3":"\/download\/music\/Uli-Graner-Flip-Top-Head-Remix.mp3"},
			{"name":"Flip Top Head","mp3":"\/download\/music\/Uli-Graner-Flip-Top-Head.mp3"},
			{"name":"Generation X","mp3":"\/download\/music\/Uli-Graner-Generation-X.mp3"},
			{"name":"Gettin Done","mp3":"\/download\/music\/Uli-Graner-Gettin-Done.mp3"},
			{"name":"Harbour Lights","mp3":"\/download\/music\/Uli-Graner-Harbour-Lights.mp3"},
			{"name":"In the fields","mp3":"\/download\/music\/Uli-Graner-In-the-fields.mp3"},
			{"name":"Living","mp3":"\/download\/music\/Uli-Graner-Living.mp3"},
			{"name":"Mugli","mp3":"\/download\/music\/Uli-Graner-Mugli.mp3"},
			{"name":"Never again","mp3":"\/download\/music\/Uli-Graner-Never-again.mp3"},
			{"name":"On my own again","mp3":"\/download\/music\/Uli-Graner-On-my-own-again.mp3"},
			{"name":"Prayer in the wind","mp3":"\/download\/music\/Uli-Graner-Prayer-in-the-wind.mp3"},
			{"name":"Starke Schulter","mp3":"\/download\/music\/Uli-Graner-Starke-Schulter.mp3"},
			{"name":"Sundog","mp3":"\/download\/music\/Uli-Graner-Sundog.mp3"},
			{"name":"Uzibar","mp3":"\/download\/music\/Uli-Graner-Uzibar.mp3"},
			{"name":"Will you take me back","mp3":"\/download\/music\/Uli-Graner-Will-you-take-me-back.mp3"},
			{"name":"Wish upon a star","mp3":"\/download\/music\/Uli-Graner-Wish-upon-a-star.mp3"},
			{"name":"With eyes open","mp3":"\/download\/music\/Uli-Graner-With-eyes-open.mp3"},
			{"name":"With you","mp3":"\/download\/music\/Uli-Graner-With-you.mp3"},
			{"name":"Worst case","mp3":"\/download\/music\/Uli-Graner-Worst-case.mp3"}
		], {
		ready: function() {
			audioPlaylist.displayPlaylist();
			audioPlaylist.playlistInit(true); // Parameter is a boolean for autoplay.
		},
		ended: function() {
			audioPlaylist.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
		},
		swfPath: "/js",
		supplied: "mp3"
	});
	
	
	window.player = {
		pause: function() {
			$("#jquery_jplayer_2").jPlayer("pause");
		},
		play: function() {
			$("#jquery_jplayer_2").jPlayer("play");
		},
		setMedia: function(media) {
			$("#jquery_jplayer_2").jPlayer("setMedia", media);
		}
	};
});

