var defaultLocation = 'Enter Location or PostCode or PropertyID.';
var defaultPostcode = 'Enter Postcode'
var minPriceLimit = 0;
var maxPriceLimit = 2050000;
var priceInterval = 25000;
var currMinPrice = 0;
var currMaxPrice = 2050000;
var currMinBedKey = 0;

var minBeds = new Array();
minBeds[0] = '0';
minBeds[1] = '1';
minBeds[2] = '2';
minBeds[3] = '3';
minBeds[4] = '4';
minBeds[5] = '5';
minBeds[6] = '6';
var currMinBathKey = 0;
var minBaths = new Array();
minBaths[0] = '0';
minBaths[1] = '1';
minBaths[2] = '2';
minBaths[3] = '3';
minBaths[4] = '4';
minBaths[5] = '5';

$(document).ready(function() {
    if($("input:text[id$='szLocation']").val() == defaultLocation) 
    {
	   $("input:text[id$='szLocation']").addClass("example");
    }
        
    $('#ctl00_Content1_searchhome_szLocation').focus(function() {
        if ($(this).val() == defaultLocation) {
            $(this).removeClass('example');
            $(this).val('');
        }
    });

    $('#ctl00_Content1_searchhome_szLocation').blur(function() {
        if ($(this).val() == '') {
            $(this).addClass('example');
            $(this).val(defaultLocation);

        }
    });

    $("input:text[id$='szLocation']").focus(function() {
        if ($(this).val() == defaultLocation) {
            $(this).removeClass('example');
            $(this).val('');
        }

    });

    $("input:text[id$='szLocation']").blur(function() {
        if ($(this).val() == '') {
            $(this).addClass('example');
            $(this).val(defaultLocation);

        }
    });
    
    $('#szLocationID').focus(function() {
        if ($(this).val() == defaultLocation) {
            $(this).removeClass('example');
            $(this).val('');
        }

    });

    $('#szLocationID').blur(function() {
        if ($(this).val() == '') {
            $(this).addClass('example');
            $(this).val(defaultLocation);

        }
    });

    $('#priceSlider').slider({
        handle: ".handle",
        min: minPriceLimit,
        max: maxPriceLimit,
        stepping: priceInterval,
        range: true,
        slide: function(e, ui) {    
            if ($(ui.handle).attr('id') == "ctl00_Content1_buying_pricehandle1") {           
                if ($('#minPrice').val() == ui.value) {
                    return;
                }
                $('#minPrice').val(ui.value);
                if (ui.value == 0) {
                    $('#minPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#minPriceDisplay').text('No Maximum');
                }
                else {
                    $('#minPriceDisplay').html(moneyFormat(ui.value));
                }
            }
            else if ($(ui.handle).attr('id') == "ctl00_Content1_buying_pricehandle2") {
                if ($('#maxPrice').val() == ui.value) {
                    return;
                }
                $('#maxPrice').val(ui.value);
                if (ui.value == 0) {
                    $('#maxPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#maxPriceDisplay').text('No Maximum');
                }
                else {
                    $('#maxPriceDisplay').html(moneyFormat(ui.value));
                }
            }
            else if ($(ui.handle).attr('id') == "ctl00_Content1_searchhome_pricehandle1") {
               if ($('#minPrice').val() == ui.value) {
                    return;
                }
                $('#minPrice').val(ui.value);
                
                if (ui.value == 0) {
                    $('#minPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#minPriceDisplay').text('No Maximum');
                }
                else {
                    $('#minPriceDisplay').html(moneyFormat(ui.value));
                }
            }
            else if ($(ui.handle).attr('id') == "ctl00_Content1_searchhome_pricehandle2") {
                if ($('#maxPrice').val() == ui.value) {
                    return;
                }
                $('#maxPrice').val(ui.value);
                if (ui.value == 0) {
                    $('#maxPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#maxPriceDisplay').text('No Maximum');
                }
                else {
                    $('#maxPriceDisplay').html(moneyFormat(ui.value));
                }
            }
            else if ($(ui.handle).attr('id') == "ctl00_Content1_Propertysearch1_pricehandle1") {
               if ($('#minPrice').val() == ui.value) {
                    return;
                }
                $('#minPrice').val(ui.value);
                
                if (ui.value == 0) {
                    $('#minPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#minPriceDisplay').text('No Maximum');
                }
                else {
                    $('#minPriceDisplay').html(moneyFormat(ui.value));
                }
            }
            else if ($(ui.handle).attr('id') == "ctl00_Content1_Propertysearch1_pricehandle2") {
                if ($('#maxPrice').val() == ui.value) {
                    return;
                }
                $('#maxPrice').val(ui.value);
                if (ui.value == 0) {
                    $('#maxPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#maxPriceDisplay').text('No Maximum');
                }
                else {
                    $('#maxPriceDisplay').html(moneyFormat(ui.value));
                }
            }
            else if ($(ui.handle).attr('id') == "adpricehandle1") {
               if ($('#adminPrice').val() == ui.value) {
                    return;
                }
                $('#adminPrice').val(ui.value);
                
                if (ui.value == 0) {
                    $('#minPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#minPriceDisplay').text('No Maximum');
                }
                else {
                    $('#minPriceDisplay').html(moneyFormat(ui.value));
                    
                }
            }
            else if ($(ui.handle).attr('id') == "adpricehandle2") {
                if ($('#admaxPrice').val() == ui.value) {
                    return;
                }
                $('#admaxPrice').val(ui.value);
                if (ui.value == 0) {
                    $('#maxPriceDisplay').text('No Minimum');
                }
                else if (ui.value >= maxPriceLimit) {
                    $('#maxPriceDisplay').text('No Maximum');
                }
                else {
                    $('#maxPriceDisplay').html(moneyFormat(ui.value));
                }
            }
            
        }
    });  
    
    $('#priceSlider').slider('moveTo', currMinPrice, 0);
    $('#priceSlider').slider('moveTo', currMaxPrice, 1);
    
    
    $('#bedsSlider').slider({
        handle: ".handle",
        min: 0,
        max: minBeds.length - 1,
        stepping: 1,
        startValue: currMinBedKey,
        slide: function(e, ui) {
            //This is an optimization, mainly for IE6 because it tends to fire
            //this function much more often than other browsers. So we only
            //edit the value when it has actually changed.
            if ($('#minBed').val() == minBeds[ui.value]) {
                return;
            }

            $('#minBed').val(minBeds[ui.value]);
            if (ui.value == 0) {
                $('#minBedDisplay').text('Any');
            }
            else {
                $('#minBedDisplay').text(minBeds[ui.value] + '+');
            }
        }
    });

    $('#bathsSlider').slider({
        handle: ".handle",
        min: 0,
        orientation: 'vertical',
        max: minBaths.length - 1,
        stepping: 1,
        startValue: currMinBathKey,
        slide: function(e, ui) {
            //This is an optimization, mainly for IE6 because it tends to fire
            //this function much more often than other browsers. So we only
            //edit the value when it has actually changed.
           
            if ($('#minBath').val() == minBaths[ui.value]) {
                return;
            }

            $('#minBath').val(minBaths[ui.value]);
            if (ui.value == 0) {
                $('#minBathDisplay').text('Any');
            }
            else {
                $('#minBathDisplay').text(minBaths[ui.value] + '+');
            }
        }
         
    });
    //Set the min and max values of sliders
    setMinMax();

    if ($('#moreOptionsLink').hasClass("moreOptionsOn")) {       
    }

    $('#propSelectAll').click(function() {
        $(".propTypeList input").each(function() {
            this.checked = "checked";
        });
        return false;
    });

    $('#propDeselectAll').click(function() {
        $(".propTypeList input").each(function() {
            this.checked = "";
        });
        return false;
    });

    $('#moreOptionsLink').click(function() {
        $('#moreOptionsLink').toggleClass("moreOptions");
        $('#moreOptionsLink').toggleClass("moreOptionsOn");
        //Firefox displays the amenities scroll bar immediately, so we hide the 
        //amenities box and bring it back when animation is done
        $('#searchOptions').animate({ height: "toggle" }, 2000, "easeOutExpo");
    });

    $('.ssLink').click(function() {
        var searchQueryPre = $(this).attr('href');
        var searchQuery = $('#searchToolsForm').serialize();
        $(this).attr('href', searchQueryPre + searchQuery);
        return true;
    });

    $('.reset').click(function() {
        $("input:text[id$='szLocation']").val('');
        $("input:text[id$='szLocation']").focus();
        $('#priceSlider').slider('moveTo', minPriceLimit, 0);
        $('#priceSlider').slider('moveTo', maxPriceLimit + priceInterval, 1);
        $('#bedsSlider').slider('moveTo', 0);
        $('#bathsSlider').slider('moveTo', 0);        
        $("input[name='iHasPhoto']").each(function() { $(this).attr('checked', '') });
        $("input[name='rgPropTypes[]']").each(function() { $(this).attr('checked', 'checked') });
        $("input[name='hasOpenHouse']").each(function() { $(this).attr('checked', '') });
        $("input[name='iAmenityID[]']").each(function() { $(this).attr('checked', '') });
        $("input[name='iRadius']").each(function() { $(this).val(['20']) });
    });

});
function setMinMax(Id){}
function applyAdvancedSliders() {
  

  
}

