//// Radio
$(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("2", [
		{
			name:"Killing Moon",
			mp3:"http://www.riodesign.com.br/beta/mp3/Killing_Moon.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Killing_Moon.ogg"
			
		},
		{
			name:"Kiss On List",
			mp3:"http://www.riodesign.com.br/beta/mp3/Kiss_On_List.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Kiss_On_List.ogg"
		},
		{
			name:"Like a Virgin",
			mp3:"http://www.riodesign.com.br/beta/mp3/Like_a_Virgin.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Like_a_Virgin.ogg"
		},
		{
			name:"Love Will Tear",
			mp3:"http://www.riodesign.com.br/beta/mp3/Love_Will_Tear.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Love_Will_Tear.ogg"
		},
		{
			name:"Protection",
			mp3:"http://www.riodesign.com.br/beta/mp3/Protection.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Protection.ogg"
		},
		{
			name:"Quelquun",
			mp3:"http://www.riodesign.com.br/beta/mp3/Quelquun.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Quelquun.ogg"
		},
		{
			name:"Silver Lining",
			mp3:"http://www.riodesign.com.br/beta/mp3/Silver_Lining.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Silver_Lining.ogg"
		},
		{
			name:"Slowly",
			mp3:"http://www.riodesign.com.br/beta/mp3/Slowly.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Slowly.ogg"
		},
		{
			name:"Sofa Rockers",
			mp3:"http://www.riodesign.com.br/beta/mp3/Sofa_Rockers.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Sofa_Rockers.ogg"
		},
		{
			name:"Stand Up Jamrockr",
			mp3:"http://www.riodesign.com.br/beta/mp3/Stand_Up_Jamrockr.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Stand_Up_Jamrockr.ogg"
		},
		{
			name:"Sweet Child",
			mp3:"http://www.riodesign.com.br/beta/mp3/Sweet_Child.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Sweet_Child.ogg"
		},
		{
			name:"The Story",
			mp3:"http://www.riodesign.com.br/beta/mp3/The_Story.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/The_Story.ogg"
		},
		{
			name:"The Times",
			mp3:"http://www.riodesign.com.br/beta/mp3/The_Times.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/The_Times.ogg"
		},
		{
			name:"The Two",
			mp3:"http://www.riodesign.com.br/beta/mp3/The_Two.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/The_Two.ogg"
		},
		{
			name:"Those Dancing Days",
			mp3:"http://www.riodesign.com.br/beta/mp3/Those_Dancing_Days.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Those_Dancing_Days.ogg"
		},
		{
			name:"Times Like These",
			mp3:"http://www.riodesign.com.br/beta/mp3/Times_Like_These.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Times_Like_These.ogg"
		},
		{
			name:"Tive Razão",
			mp3:"http://www.riodesign.com.br/beta/mp3/Tive_Razao.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Tive_Razao.ogg"
		},
		{
			name:"Into The Great Wide Open",
			mp3:"http://www.riodesign.com.br/beta/mp3/Tom Petty - Into The Great Wide Open.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Tom Petty - Into The Great Wide Open.ogg"
		},
		{
			name:"Voyance",
			mp3:"http://www.riodesign.com.br/beta/mp3/Voyance.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Voyance.ogg"
		},
		{
			name:"We Are Going To Be Friends",
			mp3:"http://www.riodesign.com.br/beta/mp3/White Stripes - We Are Going To Be Friends.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/White Stripes - We Are Going To Be Friends.ogg"
		},
		{
			name:"Wild Horses",
			mp3:"http://www.riodesign.com.br/beta/mp3/Wild_Horses.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Wild_Horses.ogg"
		},
		{
			name:"Wild Side",
			mp3:"http://www.riodesign.com.br/beta/mp3/Wild_side.mp3",
			oga:"http://www.riodesign.com.br/beta/ogg/Wild_side.ogg"
		}
		], {
			ready: function() {
				audioPlaylist.displayPlaylist();
				audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
				window.radioDisplayDefaultText = $('#radioDisplay').html();
			},
			ended: function() {
				audioPlaylist.playlistNext();
			},
			play: function() {
				$(this).jPlayer("pauseOthers");
				$('#radioDisplay')
					.html(audioPlaylist.playlist[audioPlaylist.current].name)
					.removeClass('inactive')
					.addClass('played');				
			},
			pause: function() {
				$('#radioDisplay')
					.html(radioDisplayDefaultText)
					.addClass('inactive')
					.removeClass('played');				
			},
			loadstart: function(event) {
				// console.log(audioPlaylist.current);
				// console.log(audioPlaylist.playlist[audioPlaylist.current].name);
			},
			swfPath: "../flash",
			supplied: "oga, mp3"
			// supplied: "mp3",
		});
		
		// Balloon Opens on Hover 
		$('.jp-audio').mouseenter(function() {
			if ($(this).find('.jp-play').is(':visible')) {
				$('#radioDisplay').fadeIn('fast');
			}
		});
		
		$('.jp-audio').mouseleave(function() {
			if ($('#radioDisplay').hasClass('inactive')) {
				$('#radioDisplay').fadeOut();
			};
		});
	// Radio
});
