/*
 * gearSlider - jQuery Plugin
 * 
 *
 * Examples and documentation at: http://www.aeden.it
 * 
 * Copyright (c) 2011 Aeden.it - Daniele Cerioni
 * 
 * Version: 1.0.0(14/04/2011)
 * Requires: jQuery v1.3+
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function($){  
 $.fn.gearslider = function(options) {
	 var defaults = {  
			   delay:0,
			   inspeed:300,
			   outspeed:100,
			   selector:'<span class="dot"></span>',
			   locker:'lock',
			   width:0,
			   height:0
			  };
	 var options = $.extend(defaults, options);
	 return this.each(function() {  
    	var target=this;    	
    	var slider;
    	var locked=0;
    	if(options.width!=0){$(target).css({width:options.width});};
    	if(options.heigh!=0){$(target).css({height:options.height});};
    	slider=$(target).find('.slider');
    	slidermode=$(target).find('.slider').attr('mode');
    	var navigator=$('<div></div>').addClass('selectors');
    	$(target).find('.slider').height($(target).outerHeight());
    	switch(slidermode)
    	{
    		case 'fade':
    			$(target).find('.slide').css({width:'100%',height:$(target).outerHeight()});
    	    	$(target).find('.slider').width('100%');
    			slider.find('.slide:first').css({display:'block'});
    			$(target).find('.slide').each(function(index){
    				$(this).attr('slide',index).attr('id',$(target).attr('id')+'-slide-'+index);
    				$(navigator).append(
    					$('<a></a>').addClass('selector')
    						.attr('slide-target',$(this).attr('id'))
    						.attr('id',$(target).attr('id')+'-selector-'+index)
    						.html(options.selector).click(function(){
    						$(target).find('.slide').fadeOut(options.outspeed);
    						$('#'+$(this).attr('slide-target')+'.slide').fadeIn(options.inspeed);
    						$(target).find('.selector').removeClass('active');
    						$(this).addClass('active');
    					})
    				)
    			})

    		break;
    		case 'fold-h':
    			$(target).find('.slide').css({width:$(target).outerWidth(),height:$(target).outerHeight()});
    	    	$(target).find('.slider').width($(target).find('.slide').size()*$(target).outerWidth());
    			$(target).find('.slide').each(function(index){
    				$(this).attr('slide',index).attr('id',$(target).attr('id')+'-slide-'+index);
    				$(this).css({top:0,left:($(this).outerWidth()*index)});
    				$(navigator).append(
    					$('<a></a>').addClass('selector')
    						.attr('slide-target',$(this).attr('id'))
    						.attr('id',$(target).attr('id')+'-selector-'+index)
    						.html(options.selector).click(function(){
    						$(target).find('.slider').animate({'left':-($(target).outerWidth()*index)});
    						$(target).find('.selector').removeClass('active');
    						$(this).addClass('active');
    					})
    				)
    			})
    		break;
    		case 'fold-v':
    			$(target).find('.slide').css({width:$(target).outerWidth(),height:$(target).outerHeight()});
    	    	$(target).find('.slider').width($(target).find('.slide').size()*$(target).outerWidth());
    			$(target).find('.slide').each(function(index){
    				$(this).attr('slide',index).attr('id',$(target).attr('id')+'-slide-'+index);
    				$(this).css({left:0,top:($(this).outerHeight()*index)});
    				$(navigator).append(
    					$('<a></a>').addClass('selector')
    						.attr('slide-target',$(this).attr('id'))
    						.attr('id',$(target).attr('id')+'-selector-'+index)
    						.html(options.selector).click(function(){
    						$(target).find('.slider').animate({'top':-($(target).find('.slider').outerHeight()*index)});
    						$(target).find('.selector').removeClass('active');
    						$(this).addClass('active');
    					})
    				)
    			})
    		break;
    	}
    	$(navigator).append(
				$('<a></a>').addClass('selector-locker').html(options.locker).click(function(){
					if(locked==1)
					{
						locked=0;
						$(this).removeClass('locked');
					}
					else
					{
						locked=1;
						$(this).addClass('locked');
					};
				})
			)
    	$(target).append(navigator);
		$(target).find('.selectors').find('.selector:first').addClass('active'); 
		if(options.delay>0)
		{
			var isHover=0;
			$(target).hover( 
	    		function () {isHover=1;$(target).find('.selector-locker').addClass('hovered');}, 
	    		function () {isHover=0;$(target).find('.selector-locker').removeClass('hovered');}
	    	);
			var numSlides=$(target).find('.slide').size();
			var currentSlide=1;
			setInterval(function() {
				if(locked==0 && isHover==0)
				{
					$('#'+$(target).attr('id')+'-selector-'+currentSlide).click();
					currentSlide++;
					if((currentSlide>=numSlides)){currentSlide=0;};
				}
			}, options.delay);
		}
    });  
 };  
})(jQuery);
