/**
*
*
* suSlider: Content slider / fade   using the jQuery 
*
* Author: alex wang
* Email: wpsnowwolf@gmail.com
* URL: http://www.brightyoursite.com
* 
*
**/

jQuery.fn.suSlider = function(options){
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Declare variables and functions
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	var defaults = {
		mode: 'slide', // fade
		speed: 1000,
		auto: true, 
		pause: 10000,
		select: 20000,
		width: $(this).children('.silderitem').width(), 
		wrapper_class: 'sliderContent'
	}; 
	
	options = $.extend(defaults, options); 
	var $this = $(this);

	var $parent_width = options.width; 
	var is_working = false;
	var child_count = $this.children('.silderitem').size(); 
    var idx=0;		
	var isbreak=false; 
	var expauuse=0;
	var gotoidx=-1;
	var hoverhold=false;
	
	function animate_idx(i){
		if(i == idx){
			return true;
 		}
		
		is_working = true; 
		var direction = 1;

		$this.children('.silderitem').hide();
		if(i > idx){
			$this.css('left', '-0px');
		}else{
			$this.css('left', '-951px');
			direction = 0;
		}
		
		$this.children('.silderitem').eq(idx).show();
		$this.children('.silderitem').eq(i).show();

		idx=i;
		$('#slnav li:.active').removeClass('active');
		$('#slnav li:eq('+idx+')').addClass('active');

		$this.animate({'left':'-' + $parent_width * direction  + 'px'}, options.speed, function(){ 
			is_working = false;
			
			$this.css('left', '-' + $parent_width * i + 'px');

			$this.children('.silderitem').show();
			
			if(options.auto && !isbreak){ 
				i++;
				if(i==child_count){
					i=0;
				}
				clearInterval($.t);
				$.t = setInterval(function(){animate_idx(i);}, expauuse==0 ? options.pause : expauuse);
				expauuse=0;
			}
		});		
	}
	
	 
	 function fade_idx(idx){  
	 	if(gotoidx == idx){
			return true;
 		}
	 	
		is_working = true;
				    
		$this.children('.silderitem').eq(idx).fadeTo(options.speed, 0, function(){$(this).hide();});
					
		if(gotoidx<0){
			idx++;
			if(idx==child_count){
				idx=0;
			}
		}else{
			idx=gotoidx;
		}
            
		gotoidx=-1;
		$('#slnav li:.active').removeClass('active');		
		$('#slnav li:eq('+idx+')').addClass('active');
		
		$this.children('.silderitem').eq(idx).show().fadeTo(options.speed, 1, function(){

			is_working = false; 
			if(options.auto && !isbreak){ 
				clearInterval($.t);
				$.t = setInterval(function(){fade_idx(idx);}, expauuse==0 ? options.pause : expauuse);
				expauuse=0;
			}
		}); 
	}
	 
	function add_controls(){  
		$('#slnav').children().click(function(){		
			
			var $kids = $('#slnav li').index(this);
			clearInterval($.t);
			if(!is_working){
			
			    if(options.select==0){
					isbreak=true;
				}else{ 
					expauuse = options.select;
				}
				
				if(options.mode == 'slide'){
					animate_idx($kids); 
				}else{
					var $on= $('#slnav li').index($('#slnav li:.active'));
					gotoidx=$kids;
					fade_idx($on);
				}
			}

			return false;
		});	
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Create content wrapper and set CSS
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	$this.wrap('<div class="' + options.wrapper_class + '"></div>');
	 
			
	if(options.mode == 'slide'){
		$this.parent().css({
			'overflow' : 'hidden',
			'position' : 'relative',
			'width' : options.width + 'px'
		});
		$this.css({		
			'width' : '999999px',
			'position' : 'relative' 			 	
		});
		$this.children('.silderitem').css({		
			'float' : 'left',
			'width' : $parent_width
		});	
	
	}else if(options.mode == 'fade'){
		$this.parent().css({
			'overflow' : 'hidden',
			'position' : 'relative',
			'width' : options.width + 'px',
			'display' : 'block'
		});
		$this.children('.silderitem').css({		
			'position' : 'absolute',
			'width' : $parent_width,
			'listStyle' : 'none',
			'opacity' : 0,
			'display' : 'none'	
		});
		 $this.children('.silderitem:first').css({
			'opacity' : 1,
			'display' : 'block'
		}); 
	}
	
	add_controls();
   
	if(options.auto){
		clearInterval($.t);
		
		if(options.mode == 'slide'){
			$.t = setInterval(function(){animate_idx(1);},9000);
					
		}else{
			$.t =setInterval(function(){fade_idx(0);},options.speed*2);
		}
	}
		
}
