/**
*
*
* 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',
		speed: 1000,
		auto: false, 
		pause: 4000,
		select:5000,
		width: $(this).children('.silderitem').width(), 
		wrapper_class: 'container'
	}; 
	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){
		is_working = true; 
		$this.animate({'left':'-' + $parent_width * i + 'px'}, options.speed, function(){ 
			idx=i;
			is_working = false; 
	   		 $('#slnav li:.active').removeClass('active');
			$('#slnav li:eq('+idx+')').addClass('active');
        if(options.auto && !isbreak)
          { 
            idx++;
///trace('idx '+idx);
           if(idx==child_count)
            {
                idx=0;
            }
///             $.t = window.clearInterval($.t); //clearInterval($.t);
///	         $.t = self.setInterval(function(){animate_idx(idx); $this.state = idx; }, expauuse==0 ? options.pause : expauuse);
$.t = self.setTimeout(function(){animate_idx(idx); $this.state = idx; trace('slide '+idx); }, options.pause);
	         expauuse=0;
           }
		});		
		
	}

	$this.run = function() {
///		$.t = window.clearInterval($.t);
		isbreak = false;
		if(options.auto) { 
//		  		$this.animate({'left':'-0px'}, options.speed, function(){ 
					var state = ($this.state != null? $this.state: 0); ///($this.state == child_count-1? 0: $this.state+1): 0);
//		 			$.t = setInterval(function(){animate_idx(state);},options.speed);
//		  		});
//$.t = setInterval(function(){$this.slide(state);}, options.speed);
$this.slide(state);
		}
	}

	$this.state = null;
	$this.pause = function() {
///trace('pause');
		$.t = window.clearTimeout($.t);
		isbreak = true;
	}

	$this.slide = function(id, slider) {
		slider = (null? this: slider);
	
		var $kids = $('#slnav li').index(slider);
///		$.t = window.clearInterval($.t);
		
		if(!is_working){
		
			if(options.select==0)
				{
				 isbreak=true;
				}
				else
				{ 
				  expauuse = options.select;
				}
			
			if(options.mode == 'slide'){
									 
				animate_idx(id != null? id: $kids); 
				
			}  
			else
			{
			   var $on = $('#slnav li').index($('#slnav li:.active'));
			  gotoidx=$kids;
			  fade_idx(id != null? id: $on);
			}
		}

		return false;	
	}

	function add_controls(){  
	    $('#slnav').children().click(function(){		
			$this.slide(null, this);
		});	
		 
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 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' : '2980px',
			'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' 
		});
		 
		$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();

	$this.run();
		
	return $this;
}


