﻿Type.registerNamespace("Sembo.Site.Accommodation.House");

Sembo.Site.Accommodation.House.HouseSearchFields = function(element) {
    Sembo.Site.Accommodation.House.HouseSearchFields.initializeBase(this, [element]);

    this.mapLink = $get(element.id + "_mapLink");
    this.closeMapLink = $get(element.id + "_closeMapLink");
    this.mapPanel = $get(element.id + "_mapPanel");
    this.countryDropDown = $get(element.id + "_countryDropDown");
    this.regionDropDown = $get(element.id + "_regionDropDown");
    this.destinationDropDown = $get(element.id + "_destinationDropDown");

    this._fromDateTextBox = $get(element.id + "_fromDateTextBox");
    this._toDateTextBox = $get(element.id + "_toDateTextBox");

    this._fromDateSet = false;
    this._toDateSet = false;
}

Sembo.Site.Accommodation.House.HouseSearchFields.prototype =
{
    // Properties
    get_selectedCountryCode: function ()
    {
        return Sembo.Dom.getSelectedValue(this.countryDropDown);
    },

    set_selectedCountryCode: function (value)
    {
        Sembo.Dom.setSelectedValue(this.countryDropDown, value);
    },

    get_fromDateSet: function ()
    {
        return this._fromDateSet;
    },

    get_toDateSet: function ()
    {
        return this._toDateSet;
    },

    get_fromDate: function ()
    {
        return Date.parseLocale(this._fromDateTextBox.value);
    },

    set_fromDate: function (value)
    {
        Sembo.ThrowHelper.argumentNullException(value, "value");

        var stringValue = value.localeFormat("d");

        if (this._fromDateTextBox.value !== stringValue)
        {
            this._fromDateTextBox.value = stringValue;
            this.onFromDateChanged();
        }
    },

    get_toDate: function ()
    {
        return Date.parseLocale(this._toDateTextBox.value);
    },

    set_toDate: function (value)
    {
        Sembo.ThrowHelper.argumentNullException(value, "value");

        var stringValue = value.localeFormat("d");

        if (this._toDateTextBox.value !== stringValue)
        {
            this._toDateTextBox.value = stringValue;
            this.onToDateChanged();
        }
    },


    // Methods
    initialize: function ()
    {
        Sembo.Site.Accommodation.House.HouseSearchFields.callBaseMethod(this, "initialize");
        this.setMapLinkState();
        this.createEventHandlers();
        $addHandler(this.mapLink, "click", this.mapLink_click);
        $addHandler(this.closeMapLink, "click", this.closeMapLink_click);
        $addHandler(this.countryDropDown, "change", this.countryDropDown_change);
        $addHandler(this.destinationDropDown, "focus", this.destinationDropDown_focus);
        $addHandler(this.destinationDropDown, "blur", this.destinationDropDown_blur);
        $addHandler(this.destinationDropDown, "change", this.destinationDropDown_change);
        $addHandler(this._fromDateTextBox, "change", this.fromDateTextBox_change);
        $addHandler(this._toDateTextBox, "change", this.toDateTextBox_change);
    },

    dispose: function ()
    {
        Sembo.Site.Accommodation.House.HouseSearchFields.callBaseMethod(this, "dispose");

        $removeHandler(this.mapLink, "click", this.mapLink_click);
        $removeHandler(this.closeMapLink, "click", this.closeMapLink_click);
        $removeHandler(this.countryDropDown, "change", this.countryDropDown_change);
        $removeHandler(this.destinationDropDown, "focus", this.destinationDropDown_focus);
        $removeHandler(this.destinationDropDown, "blur", this.destinationDropDown_blur);
        $removeHandler(this.destinationDropDown, "change", this.destinationDropDown_change);
        $removeHandler(this._fromDateTextBox, "change", this.fromDateTextBox_change);
        $removeHandler(this._toDateTextBox, "change", this.toDateTextBox_change);
    },

    toggleMapVisibility: function ()
    {
        if (this.getMapIsVisible())
        {
            this.hideMap();
        }
        else
        {
            this.showMap();
        }
    },

    getMapIsVisible: function ()
    {
        return this.mapPanel.style.display === "block";
    },

    hideMap: function ()
    {
        Sys.Debug.trace("Hiding map.");
        this.mapPanel.style.display = "none";
    },

    showMap: function ()
    {
        Sys.Debug.trace("Showing map.");

        Sys.Debug.traceDump(this.mapPanel);
        Sys.Debug.trace("Display: " + this.mapPanel.style.display);
        this.mapPanel.style.display = "block";
    },

    setMapLinkState: function ()
    {
        if (this.get_selectedCountryCode() === "208")
        {
            this.mapLink.style.display = "block";
        }
        else
        {
            this.mapLink.style.display = "none";
            this.hideMap();
        }
    },

    onFromDateChanged: function ()
    {
        if (!this.get_isInitialized()) return;

        var value = this.get_fromDate();

        if (value)
        {
            var calendar = $find(this.get_element().id + "_fromDateCalendar");
            if (calendar) calendar.set_selectedDate(value);

            if (value > this.get_toDate())
            {
                this.set_toDate(value);
            }
        }

        this.raiseEvent("fromDateChanged", Sys.EventArgs.empty);
    },

    onToDateChanged: function ()
    {
        if (!this.get_isInitialized()) return;

        var value = this.get_toDate();

        if (value)
        {
            var calender = $find(this.get_element().id + "_toDateCalendar");
            if (calender) calender.set_selectedDate(value);

            if (value < this.get_fromDate())
            {
                this.set_fromDate(value);
            }
        }

        this.raiseEvent("toDateChanged", Sys.EventArgs.empty);
    },

    raiseEvent: function (eventName, eventArgs)
    {
        var handler = this.get_events().getHandler(eventName);

        if (handler)
        {
            handler(this, eventArgs);
        }
    },

    createEventHandlers: function ()
    {
        var me = this;

        this.mapLink_click = function ()
        {
            Sys.Debug.trace("Map link click.");
            me.toggleMapVisibility();
            return false;
        };

        this.closeMapLink_click = function ()
        {
            Sys.Debug.trace("Close map link click.");
            me.hideMap();
            return false;
        };

        this.countryDropDown_change = function ()
        {
            me.setMapLinkState();
        };

        this.destinationDropDown_focus = function ()
        {
            if (me.get_selectedCountryCode() === "208")
            {
                me.showMap();
            }
        };

        this.destinationDropDown_blur = function ()
        {
            me.hideMap();
        };

        this.destinationDropDown_change = function ()
        {
            me.destinationDropDown.blur();
        };

        this.fromDateTextBox_change = function ()
        {
            me._fromDateSet = true;
            me.onFromDateChanged();
        };

        this.toDateTextBox_change = function ()
        {
            me._toDateSet = true;
            me.onToDateChanged();
        }
    },

    //events
    add_FromDateChanged: function (handler)
    {
        this.get_events().addHandler("fromDateChanged", handler);
    },

    remove_FromDateChanged: function (handler)
    {
        this.get_events().removeHandler("fromDateChanged", handler);
    },

    add_ToDateChanged: function (handler)
    {
        this.get_events().addHandler("toDateChanged", handler);
    },

    remove_ToDateChanged: function (handler)
    {
        this.get_events().removeHandler("toDateChanged", handler);
    }
}

Sembo.Site.Accommodation.House.HouseSearchFields.registerClass("Sembo.Site.Accommodation.House.HouseSearchFields", Sys.UI.Control);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
