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

jQuery.fn.bxSlider = function(options){
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Declare variables and functions
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	var defaults = {
		mode: 'slide',
		speed: 500,
		auto: false,
		auto_direction: 'left',
		pause: 2500,
		controls: true,
		prev_text: 'prev',
		next_text: 'next',
		width: $(this).children().width(),
		height: $(this).children().height(),
		prev_img: '',
		next_img: '',
		background_color: '#fff',
		prev_area_height: 0,
		prev_area_width: 0,
		prev_padding_top: 0,
		prev_padding_left: 0,
		next_area_height: 0,
		next_area_width: 0,
		next_padding_top: 0,
		next_padding_right: 0,
		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 $parent_height = options.height;
	var current = 0;
	var is_working = false;
	var child_count = $this.children().size();
	var i = 0;
	var j = 0;
	var k = 0;

	function animate_next(){		
		is_working = true;
		
        var useragent = navigator.userAgent;

        // IE6 and not IE7
        if ((useragent.indexOf('MSIE 6')>0) && (useragent.indexOf('MSIE 7')==-1))
        {
            // IE6
		    $this.animate({'left':'-' + ($parent_width - options.prev_area_width) + 'px'}, options.speed, function(){			
			    $this.css({'left': 0}).children(':first').appendTo($this);			
			    is_working = false;			
		    });		
        } else {
            // others
		    $this.animate({'left':'-' + ($parent_width - options.prev_area_width) + 'px'}, options.speed, function(){			
			    $this.css({'left': options.prev_area_width + 'px'}).children(':first').appendTo($this);			
			    is_working = false;			
		    });		
        }	
	}
	
	function animate_prev(){		
		is_working = true;		
		
        var useragent = navigator.userAgent;

        // IE6 and not IE7
        if ((useragent.indexOf('MSIE 6')>0) && (useragent.indexOf('MSIE 7')==-1))
        {
            // IE6
            $this.children(':last').clone().insertBefore($this.children(':first'));
            $this.css({'left': '-' + ($parent_width - options.prev_area_width) + 'px'});            
		    $this.animate({'left': 0}, options.speed, function(){			
			    $this.children(':last').remove();
			    is_working = false;			
		    });		    
        } else {
            // others
            $this.children(':last').clone().insertBefore($this.children(':first'));
            $this.css({'left': '-' + ($parent_width - options.prev_area_width) + 'px'});            
		    $this.animate({'left': options.prev_area_width + 'px'}, options.speed, function(){			
			    $this.children(':last').remove();
			    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;
		
		if(k == last_before_switch){			
			$this.children().eq(k).fadeTo(options.speed, 0, function(){$(this).hide();});
			//$this.children().eq(k).css({'left':'-9999px'});
			$this.children().eq(start_over).show().fadeTo(options.speed, 1, function(){				
			is_working = false;	
			k = start_over;			
			});			
		}else{		
			$this.children().eq(k).fadeTo(options.speed, 0, function(){$(this).hide();});
			//$this.children().eq(k).css({'left':'-9999px'});
			$this.children().eq(incr).show().fadeTo(options.speed, 1, function(){			
			is_working = false;			
			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();
			});							
		}				
	}
		
	function add_prev_control() {
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Check if user selected images to use for next / prev
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////	

		if(options.prev_img != '') {
			$this.parent().prepend('<div class="slider_prev_area"><a class="slider_prev" href=""><img src="' + options.prev_img + '" alt=""/></a></div>');			
		} /*else {		
			$this.parent().prepend('<a class="slider_prev" href="">' + options.prev_text + '</a>');
		}*/
				
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Accomodate padding-top for controls when elements are absolutely positioned
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		if ( options.mode == 'slide' || options.mode == 'fade' ) {
		    $this.parent().find('.slider_prev_area').css({
		        'background-color' : options.background_color,
		        'float' : 'left',
		        'height' : options.prev_area_height,
		        'position' : 'relative',
		        'width' : options.prev_area_width,
		        'z-index' : 1
		        });
			$this.parent().find('.slider_prev').css({
			    'left' : options.prev_padding_left,
			    'outline' : 0,
			    'position' : 'absolute',
			    'top' : options.prev_padding_top
			    });
		}
		                                                       
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Actions when user clicks prev buttons        
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		                                                       
		$this.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;					
		});		
	}
	
	function add_next_control() {
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Check if user selected images to use for next
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
		if(options.next_img != ''){			
			$this.parent().append('<div class="slider_next_area"><a class="slider_next" href=""><img src="' + options.next_img + '" alt="" /></a></div>');
		}/*else{		
			$this.parent().append('<a class="slider_next" href="">' + options.next_text + '</a>');		
		}*/
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Accomodate padding-top for controls when elements are absolutely positioned (only in fade mode)
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		if ( options.mode == 'slide' || options.mode == 'fade' ) {
            
            var useragent = navigator.userAgent;
            
            // IE6 and not IE7
            if ((useragent.indexOf('MSIE 6')>0) && (useragent.indexOf('MSIE 7')==-1))
            {
                // IE6                
                $this.parent().find('.slider_next_area').css({
		            'background-color' : options.background_color,
		            'float' : 'right',
		            'height' : options.next_area_height,
		            'position' : 'relative',
		            'width' : options.next_area_width,
		            'z-index' : 1
		            });
			    $this.parent().find('.slider_next').css({
			        'right' : options.next_padding_right,
			        'outline' : 0,
			        'position' : 'absolute',
			        'top' : options.next_padding_top
			        });
            } else {
                // others                
                $this.parent().find('.slider_next_area').css({
		            'background-color' : options.background_color,
		            'right' : 0,
		            'height' : options.next_area_height,
		            'position' : 'absolute',
		            'width' : options.next_area_width,
		            'z-index' : 1
		            });
			    $this.parent().find('.slider_next').css({
			        'right' : options.next_padding_right,
			        'outline' : 0,
			        'position' : 'absolute',
			        'top' : options.next_padding_top
			        });
            }
		}
		                                                       
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// Actions when user clicks next buttons        
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		$this.parent().find('.slider_next').click(function(){					
			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;					
		});	
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Create content wrapper and set CSS
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	$this.wrap('<div class="' + options.wrapper_class + '"></div>');

    if ( !options.auto || (options.auto && (options.mode == 'slide' || options.mode == 'fade')) ) {
		add_prev_control();
	}		
		
	if(options.mode == 'slide' || options.mode == 'ticker') {
		
        var useragent = navigator.userAgent;

        // IE6 and not IE7      
        if ((useragent.indexOf('MSIE 6')>0) && (useragent.indexOf('MSIE 7')==-1))
        {
            // IE6
	        $this.parent().css({
		        'overflow' : 'hidden',
		        'position' : 'relative'
	        });
            
		    $this.css({		
			    'width' : '999999px',
			    //'left' : '-' + $parent_width + 'px'
			    'position' : 'absolute',
			    'left' : 0		    
		    });
		    
		    $this.children().css({		
			    //'background-color' : options.background_color,
			    'display' : 'inline-block',
			    'float' : 'left',
			    'position' : 'relative',
			    'height' : $parent_height + 'px',
			    'width' : $parent_width + 'px'			
		    });
        } else {
            // others
	        $this.parent().css({
		        'overflow' : 'hidden',
		        'position' : 'relative'
	        });            
	        
		    $this.css({		
			    'width' : '999999px',
			    'position' : 'absolute',
			    'left' : 0 + options.prev_area_width + 'px'
		    });          
		    
		    $this.children().css({		
			    //'background-color' : options.background_color,
			    'display' : 'inline-block',
			    'float' : 'left',
			    'position' : 'relative',
			    'height' : $parent_height + 'px',
			    'width' : $parent_width + 'px'			
		    });
        }
				 	
		//$this.children(':last').insertBefore($this.children(':first'));
	
	}else if(options.mode == 'fade'){
		
		$this.parent().css({
			'overflow' : 'hidden',
			'position' : 'relative'
		});
		
		if(!options.controls){		
			$this.parent().css({'height' : $this.children().height()});		
		}
		
		$this.css({		
		    'float' : 'left',
		    'height' : options.height + 'px',
			'width' : options.width + 'px'
		});
		
		$this.children().css({		
			'display' : 'inline-block',
			'position' : 'absolute',
			'listStyle' : 'none',
			'opacity' : 0,
			'display' : 'none'
		});
		
		$this.children(':first').css({
			'opacity' : 1,
			'display' : 'block'
		});
						
	}
	
    if ( !options.auto || (options.auto && (options.mode == 'slide' || options.mode == 'fade')) ) {
		add_next_control();
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Check if user selected "auto"
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
	if ( options.auto ) {					
		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'){				
				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);				
				}			
			}		
		}	
	}		
}
















