/**
*
*
* bxSlider: Content slider / fade / ticker using the jQuery javascript library.
*
* Author: Steven Wanderski
* Email: wandoledzep@gmail.com
* URL: http://bxslider.com
* 
*
**/

$.fn.bxSlider = function(options){
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Declare variables and functions
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	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',
		pager: true
	};
	
	options = $.extend(defaults, options);
	o = options;
	
	if(options.mode == 'ticker'){
		options.auto = true;
	}
	
	var $this = $(this);

	var $parent_width = options.width;	
	var is_working = false;
	var child_count = $this.children().size();
	var i = 0;
	var j = 0;
	var k = 0;
	
	var $kids = $this.children();
		
	function animate_next(){
		
		is_working = true;
		
		$this.animate({'left':'-' + $parent_width * 2 + 'px'}, options.speed, function(){
			
			$this.css({'left':'-' + $parent_width + 'px'}).children(':first').appendTo($this);
			
			is_working = 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, numero){
				
		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;
			
		}	
		
		if(direction == '') {
			var last_before_switch = child_count;
			var start_over = k;
			var incr = numero;
		}
		//alert(k+"-"+incr+"-"+current+"-"+numero);
		
		is_working = true;
		
		if(k == last_before_switch){
			if(start_over == 0) {
				a_active = child_count;
			}
			else {
				a_active = start_over;
			}
			set_active(a_active);
//			$this.children().eq(start_over).html(start_over);
			$this.children().eq(k).fadeTo(options.speed, 0, function(){$(this).hide();});
			$this.children().eq(start_over).show().fadeTo(options.speed, 1, function(){
				k = start_over;				
				clearInterval($.t);
				$.t = setInterval(function(){fade("next");}, options.pause);
				is_working = false;			
			});
			
		}else{	
			set_active(incr);
//			$this.children().eq(incr).html(incr);
			$this.children().eq(k).fadeTo(options.speed, 0, function(){$(this).hide();});
			$this.children().eq(incr).show().fadeTo(options.speed, 1, function(){
				k = incr;				
				clearInterval($.t);
				$.t = setInterval(function(){fade("next");}, options.pause);
				is_working = false;
			});				
		}		
		
	}
	
	function add_controls(){
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Check if user selected images to use for next / prev
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
		if(options.prev_img != '' || options.next_img != ''){
			
			$this.parent().append('<div class="slider_control"><a class="slider_prev" href=""><img src="' + options.prev_img + '" border="0" alt=""/></a><a class="slider_next" href=""><img src="' + options.next_img + '" alt="" border="0" /></a></div>');
			
		}else{
		
			$this.parent().append('<div class="slider_control"><a class="slider_prev" href="">' + options.prev_text + '</a><a class="slider_next" href="">' + options.next_text + '</a></div>');
		
		}
		
		$this.parent().find('.slider_prev').css({'float':'left', 'outline':'0'});
		$this.parent().find('.slider_next').css({'float':'right', 'outline':'0'});
		
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Accomodate padding-top for controls when elements are absolutely positioned (only in fade mode)
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		if(options.mode == 'fade'){
			
//			$this.parent().find('.slider_prev').css({'paddingTop' : $this.children().height()})
//			$this.parent().find('.slider_next').css({'paddingTop' : $this.children().height()})
			
		}	
		
		$(".slider_content").before($(".slider_control"));
		                                                       
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Actions when user clicks next / prev buttons        
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		                                                       
		$('.slider_next').click(function() {
			slider_next();
			return false;
		});	
		
		$('.slider_prev').click(function() {
			slider_prev();
			return false;
		});	
	
	}
	
	function add_pager() {
		$this.parent().append('<div class="bx_pager"></div>');
//		$(".slider_control").append('<div class="bx_pager"></div>');
		var $a;
		$kids.each(function(index){
			$a = $('<div>'+(index+1)+'</div>');
			$('.bx_pager').append($a);
			$a.click(function(){
				if(is_working == false && k!=index) {
					is_working = true;
					fade("", index);
					return false;
				}
			});
		});
		set_active(k);	
	}
	
	function set_active(num){
		if(o.pager) {
			if(num == child_count) {
				num = 0;
			}
			$('.bx_pager').find('DIV').removeClass('active').eq(num).addClass('active');
		}
	}
	
	function slider_next(){					
		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;
				
	}
	
	function slider_prev(){	
		
		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;
				
	}
	
	
	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();

			});				
			
		}		
		
	}
		
	
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Create content wrapper and set CSS
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	$this.wrap('<div class="' + options.wrapper_class + '"></div>');
	
	//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
		});
		
		$(".info").width(800);
		 	
		$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,
			'display' : 'none'	
		});
		
		$this.children(':first').css({
			'opacity' : 1,
			'display' : 'block'
		});
		
		$(".info").width($(this).parent().width());
				
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Check if user selected "auto"
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
	if(!options.auto){
		if(options.controls == true){			
			add_controls();			
		}			
		if(options.pager == true){			
			add_pager();			
		}				
	}else{
		
		if(options.mode == 'ticker'){
			
			ticker();
			
		}else{
		
			/////////////////////////////////////////////////////////////////////////////////////////////////////////////
			// Set a timed interval 
			/////////////////////////////////////////////////////////////////////////////////////////////////////////////
			
			if(options.mode == 'slide'){
				
				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'){
				
				$.t = setInterval(function(){fade('next');}, options.pause);
			
			}
			
			if(options.controls == true){			
				add_controls();			
			}			
			if(options.pager == true){			
				add_pager();			
			}
		
		}
	
	}
		
}
