﻿var MAX_PROPERTIES_TO_SHOW = 5;

function init() {
    showTop5($("#Top5DropDownKey").attr("value"));

    $("#Top5DropDown>ul>li").click(function (event) {
        event.preventDefault();
        var id = $(this).getId();
        var href = $(this).children("a")[0];
        var key = $("#Top5Input" + id).attr("value");
        var name = $(href).html();
        $("#Top5DropDownTitle").html(name);
        $("#Top5DropDownKey").attr("value", key);
        showTop5(key);
    });
}

function showTop5(key) {
    var param = "{key : " + key + ", antall: " + MAX_PROPERTIES_TO_SHOW + "}";
    $.ajax(
        {
            type: "POST",
            url: "/layouts/EmV/sublayouts/PropertyService.asmx/GetTop5",
            dataType: "json",
            data: param,
            contentType: "application/json; charset=utf-8",
            success: function (json) {
                if (json != null) {
                    $('#PropertyList').html($('#PropertyItemsTemplate').parseTemplate(json));
                }
            },
            error: function (xhr, msg, e) {
                logErrorMsg(xhr.responseText);
            }
        });
}
function initBidsAndShow() {
    $("[name=BidLink]").each(function () {
        if ($(this).html() == "Hordaland") {
            var idItem = $(this).attr("id");
            var id = idItem.substr(12, idItem.length);
            var key = $("#BidItemKey" + id).val();
            var value = $(this).html();
            getData(key, value, "/layouts/EmV/sublayouts/PropertyService.asmx/GetBidsForRegion", "bid");
            $("#BidDropDownTilte").html(value);
            $("#BidDropDownKey").attr("value", key);
        }
    });
    $("[name=ShowLink]").each(function () {
        if ($(this).html() == "Hordaland") {
            var idItem = $(this).attr("id");
            var id = idItem.substr(13, idItem.length);
            var key = $("#ShowItemKey" + id).val();
            var value = $(this).html();
            getData(key, value, "/layouts/EmV/sublayouts/PropertyService.asmx/GetShowsForRegion", "show");
            $("#ShowDropDownTilte").html(value);
            $("#ShowDropDownKey").attr("value", key);
        }
    });
    $("[name=BidItemName]").click(function (event) {
        event.preventDefault();
        var idItem = $(this).attr("id");
        var id = idItem.substr(7, idItem.length);
        var key = $("#BidItemKey" + id).val();
        var value = $("#BidItemValue" + id).html();
        getData(key, value, "/layouts/EmV/sublayouts/PropertyService.asmx/GetBidsForRegion", "bid");
        $("#BidDropDownTilte").html(value);
        $("#BidDropDownKey").attr("value", key);

    });
    $("[name=ShowItemName]").click(function (event) {
        event.preventDefault();
        var idItem = $(this).attr("id");
        var id = idItem.substr(8, idItem.length);
        var key = $("#ShowItemKey" + id).val();
        var value = $("#ShowItemValue" + id).html();
        getData(key, value, "/layouts/EmV/sublayouts/PropertyService.asmx/GetShowsForRegion", "show");
        $("#ShowDropDownTilte").html(value);
        $("#ShowDropDownKey").attr("value", key);
    });

    $("#ButtonAlleVisninger").click(function (event) {
        var parm = new StringBuilder("Kjope/Boliger?KunVisninger");
        if ($("#ShowDropDownKey").attr("value") == "F") {
            parm.append("&Fylke=");
            parm.append(encodeURIComponent($("#ShowDropDownTilte").html()));
        } else if ($("#ShowDropDownKey").attr("value") == "K") {
            parm.append("&Kommune=");
            parm.append(encodeURIComponent($("#ShowDropDownTilte").html()));
        }
        window.location = parm.toString();
    });
    $("#ButtonAllBudgivning").click(function (event) {
        event.preventDefault();
        var parm = new StringBuilder("Kjope/Boliger/Budrunder");
        if ($("#BidDropDownKey").attr("value") == "F") {
            parm.append("?Fylke=");
            parm.append(encodeURIComponent($("#BidDropDownTilte").html()));
        } else if ($("#BidDropDownKey").attr("value") == "K") {
            parm.append("?Kommune=");
            parm.append(encodeURIComponent($("#BidDropDownTilte").html()));
        }
        window.location = parm.toString();
    });
};

function getData(key, value, url, whatToShow) {
    var param = "{key : '" + key + "', value: '" + value + "'}";
    $.ajax(
        {
            type: "POST",
            url: url,
            dataType: "json",
            data: param,
            contentType: "application/json; charset=utf-8",
            success: function (json) {
                if (json != null) {
                    if (whatToShow == "bid") {
                        $('#BidList').html($('#BidListTemplate').parseTemplate(json));
                    }
                    else {
                        $('#ShowList').html($('#ShowListTemplate').parseTemplate(json));
                    }
                }
            },
            error: function (xhr, msg, e) {
                logErrorMsg(xhr.responseText);
            }
        });
}
