/*
	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("Wnestedxcoda-slider-no-js"); //Commented out, as the original does this before we get to it.
	// Preloader
	$(".Wnestedxcoda-slider").children('.Wnestedxpanel').hide().end().prepend('<p class="Wnestedxloading">Loading Wnested Slider...<br /><img src="images/ajax-loader.gif" alt="loading..." /></p>');
});

var WnestedxsliderCount = 1;

$.fn.WnestedxcodaSlider = function(Wnestedxsettings) {

	Wnestedxsettings = $.extend({
		WnestedxautoHeight: true,
		WnestedxautoHeightEaseDuration: 1000,
		WnestedxautoHeightEaseFunction: "easeInOutExpo",
		WnestedxautoSlide: false,
		WnestedxautoSlideInterval: 7000,
		WnestedxautoSlideStopWhenClicked: true,
		WnestedxcrossLinking: true,
		WnestedxdynamicArrows: true,
		WnestedxdynamicArrowLeftText: "",
		WnestedxdynamicArrowRightText: "",
		WnestedxdynamicTabs: true,
		WnestedxdynamicTabsAlign: "center",
		WnestedxdynamicTabsPosition: "top",
		WnestedxexternalTriggerSelector: "a.Wnestedxxtrig",
		WnestedxfirstPanelToLoad: 1,
		WnestedxpanelTitleSelector: "h3.Wnestedxtitle",
		WnestedxslideEaseDuration: 1000,
		WnestedxslideEaseFunction: "easeInOutExpo"
	}, Wnestedxsettings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		//alert("Testing preloader");
		
		var Wnestedxslider = $(this);
		
		// If we need arrows
		if (Wnestedxsettings.WnestedxdynamicArrows) {
			Wnestedxslider.parent().addClass("Wnestedxarrows");
			Wnestedxslider.before('<div class="Wnestedxcoda-nav-left" id="Wnestedxcoda-nav-left-' + WnestedxsliderCount + '"><a href="#">' + Wnestedxsettings.WnestedxdynamicArrowLeftText + '</a></div>');
			Wnestedxslider.after('<div class="Wnestedxcoda-nav-right" id="Wnestedxcoda-nav-right-' + WnestedxsliderCount + '"><a href="#">' + Wnestedxsettings.WnestedxdynamicArrowRightText + '</a></div>');
		};
		
		var WnestedxpanelWidth = Wnestedxslider.find(".Wnestedxpanel").width();
		var WnestedxpanelCount = Wnestedxslider.find(".Wnestedxpanel").size();
		var WnestedxpanelContainerWidth = WnestedxpanelWidth*WnestedxpanelCount;
		var WnestedxnavClicks = 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)
		$('.Wnestedxpanel', Wnestedxslider).wrapAll('<div class="Wnestedxpanel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".Wnestedxpanel-container", Wnestedxslider).css({ width: WnestedxpanelContainerWidth });
		
		// 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 (Wnestedxsettings.WnestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= WnestedxpanelCount) {
			var WnestedxcurrentPanel = parseInt(location.hash.slice(1));
			var Wnestedxoffset = - (WnestedxpanelWidth*(WnestedxcurrentPanel - 1));
			$('.Wnestedxpanel-container', Wnestedxslider).css({ marginLeft: Wnestedxoffset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (Wnestedxsettings.WnestedxfirstPanelToLoad != 1 && Wnestedxsettings.WnestedxfirstPanelToLoad <= WnestedxpanelCount) { 
			var WnestedxcurrentPanel = Wnestedxsettings.WnestedxfirstPanelToLoad;
			var Wnestedxoffset = - (WnestedxpanelWidth*(WnestedxcurrentPanel - 1));
			$('.Wnestedxpanel-container', Wnestedxslider).css({ marginLeft: Wnestedxoffset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var WnestedxcurrentPanel = 1;
		};
			
		// Left arrow click
		$("#Wnestedxcoda-nav-left-" + WnestedxsliderCount + " a").click(function(){
			WnestedxnavClicks++;
			if (WnestedxcurrentPanel == 1) {
				Wnestedxoffset = - (WnestedxpanelWidth*(WnestedxpanelCount - 1));
				WnestedxalterPanelHeight(WnestedxpanelCount - 1);
				WnestedxcurrentPanel = WnestedxpanelCount;
				Wnestedxslider.siblings('.Wnestedxcoda-nav').find('a.Wnestedxcurrent').removeClass('Wnestedxcurrent').parents('ul').find('li:last a').addClass('Wnestedxcurrent');
			} else {
				WnestedxcurrentPanel -= 1;
				WnestedxalterPanelHeight(WnestedxcurrentPanel - 1);
				Wnestedxoffset = - (WnestedxpanelWidth*(WnestedxcurrentPanel - 1));
				Wnestedxslider.siblings('.Wnestedxcoda-nav').find('a.Wnestedxcurrent').removeClass('Wnestedxcurrent').parent().prev().find('a').addClass('Wnestedxcurrent');
			};
			$('.Wnestedxpanel-container', Wnestedxslider).animate({ marginLeft: Wnestedxoffset }, Wnestedxsettings.WnestedxslideEaseDuration, Wnestedxsettings.WnestedxslideEaseFunction);
			if (Wnestedxsettings.WnestedxcrossLinking) { location.hash = WnestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#Wnestedxcoda-nav-right-' + WnestedxsliderCount + ' a').click(function(){
			WnestedxnavClicks++;
			if (WnestedxcurrentPanel == WnestedxpanelCount) {
				Wnestedxoffset = 0;
				WnestedxcurrentPanel = 1;
				WnestedxalterPanelHeight(0);
				Wnestedxslider.siblings('.Wnestedxcoda-nav').find('a.Wnestedxcurrent').removeClass('Wnestedxcurrent').parents('ul').find('a:eq(0)').addClass('Wnestedxcurrent');
			} else {
				Wnestedxoffset = - (WnestedxpanelWidth*WnestedxcurrentPanel);
				WnestedxalterPanelHeight(WnestedxcurrentPanel);
				WnestedxcurrentPanel += 1;
				Wnestedxslider.siblings('.Wnestedxcoda-nav').find('a.Wnestedxcurrent').removeClass('Wnestedxcurrent').parent().next().find('a').addClass('Wnestedxcurrent');
			};
			$('.Wnestedxpanel-container', Wnestedxslider).animate({ marginLeft: Wnestedxoffset }, Wnestedxsettings.WnestedxslideEaseDuration, Wnestedxsettings.WnestedxslideEaseFunction);
			if (Wnestedxsettings.WnestedxcrossLinking) { location.hash = WnestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (Wnestedxsettings.WnestedxdynamicTabs) {
			var WnestedxdynamicTabs = '<div class="Wnestedxcoda-nav" id="Wnestedxcoda-nav-' + WnestedxsliderCount + '"><ul></ul></div>';
			switch (Wnestedxsettings.WnestedxdynamicTabsPosition) {
				case "bottom":
					Wnestedxslider.parent().append(WnestedxdynamicTabs);
					break;
				default:
					Wnestedxslider.parent().prepend(WnestedxdynamicTabs);
					break;
			};
			ul = $('#Wnestedxcoda-nav-' + WnestedxsliderCount + ' ul');
			// Create the nav items
			$('.Wnestedxpanel', Wnestedxslider).each(function(n) {
				ul.append('<li class="Wnestedxtab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(Wnestedxsettings.WnestedxpanelTitleSelector).text() + '</a></li>');												
			});
			WnestedxnavContainerWidth = Wnestedxslider.width() + Wnestedxslider.siblings('.Wnestedxcoda-nav-left').width() + Wnestedxslider.siblings('.Wnestedxcoda-nav-right').width();
			ul.parent().css({ width: WnestedxnavContainerWidth });
			switch (Wnestedxsettings.WnestedxdynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * WnestedxpanelCount });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#Wnestedxcoda-nav-' + WnestedxsliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				WnestedxnavClicks++;
				$(this).addClass('Wnestedxcurrent').parents('ul').find('a').not($(this)).removeClass('Wnestedxcurrent');
				Wnestedxoffset = - (WnestedxpanelWidth*z);
				WnestedxalterPanelHeight(z);
				WnestedxcurrentPanel = z + 1;
				$('.Wnestedxpanel-container', Wnestedxslider).animate({ marginLeft: Wnestedxoffset }, Wnestedxsettings.WnestedxslideEaseDuration, Wnestedxsettings.WnestedxslideEaseFunction);
				if (!Wnestedxsettings.WnestedxcrossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
			});
		});
		
		// External triggers (anywhere on the page)
		$(Wnestedxsettings.WnestedxexternalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (WnestedxsliderCount == parseInt($(this).attr("rel").slice(12))) {
				$(this).bind("click", function() {
					WnestedxnavClicks++;
					WnestedxtargetPanel = parseInt($(this).attr("href").slice(1));
					Wnestedxoffset = - (WnestedxpanelWidth*(WnestedxtargetPanel - 1));
					WnestedxalterPanelHeight(WnestedxtargetPanel - 1);
					WnestedxcurrentPanel = WnestedxtargetPanel;
					// Switch the current tab:
					Wnestedxslider.siblings('.Wnestedxcoda-nav').find('a').removeClass('Wnestedxcurrent').parents('ul').find('li:eq(' + (WnestedxtargetPanel - 1) + ') a').addClass('Wnestedxcurrent');
					// Slide
					$('.Wnestedxpanel-container', Wnestedxslider).animate({ marginLeft: Wnestedxoffset }, Wnestedxsettings.WnestedxslideEaseDuration, Wnestedxsettings.WnestedxslideEaseFunction);
					if (!Wnestedxsettings.WnestedxcrossLinking) { 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 (Wnestedxsettings.WnestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= WnestedxpanelCount) {
			$("#Wnestedxcoda-nav-" + WnestedxsliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("Wnestedxcurrent");
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (Wnestedxsettings.WnestedxfirstPanelToLoad != 1 && Wnestedxsettings.WnestedxfirstPanelToLoad <= WnestedxpanelCount) {
			$("#Wnestedxcoda-nav-" + WnestedxsliderCount + " a:eq(" + (Wnestedxsettings.WnestedxfirstPanelToLoad - 1) + ")").addClass("Wnestedxcurrent");
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			$("#Wnestedxcoda-nav-" + WnestedxsliderCount + " a:eq(0)").addClass("Wnestedxcurrent");
		};
		
		// Set the height of the first panel
		if (Wnestedxsettings.WnestedxautoHeight) {
			WnestedxpanelHeight = $('.Wnestedxpanel:eq(' + (WnestedxcurrentPanel - 1) + ')', Wnestedxslider).height();
			Wnestedxslider.css({ height: WnestedxpanelHeight });
		};
		
		// Trigger autoSlide
		if (Wnestedxsettings.WnestedxautoSlide) {
			Wnestedxslider.ready(function() {
				setTimeout(WnestedxautoSlide,Wnestedxsettings.WnestedxautoSlideInterval);
			});
		};
		
		function WnestedxalterPanelHeight(x) {
			if (Wnestedxsettings.autoHeight) {
				WnestedxpanelHeight = $('.Wnestedxpanel:eq(' + x + ')', Wnestedxslider).height()
				Wnestedxslider.animate({ height: WnestedxpanelHeight }, Wnestedxsettings.WnestedxautoHeightEaseDuration, Wnestedxsettings.WnestedxautoHeightEaseFunction);
			};
		};
		
		function WnestedxautoSlide() {
			if (WnestedxnavClicks == 0 || !Wnestedxsettings.WnestedxautoSlideStopWhenClicked) {
				if (WnestedxcurrentPanel == WnestedxpanelCount) {
					var Wnestedxoffset = 0;
					WnestedxcurrentPanel = 1;
				} else {
					var Wnestedxoffset = - (WnestedxpanelWidth*WnestedxcurrentPanel);
					WnestedxcurrentPanel += 1;
				};
				WnestedxalterPanelHeight(WnestedxcurrentPanel - 1);
				// Switch the current tab:
				Wnestedxslider.siblings('.Wnestedxcoda-nav').find('a').removeClass('Wnestedxcurrent').parents('ul').find('li:eq(' + (WnestedxcurrentPanel - 1) + ') a').addClass('Wnestedxcurrent');
				// Slide:
				$('.Wnestedxpanel-container', Wnestedxslider).animate({ marginLeft: Wnestedxoffset }, Wnestedxsettings.WnestedxslideEaseDuration, Wnestedxsettings.WnestedxslideEaseFunction);
				setTimeout(WnestedxautoSlide,Wnestedxsettings.WnestedxautoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.Wnestedxpanel', Wnestedxslider).show().end().find("p.Wnestedxloading").remove();
		Wnestedxslider.removeClass("Wnestedxpreload");
		
		WnestedxsliderCount++;
		
	});
};
