﻿var SearchControl = {
    m_noHits: 5,
    m_currentAddressPageIndex: 0,
    m_currentPropertyPageIndex: 0,
    m_currentPlacePageIndex: 0,
    m_currentPoiPageIndex: 0,
    m_currentRefmarkPageIndex: 0,
    m_currentPlanPageIndex: 0,
    m_currentSearchValue: 0,
    m_currentMunicipality: 0,
    m_currentSearchService: 0,
    m_currentSearchCompleteMethod: 0,
    m_currentSearchAnnotationID: "searchAnnotation",
    m_propertyDrawingGroup: "eiendomsflate",

    search: function(t_value, t_municipality, t_searchTypes) {
        this.m_currentAddressPageIndex = 0;
        this.m_currentPropertyPageIndex = 0;
        this.m_currentPlacePageIndex = 0;
        this.m_currentPoiPageIndex = 0;
        this.m_currentRefmarkPageIndex = 0;
        this.m_currentPlanPageIndex = 0;

        Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.Search(
            g_kartController.getCustomerId(),
            g_kartController.getThemeId(),
            t_value,
            t_municipality,
            t_searchTypes,
            this._onSearchComplete,
            this._onSearchFailed,
            this
        );
    },

    nextPage: function(t_currentPageType) {
        var t_pageIndex = 0
        if (t_currentPageType == "address") {
            t_pageIndex = ++this.m_currentAddressPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetAddressPage;
            this.m_currentSearchCompleteMethod = this._onAddressSearchComplete;
        }
        else if (t_currentPageType == "property") {
            t_pageIndex = ++this.m_currentPropertyPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPropertyPage;
            this.m_currentSearchCompleteMethod = this._onPropertySearchComplete;
        }
        else if (t_currentPageType == "place") {
            t_pageIndex = ++this.m_currentPlacePageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPlaceNamePage;
            this.m_currentSearchCompleteMethod = this._onPlaceSearchComplete;
        }
        else if (t_currentPageType == "poi") {
            t_pageIndex = ++this.m_currentPoiPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPoiPage;
            this.m_currentSearchCompleteMethod = this._onPoiSearchComplete;
        }
        else if (t_currentPageType == "refmark") {
            t_pageIndex = ++this.m_currentRefmarkPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetRefmarkPage;
            this.m_currentSearchCompleteMethod = this._onRefmarkSearchComplete;
        }
        else if (t_currentPageType == "plan") {
            t_pageIndex = ++this.m_currentPlanPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPlanPage;
            this.m_currentSearchCompleteMethod = this._onPlanSearchComplete;
        }

        this._search(
            this.m_currentSearchValue,
            this.m_currentMunicipality,
            this.m_currentSearchService,
            this.m_currentSearchCompleteMethod,
            t_currentPageType,
            t_pageIndex,
            true);

    },

    previousPage: function(t_currentPageType) {
        var t_pageIndex = 0
        if (t_currentPageType == "address") {
            t_pageIndex = --this.m_currentAddressPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetAddressPage;
            this.m_currentSearchCompleteMethod = this._onAddressSearchComplete;
        }
        else if (t_currentPageType == "property") {
            t_pageIndex = --this.m_currentPropertyPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPropertyPage;
            this.m_currentSearchCompleteMethod = this._onPropertySearchComplete;
        }
        else if (t_currentPageType == "place") {
            t_pageIndex = --this.m_currentPlacePageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPlaceNamePage;
            this.m_currentSearchCompleteMethod = this._onPlaceSearchComplete;
        }
        else if (t_currentPageType == "poi") {
            t_pageIndex = --this.m_currentPoiPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPoiPage;
            this.m_currentSearchCompleteMethod = this._onPoiSearchComplete;
        }
        else if (t_currentPageType == "refmark") {
            t_pageIndex = --this.m_currentRefmarkPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetRefmarkPage;
            this.m_currentSearchCompleteMethod = this._onRefmarkSearchComplete;
        }
        else if (t_currentPageType == "plan") {
            t_pageIndex = --this.m_currentPlanPageIndex;
            this.m_currentSearchService = Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetPlanPage;
            this.m_currentSearchCompleteMethod = this._onPlanSearchComplete;
        }

        this._search(
            this.m_currentSearchValue,
            this.m_currentMunicipality,
            this.m_currentSearchService,
            this.m_currentSearchCompleteMethod,
            t_currentPageType,
            t_pageIndex,
            true);
    },

    mapCenterAndZoom: function(t_x, t_y, t_zoomlevel, t_title) {
        t_coordinate = new Coordinate(t_x, t_y)
        g_kartController.getMap().centerAndZoom(t_coordinate, t_zoomlevel);

        this._addSearchAnnotation(t_coordinate, t_title);
    },

    mapCenterAndZoomOnAddress: function(t_dropdownID, t_zoomlevel, t_title) {
        t_coordinatestring = $F(t_dropdownID);
//        alert("mapCenterAndZoomOnAddress: " + t_coordinatestring);
        t_coordinatearray = t_coordinatestring.split(";"); // t_coordinatestring: "aid;y;x"
        t_coordinate = new Coordinate(parseFloat(t_coordinatearray[1].replace(",", ".")), parseFloat(t_coordinatearray[2].replace(",", ".")))
        g_kartController.getMap().centerAndZoom(t_coordinate, t_zoomlevel);

        t_index = $(t_dropdownID).selectedIndex;
        t_selectedText = $(t_dropdownID).options[t_index].text;

        this._addSearchAnnotation(t_coordinate, t_title + t_selectedText);
    },

    lookupProperty: function(t_gid) {
        $("mapsurface").style.cursor = "wait";
        var t_url = "/System/Search/PropertyCoordinates/" + g_kartController.getCustomerId() +
            "?layers=Eiendomskart&objCode=" + t_gid;
        this._highLightProperty(t_url);
    },

    _highLightProperty: function(t_coordinatesUrl) {
        $invokeOnResponse(
            SearchControl._zoomAndHighLightProperty,
            t_coordinatesUrl,
            true);
    },

    _zoomAndHighLightProperty: function(t_coordinates) {
        var t_propertydrawinggroup = "eiendomsflate";
        if (t_coordinates.length > 0) {
            DrawingControl.removeDrawingGroup(t_propertydrawinggroup);
            DrawingControl.drawCoordinatePolygon(t_coordinates, { groupName : t_propertydrawinggroup, width : 3, closePolygon : true});
            g_kartController.zoomOnBBox(t_coordinates);
        }
        else {
            alert("Noe gikk galt, fant ingen eiendom...");
        }
        $("mapsurface").style.cursor = "";
    },

    _addSearchAnnotation: function(t_coordinate, t_title) {
        g_kartController.getMap().removeAnnotation(this.m_currentSearchAnnotationID);

        var t_annotation = new Annotation(
            t_coordinate,
            "Søkeresultat",
            "<span style='padding-left: 6px; font-weight: bold;'>" + t_title + "</span><br/>",
            "/Static/Shared/images/ui/pilen.gif",
            -24,
            -24
        );
        t_annotation.id = this.m_currentSearchAnnotationID;

        g_kartController.getMap().addAnnotation(t_annotation);
        g_kartController.addGlobeAnnotation(t_annotation); // sjekker om globen er ekspandert og initiert
    },

    _search: function(t_value, t_municipality, t_serviceMethod, t_completeMethod, t_currentPageType, t_currentPageIndex, t_isPaging) {
        var t_isPaging = $defined(t_isPaging) ? t_isPaging : false;
        var t_pageIndex = $defined(t_currentPageIndex) ? t_currentPageIndex : 0;

        if (t_currentPageType == "address")
            this.m_currentAddressPageIndex = t_pageIndex;
        else if (t_currentPageType == "property")
            this.m_currentPropertyPageIndex = t_pageIndex;
        else if (t_currentPageType == "place")
            this.m_currentPlacePageIndex = t_pageIndex;
        else if (t_currentPageType == "poi")
            this.m_currentPoiPageIndex = t_pageIndex;
        else if (t_currentPageType == "refmark")
            this.m_currentRefmarkPageIndex = t_pageIndex;
        else if (t_currentPageType == "plan")
            this.m_currentPlanPageIndex = t_pageIndex;

        this.m_currentSearchValue = t_value;
        this.m_currentMunicipality = t_municipality;
        this.m_currentSearchService = t_serviceMethod;
        this.m_currentSearchCompleteMethod = t_completeMethod;

        if (t_currentPageType == "property" || t_currentPageType == "refmark" || t_currentPageType == "plan") {
            t_serviceMethod(
                g_kartController.getCustomerId(),
                t_pageIndex,
                this.m_noHits,
                t_value,
                t_municipality,
                !t_isPaging,
                function(t_result, t_this) {
                    t_this.m_currentSearchCompleteMethod(t_result);
                },
                function() {
                },
                this
            );
        }
        else if (t_currentPageType == "address") {
            t_serviceMethod(
                t_pageIndex,
                this.m_noHits,
                t_value,
                t_municipality,
                !t_isPaging,
                g_kartController.getThemeId(),
                function(t_result, t_this) {
                    t_this.m_currentSearchCompleteMethod(t_result);
                },
                function() {
                },
                this
            );
        }
        else {
            t_serviceMethod(
                t_pageIndex,
                this.m_noHits,
                t_value,
                t_municipality,
                !t_isPaging,
                function(t_result, t_this) {
                    t_this.m_currentSearchCompleteMethod(t_result);
                },
                function() {
                },
                this
            );
        }
    },

    _onSearchComplete: function(result, t_thisbind) {
        for (var i = 0; i < result.length; i++) {
            if (result[i].Type == 1) {
                t_thisbind._onAddressSearchComplete(result[i]);
            }
            else if (result[i].Type == 2) {
                t_thisbind._onPlaceSearchComplete(result[i]);
            }
            else if (result[i].Type == 4) {
                t_thisbind._onPropertySearchComplete(result[i]);
            }
            else if (result[i].Type == 8) {
                t_thisbind._onPoiSearchComplete(result[i]);
            }
            else if (result[i].Type == 32) {
                t_thisbind._onRefmarkSearchComplete(result[i]);
            }
            else if (result[i].Type == 64) {
                t_thisbind._onPlanSearchComplete(result[i]);
            }
        }
        showSearchDiv();
        hideLoadingBar();
    },

    _onSearchFailed: function(result, t_thisbind) {
        alert("Noe gikk galt i forbindelse med søk. Vennligst prøv igjen.");
        hideLoadingBar();
    },

    _onAddressSearchComplete: function(t_value) {
        updateAddressMenuHeader(t_value.Hit);
        $("divAddressSearchResult").update(t_value.Result);
    },

    _onPlaceSearchComplete: function(t_value) {
        updatePlaceMenuHeader(t_value.Hit);
        $("divPlaceSearchResult").update(t_value.Result);
    },

    _onPropertySearchComplete: function(t_value) {
        updatePropertyMenuHeader(t_value.Hit);
        $("divPropertySearchResult").update(t_value.Result);
    },

    _onPoiSearchComplete: function(t_value) {
        updatePoiMenuHeader(t_value.Hit);
        $("divPoiSearchResult").update(t_value.Result);
    },

    _onRefmarkSearchComplete: function(t_value) {
        updateRefmarkMenuHeader(t_value.Hit);
        $("divRefmarkSearchResult").update(t_value.Result);
    },

    _onPlanSearchComplete: function(t_value) {
        updatePlanMenuHeader(t_value.Hit);
        $("divPlanSearchResult").update(t_value.Result);
    },

    clearSearchInfo: function() {
        clearSearchText();
        this.removeSearchAnnotation();
        DrawingControl.removeDrawingGroup(this.m_propertyDrawingGroup); // Eiendomsmarkering
        hideSearchDiv();
    },

    removeSearchAnnotation: function() {
        g_kartController.getMap().removeAnnotation(this.m_currentSearchAnnotationID);
    },

    //Exact searches for property and address in case of lookup through URL
    exactAddressSearch: function(t_municipality, t_streetname, t_housenumber, t_houseletter, t_tooltipId) {
        Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetExactAddress(
            t_municipality,
            t_streetname,
            t_housenumber,
            t_houseletter,
            this._onExactAddressSearchComplete,
            this._onExactSearchFailed,
            {
                tooltipId: t_tooltipId
            }
        );
    },

    _onExactAddressSearchComplete: function(result, t_thisbind) {
        if (result != null) {
            var description = result.Streetname + " " + result.Houses[0].Number + result.Houses[0].UnderNumber;
            SearchControl.mapCenterAndZoom(result.Houses[0].Y, result.Houses[0].X, 2, description);

            var tooltip = $find(t_thisbind.tooltipId);
            if (tooltip != null) {
                tooltip.set_content(description);

                var h = Utilities.Window.getSize().height;
                var w = Utilities.Window.getSize().width;

                var tw = 200;
                var th = 50;

                var x = w / 2 - tw / 3;
                var y = h / 2 - th / 2;

                if (g_kartController.isLeftMenuExpanded()) {
                    x -= 322;
                }
                tooltip.set_offsetX(x);
                tooltip.set_offsetY(y);
                tooltip.show();
            }
        }
        else {
            alert("Beklager, fant ikke det du vil slå opp på.");
        }
    },

    exactPropertySearch: function(t_municipality, t_gnr, t_bnr, t_fnr, t_tnr, t_tooltipId) {
        Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetExactProperty(
            g_kartController.getCustomerId(),
            t_municipality,
            t_gnr,
            t_bnr,
            t_fnr,
            t_tnr,
            this._onExactPropertySearchComplete,
            this._onExactSearchFailed,
            {
                tooltipId: t_tooltipId
            }
        );
    },

    _onExactPropertySearchComplete: function(result, t_thisbind) {
        if (result != null) {
            var description = 'Eiendom (GNR/BNR): ' + result.DisplayGid;

            if (result.FromFeature) {
                SearchControl.lookupProperty(result.ObjectCode);
            }
            else {
                SearchControl.mapCenterAndZoom(result.Y, result.X, 2, description);
            }

            var tooltip = $find(t_thisbind.tooltipId);
            if (tooltip != null) {
                tooltip.set_content(description);

                var h = Utilities.Window.getSize().height;
                var w = Utilities.Window.getSize().width;

                var tw = 200;
                var th = 50;

                var x = w / 2 - tw / 3;
                var y = h / 2 - th / 2;

                if (g_kartController.isLeftMenuExpanded()) {
                    x -= 322;
                }
                tooltip.set_offsetX(x);
                tooltip.set_offsetY(y);
                tooltip.show();
            }
        }
        else {
            alert("Beklager, fant ikke det du vil slå opp på.");
        }
        $("mapsurface").style.cursor = "";
    },

    _onExactSearchFailed: function(result, t_thisbind) {
        $("mapsurface").style.cursor = "";
        alert("Beklager, fant ikke det du vil slå opp på.");
    },

    getAddresses: function(t_value, t_municipality, t_onSuccess, t_onError) {
        Norkart.Web.Fag.Applications.MapPortal.AJAX.WebServices.Search.SearchModelProxy.GetAddressPage(
            0,
            30,
            t_value,
            t_municipality,
            true,
            g_kartController.getThemeId(),
            t_onSuccess,
            t_onError
        );
    }
}
