﻿(function() {
    YAHOO.namespace('example');

    var Dom = YAHOO.util.Dom;

    // Slider has a range of 200 pixels
    var range = 100;

    // No ticks for this example
    var tickSize = 0;

    // We'll set a minimum distance the thumbs can be from one another
    var minThumbDistance = -8;
    var initMin = 0;

    if (getURLParam('minpricev') != '') {
        initMin = getURLParam('minpricev');
    } // Initial values for the thumbs
    var initMax = 100;
    
    if (getURLParam('maxpricev') != '') {
        initMax = getURLParam('maxpricev');
    } // Initial values for the thumbs
    var initValues = [initMin, initMax];

    // Conversion factor from 0-200 pixels to 100-1000
    // Note 20 pixels are subtracted from the range to account for the
    // thumb values calculated from their center point (10 pixels from
    // the center of the left thumb + 10 pixels from the center of the
    // right thumb)
    var cf = 900 / (range - 20);

    // Set up a function to convert the min and max values into something useful
    var convert = function(val) {
        return Math.round(val * cf + 100);
    };

    // Slider set up is done when the DOM is ready
    YAHOO.util.Event.onDOMReady(function() {
        var demo_bg = Dom.get("demo_bg"),
            info = Dom.get("demo_info"),
            from = Dom.get("demo_from"),
            to = Dom.get("demo_to");

        // Create the DualSlider
        var slider = YAHOO.widget.Slider.getHorizDualSlider(demo_bg,
            "demo_min_thumb", "demo_max_thumb",
            range, tickSize, initValues);

        slider.minRange = minThumbDistance;

        // Custom function to update the text fields, the converted value
        // report and the slider's title attribute
        var updateUI = function() {
            from.value = slider.minVal;
            to.value = slider.maxVal;

            // Update the converted values and the slider's title.
            // Account for the thumb width offsetting the value range by
            // subtracting the thumb width from the max value.
            var min = convert(slider.minVal),
                max = convert(slider.maxVal - 20);

            var maht;
            var miht;

            if (max == 1000) {
                maht = "<i><strong>No Limit</strong><input type=\"hidden\" name=\"priceto\" value=\"Any\"><input type=\"hidden\" name=\"pricetov\" value=\"" + to.value + "\">";
            }
            else {
                if (max < min - 55) {
                    max = min - 45;
                }
                maht = "<i><strong>$" + max + ",000</strong><input type=\"hidden\" name=\"priceto\" value=\"" + max + "000\"><input type=\"hidden\" name=\"pricetov\" value=\"" + to.value + "\">";
            }
            if (min == 100) {
                miht = "<i><strong>$0</strong><input type=\"hidden\" name=\"pricefrom\" value=\"0\"><input type=\"hidden\" name=\"pricefromv\" value=\"0\"> to ";
            }
            else {
                miht = "<i><strong>$" + (min - 55) + ",000</strong> to <input type=\"hidden\" name=\"pricefrom\" value=\"" + (min - 55) + "000\"><input type=\"hidden\" name=\"pricefromv\" value=\"" + from.value + "\"> ";
            }
            info.innerHTML = miht + maht;
            //demo_bg.title = "Price Range";

        };

        // Subscribe to the dual thumb slider's change and ready events to
        // report the state.
        slider.subscribe('ready', updateUI);
        slider.subscribe('change', updateUI);

        // Wire up the button to update the slider
        YAHOO.util.Event.on('demo_btn', 'click', function() {
            // Get the int values from the inputs
            var min = Math.abs(parseInt(from.value, 10) | 0),
                max = Math.abs(parseInt(to.value, 10) | 0);

            if (min > max) {
                var hold = min;
                min = max;
                max = hold;
            }

            // Verify the values are in range
            min = Math.min(min, range - 30);
            max = Math.max(Math.min(max, range), min + 20 + minThumbDistance);

            // Set the new values on the slider
            slider.setValues(min, max);
        });
        // Attach the slider to the YAHOO.example namespace for public probing

        YAHOO.example.slider = slider;

    });
})();

function getURLParam(strParamName) {
    var strReturn = "";
    var strHref = window.location.href;
    if (strHref.indexOf("?") > -1) {
        var strQueryString = strHref.substr(strHref.indexOf("?"));
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++) {
            if (
aQueryString[iParam].indexOf(strParamName + "=") > -1) {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}


function setFormParams() {

    if (getURLParam("minpricev") != '') {
        //remove all variables
        document.aspnetForm.studio.checked = false;
        document.aspnetForm.onebed.checked = false;
        document.aspnetForm.twobed.checked = false;
        document.aspnetForm.threebed.checked = false;
        fourbed = document.aspnetForm.fourbed.checked = false;
        document.aspnetForm.foreclosure.checked = false;
        document.aspnetForm.shortsale.checked = false;
        document.aspnetForm.any.checked = false;
        document.aspnetForm.myrtlebeach.checked = false;
        document.aspnetForm.northmyrtlebeach.checked = false;
        document.aspnetForm.alllocations.checked = false;
        document.aspnetForm.radio1[0].checked = false;
        document.aspnetForm.radio1[1].checked = false;
        document.aspnetForm.radio1[2].checked = false;

        if (getURLParam("studio") != 'true' && getURLParam("onebed") != 'true' && getURLParam("twobed") != 'true' && getURLParam("threebed") != 'true' && getURLParam("fourbed") != 'true') {
            document.aspnetForm.any.checked = true;
        }

    if (getURLParam("studio") == 'true') {
        document.aspnetForm.studio.checked = true;
    }

    if (getURLParam("onebed") == 'true') {
        document.aspnetForm.onebed.checked = true;
    }

    if (getURLParam("twobed") == 'true') {
        document.aspnetForm.twobed.checked = true;
    }

    if (getURLParam("threebed") == 'true') {
        document.aspnetForm.threebed.checked = true;
    }

    if (getURLParam("fourbed") == 'true') {
        document.aspnetForm.fourbed.checked = true;
    }
    if (getURLParam("nmb") == 'true') {
        document.aspnetForm.northmyrtlebeach.checked = true;
    }

    if (getURLParam("mb") == 'true') {
        document.aspnetForm.myrtlebeach.checked = true;
    }
    if (getURLParam("nmb") != 'true' && getURLParam("mb") != 'true') {
        document.aspnetForm.alllocations.checked = true;
    }
    if (getURLParam("foreclosure") == 'true') {
        document.aspnetForm.foreclosure.checked = true;
    }
    if (getURLParam("shortsale") == 'true') {
        document.aspnetForm.shortsale.checked = true;
    }
    if (getURLParam("oceanfront") == 'only') {
        document.aspnetForm.radio1[0].checked = true;
    }

    if (getURLParam("oceanfront") == 'exclude') {
        document.aspnetForm.radio1[1].checked = true;
    }

    if (getURLParam("oceanfront") == 'all') {
        document.aspnetForm.radio1[2].checked = true;
    }
    //getListingCount();
}

}
