jQuery(function(){
	// Preloader
	jQuery("#md_ind_inner").children('.panel').hide().end().prepend('<p class="loading">Loading testimonials...</p>');
});

var sliderCount = 1;

jQuery.fn.codaSlider = function(settings) {

	settings = jQuery.extend({
		autoHeight: true,
		autoHeightEaseDuration: 1000,
		autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: false,
		autoSlideInterval: 7000,
		autoSlideStopWhenClicked: false,
		crossLinking: false, // URL has a hash (ex: www.example.com/something#1)
		sliderCountTop: false,
		sliderCountBot: false,
		dynamicArrows: false, // left and right navigation
		dynamicArrowLeftText: '',
		dynamicArrowRightText: '',
		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 = jQuery(this);
		
		// If we need arrows
		if (settings.dynamicArrows) {
			if (settings.sliderCountTop == true) {
				slider.before('<div class="md_por_pager"><div class="clear"></div></div>');
			}
			if (settings.sliderCountBot == true) {
				slider.after('<div class="md_por_pager"><div class="clear"></div></div>');
			}
			var pager = jQuery('.md_por_pager');
			pager.append('<a href="#" class="navleft_' + sliderCount + '">' + settings.dynamicArrowLeftText + '</a>');
			pager.append('<a href="#" class="navright_' + sliderCount + '">' + settings.dynamicArrowRightText + '</a>');
		};
		
		// start and stop button
		/*
		slider.before('<div class="md_por_startstop">Slideshow <img src="images/start_btn.png" alt="" id="coda-start" class="imghover" /> <img src="images/stop_btn.png" alt="" id="coda-stop" class="imghover" /><div class="clear"></div></div>');
		$('#coda-stop').bind('click', function() {
			stopAutoSlide();
		});
		$('#coda-start').bind('click', function() {
			startAutoSlide();
		});
		*/
		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)
		// jQuery('.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)
		jQuery("#md_ind_inner", 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));
			jQuery('#md_ind_inner', 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));
			jQuery('#md_ind_inner', slider).css({ marginLeft: offset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var currentPanel = 1;
		};
			
		// Left arrow click
		/*
		jQuery(".navleft_" + sliderCount).click(function(){
			navClicks++;
			if (currentPanel == 1) {
				offset = - (panelWidth*(panelCount - 1));
				alterPanelHeight(panelCount - 1);
				currentPanel = panelCount;
				jQuery('.navtabs a.pinon').removeClass('pinon').parents('p').find('a:last').addClass('pinon');
			} else {
				currentPanel -= 1;
				alterPanelHeight(currentPanel - 1);
				offset = - (panelWidth*(currentPanel - 1));
				jQuery('.navtabs a.pinon').removeClass('pinon').prev().addClass('pinon');
			};
			jQuery('#md_ind_inner', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			jQuery('#md_ind_thedes .md_por_forpics').removeClass('forpisee').parent().find('#forpics' + currentPanel).addClass('forpisee');
			if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		jQuery('.navright_' + sliderCount).click(function(){
			navClicks++;
			if (currentPanel == panelCount) {
				offset = 0;
				currentPanel = 1;
				alterPanelHeight(0);
				jQuery('.navtabs a.pinon').removeClass('pinon').parents('p').find('a:eq(0)').addClass('pinon');
			} else {
				offset = - (panelWidth*currentPanel);
				alterPanelHeight(currentPanel);
				currentPanel += 1;
				jQuery('.navtabs a.pinon').removeClass('pinon').next().addClass('pinon');
			};
			jQuery('#md_ind_inner', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			jQuery('#md_ind_thedes .md_por_forpics').removeClass('forpisee').parent().find('#forpics' + currentPanel).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 pagerTop = jQuery('.md_por_pager:eq(0)');
			if (settings.sliderCountTop == true) {
				var dynamicTabsTop = '<p class="mp_por_pager navtabs coda-nav-' + sliderCount + ' number-nav-top"></p>';
				pagerTop.append(dynamicTabsTop);
			}
			var pagerBot = jQuery('.md_por_pager:eq(1)');
			if (settings.sliderCountBot == true) {
				var dynamicTabsBot = '<p class="mp_por_pager navtabs coda-nav-' + sliderCount + ' number-nav-bot"></p>';
				pagerBot.append(dynamicTabsBot);
			}
			pt = jQuery('.coda-nav-' + sliderCount);
			// Create the nav items
			jQuery('.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
		jQuery('.coda-nav-' + sliderCount).each(function(c) {
			var codaNav = this;
			switch (c) { // get the other navi name
				case 1:
					var numNav = 'top';
					break;
				default:
					var numNav = 'bot';
			}
			jQuery('a', codaNav).each(function(z) {
				// What happens when a nav link is clicked
				jQuery(this).bind("click", function() {
					navClicks++;
					jQuery(this).addClass('pinon').parents('p').find('a').not(jQuery(this)).removeClass('pinon');
					if (settings.sliderCountBot == true) {
						jQuery('#md_por_bigpager .number-nav-' + numNav + ' a').removeClass('pinon');
						jQuery('#md_por_bigpager .number-nav-' + numNav + ' a:eq(' + z + ')').addClass('pinon');
					}
					offset = - (panelWidth*z);
					alterPanelHeight(z);
					currentPanel = z + 1;
					jQuery('#md_ind_inner', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
					jQuery('#md_ind_thedes .md_por_forpics').removeClass('forpisee').parent().find('#forpics' + currentPanel).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) {
			jQuery(".coda-nav-" + sliderCount).each(function() {
				var navThis = jQuery(this);
				jQuery("a:eq(" + (location.hash.slice(1) - 1) + ")", navThis).addClass("pinon");
			});
			jQuery("#md_ind_thedes .md_por_forpics: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) {
			jQuery(".coda-nav-" + sliderCount).each(function() {
				var navThis = jQuery(this);
				jQuery("a:eq(" + (settings.firstPanelToLoad - 1) + ")", navThis).addClass("pinon");
			});
			jQuery("#md_ind_thedes .md_por_forpics:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("forpisee");
		// Otherwise we must be loading Panel 1, so make the first tab the pinon one.
		} else {
			jQuery(".coda-nav-" + sliderCount).each(function() {
				var navThis = jQuery(this);
				jQuery("a:eq(0)", navThis).addClass("pinon");
			});
			jQuery("#md_ind_thedes .md_por_forpics:eq(0)").addClass("forpisee");
		};
		
		// Set the height of the first panel
		if (settings.autoHeight) {
			panelHeight = jQuery('.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 = jQuery('.panel:eq(' + x + ')', slider).outerHeight(true);
				slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
			};
		};
		
		function stopAutoSlide() {
			navClicks = 0;
			clearTimeout(timeout);
		}
		
		function startAutoSlide() {
			navClicks = 0;
			clearTimeout(timeout);
			timeout = setTimeout(autoSlide,settings.autoSlideInterval);
		}
		
		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:
				jQuery('.navtabs a').removeClass('pinon').parent().find('a:eq(' + (currentPanel - 1) + ')').addClass('pinon');
				// Switch the description section:
				jQuery('#md_ind_thedes .md_por_forpics').removeClass('forpisee').parent().find('#forpics' + currentPanel).addClass('forpisee');
				// Slide:
				jQuery('#md_ind_inner', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				timeout = setTimeout(autoSlide,settings.autoSlideInterval);
			}
		};
		
		// Kill the preloader
		jQuery('.panel', slider).show().end().find("p.loading").remove();
		
		sliderCount++;
		
	});
};
