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

var nestedxsliderCount = 1;

$.fn.nestedxcodaSlider = function(nestedxsettings) {

	nestedxsettings = $.extend({
		nestedxautoHeight: true,
		nestedxautoHeightEaseDuration: 1000,
		nestedxautoHeightEaseFunction: "easeInOutExpo",
		nestedxautoSlide: false,
		nestedxautoSlideInterval: 5000,
		nestedxautoSlideStopWhenClicked: true,
		nestedxcrossLinking: true,
		nestedxdynamicArrows: true,
		nestedxdynamicArrowLeftText: "",
		nestedxdynamicArrowRightText: "",
		
		nestedxdynamicTabsAlign: "center",
		nestedxdynamicTabsPosition: "top",
		nestedxexternalTriggerSelector: "a.nestedxxtrig",
		nestedxfirstPanelToLoad: 1,
		nestedxpanelTitleSelector: "h2.nestedxtitle",
		nestedxslideEaseDuration: 1000,
		nestedxslideEaseFunction: "easeInOutExpo"
	}, nestedxsettings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		//alert("Testing preloader");
		
		var nestedxslider = $(this);
		
		// If we need arrows
		if (nestedxsettings.nestedxdynamicArrows) {
			nestedxslider.parent().addClass("nestedxarrows");
			nestedxslider.before('<div class="nestedxcoda-nav-left" id="nestedxcoda-nav-left-' + nestedxsliderCount + '"><a href="#">' + nestedxsettings.nestedxdynamicArrowLeftText + '</a></div>');
			nestedxslider.after('<div class="nestedxcoda-nav-right" id="nestedxcoda-nav-right-' + nestedxsliderCount + '"><a href="#">' + nestedxsettings.nestedxdynamicArrowRightText + '</a></div>');
		};
		
		var nestedxpanelWidth = nestedxslider.find(".nestedxpanel").width();
		var nestedxpanelCount = nestedxslider.find(".nestedxpanel").size();
		var nestedxpanelContainerWidth = nestedxpanelWidth*nestedxpanelCount;
		var nestedxnavClicks = 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)
		$('.nestedxpanel', nestedxslider).wrapAll('<div class="nestedxpanel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".nestedxpanel-container", nestedxslider).css({ width: nestedxpanelContainerWidth });
		
		// 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 (nestedxsettings.nestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= nestedxpanelCount) {
			var nestedxcurrentPanel = parseInt(location.hash.slice(1));
			var nestedxoffset = - (nestedxpanelWidth*(nestedxcurrentPanel - 1));
			$('.nestedxpanel-container', nestedxslider).css({ marginLeft: nestedxoffset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (nestedxsettings.nestedxfirstPanelToLoad != 1 && nestedxsettings.nestedxfirstPanelToLoad <= nestedxpanelCount) { 
			var nestedxcurrentPanel = nestedxsettings.nestedxfirstPanelToLoad;
			var nestedxoffset = - (nestedxpanelWidth*(nestedxcurrentPanel - 1));
			$('.nestedxpanel-container', nestedxslider).css({ marginLeft: nestedxoffset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var nestedxcurrentPanel = 1;
		};
			
		// Left arrow click
		$("#nestedxcoda-nav-left-" + nestedxsliderCount + " a").click(function(){
			nestedxnavClicks++;
			if (nestedxcurrentPanel == 1) {
				nestedxoffset = - (nestedxpanelWidth*(nestedxpanelCount - 1));
				nestedxalterPanelHeight(nestedxpanelCount - 1);
				nestedxcurrentPanel = nestedxpanelCount;
				nestedxslider.siblings('.nestedxcoda-nav').find('a.nestedxcurrent').removeClass('nestedxcurrent').parents('ul').find('li:last a').addClass('nestedxcurrent');
			} else {
				nestedxcurrentPanel -= 1;
				nestedxalterPanelHeight(nestedxcurrentPanel - 1);
				nestedxoffset = - (nestedxpanelWidth*(nestedxcurrentPanel - 1));
				nestedxslider.siblings('.nestedxcoda-nav').find('a.nestedxcurrent').removeClass('nestedxcurrent').parent().prev().find('a').addClass('nestedxcurrent');
			};
			$('.nestedxpanel-container', nestedxslider).animate({ marginLeft: nestedxoffset }, nestedxsettings.nestedxslideEaseDuration, nestedxsettings.nestedxslideEaseFunction);
			if (nestedxsettings.nestedxcrossLinking) { location.hash = nestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#nestedxcoda-nav-right-' + nestedxsliderCount + ' a').click(function(){
			nestedxnavClicks++;
			if (nestedxcurrentPanel == nestedxpanelCount) {
				nestedxoffset = 0;
				nestedxcurrentPanel = 1;
				nestedxalterPanelHeight(0);
				nestedxslider.siblings('.nestedxcoda-nav').find('a.nestedxcurrent').removeClass('nestedxcurrent').parents('ul').find('a:eq(0)').addClass('nestedxcurrent');
			} else {
				nestedxoffset = - (nestedxpanelWidth*nestedxcurrentPanel);
				nestedxalterPanelHeight(nestedxcurrentPanel);
				nestedxcurrentPanel += 1;
				nestedxslider.siblings('.nestedxcoda-nav').find('a.nestedxcurrent').removeClass('nestedxcurrent').parent().next().find('a').addClass('nestedxcurrent');
			};
			$('.nestedxpanel-container', nestedxslider).animate({ marginLeft: nestedxoffset }, nestedxsettings.nestedxslideEaseDuration, nestedxsettings.nestedxslideEaseFunction);
			if (nestedxsettings.nestedxcrossLinking) { location.hash = nestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (nestedxsettings.nestedxdynamicTabs) {
			var nestedxdynamicTabs = '<div class="nestedxcoda-nav" id="nestedxcoda-nav-' + nestedxsliderCount + '"><ul></ul></div>';
			switch (nestedxsettings.nestedxdynamicTabsPosition) {
				case "bottom":
					nestedxslider.parent().append(nestedxdynamicTabs);
					break;
				default:
					nestedxslider.parent().prepend(nestedxdynamicTabs);
					break;
			};
			ul = $('#nestedxcoda-nav-' + nestedxsliderCount + ' ul');
			// Create the nav items
			$('.nestedxpanel', nestedxslider).each(function(n) {
				ul.append('<li class="nestedxtab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(nestedxsettings.nestedxpanelTitleSelector).text() + '</a></li>');												
			});
			nestedxnavContainerWidth = nestedxslider.width() + nestedxslider.siblings('.nestedxcoda-nav-left').width() + nestedxslider.siblings('.nestedxcoda-nav-right').width();
			ul.parent().css({ width: nestedxnavContainerWidth });
			switch (nestedxsettings.nestedxdynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * nestedxpanelCount });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#nestedxcoda-nav-' + nestedxsliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				nestedxnavClicks++;
				$(this).addClass('nestedxcurrent').parents('ul').find('a').not($(this)).removeClass('nestedxcurrent');
				nestedxoffset = - (nestedxpanelWidth*z);
				nestedxalterPanelHeight(z);
				nestedxcurrentPanel = z + 1;
				$('.nestedxpanel-container', nestedxslider).animate({ marginLeft: nestedxoffset }, nestedxsettings.nestedxslideEaseDuration, nestedxsettings.nestedxslideEaseFunction);
				if (!nestedxsettings.nestedxcrossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
			});
		});
		
		// External triggers (anywhere on the page)
		$(nestedxsettings.nestedxexternalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (nestedxsliderCount == parseInt($(this).attr("rel").slice(12))) {
				$(this).bind("click", function() {
					nestedxnavClicks++;
					nestedxtargetPanel = parseInt($(this).attr("href").slice(1));
					nestedxoffset = - (nestedxpanelWidth*(nestedxtargetPanel - 1));
					nestedxalterPanelHeight(nestedxtargetPanel - 1);
					nestedxcurrentPanel = nestedxtargetPanel;
					// Switch the current tab:
					nestedxslider.siblings('.nestedxcoda-nav').find('a').removeClass('nestedxcurrent').parents('ul').find('li:eq(' + (nestedxtargetPanel - 1) + ') a').addClass('nestedxcurrent');
					// Slide
					$('.nestedxpanel-container', nestedxslider).animate({ marginLeft: nestedxoffset }, nestedxsettings.nestedxslideEaseDuration, nestedxsettings.nestedxslideEaseFunction);
					if (!nestedxsettings.nestedxcrossLinking) { 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 (nestedxsettings.nestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= nestedxpanelCount) {
			$("#nestedxcoda-nav-" + nestedxsliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("nestedxcurrent");
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (nestedxsettings.nestedxfirstPanelToLoad != 1 && nestedxsettings.nestedxfirstPanelToLoad <= nestedxpanelCount) {
			$("#nestedxcoda-nav-" + nestedxsliderCount + " a:eq(" + (nestedxsettings.nestedxfirstPanelToLoad - 1) + ")").addClass("nestedxcurrent");
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			$("#nestedxcoda-nav-" + nestedxsliderCount + " a:eq(0)").addClass("nestedxcurrent");
		};
		
		// Set the height of the first panel
		if (nestedxsettings.nestedxautoHeight) {
			nestedxpanelHeight = $('.nestedxpanel:eq(' + (nestedxcurrentPanel - 1) + ')', nestedxslider).height();
			nestedxslider.css({ height: nestedxpanelHeight });
		};
		
		// Trigger autoSlide
		if (nestedxsettings.nestedxautoSlide) {
			nestedxslider.ready(function() {
				setTimeout(nestedxautoSlide,nestedxsettings.nestedxautoSlideInterval);
			});
		};
		
		function nestedxalterPanelHeight(x) {
			if (nestedxsettings.autoHeight) {
				nestedxpanelHeight = $('.nestedxpanel:eq(' + x + ')', nestedxslider).height()
				nestedxslider.animate({ height: nestedxpanelHeight }, nestedxsettings.nestedxautoHeightEaseDuration, nestedxsettings.nestedxautoHeightEaseFunction);
			};
		};
		
		function nestedxautoSlide() {
			if (nestedxnavClicks == 0 || !nestedxsettings.nestedxautoSlideStopWhenClicked) {
				if (nestedxcurrentPanel == nestedxpanelCount) {
					var nestedxoffset = 0;
					nestedxcurrentPanel = 1;
				} else {
					var nestedxoffset = - (nestedxpanelWidth*nestedxcurrentPanel);
					nestedxcurrentPanel += 1;
				};
				nestedxalterPanelHeight(nestedxcurrentPanel - 1);
				// Switch the current tab:
				nestedxslider.siblings('.nestedxcoda-nav').find('a').removeClass('nestedxcurrent').parents('ul').find('li:eq(' + (nestedxcurrentPanel - 1) + ') a').addClass('nestedxcurrent');
				// Slide:
				$('.nestedxpanel-container', nestedxslider).animate({ marginLeft: nestedxoffset }, nestedxsettings.nestedxslideEaseDuration, nestedxsettings.nestedxslideEaseFunction);
				setTimeout(nestedxautoSlide,nestedxsettings.nestedxautoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.nestedxpanel', nestedxslider).show().end().find("p.nestedxloading").remove();
		nestedxslider.removeClass("nestedxpreload");
		
		nestedxsliderCount++;
		
	});
};
