/**
 * 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) {
    if(this.moduleElement != null) {
        if(this.lastContainer == container) {
            // still on same container, so toggle
            if(this.moduleElement.style.display == 'none') {
                this.show(propIds, container);
            }
            else {
                this.hide();
            }
        }
        else {
            // new container, so show on new container
            this.show(propIds, container, ratePlanName, selectedPropId);
        }

        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 {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.show = function(propIds, container, ratePlanName, selectedPropId) {
    if(this.moduleElement != null) {
        var theForm = this.moduleElement.getElementsByTagName("form")[0];

        // populate properties
        fillPropertySelect(theForm.inlinePropSelect, propIds, selectedPropId);

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

        this.moduleElement.style.display = "block";

        if(container != null) {
            this.moduleElement.parentNode.removeChild(this.moduleElement);
            container.appendChild(this.moduleElement);
        }
    }
}

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


addLoadEvent(
    function() {
        inlineRateSearch = new InlineRateSearchModule(document.getElementById('inlineRateSearchFormContainer'));
    }
);