(function($){
	
	$.TickerBox = function(el, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el; 
        
        // Add a reverse reference to the DOM object
        base.$el.data("TickerBox", base);
		
        base.init = function(){
			
			base.nextButton = $('.tickerbox-next', base.el);
			base.prevButton = $('.tickerbox-prev', base.el);
			
			base.entries = $('.tickerbox-entry', base.el);
			
			base.total = base.entries.size();
			
			if(base.total > 0){
				base.entries.each(function(i){
					$(this).hide();
				});
				
				base.current = 0;
				
				base.entries.eq(base.current).show();
				
				base.fading = false;
				
				base.nextButton.click(function(){
					base.next();
				});
				
				base.prevButton.click(function(){
					base.prev();
				});
				
				base.prevButton.addClass('tickerbox-nav-disabled');
				
				if(base.entries.size() == 1){
					base.nextButton.addClass('tickerbox-nav-disabled');
				}
			}
        }
		
        base.is_prev = function(){
        	return true;
        	//return base.current > 0;
        }
        
        base.is_next = function(){
        	return true;
        	//return base.entries.size() - 1 > base.current;
        }
        
        base.is_first = function(){
        	return base.current == 0;
        }
        
        base.is_last = function(){
        	return (base.entries.size() - 1 == base.current);
        }
        
		base.next = function(){
			if(base.is_next() && !base.fading){
				base.fading = true;
				base.entries.eq(base.current).fadeOut("fast", function(){
					$(this).hide();
					base.current++;
					if (base.current > base.entries.size() - 1) {
					    base.current = 0;
					}
					
				    base.entries.eq(base.current).fadeIn("fast", function(){
				    	base.fading = false;
				    });
				    
				    s = base.entries.size() - 1
					
					if(!base.is_next()){
						base.nextButton.addClass('tickerbox-nav-disabled');
					}
					
					base.prevButton.removeClass('tickerbox-nav-disabled');
				});
			}
		}
		
		base.prev = function(){
			if(base.is_prev() && !base.fading){
				base.fading = true;
				base.entries.eq(base.current).fadeOut("fast", function(){
					$(this).hide();
					base.current--;
					if (base.current < 0) {
					    base.current = base.entries.size() - 1;
					}

				    base.entries.eq(base.current).fadeIn("fast", function(){
				    	base.fading = false;
				    });
				    
					if(!base.is_prev()){
						base.prevButton.addClass('tickerbox-nav-disabled');
					}
					
					base.nextButton.removeClass('tickerbox-nav-disabled');
				});
				
			}
		}
        
        base.init();
    }

    $.fn.tickerBox = function(options){
        return this.each(function(){
            (new $.TickerBox(this, options));
        });
    }

	
})(jQuery);
