﻿var locationSearch;

function InitLocationSearch(evObj) {

    if ($("Map") && FormElementExtender.Elements) {
        FormElementExtender.Elements.get("txtCountry").RefreshElement($("txtCountry"));
        FormElementExtender.Elements.get("txtRegion").RefreshElement($("txtRegion"));
        FormElementExtender.Elements.get("txtCity").RefreshElement($("txtCity"));

        if ( locationSearch )
        {
            locationSearch.RefreshElements(
                FormElementExtender.Elements.get("txtCountry"),
                FormElementExtender.Elements.get("txtRegion"), 
                FormElementExtender.Elements.get("txtCity")
            );
        }
        else
        {
            locationSearch = new LocationSearchExtender(
                FormElementExtender.Elements.get("txtCountry"),
                FormElementExtender.Elements.get("txtRegion"), 
                FormElementExtender.Elements.get("txtCity")
            );
        }
    }

    if ( $("ObjectSearch") )
        $("ObjectSearch").down(".overlay").hide();
}

/********** LOCATION SEARCH EXTENDER **********/
var LocationSearchExtender = Class.create(
{
    initialize: function (drpCountry, drpRegion, drpCity) {

        this.DrpCountry = drpCountry;
        this.DrpRegion = drpRegion;
        this.DrpCity = drpCity;

        if (this.DrpCity.SelectBox.options.length == 1)
            this.DrpCity.Disable();

        if (this.DrpRegion.SelectBox.options.length == 1)
            this.DrpRegion.Disable();

        this.ItemOver = this.OnItemOver.bindAsEventListener(this);
        this.ItemOut = this.OnItemOut.bindAsEventListener(this);
        this.ItemClick = this.OnItemClick.bindAsEventListener(this);

        this.Item = $("Map");
        $("MapLabel").hide();

        this.Item.select("area").each(function (item) {
            item.href = "javascript:void(0);";

            Event.observe(item, "mouseover", this.ItemOver);
            Event.observe(item, "mouseout", this.ItemOut);

            item.ObjectID = item.className;
            item.ObjectText = item.alt;

            new TooltipExtender("Map:" + item.ObjectID, item, item.alt, "tooltip");

            item.writeAttribute("alt", null);

            Event.observe(item, "click", this.ItemClick);
        }, this);
    },

    RefreshElements: function (drpCountry, drpRegion, drpCity) {
        this.DrpCountry = drpCountry;
        this.DrpRegion = drpRegion;
        this.DrpCity = drpCity;

        if (this.DrpCity.SelectBox.options.length == 1)
            this.DrpCity.Disable();

        if (this.DrpRegion.SelectBox.options.length == 1)
            this.DrpRegion.Disable();

        this.Item = $("Map");
        $("MapLabel").hide();

        this.Item.select("area").each(function (item) {
            item.href = "javascript:void(0);";

            Event.observe(item, "mouseover", this.ItemOver);
            Event.observe(item, "mouseout", this.ItemOut);

            item.ObjectID = item.className;
            item.ObjectText = item.alt;

            new TooltipExtender("Map:" + item.ObjectID, item, item.alt, "tooltip");


            item.writeAttribute("alt", null);

            Event.observe(item, "click", this.ItemClick);
        }, this);

    },

    Element: function () {
        return this.Item;
    },

    OnItemOver: function (evObj) {
        var targetObj = $(Event.element(evObj));
        var overElement = $(this.Item.down(".map").id).down("." + targetObj.className + ".overElement")

        if (overElement && !overElement.hasClassName("over"))
            overElement.addClassName("over");

    },

    OnItemOut: function (evObj) {
        var targetObj = $(Event.element(evObj));
        var overElement = $(this.Item.down(".map").id).down("." + targetObj.className + ".overElement")

        if (overElement && overElement.hasClassName("over"))
            overElement.removeClassName("over");
    },

    OnItemClick: function (evObj) {
        var drp = ($("Country")) ? this.DrpCountry : this.DrpRegion;

        drp.ChangeSelection($(Event.element(evObj)).ObjectID);
        Event.simulate(drp.SelectBox, "change");
    }

});
/********** LOCATION SEARCH EXTENDER **********/



