/*
	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.
*/

var sliderCount = 2;

$.fn.codaSlider = function(settings) {

	settings = $.extend({
		//autoHeight: true,
		//autoHeightEaseDuration: 1000,
		//autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: true,
		autoSlideInterval: 4000,
		autoSlideStopWhenClicked: false,
		dynamicTabs: false,
		dynamicTabsAlign: "center",
		dynamicTabsPosition: "top",
		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);
		var panelWidth = slider.find(" .panel").width();
		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>');
		$(' .panel-container', slider).css({ width: panelWidth*panelCount });

		if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) { 
			var currentPanel = settings.firstPanelToLoad;
			var offset = - (panelWidth*(currentPanel - 1));
			$(' .panel-container', slider).css({ marginLeft: offset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var currentPanel = 1;
		};
			
		// If we need a tabbed nav
		$('#coda-nav-5 td').find(".w1").each(function(z) {
			
			$(this).hover(function() {
				$('.w1').removeClass('current');
				$(this).addClass('current');
				offset = - (panelWidth*z);
				//var offset_li = - (45*z);
				currentPanel = z + 1;
				$(' .panel-container', slider).stop().animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				}
			);
		});
				
		function autoSlide() {
			if (!settings.autoSlideStopWhenClicked) {
				if (currentPanel == panelCount) {
					var offset = 0;
					//var offset_li = 0;
					currentPanel = 1;
				} else {
					//var offset_li = - (45*currentPanel);
					var offset = - (panelWidth*currentPanel);
					currentPanel += 1;
				};
				
				//Switch the current tab:
				$('.w1').removeClass('current').parents('table').find('td:eq(' + (currentPanel) + ') .w1').addClass('current');

				//Slide:
				$(' .panel-container', slider).stop().animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);

				//$("#active_row").stop().animate({ top: -offset_li+16 }, settings.slideEaseDuration, settings.slideEaseFunction);
				 
				 $('#coda-nav-5 td').find(".w1").each(function(z) {   
					$(this).hover(function() {
					clearTimeout(timer);
						}
					);
				});
				
				$(".panel").each(function() {
					$(this).hover(function() {
					clearTimeout(timer);
						}
					);
				});
				 
				var timer = setTimeout(autoSlide,settings.autoSlideInterval);
			};
		};
		
		//Trigger autoSlide
		if (settings.autoSlide) {
			slider.ready(function() {
				var timer = setTimeout(autoSlide,settings.autoSlideInterval);
			});
		};
		
	});
};
