/*-----------------------------------------------------------------------------
Plugins

date:      09/05/2011
author:    [Olivier Robert]
-----------------------------------------------------------------------------*/

(function($) {

	$.fn.makeSlider = function(options) {
	
		var defaults = {
			start: 0,
			effect: 'fade',
			speed: 5000,
			
			parentEl: 'ul',
			childrenEl: 'li',
			
			hasPaging: false,
			pagingButton: '',
			
			hasSlideIndicator: false,
			slideIndicator: '',
			
			stopOnHover: false,
			disableSlideClick: false,
			
			disableImageTitle: true,
			
			debug: false
		};
			
		var options = $.extend(defaults, options);
	
		$(this).each(function() {
		
			var $root         = $(this),
				$slider       = $root.find(options.parentEl+':eq(0)'),
			
				$slideList    = $slider.children(options.childrenEl),
				slideNum      = $slideList.length,
				
				WIDTH         = $slider.children(options.childrenEl+':eq(0)').width();
				
			var timer         = null;
				
			if (options.hasSlideIndicator) {
			
				var	$controlSlide =  $root.find(options.slideIndicator);
			
			}
				
			if (options.debug) {
				
				console.log('Slide Width: ' + WIDTH + ' - slideNum: ' + slideNum);
			}
			
			var Init = (function(){
			
				$.data($root,'step', options.start);
				$.data($root,'total', slideNum);
				
				switch(options.effect) {
				
					case 'slide':
					
						$slider.find(options.childrenEl+':gt(' + options.start + ')').hide();
					
					break;
				
					case 'fade':
					
						$root.css({position: 'relative'})
						$slider.find(options.childrenEl+':gt(0)').hide();
						$slideList.css({position: 'absolute', zIndex:1});
					
					break;
				
				}
				
				if (options.hasSlideIndicator) {
				
					$controlSlide.find('li:eq('+options.start+')').children('a').addClass('active');
					
				}
							
			})();
						
			var goToStep = function (direction, stepSize) {
			
				var step        = null;				
				
				var	sliderStep  = $.data($root, 'step'),
					sliderTotal = $.data($root, 'total');
								
				if(direction == 'next') {
				
					step = sliderStep + stepSize < sliderTotal ? sliderStep + stepSize : 0;
				
				} else if (direction == 'previous') {
				
					step = sliderStep - stepSize >= 0 ? sliderStep - stepSize : sliderTotal - 1;
				
				}
				
				if (options.debug) {
					
					console.log('Current: ' + sliderStep + ' - Next: ' + step + ' - Total: ' + sliderTotal);
				
				}
								
				return step;
			
			};
			
			var makeTransition = function(direction, stepSize){
			
				var step, slideWidth;
					
				step = goToStep(direction, stepSize);
				
				if (options.debug) {
				
					console.log('step transition:' + step);
				
				}
				
				switch(options.effect) {
				
					case 'slide':
					
						slideWidth = WIDTH * step;
					
						$slider.find(options.childrenEl+':eq(' + $.data($root,'step') + ')').stop().fadeOut();
				
						$slider.stop().animate({marginLeft: -(slideWidth) + 'px'}, 800, function(){
						
							$.data($root,'step', step);
							
							$slider.find(options.childrenEl+':eq(' + $.data($root,'step') + ')').stop().fadeIn();
							
							if (options.hasSlideIndicator) {
						
								updateSlideIndicator();
								
							}
						});
						
						if (options.debug) {
								
							console.log('updatedSliderStep: '+step + ' - slideWidth: '+slideWidth);
						}
						
					break;
					
					case 'fade':
					
						var $currentSlide = $slider.find(options.childrenEl+':eq(' + $.data($root,'step') + ')'),
							$nextSlide    = $slider.find(options.childrenEl+':eq(' + step + ')');
					
						$currentSlide.css({zIndex:2}).stop().fadeTo(800, 0, function(){ $(this).css({zIndex:1}); });
						
						$nextSlide.stop().fadeTo(500, 1, function(){ 
							 
							if (options.hasSlideIndicator) { updateSlideIndicator(); } 
							 
						});
					
						$.data($root,'step', step);
						
						if (options.debug) {
								
							console.log('updatedSliderStep: '+step);
						}
					
					break;
				
				}
			};
			
			var automateTransition = function() {
						
				makeTransition('next', 1);
			
			};
			
			timer = setInterval(function () { automateTransition() }, options.speed );
			
			////////////////////////
			//
			// 		FUNCTIONS
			//
			///////////////////////
			
			function updateSlideIndicator() {
			
				var currentStep, $controls;
				
				currentStep = $.data($root,'step');
				
				$controls   = $controlSlide.find('li').children('a');
				
				$controls.removeClass('active');
				
				$controlSlide.find('li:eq('+ currentStep +')').children('a').addClass('active');
			
			}
			
			////////////////////////
			//
			// 		EVENTS
			//
			///////////////////////
			
			if ( options.stopOnHover ) {
		
				$root
				.mouseenter(function () {
		
					clearInterval(timer);
		
				})
				.mouseleave(function () {
								
					timer = setInterval(function () { automateTransition() }, options.speed );
		
				});
			
			}
			
			if (options.hasPaging) {
	
				$root.delegate(options.pagingButton, 'click', function(e){
					
					e.preventDefault();
									
					clearInterval(timer);
										
					var $this     = $(this),
						direction = $this.hasClass('next') ? 'next' : 'previous';
						
					makeTransition(direction, 1);			
																
					return false;
					
				});
			
			}	
			
			if (options.hasSlideIndicator) {
			
				$controlSlide.delegate('a', 'click', function(e){
					
					e.preventDefault();
						
					var $this = $(this);
					
					if (!$this.hasClass('active')) {
									
						clearInterval(timer);
										
						var $parentEl = $this.parent('li'),
							elIndex   = $parentEl.index();
							
						var sliderStep  = $.data($root, 'step');
							
						var direction, step;
						
						direction = elIndex < sliderStep ? 'previous' : 'next';
						step      = elIndex < sliderStep ? sliderStep - elIndex : elIndex - sliderStep;
										
						makeTransition(direction, step);
						
						timer = setInterval(function () { automateTransition() }, options.speed );
						
						if (options.debug) {
						
							console.log('elIndex: ' + elIndex + ' - sliderStep: ' + sliderStep + ' - direction: ' + direction + ' - step: ' + step);
						
						}
					}			
																
					return false;
					
				});
			
			}
			
			if ( options.disableSlideClick ) {
			
				$slideList.find('a').click(function(e){
				
					e.preventDefault();
					
					return false;
				
				});
			
			}
			
			if ( options.disableImageTitle ) {
			
				$slideList.find('a, img').hover(function(){
				
					$(this).removeAttr('title').removeAttr('alt');
				
				});
			
			}
			
	    });
	}
	
})(jQuery);
