/**
 * Sliders plugin for jQuery
 * v2.0
 * Create a slider width handles (prev, next)
 *
 * By Olivier Lacombe, Dattee David Subvitamine.com
 *
 * Please use as you wish at your own risk.
 */

/**
 ****** DOCUMENTATION ******
 *
 * HTML structure awaited (with css requirement): (<xxx> are to be replace with valid html tags)
 *  <aaa> (position:relative, width:xxx, height:xxx)
 *	<bbb class='slides'>
 *	    <ccc class='slide'></ccc>
 *	    <ccc class='slide'></ccc>
 *	    ...
 *	</bbb>
 *	<ddd class='handles'>
 *	    <eee class='handle prev'></eee>
 *	    <eee class='handle next'></eee>
 *	</ddd>
 *  </aaa>
 *
 * Initialisation:
 *	$(#aaa#).sliders(options);
 *     
 *	#aaa# selector wrapping the whole slider
 *	default options = {
 *	    auto : true -- auto-moving slider  (optional)
 *	    current : 0 -- index of the element to start the slider with (optional)
 *	    speed : 800 -- speed of animation in milliseconds (optional)
 *	    tempo : 5 -- tempo in seconds (optional)
 *	}
 *       
 * Usage:
 *	$(#aaa#).sliders(options) : initialisation
 *	$(#aaa#).sliders('auto') : run the auto sliding
 *	$(#aaa#).sliders('stop') : stop the auto sliding
 *	$(#aaa#).sliders('next') : slide to the next slide
 *	$(#aaa#).sliders('prev') : slide to the previous slide
 *	$(#aaa#).sliders('destroy') : kill the plugin (unbind the events from the handles and kill the auto slider)
 *	
 */

(function($){
    
	//Method définition
	var methods = {
		init: function(params){
		    return this.each(function(){
			
			//Merge default and user parameters
			params = $.extend(
			    {
				auto: true,
				current: 0,
				speed: 800,
				tempo: 5
			    },
			    params
			);

			//Define vars
			var sld_data = {
			    frame: $(this),
			    width : $(this).width(),
			    interval : 0
			};
			var slide_container = $(this).children('.slides');
			var slider_handles = $(this).children('.handles');
			var slide_count = slide_container.children('.slide').length;
			
			//Store the data in the element for futur use
			params = $.extend(sld_data,params);
			$(this).data('params',params);

			/**
			 * Default CSS 
			 */
			//Define the 'window' behavior
			$(this).css({
			    overflow: 'hidden'
			});
			//Define the width for slide container
			slide_container.css({ 
			    width: params.width * slide_count
			});
			//Define the width for each slide based on the window container
			slide_container.children('.slide').css({ 
			    width: params.width,
			    'float': 'left'
			});
			//Define the css for the handles
			slider_handles.children('.handle').css({
			    position: 'absolute', 
			    cursor:'pointer'
			});
			

			/** 
			 * Events activation 
			 */
			//Activate the handle
			slider_handles.children('.next').bind('click', function(){ $(params.frame).sliders('stop'); $(params.frame).sliders('next');});
			slider_handles.children('.prev').bind('click', function(){ $(params.frame).sliders('stop'); $(params.frame).sliders('prev');});
			
			//Ini the auto slider
			if(params.auto){
			    $(this).sliders('auto');
			}
		    
		    });
		},
		prev: function(){
		    return this.each(function(){
			
			//Get the slider params
			var params = $(this).data('params');
			var last_index= $(this).find('.slide').length - 1;
			
			//Update the current slide index
			if(params.current > 0)
			{
			    params.current--;
			}
			else
			{
			    //Retreive the first slide and move it at the end
			    var last_slide = $(this).find('.slide:last');
			    //Move the slider frame to the previous slide
			    $(this).children('.slides').prepend(last_slide).css({ left: params.width * (params.current + 1) * -1 });
			}
			
			//Move the slide to the right
			$(this).children('.slides').stop().animate(
			    {left: params.width * params.current * -1}, 
			    params.speed
			);
			    
			//Save the new current index of the slider  
			$(this).data('params',params);
			
		    })
		},
		next: function(){
		    return this.each(function(){
			
			//Get slider relative data
			var params = $(this).data('params');
			var last_index = $(this).find('.slide').length - 2;
			
			//Update the current slide index
			if(params.current < last_index)
			{
			    params.current++;
			}
			else
			{
			    //Retreive the first slide and move it at the end
			    var first_slide = $(this).find('.slide:first');
			    //Move the slider frame to the previous slide
			    $(this).children('.slides').children('.clear').before(first_slide).parent('.slides').css({ left: params.width * (params.current - 1) * -1 });
			    
			}
			
			//Move the slide to the right
			$(this).children('.slides').stop().animate(
			    {left: params.width * params.current * -1}, 
			    params.speed
			);
			    
			//Save the new current index of the slider  
			$(this).data('params',params);
			
		    });
		},
		auto:function(){
		    return this.each(function(){
		    
			//Get slider relative data
			var params = $(this).data('params');
			var slider_frame = $(this);

			//Clear the timer
			clearInterval(params.interval);

			//Activate the slider
			params.interval = setInterval(
			    function(){ 
				slider_frame.sliders('next');
			    }, 
			    params.tempo * 1000 
			);
			    
			//Save the params
			$(this).data('params', params);

		    });
		},
		stop:function(){
		    //Stop the auto slider
		    var params = $(this).data('params');
		    clearInterval(params.interval);
		},
		destroy:function(){
		    
		    //Unbind the event on the handles
		    $(this).children('.handles').children('.next').unbind();
		    $(this).children('.handles').children('.prev').unbind();
		    
		    //Stop the auto slider
		    var params = $(this).data('params');
		    clearInterval(params.interval);
		    
		}
	}
	
	//Create the plugin
	$.fn.sliders = function(method){
		
		//If method exists run it
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		//If object passed in parameters, ini the plugin
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		//Else throw a jQuery error
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.sliders' );
		}
	};
	
})(jQuery)



