﻿try {
    if (typeof ($) != 'undefined'
        && typeof ($(document)) != 'undefined' 
        && typeof ($(document).ready) != 'undefined')
        $(document).ready(MLX_Mobile_SetWidth);
    else
        setTimeout("MLX_Mobile_SetWidth(true)", 1000);
}
catch (err) {
    setTimeout("MLX_Mobile_SetWidth(true)", 1000);
}

function MLX_Mobile_SetWidth(noJQuery) {
    var url = "/mobile/actionhandler.aspx?" 
        + new Date().getUTCMilliseconds().toString() + "&screenwidth=";
    try{
        if (noJQuery != true) {
            var width = $("body").width() - (MLX_Mobile_HasVScrollBar() ? 0 : MLX_Mobile_ScrollbarWidth());
            url += width ; 
            $.get(url);
        } else {
            var body = document.getElementById("body");
            var width = body.offsetWidth;
            var img = new Image();
            img.src = url + width + "&returnImage=yes";
        }
    }
    catch(err){
        var body = document.getElementById("body");
        var width = body.offsetWidth;
        var img = new Image();
        img.src = url + width + "&returnImage=yes";
    }        
}

function MLX_Mobile_HasVScrollBar() {
    var root = document.compatMode == 'BackCompat' ? document.body : document.documentElement;
    return root.scrollHeight > root.clientHeight;
}

function MLX_Mobile_ScrollbarWidth(noJQuery) {
    if (noJQuery != true) {
        var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;background-color:red;"><div style="height:100px;width:100%;background-color:blue;"></div></div>');
        // Append our div, do our calculation and then remove it 
        $('body').append(div);
        var w1 = $('div', div).innerWidth();
        div.css('overflow-y', 'scroll');
        var w2 = $('div', div).innerWidth();
        if (w2 - w1 == 0 && div[0].clientWidth < div.width()) {
            w2 = div[0].clientWidth;
        }
        $(div).remove();
        return (w1 - w2);
    }
    else {
        return 0;
    }
}

function MLX_Mobile_LoadSearchPrice(minPrice, maxPrice) {
    var _RentalPrices = ["0|0", "100|$100", "200|$200", "300|$300", "400|$400", "500|$500", "600|$600", "700|$700", "800|$800", "900|$900", "1000|$1,000", "1100|$1,100", "1200|$1,200", "1300|$1,300", "1400|$1,400", "1500|$1,500", "1600|$1,600", "1700|$1,700", "1800|$1,800", "1900|$1,900", "2000|$2,000", "2500|$2,500", "3000|$3,000", "4000|$4,000", "5000|$5,000", "7500|$7,500", "10000|$10,000", "15000|$15,000", "20000|$20,000"];
    var _ListingPrices = ["25000|$25,000", "50000|$50,000", "75000|$75,000", "100000|$100,000", "125000|$125,000", "150000|$150,000", "175000|$175,000", "200000|$200,000", "225000|$225,000", "250000|$250,000", "275000|$275,000", "300000|$300,000", "325000|$325,000", "350000|$350,000", "375000|$375,000", "400000|$400,000", "425000|$425,000", "450000|$450,000", "475000|$475,000", "500000|$500,000", "525000|$525,000", "550000|$550,000", "575000|$575,000", "600000|$600,000", "625000|$625,000", "650000|$650,000", "675000|$675,000", "700000|$700,000", "725000|$725,000", "750000|$750,000", "775000|$775,000", "800000|$800,000", "825000|$825,000", "850000|$850,000", "875000|$875,000", "900000|$900,000", "925000|$925,000", "950000|$950,000", "975000|$975,000", "1000000|$1M", "1100000|$1.1M", "1200000|$1.2M", "1300000|$1.3M", "1400000|$1.4M", "1500000|$1.5M", "1600000|$1.6M", "1700000|$1.7M", "1800000|$1.8M", "1900000|$1.9M", "2000000|$2M", "2250000|$2.25M", "2500000|$2.5M", "2750000|$2.75M", "3000000|$3M", "3250000|$3.25M", "3500000|$3.5M", "3750000|$3.75M", "4000000|$4M", "4250000|$4.25M", "4500000|$4.5M", "4750000|$4.75M", "5000000|$5M", "6000000|$6M", "7000000|$7M", "8000000|$8M", "9000000|$9M", "10000000|$10M", "15000000|$15M", "20000000|$20M", "25000000|$25M", "30000000|$30M"];

    var ctlMin = document.getElementById('ctlSearch_ddlMinPrice');
    var ctlMax = document.getElementById('ctlSearch_ddlMaxPrice');
    var ctlPropertyType = document.getElementById('ctlSearch_ddlPropertyType');

    var i;
    for (i = ctlMin.options.length - 1; i >= 0; i--) {
        ctlMin.remove(i);
    }
    for (i = ctlMax.options.length - 1; i >= 0; i--) {
        ctlMax.remove(i);
    }

    var opt = new Option();
    opt.text = "Any";
    opt.value = '';
    ctlMin.options[ctlMin.length] = opt;

    opt = new Option();
    opt.text = "Any";
    opt.value = '';
    ctlMax.options[ctlMax.length] = opt;

    if (ctlPropertyType.options[ctlPropertyType.selectedIndex].value == 'RNT') {
        for (i = 0; i < _RentalPrices.length; i++) {
            var price = _RentalPrices[i].split("|");

            opt = new Option();
            opt.text = price[1];
            opt.value = price[0];
            ctlMin.options[ctlMin.length] = opt;

            opt = new Option();
            opt.text = price[1];
            opt.value = price[0];
            ctlMax.options[ctlMax.length] = opt;
        }
    }
    else {
        for (i = 0; i < _ListingPrices.length; i++) {
            var price = _ListingPrices[i].split("|");

            opt = new Option();
            opt.text = price[1];
            opt.value = price[0];
            ctlMin.options[ctlMin.length] = opt;

            opt = new Option();
            opt.text = price[1];
            opt.value = price[0];
            ctlMax.options[ctlMax.length] = opt;
        }
    }

    var selectedIndex = 0;
    if (minPrice) {
        for (i = 0; i < ctlMin.options.length; i++) {
            if (ctlMin[i].value == minPrice) {
                ctlMin.selectedIndex = selectedIndex;
                break;
            }
            selectedIndex++;
        }
    }

    selectedIndex = 0;
    if (maxPrice) {
        for (i = 0; i < ctlMax.options.length; i++) {
            if (ctlMax[i].value == maxPrice) {
                ctlMax.selectedIndex = selectedIndex;
                break;
            }
            selectedIndex++;
        }
    }

}