/*
	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.
*/

(function($) {
	// Remove the coda-slider-no-js class from the body
	//$('body').removeClass('coda-slider-no-js');
	// Preloader
	//$('.coda-slider').children('.panel').hide().end().prepend('<p class='loading'>Loading...<br /><img src='../images/ajax-loader.gif' alt='loading...' /></p>');
	
	var sliderCount = 1;
	var timeout;
	
	
	
	var methods = {
		    init : function(options) {
		    	
		    	var settings = {
					autoHeight				: true,
					autoHeightEaseDuration	: 1000,
					autoHeightEaseFunction	: 'easeInOutExpo',
					autoSlide				: false,
					autoSlideInterval		: 7000,
					autoSlideStopWhenClicked: true,
					crossLinking			: true,
					dynamicArrows			: true,
					dynamicArrowLeftText	: '&#171; left',
					dynamicArrowRightText	: 'right &#187;',
					dynamicTabs				: true,
					dynamicTabsAlign		: 'center',
					dynamicTabsPosition		: 'top',
					easeDuration			: 1000,
					easeFunction			: 'easeInOutExpo',
					externalTriggerSelector	: 'a.xtrig',
					effect					: 'slide',
					firstPanelToLoad		: 1,
					increment				: true,
					panelTitleSelector		: 'h2.title'
				};
				
				return this.each(function(){
				
					if (options) 
						$.extend(settings, options);
					
					var $this = $(this),
						slider = $(this),
						$navigation = $('#coda-nav-' + sliderCount);
						panelWidth = 0,
						panelCount = 0,
						panelContainerWidth = 0,
						navClicks = 0, // Used if autoSlideStopWhenClicked = true
						offset = 0,
						currentPanel = settings.firstPanelToLoad - 1;						
					
					$this.data('codaSlider', {
						autoHeight 				: settings.autoHeight,
						autoHeightEaseDuration 	: settings.autoHeightEaseDuration,
						autoHeightEaseFunction 	: settings.autoHeightEaseFunction,
						autoSlide 				: settings.autoSlide,
						autoSlideInterval		: settings.autoSlideInterval,
						autoSlideStopWhenClicked: settings.autoSlideStopWhenClicked,
						crossLinking			: settings.crossLinking,
						dynamicArrows			: settings.dynamicArrows,
						dynamicArrowLeftText	: settings.dynamicArrowLeftText,
						dynamicArrowRightText	: settings.dynamicArrowRightText,
						dynamicTabs				: settings.dynamicTabs,
						dynamicTabsAlign		: settings.dynamicTabsAlign,
						dynamicTabsPosition		: settings.dynamicTabsPosition,
						easeDuration			: settings.easeDuration,
						easeFunction			: settings.easeFunction,
						externalTriggerSelector	: settings.externalTriggerSelector,
						firstPanelToLoad		: settings.firstPanelToLoad,
						increment				: settings.increment,
						panelTitleSelector		: settings.panelTitleSelector,
						navigation				: $navigation,
						target					: $this
					});
					 
			
					// If we need arrows
					if (settings.dynamicArrows) {
						slider.parent().addClass('arrows');
						slider.before('<div class="coda-nav-left" id="coda-nav-left-' + sliderCount + '"><a href="#">' + settings.dynamicArrowLeftText + '</a></div>');
						slider.after('<div class="coda-nav-right" id="coda-nav-right-' + sliderCount + '"><a href="#">' + settings.dynamicArrowRightText + '</a></div>');
					};
					
					
					
					panelWidth = slider.find('.panel').width();
					panelCount = slider.find('.panel').size();
					panelContainerWidth = panelWidth*panelCount;
					
					// 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)
					$('.panel-container', 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));
						$('.panel-container', 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));
						$('.panel-container', slider).css({ marginLeft: offset });
					// Otherwise, we'll just set the current panel to 1...
					} else { 
						var currentPanel = 1;
					};*/
						
					// Left arrow click
					$('#coda-nav-left-' + sliderCount + ' a').click(function(e){
						
						e.preventDefault();
						clearTimeout(timeout);
						settings.autoSlide = false;
						changeToPanel(currentPanel - 1);
						
					});
						
					// Right arrow click
					$('#coda-nav-right-' + sliderCount + ' a').click(function(e){
						
						e.preventDefault();
						clearTimeout(timeout);
						settings.autoSlide = false;
						changeToPanel(currentPanel + 1);
						
					});
					
					// If we need a dynamic menu
					if (settings.dynamicTabs) {
						var dynamicTabs = '<div class="coda-nav" id="coda-nav-' + sliderCount + '"><ul></ul></div>';
						switch (settings.dynamicTabsPosition) {
							case 'bottom':
								slider.parent().append(dynamicTabs);
								break;
							default:
								slider.parent().prepend(dynamicTabs);
								break;
						};
						ul = $('#coda-nav-' + sliderCount + ' ul');
						// Create the nav items
						$('.panel', slider).each(function(n) {
							ul.append('<li class="tab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(settings.panelTitleSelector).text() + '</a></li>');												
						});
						navContainerWidth = slider.width() + slider.siblings('.coda-nav-left').width() + slider.siblings('.coda-nav-right').width();
						ul.parent().css({ width: navContainerWidth });
						switch (settings.dynamicTabsAlign) {
							case 'center':
								ul.css({ width: ($('li', ul).width() + 2) * panelCount });
								break;
							case 'right':
								ul.css({ 'float': 'right' });
								break;
						};
					};
						
					// 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('current').parents('ul').find('a').not($(this)).removeClass('current');
							offset = - (panelWidth*z);
							alterPanelHeight(z);
							currentPanel = z + 1;
							$('.panel-container', slider).animate({ marginLeft: offset }, settings.easeDuration, settings.easeFunction);
							if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
						});
					}); */
					
					
					$('#coda-nav-' + sliderCount + ' a').click(function(e) {
						// What happens when a nav link is clicked
						e.preventDefault();
						var panel = parseInt($(this).attr('href').slice(1));
						
						settings.autoSlide = false;
						changeToPanel(panel - 1);
						
					});
					
					// External triggers (anywhere on the page)
					$(settings.externalTriggerSelector).each(function() {
						// Make sure this only affects the targeted slider
						//if (sliderCount == parseInt($(this).attr('rel').slice(12))) {
							$(this).click(function() {
								
								var panel = parseInt($(this).attr('href').slice(1));
								
								settings.autoSlide = false;
								changeToPanel(panel - 1);
							});
						//};
					});
						
					// Specify which tab is initially set to 'current'. 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('current');
					// 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('current');
					// Otherwise we must be loading Panel 1, so make the first tab the current one.
					} else {
						$('#coda-nav-' + sliderCount + ' a:eq(0)').addClass('current');
					};
					*/
					
					
					
					//changeToPanel(currentPanel);
					
					// Set the height of the first panel
					if (settings.autoHeight) {
						panelHeight = $('.panel:eq(' + (currentPanel) + ')', slider).height();
						slider.css({ height: panelHeight });
					};
					
					// Trigger autoSlide
					if (settings.autoSlide) {
						timeout = setTimeout(changeToPanel, settings.autoSlideInterval);
						
						$this.bind({
							mouseenter: function(){
								settings.autoSlide = false;
								clearTimeout(timeout);
							},
							mouseleave: function(){
								settings.autoSlide = true;
								timeout = setTimeout(changeToPanel, settings.autoSlideInterval);
							}
						});
						
						$navigation.bind({
							mouseenter: function(){
								clearTimeout(timeout);
								settings.autoSlide = false;
							},
							mouseleave: function(){
								settings.autoSlide = true;
								timeout = setTimeout(changeToPanel, settings.autoSlideInterval);
							}
						});
					};
					
					function alterPanelHeight(x) {
						if (settings.autoHeight) {
							panelHeight = $('.panel:eq(' + x + ')', slider).height()
							slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
						};
					};
					
					
					function changeToPanel(panel) {
						
						if (panel == currentPanel)
							return false;
							
						if (panel == undefined || panel == null)
							panel = currentPanel + 1;
							
						var links = $navigation.find('li').length - 1;
						
						if (panel > links)
							panel = 0;
						
						if (panel < 0)
							panel = links;
							
						
						
						$navigation.find('a').removeClass('current');
						$navigation.find('li:eq(' + panel + ') a').addClass('current');
						
						offset = - (panelWidth*panel);
						alterPanelHeight(panel);
						
						if (settings.effect == 'slide') {
							$('.panel-container', slider).animate({ marginLeft: offset }, settings.easeDuration, settings.easeFunction);
						}
						else if(settings.effect == 'alpha') {
							$('.panel-container', slider).animate({ opacity: 0 }, settings.easeDuration, settings.easeFunction, function(){
								$(this).css({marginLeft: offset}).animate({ opacity: 1 }, settings.easeDuration, settings.easeFunction);
							});
						}
						else if(settings.effect == 'none') {
							$('.panel-container', slider).css({marginLeft: offset})
						}
							
						currentPanel = panel;
							
						if (settings.autoSlide)
							timeout = setTimeout(changeToPanel, settings.autoSlideInterval);
						else
							clearTimeout(timeout);				
					}
					
					// Kill the preloader
					$('.panel', slider).show().end().find('p.loading').remove();
					slider.removeClass('preload');
					
					if (settings.increment)
						sliderCount++;
				
					$navigation.find('a:eq(' + currentPanel + ')').addClass('current');
					offset = - (panelWidth * currentPanel);
					$('.panel-container', slider).css({ marginLeft: offset });
				
				});
			},
			
			reset : function() {
				
				return this.each(function(){
				
					var data = $(this).data('codaSlider');
		    		var $this = $(this);

					clearTimeout(timeout);
					sliderCount = 1;
					
					$this.unbind('mouseenter mouseleave');
					data.navigation.unbind('mouseleave');
					data.navigation.find('a').removeClass('current');
					
					$('#coda-nav-left-' + sliderCount + ' a').unbind('click');
					$('#coda-nav-right-' + sliderCount + ' a').unbind('click');
					$('#coda-nav-' + sliderCount + ' a').unbind('click');
				
				});
			}
	};
   
	   
	$.fn.codaSlider = function(method) { 	
		
		// logique des appels des methods
		if (methods[method]) {
	      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    } 
	    else if ( typeof method === 'object' || !method ) {
	      return methods.init.apply( this, arguments );
	    } 
	    else {
	      $.error( 'Method ' +  method + ' does not exist on jQuery.laaGallery' );
	    } 
	};
})(jQuery);

