﻿
$(function () {
    var newLogic = true;

    $("#office")
        .attr("autocomplete", "off")
        .autocomplete({
            listId: "OfficeList",
            filter: function (query) {
                var $list = $("#OfficeList");
                $("#office").data("officeid", "");
                if (query !== "") {
                    $.ajax({
                        url: "/Customers/Eime/Handlers/GetOffices.ashx",
                        type: "get", dataType: "json",
                        data: { search: query.toLowerCase() },
                        contentType: "application/json",
                        success: function (data) {
                            $list.empty().show();
                            if (data.length > 0) {
                                var max = Math.min(data.length, 15);

                                for (i = 0; i < max; i++) {
                                    //removing prefix
                                    var name = data[i].Name;
                                    name = name.replace(/([Ee]iendoms[Mm]egler ?1)/, "");

                                    $("<li>")
                                            .text(name)
                                            .data("officeid", data[i].ID)
                                            .data("friendlyurl", data[i].FriendlyUrl)
                                            .appendTo($list);
                                }
                                $list.find("li:first").addClass("hover");
                                if ($list.find("li").length === 0) $list.hide();
                            }
                        },
                        error: function (data, message) {
                            bNet.log(data);
                        }
                    });
                } else {
                    $list.empty().hide();
                }
            },
            selectItem: function ($this, $sel) {
                $this.val($sel.text()).data({
                    "officeid": $sel.data("officeid"),
                    "friendlyurl": $sel.data("friendlyurl")
                });
                $("#OfficeList").empty().hide();
                $("#btSearchOfficeForm").click();
            },
            blur: function ($this, $sel) {
                setTimeout(function () {
                    $this.val($sel.text()).data({
                        "officeid": $sel.data("officeid"),
                        "friendlyurl": $sel.data("friendlyurl")
                    });
                    $("#OfficeList").empty().hide();
                }, 200);
            }
        });

    /* SØK ETTER KONTOR */
    $("#btSearchOfficeForm").click(function (event) {

        var $office = $("#office");

        //Validate fields
        if ($office.val().trim() === "") {
            alert("Kontorfeltet kan ikke være tomt. Prøv igjen.");
            return;
        }

        if (newLogic) {
            if ($office.data("friendlyurl") == undefined) {
                location.href = "http://www.eiendomsmegler1.no/finn-meglerkontor/resultat/?q=" + $office.val().trim();
                return;
            }
            location.href = $office.data("friendlyurl");

        } else {
            if (!$office.data("officeid")) {
                location.href = SERVER_PATH + '/CompanyPage.aspx?PageID=5011&SearchTypeID=2&SortBy=SortIndex&SearchKeyword=' + encodeURI($office.val());
            } else {
                location.href = "http://www.eiendomsmegler1.no/em1/CompanyPage.aspx?ContainerID=" + $office.data("officeid") + "&PageID=5150";
            }
        }
    });

    /* SØK VIA POSTNR */
    $("#postnr").keypress(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            $("#btFindByPostNumber").trigger("click");
        }
    });

    $("#btFindByPostNumber").click(function (event) {

        var postnr = encodeURI($("#SearchOfficeForm #postnr").val().trim());
        //Validate fields
        if (postnr === "") {
            alert("Postnr kan ikke være tomt. Prøv igjen.");
            return;
        }
        var $this = $(this);

        switchButtonForLoader($this);

        $.ajax({
            url: "/Customers/Eime/Handlers/GetOffices.ashx",
            data: { postnum: postnr },
            success: function (data) {

                if (newLogic)
                    location.href = data;
                else
                    location.href = "http://www.eiendomsmegler1.no/em1/CompanyPage.aspx?ContainerID=" + data + "&PageID=5150";
            },
            error: function (err) {
                if (newLogic) {
                    switchLoaderForButton($this);
                    alert("Vi fant ingen kontor som betjener det postnummeret.");
                } else {
                    if (err.responseText.indexOf("-") !== -1) {
                        location.href = "http://www.eiendomsmegler1.no/em1/CompanyPage.aspx?PageID=5150&SearchKeyword= &Postnummer=" + err.responseText + "&SearchTypeID=2&SortBy=SortIndex";
                    } else {
                        alert("Vi fant ingen kontor som betjener det postnummeret.");
                        switchLoaderForButton($this);
                    }
                }
            }
        });


    });

    function switchButtonForLoader($btn) {
        $("<img/>").attr({
            "src": "/Customers/Eime/Skin/Images/loader-smallcircle.gif",
            "style": "margin-left: 10px",
            "id": "searchLoader"
        }).insertBefore($btn);
        $btn.hide();
    }

    function switchLoaderForButton($btn) {
        $("#searchLoader").remove();
        $btn.show();
    }

});


    

