﻿/// <reference path="../../Default/Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="../../Default/Scripts/jquery.bnet-vsdoc.js" />
var SERVER_PATH = "/em1"; // "http://localhost:2345/masterweb";
var GENERIC_ERROR_MESSAGE = "En feil oppstod. Prøv igjen senere eller kontakt oss på telefon 800 34 363";

$(document).ready(function () {
    // PNG fix for IE
    $(document).pngFix();
    try {
        $("#Accordion").accordion();
    } catch (e) {/* accordion not included on page */}

    /* Menu hover */
    $("#Menu li").hover(function () {
        var $ul = $(this).children("ul");
        if ($ul.hasClass("level2")) {
            //no animation for ie7 or ie6
            if ($ul.parents(".ie7, .ie6").length > 0)
                $ul.show();
            else
                $ul.slideDown(300);
        } else {
            $ul.animate({ width: 'toggle' }, 300);
        }
    }, function () {
        $(this).children("ul").hide();
    });

    /* KONTAKTSKJEMA */
    $("#FormID-1 .FormConfirmationMessage").parents(".toolboxform").prev("h3").trigger("click");

    /* SØK ETTER BOLIG */
    $("#SearchEstateForm input:text").keypress(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            $("#btSearchEstateForm").trigger("click");
        }
    });
    $("#btSearchEstateForm").click(function (event) {
        var query = $("#estate").val().trim();

        var searchUrl = $(this).attr("data-searchurl");

        if (searchUrl.indexOf("?") > -1) { //old logic
            location.href = searchUrl + encodeURI(query);
            return;
        }

        var areaIds = $("#AreaList :selected").val();
        if (areaIds.indexOf(',') == -1)
            query = escape(query) + "&areaId=" + areaIds;
        else {
            var arrayIds = areaIds.split(',');
            for (var i in arrayIds)
                query =query + "&areaId=" + arrayIds[i];
        }

        $('.cbPropertyType:checked').each(function () {
            query += "&PROPERTY_TYPE=" + $(this).val();
        });

        $(".cbPropertyLocation:checked").each(function () {
            query += "&LEISURE_SITUATION=" + $(this).val();
        });

        //Validate fields
        if (query === "") {
            alert("Søkefeltet kan ikke være tomt. Prøv igjen.");
            return;
        }

        location.href = searchUrl + "?keyword=" + query;
    });


});
