/**
 * Inline rate search module controller object
 *
 * @param moduleElement element object containing the body of the module
 */
function InlineRateSearchModule(moduleElement) {
    this._moduleElement = moduleElement;
    this.lastContainer = null;
}

/**
 * Toggles the module between shown/hidden if still attached to the same container,
 * otherwise just shows on a new container.
 *
 * @param propIds {String}      comma delimited list of property ids
 * @param container {Node}      node to attach this module to
 * @param ratePlanName {String}     name of rate plan
 * @param selectedPropId {String}       optional id of pre-selected property in property dropdown
 */
InlineRateSearchModule.prototype.toggle = function(propIds, container, ratePlanName, selectedPropId, offerCode) {
    var moduleEl = this.getModuleElement();
    if(moduleEl != null) {
        if(this.lastContainer == container) {
            // still on same container, so toggle
            if(moduleEl.style.display == 'none') {
                this.show(propIds, container);
            }
            else {
                this.hide();
            }
        }
        else {
            // new container, so show on new container
            this.show(propIds, container, ratePlanName, selectedPropId, offerCode);
        }

        this.lastContainer = container;
    }
}

/**
 * Attaches the search module to the given container and makes it visible
 *
 * @param propIds {String}              comma delimited list of property ids
 * @param container {Element | String}  element to attach this module to (or id of the element)
 * @param ratePlanName {String}         name of rate plan
 * @param selectedPropId {String}       optional id of pre-selected property in property dropdown
 */
InlineRateSearchModule.prototype.show = function(propIds, container, ratePlanName, selectedPropId, offerCode) {
    var moduleEl = this.getModuleElement();
    if(moduleEl != null) {
        // hide module while updating
        YAHOO.util.Dom.setStyle(moduleEl, 'opacity', 0);

        var theForm = moduleEl.getElementsByTagName("form")[0];

        // populate properties
        fillPropertySelect(
                theForm.inlinePropSelect, propIds, selectedPropId, null,
                function() {
                    // display module after properties have been populated
                    moduleEl.style.display = "block";
                    YAHOO.util.Dom.setStyle(moduleEl, 'opacity', 1);
        });

        // update rate plan name
        theForm.ratePlanName.value = ratePlanName;
        theForm.offerCode.value = offerCode;

        if(YAHOO.lang.isString(container))
            container = document.getElementById(container);

        if(container != null) {
            moduleEl.parentNode.removeChild(moduleEl);
            container.appendChild(moduleEl);

            // update z-index of module to match container's z-index
            moduleEl.style.zIndex = container.style.zIndex ? container.style.zIndex : 0;
        }
    }
}

/**
 * Hides the search module
 */
InlineRateSearchModule.prototype.hide = function() {
    var moduleEl = this.getModuleElement();
    if(moduleEl != null) {
        moduleEl.style.display = "none";
    }
}

/**
 * Returns module element object
 */
InlineRateSearchModule.prototype.getModuleElement = function() {
    if(YAHOO.lang.isString(this._moduleElement))
        return document.getElementById(this._moduleElement);
    else
        return this._moduleElement;
}

// instantiate global inline rate search instance
var inlineRateSearch = new InlineRateSearchModule('inlineRateSearchFormContainer');
