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

var VnestedxsliderCount = 1;

$.fn.VnestedxcodaSlider = function(Vnestedxsettings) {

	Vnestedxsettings = $.extend({
		VnestedxautoHeight: true,
		VnestedxautoHeightEaseDuration: 1000,
		VnestedxautoHeightEaseFunction: "easeInOutExpo",
		VnestedxautoSlide: false,
		VnestedxautoSlideInterval: 7000,
		VnestedxautoSlideStopWhenClicked: true,
		VnestedxcrossLinking: true,
		VnestedxdynamicArrows: true,
		VnestedxdynamicArrowLeftText: "",
		VnestedxdynamicArrowRightText: "",
		VnestedxdynamicTabs: false,
		VnestedxdynamicTabsAlign: "center",
		VnestedxdynamicTabsPosition: "top",
		VnestedxexternalTriggerSelector: "a.Vnestedxxtrig",
		VnestedxfirstPanelToLoad: 1,
		VnestedxpanelTitleSelector: "h3.Vnestedxtitle",
		VnestedxslideEaseDuration: 1000,
		VnestedxslideEaseFunction: "easeInOutExpo"
	}, Vnestedxsettings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		//alert("Testing preloader");
		
		var Vnestedxslider = $(this);
		
		// If we need arrows
		if (Vnestedxsettings.VnestedxdynamicArrows) {
			Vnestedxslider.parent().addClass("Vnestedxarrows");
			Vnestedxslider.before('<div class="Vnestedxcoda-nav-left" id="Vnestedxcoda-nav-left-' + VnestedxsliderCount + '"><a href="#">' + Vnestedxsettings.VnestedxdynamicArrowLeftText + '</a></div>');
			Vnestedxslider.after('<div class="Vnestedxcoda-nav-right" id="Vnestedxcoda-nav-right-' + VnestedxsliderCount + '"><a href="#">' + Vnestedxsettings.VnestedxdynamicArrowRightText + '</a></div>');
		};
		
		var VnestedxpanelWidth = Vnestedxslider.find(".Vnestedxpanel").width();
		var VnestedxpanelCount = Vnestedxslider.find(".Vnestedxpanel").size();
		var VnestedxpanelContainerWidth = VnestedxpanelWidth*VnestedxpanelCount;
		var VnestedxnavClicks = 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)
		$('.Vnestedxpanel', Vnestedxslider).wrapAll('<div class="Vnestedxpanel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".Vnestedxpanel-container", Vnestedxslider).css({ width: VnestedxpanelContainerWidth });
		
		// 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 (Vnestedxsettings.VnestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= VnestedxpanelCount) {
			var VnestedxcurrentPanel = parseInt(location.hash.slice(1));
			var Vnestedxoffset = - (VnestedxpanelWidth*(VnestedxcurrentPanel - 1));
			$('.Vnestedxpanel-container', Vnestedxslider).css({ marginLeft: Vnestedxoffset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (Vnestedxsettings.VnestedxfirstPanelToLoad != 1 && Vnestedxsettings.VnestedxfirstPanelToLoad <= VnestedxpanelCount) { 
			var VnestedxcurrentPanel = Vnestedxsettings.VnestedxfirstPanelToLoad;
			var Vnestedxoffset = - (VnestedxpanelWidth*(VnestedxcurrentPanel - 1));
			$('.Vnestedxpanel-container', Vnestedxslider).css({ marginLeft: Vnestedxoffset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var VnestedxcurrentPanel = 1;
		};
			
		// Left arrow click
		$("#Vnestedxcoda-nav-left-" + VnestedxsliderCount + " a").click(function(){
			VnestedxnavClicks++;
			if (VnestedxcurrentPanel == 1) {
				Vnestedxoffset = - (VnestedxpanelWidth*(VnestedxpanelCount - 1));
				VnestedxalterPanelHeight(VnestedxpanelCount - 1);
				VnestedxcurrentPanel = VnestedxpanelCount;
				Vnestedxslider.siblings('.Vnestedxcoda-nav').find('a.Vnestedxcurrent').removeClass('Vnestedxcurrent').parents('ul').find('li:last a').addClass('Vnestedxcurrent');
			} else {
				VnestedxcurrentPanel -= 1;
				VnestedxalterPanelHeight(VnestedxcurrentPanel - 1);
				Vnestedxoffset = - (VnestedxpanelWidth*(VnestedxcurrentPanel - 1));
				Vnestedxslider.siblings('.Vnestedxcoda-nav').find('a.Vnestedxcurrent').removeClass('Vnestedxcurrent').parent().prev().find('a').addClass('Vnestedxcurrent');
			};
			$('.Vnestedxpanel-container', Vnestedxslider).animate({ marginLeft: Vnestedxoffset }, Vnestedxsettings.VnestedxslideEaseDuration, Vnestedxsettings.VnestedxslideEaseFunction);
			if (Vnestedxsettings.VnestedxcrossLinking) { location.hash = VnestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#Vnestedxcoda-nav-right-' + VnestedxsliderCount + ' a').click(function(){
			VnestedxnavClicks++;
			if (VnestedxcurrentPanel == VnestedxpanelCount) {
				Vnestedxoffset = 0;
				VnestedxcurrentPanel = 1;
				VnestedxalterPanelHeight(0);
				Vnestedxslider.siblings('.Vnestedxcoda-nav').find('a.Vnestedxcurrent').removeClass('Vnestedxcurrent').parents('ul').find('a:eq(0)').addClass('Vnestedxcurrent');
			} else {
				Vnestedxoffset = - (VnestedxpanelWidth*VnestedxcurrentPanel);
				VnestedxalterPanelHeight(VnestedxcurrentPanel);
				VnestedxcurrentPanel += 1;
				Vnestedxslider.siblings('.Vnestedxcoda-nav').find('a.Vnestedxcurrent').removeClass('Vnestedxcurrent').parent().next().find('a').addClass('Vnestedxcurrent');
			};
			$('.Vnestedxpanel-container', Vnestedxslider).animate({ marginLeft: Vnestedxoffset }, Vnestedxsettings.VnestedxslideEaseDuration, Vnestedxsettings.VnestedxslideEaseFunction);
			if (Vnestedxsettings.VnestedxcrossLinking) { location.hash = VnestedxcurrentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (Vnestedxsettings.VnestedxdynamicTabs) {
			var VnestedxdynamicTabs = '<div class="Vnestedxcoda-nav" id="Vnestedxcoda-nav-' + VnestedxsliderCount + '"><ul></ul></div>';
			switch (Vnestedxsettings.VnestedxdynamicTabsPosition) {
				case "bottom":
					Vnestedxslider.parent().append(VnestedxdynamicTabs);
					break;
				default:
					Vnestedxslider.parent().prepend(VnestedxdynamicTabs);
					break;
			};
			ul = $('#Vnestedxcoda-nav-' + VnestedxsliderCount + ' ul');
			// Create the nav items
			$('.Vnestedxpanel', Vnestedxslider).each(function(n) {
				ul.append('<li class="Vnestedxtab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(Vnestedxsettings.VnestedxpanelTitleSelector).text() + '</a></li>');												
			});
			VnestedxnavContainerWidth = Vnestedxslider.width() + Vnestedxslider.siblings('.Vnestedxcoda-nav-left').width() + Vnestedxslider.siblings('.Vnestedxcoda-nav-right').width();
			ul.parent().css({ width: VnestedxnavContainerWidth });
			switch (Vnestedxsettings.VnestedxdynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * VnestedxpanelCount });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#Vnestedxcoda-nav-' + VnestedxsliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				VnestedxnavClicks++;
				$(this).addClass('Vnestedxcurrent').parents('ul').find('a').not($(this)).removeClass('Vnestedxcurrent');
				Vnestedxoffset = - (VnestedxpanelWidth*z);
				VnestedxalterPanelHeight(z);
				VnestedxcurrentPanel = z + 1;
				$('.Vnestedxpanel-container', Vnestedxslider).animate({ marginLeft: Vnestedxoffset }, Vnestedxsettings.VnestedxslideEaseDuration, Vnestedxsettings.VnestedxslideEaseFunction);
				if (!Vnestedxsettings.VnestedxcrossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
			});
		});
		
		// External triggers (anywhere on the page)
		$(Vnestedxsettings.VnestedxexternalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (VnestedxsliderCount == parseInt($(this).attr("rel").slice(12))) {
				$(this).bind("click", function() {
					VnestedxnavClicks++;
					VnestedxtargetPanel = parseInt($(this).attr("href").slice(1));
					Vnestedxoffset = - (VnestedxpanelWidth*(VnestedxtargetPanel - 1));
					VnestedxalterPanelHeight(VnestedxtargetPanel - 1);
					VnestedxcurrentPanel = VnestedxtargetPanel;
					// Switch the current tab:
					Vnestedxslider.siblings('.Vnestedxcoda-nav').find('a').removeClass('Vnestedxcurrent').parents('ul').find('li:eq(' + (VnestedxtargetPanel - 1) + ') a').addClass('Vnestedxcurrent');
					// Slide
					$('.Vnestedxpanel-container', Vnestedxslider).animate({ marginLeft: Vnestedxoffset }, Vnestedxsettings.VnestedxslideEaseDuration, Vnestedxsettings.VnestedxslideEaseFunction);
					if (!Vnestedxsettings.VnestedxcrossLinking) { 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 (Vnestedxsettings.VnestedxcrossLinking && location.hash && parseInt(location.hash.slice(1)) <= VnestedxpanelCount) {
			$("#Vnestedxcoda-nav-" + VnestedxsliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("Vnestedxcurrent");
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (Vnestedxsettings.VnestedxfirstPanelToLoad != 1 && Vnestedxsettings.VnestedxfirstPanelToLoad <= VnestedxpanelCount) {
			$("#Vnestedxcoda-nav-" + VnestedxsliderCount + " a:eq(" + (Vnestedxsettings.VnestedxfirstPanelToLoad - 1) + ")").addClass("Vnestedxcurrent");
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			$("#Vnestedxcoda-nav-" + VnestedxsliderCount + " a:eq(0)").addClass("Vnestedxcurrent");
		};
		
		// Set the height of the first panel
		if (Vnestedxsettings.VnestedxautoHeight) {
			VnestedxpanelHeight = $('.Vnestedxpanel:eq(' + (VnestedxcurrentPanel - 1) + ')', Vnestedxslider).height();
			Vnestedxslider.css({ height: VnestedxpanelHeight });
		};
		
		// Trigger autoSlide
		if (Vnestedxsettings.VnestedxautoSlide) {
			Vnestedxslider.ready(function() {
				setTimeout(VnestedxautoSlide,Vnestedxsettings.VnestedxautoSlideInterval);
			});
		};
		
		function VnestedxalterPanelHeight(x) {
			if (Vnestedxsettings.autoHeight) {
				VnestedxpanelHeight = $('.Vnestedxpanel:eq(' + x + ')', Vnestedxslider).height()
				Vnestedxslider.animate({ height: VnestedxpanelHeight }, Vnestedxsettings.VnestedxautoHeightEaseDuration, Vnestedxsettings.VnestedxautoHeightEaseFunction);
			};
		};
		
		function VnestedxautoSlide() {
			if (VnestedxnavClicks == 0 || !Vnestedxsettings.VnestedxautoSlideStopWhenClicked) {
				if (VnestedxcurrentPanel == VnestedxpanelCount) {
					var Vnestedxoffset = 0;
					VnestedxcurrentPanel = 1;
				} else {
					var Vnestedxoffset = - (VnestedxpanelWidth*VnestedxcurrentPanel);
					VnestedxcurrentPanel += 1;
				};
				VnestedxalterPanelHeight(VnestedxcurrentPanel - 1);
				// Switch the current tab:
				Vnestedxslider.siblings('.Vnestedxcoda-nav').find('a').removeClass('Vnestedxcurrent').parents('ul').find('li:eq(' + (VnestedxcurrentPanel - 1) + ') a').addClass('Vnestedxcurrent');
				// Slide:
				$('.Vnestedxpanel-container', Vnestedxslider).animate({ marginLeft: Vnestedxoffset }, Vnestedxsettings.VnestedxslideEaseDuration, Vnestedxsettings.VnestedxslideEaseFunction);
				setTimeout(VnestedxautoSlide,Vnestedxsettings.VnestedxautoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.Vnestedxpanel', Vnestedxslider).show().end().find("p.Vnestedxloading").remove();
		Vnestedxslider.removeClass("Vnestedxpreload");
		
		VnestedxsliderCount++;
		
	});
};
