﻿function showPropertyInfo(locale, id) {
    var heightt = 530;
    var widthh = 640;
    tp = Math.ceil((screen.height - heightt) / 2);
    lf = Math.ceil((screen.width - widthh) / 2);

    var url = '/' + locale + '/Home/PropertyPopup/' + id;
    var win = window.open(url, 'RealEstatePropertyWindow' + id.replace(/\-/g, ''), "width=" + widthh + ",height=" + heightt + ",top=" + tp + ",left=" + lf + "location=no,status=no,toolbar=no,menubar=no,resizable=no,scrollbars=yes");
    win.focus();

    return false;
}

function openPropertyMap(locale, id) {
    var heightt = 530;
    var widthh = 640;
    tp = Math.ceil((screen.height - heightt) / 2);
    lf = Math.ceil((screen.width - widthh) / 2);

    var url = '/' + locale + '/Home/PropertyMap/' + id;
    var win = window.open(url, 'RealEstatePropertyMapWindow' + id.replace(/\-/g, ''), "width=" + widthh + ",height=" + heightt + ",top=" + tp + ",left=" + lf + "location=no,status=no,toolbar=no,menubar=no,resizable=no,scrollbars=yes");
    win.focus();
}

function previewPhoto(locale, propertyId, photoId) {
    var photoId2 = new String(photoId);
    photoId2 = photoId2.replace(/-/g, '');
    var w = 800;
    var h = 600;
    window.open('/' + locale + '/Home/Photo/' + propertyId + '/' + photoId, 'Photo' + photoId2, 'width=' + w + ',height=' + h + ',resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,left=' + (screen.width - w) / 2 + ',top=' + (screen.height - h) / 2);
}

function searchPropertyByCode(locale) {
    var code = $("#searchCode").val();

    if (code != undefined && code != null && code != '' && !isNaN(Number(code))) {
        window.location.href = '/' + locale + '/Home/Search/' + code;
    }
}

function valueOrDefault(val, def) {
    if (val == null || val == undefined || val == '') {
        return def;
    }
    else {
        return val;
    }
}

function searchProperty(locale) {
    window.location.href = '/' + locale + '/Home/Search' +
                            '/' + valueOrDefault($("#searchOperation").val(), 'any') +
                            '/' + valueOrDefault($("#searchRegion").val(), 'any') +
                            '/' + valueOrDefault($("#searchSubRegion").val(), 'any') +
                            '/' + valueOrDefault($("#searchStreet").val(), 'any') +
                            '/' + valueOrDefault($("#searchPropertyType").val(), 'any') +
                            '/' + valueOrDefault($("#searchPropertySubType").val(), 'any') +
                            '/' + valueOrDefault($("#searchSquareFrom").val(), 'any') +
                            '/' + valueOrDefault($("#searchSquareTo").val(), 'any') +
                            '/' + valueOrDefault($("#searchPriceFrom").val(), 'any') +
                            '/' + valueOrDefault($("#searchPriceTo").val(), 'any') +
                            '/' + valueOrDefault($("#searchRoomCountFrom").val(), 'any') +
                            '/' + valueOrDefault($("#searchRoomCountTo").val(), 'any') +
                            '/' + valueOrDefault($("#searchCurrency").val(), 'any') +
                            '/' + valueOrDefault($("#searchPriceUnit").val(), 'any');
    return false;
}

function calculateCurrency(locale) {
    var amount = $('#currencyConverterAmount').val();
    $.getJSON("/" + locale + "/Home/GetCurrencyRate", { fromCurrencyId: $('#currencyFrom').val(), toCurrencyId: $('#currencyTo').val() }, function(data) {
        $("#currencyConverterResult").val(data * amount);
    });
}

function calculateLoan(locale) {
    $.getJSON("/" + locale + "/Home/GetLoanMonthlyPayment", { loanAmount: $('#loanAmount').val(), rate: $('#loanPercent').val(), period: $('#loanPeriod').val(), periodType: $('#loanPeriodType').val() }, function(data) {
        $("#loanResult").val(data);
    });
}

function reloadSubRegions(locale) {
    $("#searchRegion").attr('disabled', true);
    $("#searchSubRegion").attr('disabled', true);
    $.getJSON("/" + locale + "/Home/GetSubRegions", { regionId: $("#searchRegion").val() }, function(j) {
        var options = '';
        for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].RegionId + '">' + j[i].Title + '</option>';
        }
        $("#searchSubRegion").html(options);
        $("#searchRegion").removeAttr('disabled');
        $("#searchSubRegion").removeAttr('disabled');
    });
}

function reloadPropertySubTypes(locale) {
    $("#searchPropertyType").attr('disabled', true);
    $("#searchPropertySubType").attr('disabled', true);
    $.getJSON("/" + locale + "/Home/GetPropertySubTypes", { propertyTypeId: $("#searchPropertyType").val() }, function(j) {
        var options = '';
        for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].objectSubTypeId + '">' + j[i].name + '</option>';
        }
        $("#searchPropertySubType").html(options);
        $("#searchPropertyType").removeAttr('disabled');
        $("#searchPropertySubType").removeAttr('disabled');
    });
}

// How to find company location
$(document).ready(function() {
    var mapDiv = document.getElementById('howToFindMap');
    if (mapDiv != null) {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(mapDiv);
            var point = new GLatLng(56.9583355, 24.1196794);

            map.setCenter(point, 13);
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            map.setMapType(G_NORMAL_MAP);

            var marker = new GMarker(point);
            var html = "<b>InCity SIA</b><br /><br />Skolas iela 22-1, Rīga, LV-1010<br /><br />Tālrunis: +371 62740392<br />Fakss: +371 62740391";
            GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(html); });

            map.addOverlay(marker);

            marker.openInfoWindowHtml(html);
        }
    }
});

//Arrows navigation
$(document).ready(function() {
    $(window).keydown(function NavigateThrough(event) {
        if (!document.getElementById) return;

        if (window.event) event = window.event;

        if (event.ctrlKey) {
            var link = null;
            var href = null;
            switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
                case 0x25:
                    link = document.getElementById('PrevLink');
                    break;
                case 0x27:
                    link = document.getElementById('NextLink');
                    break;
            }

            if (link && link.href) document.location = link.href;
            if (href) document.location = href;
        }
    });
});