YAHOO.namespace("SW.widget.componentLoader");

/**
 * Component loader and supporting functions
 *
 * Loads components via ajax call.  The following is the structure of the config used
 * with the different functions in this file:
 *
 * config {
 *   queryString (optional) - Contains the query string to attach to the ajax call that loads the component
 *   callback    (optional) - Function that is called when the ajax call returns and the dom is rendered.
 *      If not present, the default functionality inserts the markup returned by the call as innerHTML on the
 *      element specified by 'container' (see below).
 *   container   (required) - Must be specified if 'callback' is not.  Contains the element that the ajax response is
 *      inserted into via innerHTML if 'callback' (see above) is not specified.
 * }
 */
(function() {
    
    var componentAction = {
        currencyConverter   : { url: "/common/currencyConverter.do" },
        rateSelector        : { url: "/common/rateSelector.do" }
    };

    function Component(config) {

        var callback = {
            success : function(o) {
                // process data
                config.showCallback(o)
            },
            failure : function(o) {
                alert("Failed");
            },
            scope: Component
        };
        
        var self = {

            load : function(componentName) {
                var aUrl = componentAction[componentName].url;

                YAHOO.util.Connect.asyncRequest(
                        'GET',
                        aUrl + "?"
                        + config['queryString'],
                        callback,
                        null);
            },

            show : function() {
                // default callback if none configured
                if ( ! config['showCallback'] ) {
                    var container = yuiDom.get(config.container);
                    config['showCallback'] = function(response) {
                        container.innerHTML = response.responseText;
                    };
                }

                this.load(config.componentName);
            },

            hide : function() {
                var container = yuiDom.get(config.container);
                if ( ! config['hideCallback'] ) {
                    config['hideCallback'] = function() {
                        container.innerHTML = "";
                    }
                }

                config['hideCallback']();
            },
            hideComponent : function(){
               var container = yuiDom.get(config.container);
                var trigger = yuiDom.get(config.trigger);

                if (yuiDom.hasClass(container, 'show')) {
                   this.hide();
                    yuiDom.removeClass(container, 'show');
                    yuiDom.addClass(container, 'hide');
                    yuiDom.removeClass(trigger, 'show');

                    yuiDom.setStyle(container, 'zIndex', '');
                    yuiDom.setStyle(trigger, 'zIndex', '');

                }
            },
            toggle : function() {
                var container = yuiDom.get(config.container);
                var trigger = yuiDom.get(config.trigger);

                if (yuiDom.hasClass(container, 'show')) {
                    this.hide();
                    yuiDom.removeClass(container, 'show');
                    yuiDom.addClass(container, 'hide');
                    yuiDom.removeClass(trigger, 'show');

                    yuiDom.setStyle(container, 'zIndex', '');
                    yuiDom.setStyle(trigger, 'zIndex', '');
                }
                else {
                    this.show();
                    yuiDom.removeClass(container, 'hide');
                    yuiDom.addClass(container, 'show');
                    yuiDom.addClass(trigger, 'show');
            
                     yuiDom.setStyle(container, 'zIndex', "499");
                    yuiDom.setStyle(trigger, 'zIndex', "500");

                }
            }
        };
        
        return self;
    }

    SW.widget.Component = Component;
}());
