﻿var show_search_areas = function show_search_areas(selector) {
    $("#areas_div div").hide();



    if (selector == "#van_div") {

        $("#mapImg").attr("src", "/images/template/map.gif");
        $(selector).show();

    }
    else {
        $("#mapImg").attr("src", "/images/template/map_richmond.gif");
        $("#rmd_div").show();
    }
};


function clickArea(areaCode) {
    if (areaCode == "BG") {
        $("#subAreasCheckBoxList_0").attr("checked", true);
    }

    if (areaCode == "MC") {
        $("#subAreasCheckBoxList_14").attr("checked", true);
    }

    if (areaCode == "BR") {
        $("#subAreasCheckBoxList_1").attr("checked", true);
    }

    if (areaCode == "QU") {
        $("#subAreasCheckBoxList_15").attr("checked", true);
    }

    if (areaCode == "BM") {
        $("#subAreasCheckBoxList_2").attr("checked", true);
    }

    if (areaCode == "EC") {
        $("#subAreasCheckBoxList_4").attr("checked", true);
    }

    if (areaCode == "GC") {
        $("#subAreasCheckBoxList_6").attr("checked", true);
    }

    if (areaCode == "GI") {
        $("#subAreasCheckBoxList_7").attr("checked", true);
    }

    if (areaCode == "GR") {
        $("#subAreasCheckBoxList_8").attr("checked", true);
    }

    if (areaCode == "HA") {
        $("#subAreasCheckBoxList_9").attr("checked", true);
    }

    if (areaCode == "IR") {
        $("#subAreasCheckBoxList_10").attr("checked", true);
    }

    if (areaCode == "LA") {
        $("#subAreasCheckBoxList_11").attr("checked", true);
    }

    if (areaCode == "MN") {
        $("#subAreasCheckBoxList_12").attr("checked", true);
    }

    if (areaCode == "ML") {
        $("#subAreasCheckBoxList_13").attr("checked", true);
    }

    if (areaCode == "MC") {
        $("#subAreasCheckBoxList_14").attr("checked", true);
    }

    if (areaCode == "QU") {
        $("#subAreasCheckBoxList_15").attr("checked", true);
    }

    if (areaCode == "RI") {
        $("#subAreasCheckBoxList_16").attr("checked", true);
    }

    if (areaCode == "SA") {
        $("#subAreasCheckBoxList_17").attr("checked", true);
    }

    if (areaCode == "SI") {
        $("#subAreasCheckBoxList_18").attr("checked", true);
    }

    if (areaCode == "SO") {
        $("#subAreasCheckBoxList_20").attr("checked", true);
    }

    if (areaCode == "SV") {
        $("#subAreasCheckBoxList_21").attr("checked", true);
    }

    if (areaCode == "SN") {
        $("#subAreasCheckBoxList_22").attr("checked", true);
    }

    if (areaCode == "SS") {
        $("#subAreasCheckBoxList_23").attr("checked", true);
    }

    if (areaCode == "TN") {
        $("#subAreasCheckBoxList_24").attr("checked", true);
    }

    if (areaCode == "WC") {
        $("#subAreasCheckBoxList_25").attr("checked", true);
    }

    if (areaCode == "WW") {
        $("#subAreasCheckBoxList_26").attr("checked", true);
    }

    if (areaCode == "WO") {
        $("#subAreasCheckBoxList_27").attr("checked", true);
    }
}

        Number.prototype.formatMoney = function(c, d, t){
            var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "",
            i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
            return "$" + s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
            + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
        };

        $(document).ready(function () {
            $(".fancyboxable").fancybox({
                    onStart:show_search_areas
            });

            //This places the fancybox inside of the form so that the server controls work.
            $("#fancybox-tmp, #fancybox-loading, #fancybox-overlay, #fancybox-wrap").appendTo("form");

            var priceValues = [0, 25000, 50000, 100000, 150000, 200000, 250000, 300000, 400000, 500000, 750000, 1000000, 2000000, 3000000, 4000000, 5000000],
                sqftValues = [0, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000, 8000, 12000];

            var min_price_hidden_value = parseFloat($("#min_price_hidden").val()),
                max_price_hidden_value = parseFloat($("#max_price_hidden").val()),
                min_price_index = _.sortedIndex(priceValues, min_price_hidden_value),
                max_price_index = _.sortedIndex(priceValues, max_price_hidden_value);
            if(priceValues[min_price_index] !== min_price_hidden_value) min_price_index -= 1;

            //This code adapted from http://stackoverflow.com/questions/967372/jquery-slider-how-to-make-step-size-change
            $("#price_slider").slider({
                range: true,
                min: 0,
                max: priceValues.length,
                values: [min_price_index, max_price_index],
                create: function(event, ui){
                    $(".start_reader", $(this).parent()).text(priceValues[min_price_index].formatMoney(0));
                    $(".end_reader", $(this).parent()).text(priceValues[max_price_index].formatMoney(0));
                    $(this).siblings("#min_price_hidden").val(priceValues[min_price_index]);
                    $(this).siblings("#max_price_hidden").val(priceValues[max_price_index]);
                },
                slide: function(event, ui){
                    if(ui.value === ui.values[0]){
                        $(".start_reader", $(this).parent()).text(priceValues[ui.value].formatMoney(0));
                        $(this).siblings("#min_price_hidden").val(priceValues[ui.value]);
                    } else {
                        $(".end_reader", $(this).parent()).text((ui.value === priceValues.length - 1)?"Any":(priceValues[ui.value].formatMoney(0)));
                        $(this).siblings("#max_price_hidden").val(priceValues[ui.value]);
                    }
                }
            });

            var min_sqft_hidden_value = parseFloat($("#min_sqft_hidden").val()),
                max_sqft_hidden_value = parseFloat($("#max_sqft_hidden").val()),
                min_sqft_index = _.sortedIndex(sqftValues, min_sqft_hidden_value),
                max_sqft_index = _.sortedIndex(sqftValues, max_sqft_hidden_value);
            if(sqftValues[min_sqft_index] !== min_sqft_hidden_value) min_price_index -= 1;

            $("#sqft_slider").slider({
                range:true,
                min: 0,
                max: sqftValues.length,
                values: [min_sqft_index, max_sqft_index],
                create: function(event, ui){
                    $(".start_reader", $(this).parent()).text(sqftValues[min_sqft_index]);
                    $(".end_reader", $(this).parent()).text(sqftValues[max_sqft_index]);
                    $(this).siblings("#min_sqft_hidden").val(sqftValues[min_sqft_index]);
                    $(this).siblings("#max_sqft_hidden").val(sqftValues[max_sqft_index]);
                },
                slide: function(event, ui){
                    if(ui.value === ui.values[0]){
                        $(".start_reader", $(this).parent()).text(sqftValues[ui.value]);
                        $(this).siblings("#min_sqft_hidden").val(sqftValues[ui.value]);
                    } else {
                        $(".end_reader", $(this).parent()).text(sqftValues[ui.value]);
                        $(this).siblings("#max_sqft_hidden").val(sqftValues[ui.value]);
                    }
                }
            });
        });

