﻿$(function () {

    // Tabs
    $('#tabs').tabs({
        select: function (e, ui) {
            $("[id$=hidSelectedTab]").val(ui.index);
        },
        fx: { opacity: 'toggle', duration: 'fast' }
    });
    var itab = parseInt($("[id$=hidSelectedTab]").val());
    $("#tabs").tabs('select', itab);


    // Bedrooms
    function updateBeds(value) {
        $("#minBeds").text(value == 0 ? "Any" : value);
    }

    $('#slideBeds').slider({
        range: "max",
        min: 0,
        max: 6,
        value: $("[id$=hidBedsMin]").val(),
        slide: function (e, ui) {
            updateBeds(ui.value);
            $("[id$=hidBedsMin]").val(ui.value);
        }
    });
    updateBeds($('#slideBeds').slider("value"));
    
    // Sale Price
    function updateSalePrice() {
        var min = $("#slidePrice").slider("values", 0);
        var max = $("#slidePrice").slider("values", 1);
        if (min == 0 && max == 600) {
            $("#salePriceTxt").text('Any');
        } else if (min == 0) {
            $("#salePriceTxt").text('Up to £' + max + 'K');
        } else if (max == 600) {
            $("#salePriceTxt").text('' + min + 'K and above');
        } else {
            $("#salePriceTxt").text('£' + min + 'K - £' + max + 'K');
        }
    }

    $('#slidePrice').slider({
        range: true,
        min: 0,
        max: 600,
        step: 10,
        values: [parseInt($("[id$=hidPriceMin]").val()), parseInt($("[id$=hidPriceMax]").val())],
        slide: function (e, ui) {
            updateSalePrice();

            $("[id$=hidPriceMax]").val($("#slidePrice").slider("values", 1));
            $("[id$=hidPriceMin]").val($("#slidePrice").slider("values", 0));
        }
    });
    updateSalePrice();

    // Rental Prices
    function updateRentPrice() {
        var min = $("#rentPrice").slider("values", 0);
        var max = $("#rentPrice").slider("values", 1);
        if (min == 200 && max == 1400) {
            $("#rentPriceTxt").text('Any');
        } else if (min == 200) {
            $("#rentPriceTxt").text('Up to £' + max + ' pcm');
        } else if (max == 1400) {
            $("#rentPriceTxt").text('£' + min + ' pcm and above');
        } else {
            $("#rentPriceTxt").text('£' + min + ' pcm - £' + max + ' pcm');
        }
    }

    $('#rentPrice').slider({
        range: true,
        min: 200,
        max: 1400,
        step: 10,
        values: [$("[id$=hidRentMin]").val(), $("[id$=hidRentMax]").val()],
        slide: function (e, ui) {
            updateRentPrice();

            $("[id$=hidRentMax]").val($("#rentPrice").slider("values", 1));
            $("[id$=hidRentMin]").val($("#rentPrice").slider("values", 0));

        }
    });
    updateRentPrice();

    $("INPUT[id$=tbFindInAddress]").watermark("Anywhere", { className: 'Watermark' });

    //Highlight
    function highlight(elemId) {
        var elem = $(elemId);
        setTimeout(function () { elem.css("backgroundColor", "#41924C"); elem.animate({ backgroundColor: "#E51900" }, 500) }, 500);
        setTimeout(function () { elem.animate({ backgroundColor: "#41934D" }, 1500) }, 4200);
        setTimeout(function () { elem.css("backgroundColor", "transparent") }, 7000);
    }
    highlight(".resultCount");

    $('#tabs').fadeOut(10, function () { $('#tabs').css("visibility", "inherit"); $('#tabs').fadeIn('fast'); });

});


