/*
	jQuery Coda-Slider v2.0 - http://www.ndoherty.biz/coda-slider
	Copyright (c) 2009 Niall Doherty
	This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
	Nesting capability added by Live Method Design - http://www.LiveMethodDesign.com
*/

$(function(){
	// Remove the coda-slider-no-js class from the body
	//$("body").removeClass("Pnestedxcoda-slider-no-js"); //Commented out, as the original does this before we get to it.
	// Preloader
	$(".Pnestedxcoda-slider").children('.Pnestedxpanel').hide().end().prepend('<p class="Pnestedxloading">Loading Pnested Slider...<br /><img src="images/ajax-loader.gif" alt="loading..." /></p>');
});

var PnestedxsliderCount = 1;

$.fn.PnestedxcodaSlider = function(Pnestedxsettings) {

	Pnestedxsettings = $.extend({
		PnestedxautoHeight: true,
		PnestedxautoHeightEaseDuration: 1000,
		PnestedxautoHeightEaseFunction: "easeInOutExpo",
		PnestedxautoSlide: false,
		PnestedxautoSlideInterval: 7000,
		PnestedxautoSlideStopWhenClicked: true,
		PnestedxcrossLinking: true,
		PnestedxdynamicArrows: true,
		PnestedxdynamicArrowLeftText: "",
		PnestedxdynamicArrowRightText: "",
		PnestedxdynamicTabs: true,
		PnestedxdynamicTabsAlign: "center",
		PnestedxdynamicTabsPosition: "top",
		PnestedxexternalTriggerSelector: "a.Pnestedxxtrig",
		PnestedxfirstPanelToLoad: 1,
		PnestedxpanelTitleSelector: "h3.Pnestedxtitle",
		PnestedxslideEaseDuration: 1000,
		PnestedxslideEaseFunction: "easeInOutExpo"
	}, Pnestedxsettings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		//alert("Testing preloader");
		
		var Pnestedxslider = $(this);
		
		// If we need arrows
		if (Pnestedxsettings.PnestedxdynamicArrows) {
			Pnestedxslider.parent().addClass("Pnestedxarrows");
			Pnestedxslider.before('<div class="Pnestedxcoda-nav-left" id="Pnestedxcoda-nav-left-' + PnestedxsliderCount + '"><a href="#">' + Pnestedxsettings.PnestedxdynamicArrowLeftText + '</a></div>');
			Pnestedxslider.after('<div class="Pnestedxcoda-nav-right" id="Pnestedxcoda-nav-right-' + PnestedxsliderCount + '"><a href="#">' + Pnestedxsettings.PnestedxdynamicArrowRightText + '</a></div>');
		};
		
		var PnestedxpanelWidth = Pnestedxslider.find(".Pnestedxpanel").width();
		var PnestedxpanelCount = Pnestedxslider.find(".Pnestedxpanel").size();
		var PnestedxpanelContainerWidth = PnestedxpanelWidth*PnestedxpanelCount;
		var PnestedxnavClicks = 0; // Used if autoSlideStopWhenClicked = true
		
		// Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
		$('.Pnestedxpanel', Pnestedxslider).wrapAll('<div class="Pnestedxpanel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".Pnestedxpanel-container", Pnestedxslider).css({ width: PnestedxpanelContainerWidth });
		
		// Specify the current panel.
		// If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
		if (Pnestedxsettings.PnestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= PnestedxpanelCount) {
			var PnestedxcurrentPanel = parseInt(location.hash.slice(1));
			var Pnestedxoffset = - (PnestedxpanelWidth*(PnestedxcurrentPanel - 1));
			$('.Pnestedxpanel-container', Pnestedxslider).css({ marginLeft: Pnestedxoffset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (Pnestedxsettings.PnestedxfirstPanelToLoad != 1 && Pnestedxsettings.PnestedxfirstPanelToLoad <= PnestedxpanelCount) { 
			var PnestedxcurrentPanel = Pnestedxsettings.PnestedxfirstPanelToLoad;
			var Pnestedxoffset = - (PnestedxpanelWidth*(PnestedxcurrentPanel - 1));
			$('.Pnestedxpanel-container', Pnestedxslider).css({ marginLeft: Pnestedxoffset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var PnestedxcurrentPanel = 1;
		};
			
		// Left arrow click
		$("#Pnestedxcoda-nav-left-" + PnestedxsliderCount + " a").click(function(){
			PnestedxnavClicks++;
			if (PnestedxcurrentPanel == 1) {
				Pnestedxoffset = - (PnestedxpanelWidth*(PnestedxpanelCount - 1));
				PnestedxalterPanelHeight(PnestedxpanelCount - 1);
				PnestedxcurrentPanel = PnestedxpanelCount;
				Pnestedxslider.siblings('.Pnestedxcoda-nav').find('a.Pnestedxcurrent').removeClass('Pnestedxcurrent').parents('ul').find('li:last a').addClass('Pnestedxcurrent');
			} else {
				PnestedxcurrentPanel -= 1;
				PnestedxalterPanelHeight(PnestedxcurrentPanel - 1);
				Pnestedxoffset = - (PnestedxpanelWidth*(PnestedxcurrentPanel - 1));
				Pnestedxslider.siblings('.Pnestedxcoda-nav').find('a.Pnestedxcurrent').removeClass('Pnestedxcurrent').parent().prev().find('a').addClass('Pnestedxcurrent');
			};
			$('.Pnestedxpanel-container', Pnestedxslider).animate({ marginLeft: Pnestedxoffset }, Pnestedxsettings.PnestedxslideEaseDuration, Pnestedxsettings.PnestedxslideEaseFunction);
			if (Pnestedxsettings.PnestedxcrossLinking) { location.hash = PnestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#Pnestedxcoda-nav-right-' + PnestedxsliderCount + ' a').click(function(){
			PnestedxnavClicks++;
			if (PnestedxcurrentPanel == PnestedxpanelCount) {
				Pnestedxoffset = 0;
				PnestedxcurrentPanel = 1;
				PnestedxalterPanelHeight(0);
				Pnestedxslider.siblings('.Pnestedxcoda-nav').find('a.Pnestedxcurrent').removeClass('Pnestedxcurrent').parents('ul').find('a:eq(0)').addClass('Pnestedxcurrent');
			} else {
				Pnestedxoffset = - (PnestedxpanelWidth*PnestedxcurrentPanel);
				PnestedxalterPanelHeight(PnestedxcurrentPanel);
				PnestedxcurrentPanel += 1;
				Pnestedxslider.siblings('.Pnestedxcoda-nav').find('a.Pnestedxcurrent').removeClass('Pnestedxcurrent').parent().next().find('a').addClass('Pnestedxcurrent');
			};
			$('.Pnestedxpanel-container', Pnestedxslider).animate({ marginLeft: Pnestedxoffset }, Pnestedxsettings.PnestedxslideEaseDuration, Pnestedxsettings.PnestedxslideEaseFunction);
			if (Pnestedxsettings.PnestedxcrossLinking) { location.hash = PnestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (Pnestedxsettings.PnestedxdynamicTabs) {
			var PnestedxdynamicTabs = '<div class="Pnestedxcoda-nav" id="Pnestedxcoda-nav-' + PnestedxsliderCount + '"><ul></ul></div>';
			switch (Pnestedxsettings.PnestedxdynamicTabsPosition) {
				case "bottom":
					Pnestedxslider.parent().append(PnestedxdynamicTabs);
					break;
				default:
					Pnestedxslider.parent().prepend(PnestedxdynamicTabs);
					break;
			};
			ul = $('#Pnestedxcoda-nav-' + PnestedxsliderCount + ' ul');
			// Create the nav items
			$('.Pnestedxpanel', Pnestedxslider).each(function(n) {
				ul.append('<li class="Pnestedxtab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(Pnestedxsettings.PnestedxpanelTitleSelector).text() + '</a></li>');												
			});
			PnestedxnavContainerWidth = Pnestedxslider.width() + Pnestedxslider.siblings('.Pnestedxcoda-nav-left').width() + Pnestedxslider.siblings('.Pnestedxcoda-nav-right').width();
			ul.parent().css({ width: PnestedxnavContainerWidth });
			switch (Pnestedxsettings.PnestedxdynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * PnestedxpanelCount });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#Pnestedxcoda-nav-' + PnestedxsliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				PnestedxnavClicks++;
				$(this).addClass('Pnestedxcurrent').parents('ul').find('a').not($(this)).removeClass('Pnestedxcurrent');
				Pnestedxoffset = - (PnestedxpanelWidth*z);
				PnestedxalterPanelHeight(z);
				PnestedxcurrentPanel = z + 1;
				$('.Pnestedxpanel-container', Pnestedxslider).animate({ marginLeft: Pnestedxoffset }, Pnestedxsettings.PnestedxslideEaseDuration, Pnestedxsettings.PnestedxslideEaseFunction);
				if (!Pnestedxsettings.PnestedxcrossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
			});
		});
		
		// External triggers (anywhere on the page)
		$(Pnestedxsettings.PnestedxexternalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (PnestedxsliderCount == parseInt($(this).attr("rel").slice(12))) {
				$(this).bind("click", function() {
					PnestedxnavClicks++;
					PnestedxtargetPanel = parseInt($(this).attr("href").slice(1));
					Pnestedxoffset = - (PnestedxpanelWidth*(PnestedxtargetPanel - 1));
					PnestedxalterPanelHeight(PnestedxtargetPanel - 1);
					PnestedxcurrentPanel = PnestedxtargetPanel;
					// Switch the current tab:
					Pnestedxslider.siblings('.Pnestedxcoda-nav').find('a').removeClass('Pnestedxcurrent').parents('ul').find('li:eq(' + (PnestedxtargetPanel - 1) + ') a').addClass('Pnestedxcurrent');
					// Slide
					$('.Pnestedxpanel-container', Pnestedxslider).animate({ marginLeft: Pnestedxoffset }, Pnestedxsettings.PnestedxslideEaseDuration, Pnestedxsettings.PnestedxslideEaseFunction);
					if (!Pnestedxsettings.PnestedxcrossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
				});
			};
		});
			
		// Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
		if (Pnestedxsettings.PnestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= PnestedxpanelCount) {
			$("#Pnestedxcoda-nav-" + PnestedxsliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("Pnestedxcurrent");
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (Pnestedxsettings.PnestedxfirstPanelToLoad != 1 && Pnestedxsettings.PnestedxfirstPanelToLoad <= PnestedxpanelCount) {
			$("#Pnestedxcoda-nav-" + PnestedxsliderCount + " a:eq(" + (Pnestedxsettings.PnestedxfirstPanelToLoad - 1) + ")").addClass("Pnestedxcurrent");
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			$("#Pnestedxcoda-nav-" + PnestedxsliderCount + " a:eq(0)").addClass("Pnestedxcurrent");
		};
		
		// Set the height of the first panel
		if (Pnestedxsettings.PnestedxautoHeight) {
			PnestedxpanelHeight = $('.Pnestedxpanel:eq(' + (PnestedxcurrentPanel - 1) + ')', Pnestedxslider).height();
			Pnestedxslider.css({ height: PnestedxpanelHeight });
		};
		
		// Trigger autoSlide
		if (Pnestedxsettings.PnestedxautoSlide) {
			Pnestedxslider.ready(function() {
				setTimeout(PnestedxautoSlide,Pnestedxsettings.PnestedxautoSlideInterval);
			});
		};
		
		function PnestedxalterPanelHeight(x) {
			if (Pnestedxsettings.autoHeight) {
				PnestedxpanelHeight = $('.Pnestedxpanel:eq(' + x + ')', Pnestedxslider).height()
				Pnestedxslider.animate({ height: PnestedxpanelHeight }, Pnestedxsettings.PnestedxautoHeightEaseDuration, Pnestedxsettings.PnestedxautoHeightEaseFunction);
			};
		};
		
		function PnestedxautoSlide() {
			if (PnestedxnavClicks == 0 || !Pnestedxsettings.PnestedxautoSlideStopWhenClicked) {
				if (PnestedxcurrentPanel == PnestedxpanelCount) {
					var Pnestedxoffset = 0;
					PnestedxcurrentPanel = 1;
				} else {
					var Pnestedxoffset = - (PnestedxpanelWidth*PnestedxcurrentPanel);
					PnestedxcurrentPanel += 1;
				};
				PnestedxalterPanelHeight(PnestedxcurrentPanel - 1);
				// Switch the current tab:
				Pnestedxslider.siblings('.Pnestedxcoda-nav').find('a').removeClass('Pnestedxcurrent').parents('ul').find('li:eq(' + (PnestedxcurrentPanel - 1) + ') a').addClass('Pnestedxcurrent');
				// Slide:
				$('.Pnestedxpanel-container', Pnestedxslider).animate({ marginLeft: Pnestedxoffset }, Pnestedxsettings.PnestedxslideEaseDuration, Pnestedxsettings.PnestedxslideEaseFunction);
				setTimeout(PnestedxautoSlide,Pnestedxsettings.PnestedxautoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.Pnestedxpanel', Pnestedxslider).show().end().find("p.Pnestedxloading").remove();
		Pnestedxslider.removeClass("Pnestedxpreload");
		
		PnestedxsliderCount++;
		
	});
};
