/*
	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("nested-slider-no-js"); //Commented out, as the original does this before we get to it.
	// Preloader
	$(".nested-slider").children('.nestedpanel').hide().end().prepend('<p class="nestedloading">Loading Nested Slider...<br /><img src="images/ajax-loader.gif" alt="loading..." /></p>');
});

var nestedsliderCount = 1;

$.fn.nestedSlider = function(nestedsettings) {

	nestedsettings = $.extend({
		nestedautoHeight: true,
		nestedautoHeightEaseDuration: 1000,
		nestedautoHeightEaseFunction: "easeInOutExpo",
		nestedautoSlide: false,
		nestedautoSlideInterval: 7000,
		nestedautoSlideStopWhenClicked: true,
		nestedcrossLinking: false,
		nesteddynamicArrows: false,
		nesteddynamicArrowLeftText: "&#171; left",
		nesteddynamicArrowRightText: "right &#187;",
		nesteddynamicTabs: false,
		nesteddynamicTabsAlign: "center",
		nesteddynamicTabsPosition: "top",
		nestedexternalTriggerSelector: "a.nestedxtrig",
		nestedfirstPanelToLoad: 1,
		nestedpanelTitleSelector: "h2.nestedtitle",
		nestedslideEaseDuration: 1000,
		nestedslideEaseFunction: "easeInOutExpo"
	}, nestedsettings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		//alert("Testing preloader");
		
		var nestedslider = $(this);
		
		// If we need arrows
		if (nestedsettings.nesteddynamicArrows) {
			nestedslider.parent().addClass("nestedarrows");
			nestedslider.before('<div class="nested-nav-left" id="nested-nav-left-' + nestedsliderCount + '"><a href="#">' + nestedsettings.nesteddynamicArrowLeftText + '</a></div>');
			nestedslider.after('<div class="nested-nav-right" id="nested-nav-right-' + nestedsliderCount + '"><a href="#">' + nestedsettings.nesteddynamicArrowRightText + '</a></div>');
		};
		
		var nestedpanelWidth = nestedslider.find(".nestedpanel").width();
		var nestedpanelCount = nestedslider.find(".nestedpanel").size();
		var nestedpanelContainerWidth = nestedpanelWidth*nestedpanelCount;
		var nestednavClicks = 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)
		$('.nestedpanel', nestedslider).wrapAll('<div class="nestedpanel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".nestedpanel-container", nestedslider).css({ width: nestedpanelContainerWidth });
		
		// 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 (nestedsettings.nestedcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= nestedpanelCount) {
			var nestedcurrentPanel = parseInt(location.hash.slice(1));
			var nestedoffset = - (nestedpanelWidth*(nestedcurrentPanel - 1));
			$('.nestedpanel-container', nestedslider).css({ marginLeft: nestedoffset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (nestedsettings.nestedfirstPanelToLoad != 1 && nestedsettings.nestedfirstPanelToLoad <= nestedpanelCount) { 
			var nestedcurrentPanel = nestedsettings.nestedfirstPanelToLoad;
			var nestedoffset = - (nestedpanelWidth*(nestedcurrentPanel - 1));
			$('.nestedpanel-container', nestedslider).css({ marginLeft: nestedoffset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var nestedcurrentPanel = 1;
		};
			
		// Left arrow click
		$("#nested-nav-left-" + nestedsliderCount + " a").click(function(){
			nestednavClicks++;
			if (nestedcurrentPanel == 1) {
				nestedoffset = - (nestedpanelWidth*(nestedpanelCount - 1));
				nestedalterPanelHeight(nestedpanelCount - 1);
				nestedcurrentPanel = nestedpanelCount;
				nestedslider.siblings('.nested-nav').find('a.nestedcurrent').removeClass('nestedcurrent').parents('ul').find('li:last a').addClass('nestedcurrent');
			} else {
				nestedcurrentPanel -= 1;
				nestedalterPanelHeight(nestedcurrentPanel - 1);
				nestedoffset = - (nestedpanelWidth*(nestedcurrentPanel - 1));
				nestedslider.siblings('.nested-nav').find('a.nestedcurrent').removeClass('nestedcurrent').parent().prev().find('a').addClass('nestedcurrent');
			};
			$('.nestedpanel-container', nestedslider).animate({ marginLeft: nestedoffset }, nestedsettings.nestedslideEaseDuration, nestedsettings.nestedslideEaseFunction);
			if (nestedsettings.nestedcrossLinking) { location.hash = nestedcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#nested-nav-right-' + nestedsliderCount + ' a').click(function(){
			nestednavClicks++;
			if (nestedcurrentPanel == nestedpanelCount) {
				nestedoffset = 0;
				nestedcurrentPanel = 1;
				nestedalterPanelHeight(0);
				nestedslider.siblings('.nested-nav').find('a.nestedcurrent').removeClass('nestedcurrent').parents('ul').find('a:eq(0)').addClass('nestedcurrent');
			} else {
				nestedoffset = - (nestedpanelWidth*nestedcurrentPanel);
				nestedalterPanelHeight(nestedcurrentPanel);
				nestedcurrentPanel += 1;
				nestedslider.siblings('.nested-nav').find('a.nestedcurrent').removeClass('nestedcurrent').parent().next().find('a').addClass('nestedcurrent');
			};
			$('.nestedpanel-container', nestedslider).animate({ marginLeft: nestedoffset }, nestedsettings.nestedslideEaseDuration, nestedsettings.nestedslideEaseFunction);
			if (nestedsettings.nestedcrossLinking) { location.hash = nestedcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (nestedsettings.nesteddynamicTabs) {
			var nesteddynamicTabs = '<div class="nested-nav" id="nested-nav-' + nestedsliderCount + '"><ul></ul></div>';
			switch (nestedsettings.nesteddynamicTabsPosition) {
				case "bottom":
					nestedslider.parent().append(nesteddynamicTabs);
					break;
				default:
					nestedslider.parent().prepend(nesteddynamicTabs);
					break;
			};
			ul = $('#nested-nav-' + nestedsliderCount + ' ul');
			// Create the nav items
			$('.nestedpanel', nestedslider).each(function(n) {
				ul.append('<li class="nestedtab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(nestedsettings.nestedpanelTitleSelector).text() + '</a></li>');												
			});
			nestednavContainerWidth = nestedslider.width() + nestedslider.siblings('.nested-nav-left').width() + nestedslider.siblings('.nested-nav-right').width();
			ul.parent().css({ width: nestednavContainerWidth });
			switch (nestedsettings.nesteddynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * nestedpanelCount });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#nested-nav-' + nestedsliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				nestednavClicks++;
				$(this).addClass('nestedcurrent').parents('ul').find('a').not($(this)).removeClass('nestedcurrent');
				nestedoffset = - (nestedpanelWidth*z);
				nestedalterPanelHeight(z);
				nestedcurrentPanel = z + 1;
				$('.nestedpanel-container', nestedslider).animate({ marginLeft: nestedoffset }, nestedsettings.nestedslideEaseDuration, nestedsettings.nestedslideEaseFunction);
				if (!nestedsettings.nestedcrossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
			});
		});
		
		// External triggers (anywhere on the page)
		$(nestedsettings.nestedexternalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (nestedsliderCount == parseInt($(this).attr("rel").slice(12))) {
				$(this).bind("click", function() {
					nestednavClicks++;
					nestedtargetPanel = parseInt($(this).attr("href").slice(1));
					nestedoffset = - (nestedpanelWidth*(nestedtargetPanel - 1));
					nestedalterPanelHeight(nestedtargetPanel - 1);
					nestedcurrentPanel = nestedtargetPanel;
					// Switch the current tab:
					nestedslider.siblings('.nested-nav').find('a').removeClass('nestedcurrent').parents('ul').find('li:eq(' + (nestedtargetPanel - 1) + ') a').addClass('nestedcurrent');
					// Slide
					$('.nestedpanel-container', nestedslider).animate({ marginLeft: nestedoffset }, nestedsettings.nestedslideEaseDuration, nestedsettings.nestedslideEaseFunction);
					if (!nestedsettings.nestedcrossLinking) { 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 (nestedsettings.nestedcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= nestedpanelCount) {
			$("#nested-nav-" + nestedsliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("nestedcurrent");
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (nestedsettings.nestedfirstPanelToLoad != 1 && nestedsettings.nestedfirstPanelToLoad <= nestedpanelCount) {
			$("#nested-nav-" + nestedsliderCount + " a:eq(" + (nestedsettings.nestedfirstPanelToLoad - 1) + ")").addClass("nestedcurrent");
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			$("#nested-nav-" + nestedsliderCount + " a:eq(0)").addClass("nestedcurrent");
		};
		
		// Set the height of the first panel
		if (nestedsettings.nestedautoHeight) {
			nestedpanelHeight = $('.nestedpanel:eq(' + (nestedcurrentPanel - 1) + ')', nestedslider).height();
			nestedslider.css({ height: nestedpanelHeight });
		};
		
		// Trigger autoSlide
		if (nestedsettings.nestedautoSlide) {
			nestedslider.ready(function() {
				setTimeout(nestedautoSlide,nestedsettings.nestedautoSlideInterval);
			});
		};
		
		function nestedalterPanelHeight(x) {
			if (nestedsettings.autoHeight) {
				nestedpanelHeight = $('.nestedpanel:eq(' + x + ')', nestedslider).height()
				nestedslider.animate({ height: nestedpanelHeight }, nestedsettings.nestedautoHeightEaseDuration, nestedsettings.nestedautoHeightEaseFunction);
			};
		};
		
		function nestedautoSlide() {
			if (nestednavClicks == 0 || !nestedsettings.nestedautoSlideStopWhenClicked) {
				if (nestedcurrentPanel == nestedpanelCount) {
					var nestedoffset = 0;
					nestedcurrentPanel = 1;
				} else {
					var nestedoffset = - (nestedpanelWidth*nestedcurrentPanel);
					nestedcurrentPanel += 1;
				};
				nestedalterPanelHeight(nestedcurrentPanel - 1);
				// Switch the current tab:
				nestedslider.siblings('.nested-nav').find('a').removeClass('nestedcurrent').parents('ul').find('li:eq(' + (nestedcurrentPanel - 1) + ') a').addClass('nestedcurrent');
				// Slide:
				$('.nestedpanel-container', nestedslider).animate({ marginLeft: nestedoffset }, nestedsettings.nestedslideEaseDuration, nestedsettings.nestedslideEaseFunction);
				setTimeout(nestedautoSlide,nestedsettings.nestedautoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.nestedpanel', nestedslider).show().end().find("p.nestedloading").remove();
		nestedslider.removeClass("nestedpreload");
		
		nestedsliderCount++;
		
	});
};
