/*
 * JS for Cupboard under the stairs
 * Create the slideshow on the work page
 */

/*
 * the slideshow
 */
var slideShow = function(){
    
    /**
     * The options passed through to this function
     *
     * @var Object
     * @private
     */
    var options = {
        
        /**
         * HTML form element 
         *
         * @var String
         */
        form : null,
        
        /**
         * HTML form select element, to listen to
         *
         * @var String
         */
        select : null,
        
        /**
         * HTML form submit element to remove
         *
         * @var String
         */        
        remove : null
        
    };
    
    
    /**
     * Initialise the global search button functionality
     * @param {Object} options The initialisation options
     * @return void
     * @public
     */
    var init = function(initOptions) {
        
        // save any options sent through to the intialisation script, if set
        for (var option in options) {
            if (!!initOptions[option]) {
                options[option] = initOptions[option];
            }
            
            // error check, if no element is specified then stop
            if (options[option].length < 1) {
                return false;
            }
        }
        
        // find the element to replace
        if ($(options.remove).length < 1) {
            return false;
        } else {
            removeSearchButton();
        }
        
        // set listener for select option change
        $(options.select).bind('change', function(e){
            projectSelected(e, this);
        });

        
        // initialise slideshow if there is more than one image
        areaFader.init();
        
    };
    
    
    /**
     * Replace the search button with the supplied html
     * @return void
     * @private
     */    
    var removeSearchButton = function() {

        // remove search button
        $(options.remove).remove();

    };
    
    
    /**
     * Update page content when the select element is changed
     * @return void
     * @private
     */    
    var projectSelected = function(e, el) {

        // get the selected element
        var selected = $(el).val();

        // reload the page using the selected option
        $(options.form).submit();

    };
    
    
    /**
     * Return value, expose certain methods above
     */
    return {
        init: init
    };
    
}();
