if(typeof jQuery !== "undefined") {
	// add content slider
	(function($){
		$.fn.extend({
			addSlider: function(options) {
				var settings = $.extend({}, $.fn.addSlider.defaults, options);

				return this.each(function() {
					var $$, o;

					$$		= $(this);
					o		= $.metadata ? $.extend({}, settings, $$.metadata()) : settings;

					if(o.url === null) {
						$$.setupSlider(o).setupEvents(o);
					}
					else if(typeof o.options === 'object') {
						$$.empty().nextAll('#slider').remove();
						$.post(o.url, $.extend({action: 'srch'}, o.options), function(data, textStatus){
							if(textStatus === 'success') {
								$$.html(data);
								$$.setupSlider(o).setupEvents(o);
							}
						});
					}
					else {
						$$.empty().nextAll('#slider').remove();
						$$.load(o.url, function(){
							$$.setupSlider(o).setupEvents(o);
						});
					}
				});
			}
		});

		// defaults
		$.fn.addSlider.defaults = {
			url:		null,
			options:	null,
			duration:	1000
		};

		// setup the jQuery UI Slider, based on width of LIs
		$.fn.setupSlider = function(o){
			return this.each(function() {
				var $li, $liCalc, $listing, $container,
					$shownLi, $shownUl, $shownLiPane, shownIndex, shownPosition, shownAnimateLeft;

				o.content	= $(this);
				$container	= o.content.parent();
				$listing	= $('> ul.listings', this);
				$li			= $('> ul.listings > li', this);
				$liCalc		= $li.eq(0);

				if($liCalc.is('.shown')) {
					$liCalc = $liCalc.next('li').eq(0);
					if($liCalc.length === 0) {
						$liCalc = $liCalc.prev('li').eq(0);
					}
				}

				if($.ui && $.ui.slider) {
					o.sliderSettings = {
						width:		parseInt($liCalc.outerWidth(true), 10),
						total:		parseInt($li.length, 10),
						distance:	parseInt($container.width(), 10),
						margin:		parseInt($liCalc.css('marginRight'), 10),
						columns:	($li.parent().is('.text')) ? 1 : 2,
						enabled:	true,
						active:		null
					};
					if(o.sliderSettings.total < 1) {
						return false;
					}

					o.sliderSettings.maxDefault	= Math.round(o.sliderSettings.width * Math.ceil(o.sliderSettings.total / o.sliderSettings.columns));
					o.sliderSettings.max		= o.sliderSettings.maxDefault;

					// check if there is an active open product
					$shownLi = $listing.find('li.shown');
					if($shownLi.length === 1) {
						$shownUl				= $shownLi.closest('ul');
						$shownLiPane			= ($shownUl.is(':not(.listings)')) ? $shownUl.closest('li') : $shownLi;
						shownIndex				= parseInt($shownLiPane.prevAll().length, 10);
						shownPosition			= shownIndex + 1;
						o.sliderSettings.active	= $shownLi;
						
						o.sliderSettings.max	+= o.sliderSettings.distance + o.sliderSettings.margin;
					}

					o.sliderOptions = {
						min:		0,
						max:		parseInt(o.sliderSettings.max - o.sliderSettings.distance - o.sliderSettings.margin, 10),
						change:		function(event, ui) {},
						stop:		function(event, ui) {
							o.content.css('left', ui.value * -1);
						},
						slide:		function(event, ui) {
							o.content.css('left', ui.value * -1);
						}
					};
					o.sliderIndex				= Math.round(o.sliderSettings.total / 2);
					o.sliderSettings.start		= Math.round(o.sliderOptions.max / 2);
					o.sliderSettings.value		= o.sliderSettings.start;
					o.sliderSettings.step		= o.sliderSettings.width; //parseInt($liCalc.outerWidth(true), 10);
					o.sliderSettings.center		= Math.floor(o.sliderSettings.total / 4);
					o.sliderOptions.maxDefault	= parseInt(o.sliderSettings.maxDefault - o.sliderSettings.distance - o.sliderSettings.margin, 10);

					if($listing.is('.text')) {
						o.sliderSettings.center	= Math.ceil(o.sliderSettings.total / 2);
					}
					
					if(o.sliderSettings.active !== null) {
						// center based on the active artwork
						o.sliderSettings.start	= o.sliderSettings.step * shownPosition;
						o.sliderSettings.value	= o.sliderSettings.start;
					}

					if(o.sliderOptions.max < 0) {
						o.sliderOptions.max		= 0;
						o.sliderSettings.value	= 0;
					}

					o.slider = $('#slider');
					if(o.slider.length !== 1) {
						o.slider.remove();
						o.slider = $('<div />').attr('id', 'slider');
					}
					o.content.after(o.slider).css('width', o.sliderSettings.max).css('left', (o.sliderSettings.start * -1));
					o.slider.slider(o.sliderOptions).slider('option', 'value', o.sliderSettings.value);
					o.handle = $('a', o.slider);

					// not enough items, set to center and disable the slider
					if(o.sliderSettings.max < o.sliderSettings.distance) {
						o.sliderSettings.fullscreen = false;
						o.slider.slider('disable');
						o.handle.css('left', '50%');
					}
				}
			});
		};

		// setup the hide/show events
		$.fn.setupEvents = function(o){
			return this.each(function() {
				var $liAll, $liActive, $clickable,
					animating = false,
					clickIndex = 100,
					videoIndex = 1;

				$liAll			= $('> ul.listings > li', this);
				$liActive		= $('> ul.listings li', this);
				$clickable		= $('> ul.listings:not(.gallery) > li > a', this);
				$clickable		= $clickable.add($liAll.children('div[id^=listings]').find('> ul > li > a'));

				$clickable.bind('click.sliderContent', function(event, data){
					var $$, $li, $image, $liPane, $ul, $hwrapper, $hproduct, $hside,
						distance, index, position, $lis,
						$contentShown, $loading,
						optionMax, animateLeft, percent, side, href,
						videoPlayerObject, setUrl;

					$$				= $(event.target).closest('a');
					$ul				= $$.closest('ul');
					$li				= $$.closest('li');
					$image			= $li.find('> a > img');
					$liPane			= ($ul.is(':not(.listings)')) ? $ul.closest('li') : $$.closest('li');
					$hwrapper		= $('div.hwrapper', $li);
					$hproduct		= $('div.hproduct', $li);
					distance		= parseInt(o.sliderSettings.distance + o.sliderSettings.margin, 10);
					index			= parseInt($liPane.prevAll().length, 10);
					position		= index + 1;
					$lis			= $('> ul.listings > li:nth-child(' + (position) + ')', o.content);
					$contentShown	= $('div.hwrapper:not(.hidden)', o.content);
					side			= (position > o.sliderSettings.center) ? 'right' : 'left';

					$liPane.addClass(side).css('z-index', clickIndex++).closest('ul').css('z-index', clickIndex++);
					$$.blur();
					event.preventDefault();

					if($contentShown.length > 0 && !$hwrapper.is(':not(.hidden)')) {
						// there are already items open
						// trigger the close on the current opened item
						// pass in the index of the item which was clicked
						// item is them opened when the animation is finished
						$contentShown.closest('li').children('a').eq(0).trigger('click.sliderContent', {open: $clickable.index($$)});
						return true;
					}

					if(animating === false) {
						animating = true;

						// fading image and loading avatar
						if($hwrapper.is('.hidden') || $hwrapper.length === 0) {
							$liActive.removeClass('faded').removeClass('shown');
							if($image.length === 1) {
								$li.find('> a > img').animate({opacity: '0.25'}, o.duration / 2, function(){
									$li.addClass('faded').addClass('shown');
									if($('div.details, div.tabs, div.content', $hwrapper).length === 0) {
										$loading = $('<span />').addClass('loading').hide();
										$$.append($loading);
										$loading.fadeIn(o.duration / 2);
									}
								});
							}
							else {
								$li.addClass('faded').addClass('shown');
							}
						}

						if($hwrapper.length === 0) {
							$hwrapper	= $('<div />').addClass('hwrapper').addClass('hidden');
							$hproduct	= $('<div />').addClass('hproduct').css('left', '-1000px');
							$hside		= $('<div />').addClass('side');
							$li.append($hwrapper.append($hside).append($hproduct));

							// LOAD IN THE CONTENT
							href = $(this).attr('href');
							switch(href.split('/')[href.split('/').length - 1]) {
								case 'discography-more.html':
								case 'discography-dvd-more.html':
								case 'lyrics-more.html':
								case 'media-audio-more.html':
								case 'media-video-more.html':
									href = 'ajax/' + href.split('/')[href.split('/').length - 1];
									break;
							}

							$hproduct.load(href, function(){
								scrollbarInit('#' + $('div.section:visible', this).attr('id'));
								cycleInit();
								cornerInit();
								listenInit();
								ieHoverInit();

								$hproduct.find('.content').show();
								if(typeof videoPlayer === 'object') {
									videoPlayerObject = videoPlayer;
								}
							});
						}

						if($$.is('a') && $$.length === 1) {
							if($hwrapper.is('.hidden')) {
								optionMax	= o.sliderOptions.max + o.sliderSettings.distance + o.sliderSettings.margin;
								animateLeft	= (o.sliderSettings.step * position) * -1;

								if(o.sliderSettings.fullscreen === false) {
									optionMax = o.sliderSettings.max;
								}
								percent		= ((Math.abs(animateLeft) / optionMax) * 100);

								$hwrapper.add($liPane).removeClass('hidden');
								o.slider.moveHandle(percent, o.duration);
								o.content.animate({width: '+=' + distance + 'px', left: animateLeft + 'px'}, o.duration, function(){
									o.slider.slider('option', 'max', optionMax);
									o.slider.slider('option', 'value', animateLeft * -1);
									o.slider.slider('enable');
									
									// history URL
									getUrl	= $$.attr('href');
									getUrl	= getUrl.replace(document.location.hostname, '');
									getUrl	= getUrl.replace(document.location.protocol, '');
									getUrl	= getUrl.replace(/^.*#/, '');
									getUrl	= getUrl.replace(/\/{1,}/, '/');
									$.historyLoad(getUrl, false);
								});
								$lis.animate({width: '+=' + distance + 'px'}, o.duration);
								$hproduct.animate({left: '28px'}, o.duration, function(){
									animating = false;
									
									$hproduct.find('.content').show();
									if(typeof $loading === 'object' && $loading.length === 1) {
										$loading.fadeOut(o.duration / 2, function(){
											$loading.remove();
										});
									}
									setTimeout(function(){									
										if(typeof videoPlayerObject === 'object') {
											flashVideoPlayerInit(videoIndex++, videoPlayerObject.videoPath, 'img.video-player', $li);
										}
									}, 1000);
								});
							}
							else {								
								optionMax	= o.sliderOptions.maxDefault;
								animateLeft = (o.sliderSettings.step * index) * -1;
								if(side === 'right') {
									animateLeft = ((o.sliderSettings.step * -1) * (index - o.sliderSettings.center));
								}
								percent		= ((Math.abs(animateLeft) / optionMax) * 100);

								// reset back to the center
								if(o.sliderSettings.fullscreen === false) {
									percent		= 50;
									animateLeft	= (o.sliderSettings.start * -1); // the original center
									o.slider.slider('disable');
								}

								// change the URL hash to the main section
								setUrl	= $('#nav li.active a').attr('href');
								setUrl	= setUrl.replace(document.location.hostname, '');
								setUrl	= setUrl.replace(document.location.protocol, '');
								setUrl	= setUrl.replace(/^.*#/, '');
								setUrl	= setUrl.replace(/\/{1,}/, '/');
								$.historyLoad(setUrl, false);

								// do the movement on the product, lis, handle and content
								$hproduct.find('.content').hide();
								$hproduct.animate({left: '-1000px'}, o.duration);
								$lis.animate({width: '-=' + distance + 'px'}, o.duration);
								o.slider.moveHandle(percent, o.duration);
								o.content.animate({left: animateLeft + 'px', width: '-=' + distance + 'px'}, o.duration, function(){
									o.slider.slider('option', 'max', optionMax);
									$hwrapper.add($liPane).addClass('hidden');
									animating = false;
									
									if($image.length === 1) {
										$li.find('> a > img').animate({opacity: '1'}, o.duration / 2, function(){
											$liActive.removeClass('faded').removeClass('shown');
										});
									}
									else {
										$liActive.removeClass('faded').removeClass('shown');
									}
									
									// animation finished, check nothing needs to be opened
									if(typeof data !== 'undefined' && typeof data.open !== 'undefined') {
										$clickable.eq(data.open).trigger('click.sliderContent');
									}
								});
							}
						}
					}
				});
			});
		};

		$.fn.moveHandle = function(percent, duration) {
			var $slider, $handle;

			$slider	= $(this);
			$handle	= $('a', $slider);

			if(duration > 10) {
				$handle.animate({left: (("" + percent) + '%')}, duration);
			}
			else {
				$handle.css({left: (("" + percent) + '%')});
			}
		};

	})(jQuery);
}