/*
    infinite slider v1.0 based on anythingSlider v1.2
   
*/

var busyscrolling = false;

(function($){
	
    $.infiniteSlider = 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; 

		// Set up a few defaults
        base.currentPage = 1;
		base.timer = null;
		base.playing = false;

        // Add a reverse reference to the DOM object
        base.$el.data("AnythingSlider", base);
        
        base.init = function(){
            base.options = $.extend({},$.infiniteSlider.defaults, options);
			
			// Cache existing DOM elements for later 
			base.$wrapper = base.$el.find('> div').css('overflow', 'hidden');
            base.$slider  = base.$wrapper.find('> ul');
            base.$items   = base.$slider.find('> li');
            base.$single  = base.$items.filter(':first');

        
        	// Get the details
            base.singleWidth = base.$single.outerWidth();
            base.pages = base.$items.length;

            // Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
			// This supports the "infinite" scrolling
			var itemslength = base.$items.length;
			
			for (i=itemslength;i>=0;i--)
			{ 
				// Always clone the items 'before', otherwise the real items are not visible because they are placed in the 'cloned' part
			
				base.$items.filter(':first').before(base.$items.eq(itemslength-i).clone().addClass('cloned'));
			}

			if (itemslength > base.options.step) {
				// Add clones to be able to scroll. (There are more items then there is room to show)
				
				for (i=itemslength-1;i>=0;i--)
				{
					base.$items.filter(':last' ).after(base.$items.eq(i).clone().addClass('cloned'));
				}

				
				// Setup our forward/backward navigation
				base.buildNextBackButtons();
			}
			
			// We just added items, time to re-cache the list
			base.$items = base.$slider.find('> li'); // reselect

			
			//set first page
			base.setCurrentPage(0);
			
        };

		base.gotoPage = function(forceleft){

			if (!busyscrolling) {
				busyscrolling = true;
				if (!forceleft) {
					base.setCurrentPage(base.currentPage - base.pages);
				}
				var dir = (forceleft) ? -1 : 1;
				var n = base.options.step;
				var left = base.singleWidth * dir * n;
				
				base.$wrapper.filter(':not(:animated)').animate({
					scrollLeft : '+=' + left
				}, base.options.animationTime, base.options.easing, function () {
					var gotopage = base.currentPage + (dir * base.options.step);
					if (gotopage < 0) {
						base.setCurrentPage(gotopage + base.pages );
					} else
					{
						if (gotopage >= base.pages) {
							base.setCurrentPage(gotopage - base.pages );
						} else {
							base.setCurrentPage(gotopage);
						}
					}
					busyscrolling = false;
				});
				
			}
		};
	
	
		base.setCurrentPage = function(page){
			
			base.$wrapper.scrollLeft(base.singleWidth * (page + base.pages));
			
			// Update local variable
			base.currentPage = page;
		};	
		
		
		base.goForward = function(){
			if (!busyscrolling) {
				base.gotoPage(false);
			}
		};
		
		base.goBack = function(){
			if (!busyscrolling) {
				base.gotoPage(true);
			}
		};    
		
		// Creates the Forward/Backward buttons
		base.buildNextBackButtons = function(){
			var $forward = $('<a class="arrow forward">&gt;</a>'),
				$back    = $('<a class="arrow back">&lt;</a>');
				
            // Bind to the forward and back buttons
            $back.click(function(e){
                base.goBack();
				e.preventDefault();
            });

            $forward.click(function(e){
                base.goForward();
				e.preventDefault();
            });
			
			$back.hover(function(){
				$(this).addClass('backhover');
			}, function(){
				$(this).removeClass('backhover');
			});
			
			$forward.hover(function(){
				$(this).addClass('forwardhover');
			}, function(){
				$(this).removeClass('forwardhover');
			});			

			// Append elements to page
			base.$wrapper.after($back).after($forward);
		};

		// Trigger the initialization
        base.init();
    };

	
    $.infiniteSlider.defaults = {
        easing: "swing",
        animationTime: 1000,             
		step: 1
    };
	

    $.fn.infiniteSlider = function(options){
		if(typeof(options) == "object"){
		    return this.each(function(i){			
				(new $.infiniteSlider(this, options));
	        });	
		} 
    };
	
})(jQuery);
