/**
 * jQuery Category Picture Scroller plugin
 *
 * Copyright (c) 2011 Kenneth Frank (http://www.ClaremontMcKenna.edu/ITS/)
 */
;
(function($) {
    /**
	 * Initialize global variables here.
	 */
    var arr = [];			// The array of all the category elements.
    var catCount = 0;			// The number of elements currently in the array.
    var defaults = {			// The defaults for this script.
        windowSize:			4,                                                 // The number of items to show on the page.								   // The number of elements to be shown.
        imageCanvas:			"ul.categories-galleries",                         // The area on the page to begin to look for our elements.
        imageContainerSelector:         "li.box",                                          // The name of the element that actually contains our element.
        forwardSelector:		".categories-gallery .heading .controls .next a",  // The jQuery selector for the forward 'button'.
        backwardSelector:		".categories-gallery .heading .controls .prev a"   // The jQuery selector for the backward 'button'.
    };

    // catScroll static class
    $.catScroll = {
        version:            '0.1.0',			// The current version number for this script.
        windowLow:          0,				// The marker for the first visible element.
        windowHigh:         0,				// The marker for the last visible element.
        forwardSelector:    '.categories-gallery .heading .controls .next a',        // The jQuery selector for the forward 'button'
        backwardSelector:   '.categories-gallery .heading .controls .prev a',		// The jQuery selector for the back 'button'
        getImageCount: function(){  // Returns the current number of elements in the categories.
            return catCount;
        },
        advance: function(){        // Function to scroll forward one item.
            if(this.windowHigh < arr.length){
                $(arr[this.windowLow]).hide();
                $(arr[this.windowHigh]).show();
                this.windowLow++;
                this.windowHigh++;
            }
            $.catScroll.buttonEval();
        },
        regress: function(){		// Function to scroll backward one item.
            if(this.windowLow >= 0){
                $(arr[this.windowHigh]).hide();
                $(arr[this.windowLow]).show();
                this.windowLow--;
                this.windowHigh--;
            }
            $.catScroll.buttonEval();
        },
        buttonEval: function(){		// Function to enable/disable the forward/back 'buttons'
            if(this.windowHigh >= arr.length){
                $(this.forwardSelector).attr('disabled', 'disabled');
                $(this.forwardSelector).addClass('disabled');
            }else{
                if($(this.forwardSelector + '[disabled]')){
                    $(this.forwardSelector).removeAttr('disabled');
                    $(this.forwardSelector).removeClass('disabled');
                }
            }
            if(this.windowLow <= 0){
                $(this.backwardSelector).attr('disabled', 'disabled');
                $(this.backwardSelector).addClass('disabled');
            }else{
                if($(this.backwardSelector + '[disabled]')){
                    $(this.backwardSelector).removeAttr('disabled');
                    $(this.backwardSelector).removeClass('disabled');
                }
            }
        }
    }

    // Primary catScroll initialization function that should be called on the category container.
    $.fn.catScroll = function(settings) {
        //  Extend Gallery Object
        $.extend(this, {
            // Returns the version of the script
            version: $.catScroll.version,
            initialized: false,
            init: function(){
                this.initialized = true;
                /**
                 * Create a handler for moving forward in the element array.
                 */
                $(this.forwardSelector).click(function(){
                    $.catScroll.advance();
                });

                /**
                 * Create a handler for moving backward in the element array.
                 */
                $(this.backwardSelector).click(function(){
                    $.catScroll.regress();
                });
            }
        });

        // Now initialize the gallery
        $.extend(this, defaults, settings);

        $.catScroll.windowHigh = $.catScroll.windowLow + this.windowSize;   // The marker for the last visible element.

        // Set the selectors in the static parameters to match the current items.
        $.catScroll.backwardSelector = this.backwardSelector;
        $.catScroll.forwardSelector = this.forwardSelector;

        /**
         * Scrape the container and add the images to the internal
         * tracking array.
         */
        arr = $(this.imageCanvas + ' ' + this.imageContainerSelector);
        catCount = arr.length;
        /**
         * Hide all of the elements that are outside of the current
         * "window" of visible elements.
         */
        for(var i = this.windowSize; i<arr.length; i++){
            $(arr[i]).hide();
        }

        /**
         * Check to see if the buttons should be updated.
         */
        $.catScroll.buttonEval();

        if(!this.initialized){
            this.init();
        }

        return this;
    };
})(jQuery);