//(function($){
//
//    $.fn.sliders = function(params) {
//
//        // merge default and user parameters
//        params = $.extend(
//            {
//                current: 0,
//                speed: 800,
//                tempo: 5
//            },
//            params
//        );
//
//	//Define vars
//        var current = params.current;
//        var slider_target = $(this);
//        var slider_elements = slider_target.children('.slides');
//        var slider_handles = slider_target.children('.handles');
//        var slide_max = slider_elements.children('.slide').length;
//        var slide_last = slider_elements.children('.slide').length - 1;
//        var slider_width = slider_target.width();
//
//	//Define the width for slider window (the transparent element taht show the right slide)
//	slider_elements.css({ width: slider_width * slide_max });
//        
//	//Define the width for each slide
//        slider_elements.children('.slide').each(function(e){
//            $(this).css({ width: slider_width });
//        });
//
//	//Active the previous handle
//        slider_handles.children('.prev').click(function(){
//	    //Clear the timer
//            clearInterval(slider_interval);
//	    //Set the current slide
//            if(params.current > 0)
//            {
//                params.current--;
//            }
//            else
//            {
//                params.current = slide_last;
//            }
//	    //Move the slide to the right
//            slider_elements.stop().animate(
//		{ left: slider_width * params.current * -1 }, 
//		params.speed
//            );
//        });
//
//	//Active the next handle
//        slider_handles.children('.next').click(function(){
//	    //Clear the timer
//            clearInterval(slider_interval);
//	    //Set the current slide
//            if(params.current < slide_last)
//            {
//                params.current++;
//            }
//            else
//            {
//                params.current = 0;
//            }
//	    //Move the slide to the right
//            slider_elements.stop().animate(
//                { left: slider_width * params.current * -1 },
//                params.speed
//            );
//        });
//
//	//Activate the slider
//        var slider_interval = setInterval(function(){
//		//Increment the current slide
//                if(params.current < slide_last)
//                {
//                    params.current++;
//                }
//		//Re-initialize the current to the first slide
//                else
//                {
//                    params.current = 0;
//                }
//		
//		//Slide to the next item
//                slider_elements.stop().animate(
//                    { left: slider_width * params.current * -1 },
//                    params.speed
//                );
//
//            }, params.tempo*1000 );
//    };
//    
//})(jQuery); 
