$(function(){
	// Preloader
	$("#allslidesi").children('.panel').hide().end().prepend('<p class="loading">Loading pictures...</p>');
});

var sliderCount = 1;

$.fn.codaSlider = function(settings) {

	settings = $.extend({
		autoHeight: false,
		autoHeightEaseDuration: 1000,
		autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: false,
		autoSlideInterval: 7000,
		autoSlideStopWhenClicked: true,
		crossLinking: false, // URL has a hash (ex: www.example.com/something#1)
		dynamicArrows: false, // left and right navigation
		dynamicArrowLeftText: '<img src="images/blank.gif" alt="" id="slideleft_img" class="ind_slideleft_img" />',
		dynamicArrowRightText: '<img src="images/blank.gif" alt="" id="slideright_img" class="ind_slideright_img" />',
		dynamicTabs: false, // 'pages' - panels - numbers
		firstPanelToLoad: 1,
		slideEaseDuration: 1000,
		slideEaseFunction: "easeInOutExpo"
	}, settings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		// alert("Testing preloader");
		
		var slider = $(this);
		
		// If we need arrows
		if (settings.dynamicArrows) {
			slider.append('<a href="#" id="navleft_' + sliderCount + '">' + settings.dynamicArrowLeftText + '</a>');
			slider.append('<a href="#" id="navright_' + sliderCount + '">' + settings.dynamicArrowRightText + '</a>');
			slider.append('<div class="clear">');
		};
		
		var panelWidth = slider.find(".panel").outerWidth(true);
		var panelCount = slider.find(".panel").size();
		var panelContainerWidth = panelWidth*panelCount;
		var navClicks = 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)
		// $('.panel', slider).wrapAll('<div class="panel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$("#allslidesi", slider).css({ width: panelContainerWidth });
		
		// 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 (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			var currentPanel = parseInt(location.hash.slice(1));
			var offset = - (panelWidth*(currentPanel - 1));
			$('#allslidesi', slider).css({ marginLeft: offset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) { 
			var currentPanel = settings.firstPanelToLoad;
			var offset = - (panelWidth*(currentPanel - 1));
			$('#allslidesi', slider).css({ marginLeft: offset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var currentPanel = 1;
		};
			
		// Left arrow click
		$("#navleft_" + sliderCount).click(function(){
			navClicks++;
			if (currentPanel == 1) {
				offset = - (panelWidth*(panelCount - 1));
				alterPanelHeight(panelCount - 1);
				currentPanel = panelCount;
				$('.navtabs a.pinon').removeClass('pinon').parents('p').find('a:last').addClass('pinon');
			} else {
				currentPanel -= 1;
				alterPanelHeight(currentPanel - 1);
				offset = - (panelWidth*(currentPanel - 1));
				$('.navtabs a.pinon').removeClass('pinon').prev().addClass('pinon');
			};
			$('#allslidesi', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			$('#md_ind_thedes div').removeClass('forpisee').parent().find('div:eq(' + (currentPanel - 1) + ')').addClass('forpisee');
			if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#navright_' + sliderCount).click(function(){
			navClicks++;
			if (currentPanel == panelCount) {
				offset = 0;
				currentPanel = 1;
				alterPanelHeight(0);
				$('.navtabs a.pinon').removeClass('pinon').parents('p').find('a:eq(0)').addClass('pinon');
			} else {
				offset = - (panelWidth*currentPanel);
				alterPanelHeight(currentPanel);
				currentPanel += 1;
				$('.navtabs a.pinon').removeClass('pinon').next().addClass('pinon');
			};
			$('#allslidesi', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			$('#md_ind_thedes div').removeClass('forpisee').parent().find('div:eq(' + (currentPanel - 1) + ')').addClass('forpisee');
			if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (settings.dynamicTabs && settings.dynamicArrows) {
			var dynamicTabs = '<p class="mp_por_pager navtabs" id="coda-nav-' + sliderCount + '"></p>';
			slider.append(dynamicTabs);
			pt = $('#coda-nav-' + sliderCount);
			// Create the nav items
			$('.panel', slider).each(function(n) {
				pt.append('<a href="#' + (n+1) + '">' + (n+1) + '</a> ');
			});
			// navContainerWidth = slider.width() + slider.siblings('.coda-nav-left').width() + slider.siblings('.coda-nav-right').width();
			// pt.parent().css({ width: navContainerWidth });
		};
			
		// If we need a tabbed nav
		$('#coda-nav-' + sliderCount + ' a').each(function(z) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				navClicks++;
				$(this).addClass('pinon').parents('p').find('a').not($(this)).removeClass('pinon');
				offset = - (panelWidth*z);
				alterPanelHeight(z);
				currentPanel = z + 1;
				$('#allslidesi', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				$('#md_ind_thedes div').removeClass('forpisee').parent().find('div:eq(' + (currentPanel - 1) + ')').addClass('forpisee');
				if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
			});
		});
			
		// Specify which tab is initially set to "pinon". Depends on if the loaded URL had a hash or not (cross-linking).
		if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			$("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("pinon");
			$("#md_ind_thedes div:eq(" + (location.hash.slice(1) - 1) + ")").addClass("forpisee");
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
			$("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("pinon");
			$("#md_ind_thedes div:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("forpisee");
		// Otherwise we must be loading Panel 1, so make the first tab the pinon one.
		} else {
			$("#coda-nav-" + sliderCount + " a:eq(0)").addClass("pinon");
			$("#md_ind_thedes div:eq(0)").addClass("forpisee");
		};
		
		// Set the height of the first panel
		if (settings.autoHeight) {
			panelHeight = $('.panel:eq(' + (currentPanel - 1) + ')', slider).outerHeight(true);
			slider.css({ height: panelHeight });
		};
		
		// Trigger autoSlide
		if (settings.autoSlide) {
			slider.ready(function() {
				timeout = setTimeout(autoSlide,settings.autoSlideInterval);
			});
		};
		
		function alterPanelHeight(x) {
			if (settings.autoHeight) {
				panelHeight = $('.panel:eq(' + x + ')', slider).outerHeight(true);
				slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
			};
		};
		
		function autoSlide() {
			if (navClicks != 0 && !settings.autoSlideStopWhenClicked) {
				navClicks = 0;
				clearTimeout(timeout);
				timeout = setTimeout(autoSlide,settings.autoSlideInterval);
			}
			else if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
				if (currentPanel == panelCount) {
					var offset = 0;
					currentPanel = 1;
				} else {
					var offset = - (panelWidth*currentPanel);
					currentPanel += 1;
				};
				alterPanelHeight(currentPanel - 1);
				// Switch the current tab:
				$('.navtabs a').removeClass('pinon').parent().find('a:eq(' + (currentPanel - 1) + ')').addClass('pinon');
				// Switch the description section:
				$('#md_ind_thedes div').removeClass('forpisee').parent().find('div:eq(' + (currentPanel - 1) + ')').addClass('forpisee');
				// Slide:
				$('#allslidesi', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				timeout = setTimeout(autoSlide,settings.autoSlideInterval);
			}
		};
		
		// Kill the preloader
		$('.panel', slider).show().end().find("p.loading").remove();
		
		sliderCount++;
		
	});
};