function alterQueryString(varArray) {
    $('.searchQuery').each(function() {
        queryString = $(this).attr('href');
        for (i in varArray) {
            varName = i;
            varValue = varArray[i];
            pattern = new RegExp(varName + "=.*?(?=&|$)");
            if (queryString.search(pattern) == -1) {
                queryString = queryString + "&" + varName + "=" + varValue;
            }
            else {
                queryString = queryString.replace(pattern, varName + "=" + varValue);
            }
        }
        $(this).attr('href', queryString);
    });
}

function moneyFormat(num) {
    var numStr = "" + num;
    var dec = numStr.indexOf(".");
    var end = ((dec > -1) ? "" + numStr.substring(dec, numStr.length) : "");
    numStr = "" + parseInt(numStr);

    var count = 0;
    var temp1 = "";
    var temp2 = "";
    for (var k = numStr.length - 1; k >= 0; k--) {
        var oneChar = numStr.charAt(k);
        if (count == 3) {
            temp1 += ",";
            temp1 += oneChar;
            count = 1;
            continue
        }
        else {
            temp1 += oneChar;
            count++;
        }
    }

    for (var k = temp1.length - 1; k >= 0; k--) {
        var oneChar = temp1.charAt(k);
        temp2 += oneChar;
    }

    temp2 = '&pound;' + temp2 + end;
    return temp2;
}