/**
 * jQuery webinov carrousel
 * http://www.webinov.fr
 *
 * Copyright 2011, webinov
 * 
 */


(function($){
	
	$.fn.webinovCarrousel = function(options){		
				
		var defaults = {
			mode: 'fade',				// 'fade'
			loop: true,					// true, false - restart automtically after the last slide
			speed: 2000,				// integer , duration between slides
			speedTransition: 500,		// integer , Transtion duration 
			autoMode: true,				// true, false automatically go to next slide afer speed ms
			buttonNav: true,			// true, false Navigation button
			useTitle: false				// true, false, ajoute un overflow avec le title dedans 
		}
		
		var options = $.extend(defaults, options);
		
		var base = this;
		var currentSlide = 0;	
		var firstSlide = 0;
		var $parent = '';
		var $children = '';
		var nbSlide = -1;
		var $firstChild = '';
		var isBusy = false;
		var useTitle = false;
		var $title = ''; /* title dom */
		// PUBLIC FUNCTIONS
						
		/**
		 * Go to Next slide
		 */		
		this.goToNextSlide = function(){
			if(!base.isBusy)
				base.goToSlide( currentSlide + 1 );
		}
		this.init = function(){
			$parent = $(this);
			$origElement = $parent.clone();
			$children = $parent.children();
			$firstChild = $parent.children(':first');
			childrenWidth = $firstChild.width();
			childrenHeight = $firstChild.height();
			nbSlide = $children.length;	

			if(options.mode == 'fade'){
				$parent.wrap('<div class="webinovCarrouselContainer" style="position:relative;"></div>');
				$parent.css({
					margin:0,
					padding:0,
					height: childrenHeight,
					width: childrenWidth,
					overflow:'hidden',
				  listStyle: 'none'
				});
				$parent.children().css({
				  listStyle: 'none',
				  position: 'absolute',
					top: 0,
					left: 0,
					height: childrenHeight,
					width: childrenWidth,
					margin:0,
					padding:0,
					zIndex: 1000
				});
				$children.not(':eq('+currentSlide+')').fadeTo(0, 0);
				$children.eq(currentSlide).css('zIndex', 1001);
				
				if(options.autoMode) {
					setTimeout(function(){
						if(!base.isBusy)
							base.goToNextSlide();
					}, options.speed);
				}
				if(options.useThumb)
					options.buttonNav = false; //on ne peut pas avoir les 2
				
				if(options.buttonNav) {
					tmp="<div class='buttonNavContainer' style='position:absolute;  z-index:1005;'><ul>";
					for(i=0; i<nbSlide; i++)
					{
						tmp += "<li></li>";
					}
					tmp +="</ul></div>";
					$parent.append(tmp);

					$buttonNav = $parent.find('.buttonNavContainer > UL').children();
		
					$buttonNav.not(':eq('+currentSlide+')').removeClass('Active');
					$buttonNav.eq(currentSlide).addClass('Active');
					
					$buttonNav.click(function(e) {
						options.autoMode=false;
						if(!base.isBusy)
							base.goToSlide($(e.target).index());
					});
			
				}
				if(options.useTitle) {
					tmp="<div class='overlayTitle' style='position:absolute;  z-index:1002;'></div>";
					$parent.append(tmp);
					$title = $parent.find('.overlayTitle');
					$title.html($children.eq(currentSlide).attr('title'));

					
				}
				if(options.useThumb) {
					tmp="<div class='thumbContainer' style='position:absolute;  z-index:1005;'><ul>";
					for(i=0; i<nbSlide; i++)
					{
						tmp += "<li><img src='" + $children.eq(i).find("IMG").attr("src") + "'></li>";
					}
					tmp +="</ul></div>";
					$parent.append(tmp);

					$buttonNav = $parent.find('.thumbContainer > UL').children();
		
					$buttonNav.not(':eq('+currentSlide+')').removeClass('Active');
					$buttonNav.eq(currentSlide).addClass('Active');
					
					$buttonNav.find("img").click(function(e) {
						options.autoMode=false;
						if(!base.isBusy)
							base.goToSlide($(e.target).parent().index());
					});

				}
				if(options.useThumbOnMouse)
				{
					$buttonNav.hide();
					$parent.hover(
							function(){
								$buttonNav.fadeIn(500);
							},
							function(){
								$buttonNav.fadeOut(1000);
								
						});
				}
			}
		}
		this.goToSlide = function(num){
			if(!base.isBusy)
			{
				currentSlide=num;
				if(currentSlide >= nbSlide )
				{
					if(options.loop)
						currentSlide=0;
					else options.autoMode=false;
	
					
				}
				base.isBusy=true;
				if(options.useTitle) {

					title=$children.eq(currentSlide).attr('title');
					$title.html($children.eq(currentSlide).attr('title'));
				}


				$children.not(':eq('+currentSlide+')').fadeTo(options.speedTransition, 0).css('zIndex', 1000);
				// fade in the current slide
				$children.eq(currentSlide).css('zIndex', 1001).fadeTo(options.speedTransition, 1, function(){
					if(jQuery.browser.msie){
						$children.eq(currentSlide).get(0).style.removeAttribute('filter');
					}
					base.isBusy=false;
					$()
				});
				$buttonNav.not(':eq('+currentSlide+')').removeClass('Active');
				$buttonNav.eq(currentSlide).addClass('Active');
	
				if(options.autoMode)
					setTimeout(function(){	base.goToNextSlide();	}, options.speed);
			}
		}
		this.each(function(){	
			base.init();
		});
				
		return this;						
	}
})(jQuery);
