var $j = jQuery.noConflict();

$j(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 = {};

			$j.each(this.cssId, function(entity, id) {
				self.cssSelector[entity] = "#" + id + self.instance;
			});

			if(!this.options.cssSelectorAncestor) {
				this.options.cssSelectorAncestor = this.cssSelector.interface;
			}

			$j(this.cssSelector.jPlayer).jPlayer(this.options);

			$j(this.cssSelector.interface + " .jp-previous").click(function() {
				self.playlistPrev();
				$j(this).blur();
				return false;
			});

			$j(this.cssSelector.interface + " .jp-next").click(function() {
				self.playlistNext();
				$j(this).blur();
				return false;
			});
		};

		Playlist.prototype = {
			displayPlaylist: function() {
				var self = this;
				for (i=0; i < this.playlist.length; i++) {				
					var listItem = (i === this.playlist.length-1);

					// Associate playlist items with their media
					$j(this.cssSelector.playlist + " ul").append(listItem);

					$j(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {					
						var index = $j(this).data("index");
						if(self.current !== index) {
							self.playlistChange(index);
						} else {
							$j(self.cssSelector.jPlayer).jPlayer("play");
						}
						$j(this).blur();
						return false;
					});
					$j("#play_" + i).data("index", i).click(function() {
						var index = $j(this).data("index");
						if (self.current !== index) {
							self.playlistChange(index);
						} else { $j(self.cssSelector.jPlayer).jPlayer("play"); 	
						 }
						$j(this).blur();
						return false;

					});


					// Disable free media links to force access via right click
					if(this.playlist[i].free) {
						$j.each(this.playlist[i], function(property,value) {
							if($j.jPlayer.prototype.format[property]) { // Check property is a media format.
								$j(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function() {
									var index = $j(this).data("index");
									$j(self.cssSelector.playlist + "_item_" + index).click();
									$j(this).blur();
									return false;
								});
							}
						});
					}
				}
			},
			playlistInit: function(autoplay) {
				if(autoplay) {
					this.playlistChange(this.current);
				} else {
					this.playlistConfig(this.current);
				}
			},
			playlistConfig: function(index) {
				$j(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
				$j(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
				this.current = index;
				$j(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);

			},
			playlistChange: function(index) {
				this.playlistConfig(index);
				$j(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);
			}
		};

		$j(".linkstopsmusic").click(function() {
			$j("#jquery_jplayer_1").jPlayer("pause");
			$j(this).blur();
			return false;
		});

		var audioPlaylist = new Playlist("1", [
		{name:"Fire Engine",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/fireengine.mp3"},
		{name:"Slippery Fish",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/slipperyfish.mp3"},
		{name:"Gallop Little Horsey",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/galloplittle.mp3"},
		{name:"Lollipop Tree",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/lollipoptree.mp3"},
		{name:"Space Doggie",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/spacedoggie.mp3"},
		{name:"Goin' to the Zoo",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/goingtothezoo.mp3"},
		{name:"I Am A Pizza",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/iampizza.mp3"},
		{name:"Make a Sound",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/makeasound.mp3"},
		{name:"Three Bear Rap",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/threebearrap.mp3"},
		{name:"World Goes Around",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/worldgoesaround.mp3"},
		{name:"Don\'t Say Ew",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/dontsayew.mp3"},
		{name:"The Bottle Song",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/thebottlesong.mp3"},
		{name:"Hairy Caterpillar",mp3:"http://www.amywigton.com/site/wp-content/themes/amy_wigton_theme/soundsamples/hairycaterpillar.mp3"}


		], {
			ready: function() {			
				audioPlaylist.displayPlaylist();
				audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
			},
			ended: function() {
				audioPlaylist.playlistNext();
			},
			play: function() {
				$j(this).jPlayer("pauseOthers");
				$j("#trackname").text(audioPlaylist.playlist[audioPlaylist.current].name);

			},
			swfPath: "/site/wp-content/themes/amy_wigton_theme/scripts",
			supplied: "mp3",
			preload: "auto",
			volume: 1
		});
		$j("#jquery_jplayer_1").bind($j.jPlayer.event.pause, function() {
					$j("#trackname").text("");
					return false;
				});
	})	
