jQuery.fn.bxSlider = function(options){
	
		var defaults = {
		mode: 'slide',
		speed: 500,
		auto: false,
		auto_direction: 'left',
		pause: 2500,
		controls: false,
		prev_text: 'prev',
		next_text: 'next',
		width: $(this).children().width(),
		prev_img: '',
		next_img: '',
		ticker_direction: 'left',
		wrapper_class: 'container'
	};
	
	options = $.extend(defaults, options);

	/*
	if(options.mode == 'ticker'){
		options.auto = true;
	}
	*/
	var $this = $(this);
	var $parent_width = options.width;	

	var current = 0;
	var is_working = false;
	var child_count = $this.children().size();
	var i = 0;
	var j = 0;
	var k = 0;
	var $parent = $this.parent();
	
	
	function animate_next(){
		is_working = true;
		//alert($this.attr('id'));
		$this.animate({'left':'-' + $parent_width * 2 + 'px'}, options.speed, function(){
			//alert($this.css('left') + $this.attr('id'));
			//alert($this.css({'left':'-' + $parent_width + 'px'}).children(':first').attr('id'));
			$this.css({'left':'-' + $parent_width + 'px'}).children(':first').appendTo($this);
			//alert($this.css('left') + $this.attr('id'));
			is_working = false;
		});		
	}
	
	function animate_to_index(index){
		animate_next(); 
	}
	
	/*
	$parent.find('.slider-next').click(function(){		// Actions when user clicks next / prev buttons    
		if(!is_working){
			if(options.mode == 'slide'){
				animate_next();
				if(options.auto){
					clearInterval($.t);
					$.t = setInterval(function(){animate_next();}, options.pause);
				}
			}
		}				
		return false;		
	});
	*/
	
	
	function animate_prev(){		
		is_working = true;
		$this.animate({'left': 0}, options.speed, function(){
			$this.css({'left':'-' + $parent_width + 'px'}).children(':last').insertBefore($this.children(':first'));
			is_working = false;
		});				
	}
	
	function fade(direction){
		if(direction == 'next'){
			var last_before_switch = child_count - 1;
			var start_over = 0;
			var incr = k + 1;
		}else if(direction == 'prev'){
			var last_before_switch = 0;
			var start_over = child_count -1;
			var incr = k - 1;
		}		
		is_working = true;
		var next = (k==($this.children().size() - 1)) ? 0 : k+1;
		$this.children().css('z-index', '0');
		$parent.find('.index-bullet').removeClass('active');
		$parent.find('.index-bullet').eq(next).addClass('active');
		$this.children().eq(next).css('z-index', '1');
		$this.children().eq(k).fadeTo(options.speed, 0);
		is_working = false;
		if(k == last_before_switch)
			$this.children().eq(start_over).fadeTo(options.speed, 1, function(){
				k = start_over;				
			});
		else
			$this.children().eq(incr).fadeTo(options.speed, 1, function(){
				k = incr;				
			});		
	}
	
	/*
	function ticker() {
		if(options.ticker_direction == 'left'){
			$this.animate({'left':'-' + $parent_width * 2 + 'px'}, options.speed, 'linear', function(){
				$this.css({'left':'-' + $parent_width + 'px'}).children(':first').appendTo($this);
				ticker();
			});		
		}else if(options.ticker_direction == 'right'){
			$this.animate({'left': 0}, options.speed, 'linear', function(){
				$this.css({'left':'-' + $parent_width + 'px'}).children(':last').insertBefore($this.children(':first'));
				ticker();
			});				
		}		
	}
	*/
		
	$parent.find('.index-bullet').eq(0).addClass('active');	
	$parent.find('.index-bullet').each(function(i){
		$(this).click(function(){
				k = (i==0) ? $('.nav').children().size() - 1 : i-1;
			fade('next');
		});
	});
	$this.wrap('<div class="' + options.wrapper_class + '"></div>'); 	// Create content wrapper and set CSS	
	//console.log($this.parent().css('paddingTop'));
	
	if(options.mode == 'slide' || options.mode == 'ticker'){
		$this.parent().css({
			'overflow' : 'hidden',
			'position' : 'relative',
			'width' : options.width + 'px'
		});
		$this.css({		
			'width' : '999999px',
			'position' : 'relative',
			'left' : '-' + $parent_width + 'px'		
		});
		$this.children().css({		
			'float' : 'left',
			'width' : $parent_width
		});
		$this.children(':last').insertBefore($this.children(':first'));
	}else if(options.mode == 'fade'){
	
		$this.parent().css({
			'overflow' : 'hidden',
			'position' : 'relative',
			'width' : options.width + 'px'
			//'height' : $this.children().height()
		});
		if(!options.controls){		
			$this.parent().css({'height' : $this.children().height()});		
		}
		$this.children().css({		
			'position' : 'absolute',
			'width' : $parent_width,
			'listStyle' : 'none',
			'opacity' : 0		
		});
		
		$this.children(':first').css({
			'opacity' : 1
		});
				
	}
			
	//if(!options.auto)
		//add_controls();
	//else{
		//if(options.mode == 'ticker')
			//ticker();
		//else{			// Set a timed interval 
				if(options.mode == 'slide' && options.auto){
					if(options.auto_direction == 'left'){
						$.t = setInterval(function(){animate_next();}, options.pause);		
					}else if(options.auto_direction == 'right'){
						$.t = setInterval(function(){animate_prev();}, options.pause);
					}
				}else if(options.mode == 'fade'){
					if(options.auto_direction == 'left'){
						$.t = setInterval(function(){fade('next');}, options.pause);
					}else if(options.auto_direction == 'right'){
						$.t = setInterval(function(){fade('prev');}, options.pause);
					}
				}
				if(options.controls){
					$parent.find('.slider-next').click(function(){		// Actions when user clicks next / prev buttons    
						if(!is_working){
							if(options.mode == 'slide'){
								animate_next();
								if(options.auto){
									clearInterval($.t);
									$.t = setInterval(function(){animate_next();}, options.pause);
								}
							}else if(options.mode == 'fade'){
								fade('next');
								if(options.auto){
									clearInterval($.t);
									$.t = setInterval(function(){fade('next');}, options.pause);
								}
							}
						}				
						return false;		
					});	
					
					$parent.find('.slider-prev').click(function(){	
						if(!is_working){
							if(options.mode == 'slide'){
								animate_prev();
								if(options.auto){
									clearInterval($.t);
									$.t = setInterval(function(){animate_prev();}, options.pause);
								}
							}else if(options.mode == 'fade'){
								fade('prev');
								if(options.auto){
									clearInterval($.t);
									$.t = setInterval(function(){fade('prev');}, options.pause);
								}
							}
						}	
						return false;		
					});
				}
		//}
	//} 
}

















