$(document).ready(

    function() 
    {
		$("#advancedSearchButton").click(
            function()
            {
                $("#advancedSearch").toggle();
                return false;
            }
        );
        
        $('#PropertyFlatCostMin, #PropertyFlatCostMax').selectToUISlider({
            labels: 10,
            labelSrc: "text"
        });
        $('#PropertyFlatCostMax').next().css('padding', '0px');
        $('#PropertyFlatCostMax').next('.ui-widget').css('font-size', '0.8em');
        $('#PropertyFlatCostMax').next().children('div.ui-slider-range.ui-widget-header').css('height', 'auto');
        $('#PropertyFlatCostMax').next().css('margin-top', '0.8em');		
		
		$('#PropertyTypeMin, #PropertyTypeMax').selectToUISlider({
            labels: 9
        });
		$('#PropertyTypeMax').next().css('padding', '0px');
        $('#PropertyTypeMax').next('.ui-widget').css('font-size', '0.8em');
        $('#PropertyTypeMax').next().children('div.ui-slider-range.ui-widget-header').css('height', 'auto');
        $('#PropertyTypeMax').next().css('margin-top', '0.8em');
        
        $('#PropertyFloorMin, #PropertyFloorMax').selectToUISlider({
            labels: 6,
            labelSrc: "text"
        });
		$('#PropertyFloorMax').next().css('padding', '0px');
        $('#PropertyFloorMax').next('.ui-widget').css('font-size', '0.8em');
        $('#PropertyFloorMax').next().children('div.ui-slider-range.ui-widget-header').css('height', 'auto');
        $('#PropertyFloorMax').next().css('margin-top', '0.8em');
        
		$('#PropertyBuiltupAreaMin, #PropertyBuiltupAreaMax').selectToUISlider({
            labels: 6
        });
		$('#PropertyBuiltupAreaMax').next().css('padding', '0px');
        $('#PropertyBuiltupAreaMax').next('.ui-widget').css('font-size', '0.8em');
        $('#PropertyBuiltupAreaMax').next().children('div.ui-slider-range.ui-widget-header').css('height', 'auto');
        $('#PropertyBuiltupAreaMax').next().css('margin-top', '0.8em');        
        
        $('.ui-slider-label').css('position', 'static');
        $('.ui-slider-label:visible').css('display', 'inline');
        
        function getLocalities()
        {
            $("#PropertyLocalityId").parent().find('img.loading').show();
            $("#PropertyLocalityId").hide();
            
            $.post(
                $("#logo a").attr("href") + "localities/index.json",
                {
                    "data[Locality][id]": $("#PropertyCity").find(":selected").val()
                },
                function (data)
                {
                    if (!data.error)
                    {
                        $("#PropertyLocalityId").html("");
                        $.each(
                            data.data,
                            function(index, value)
                            {
                                $("#PropertyLocalityId").append(
                                    '<option value="' + index + '">' + value + '</option>'
                                );
                            }
                        );
                        
                        $("#PropertyLocalityId").parent().find('img.loading').hide();
                        $("#PropertyLocalityId").show();
                    }
                },
                "json"
            );
        }
        
        $('#PropertyCity').change(
            function ()
            {
                getLocalities();
            }
        );
        
        getLocalities();
        
        
        function slug(str)
        {
            str = str.replace(/ /g, '-');
            str = str.replace(/[^0-9a-zA-Z\-.]/g, '');
            return str;
        }
        
        $("#PropertySearchForm").submit(
            function()
            {
                var url = $(this).attr("action") + "/";
                
                //url format
                //Navi-Mumbai_3/New-Resale-Flats/Grand+Square/1-RK-2.5-BHK/2200000-3200000_rates-1/0-30_f/300-3000_a/13-14-15-16/Page-2
                
                url += slug($("#PropertyCity :selected").text()) + "_" + $("#PropertyCity :selected").val() + "/";

                var skip = true;
                if ($("#PropertyTxnTypeNew:checked").length)
                {
                    url += "New-";
                    skip = false;
                }
                if ($("#PropertyTxnTypeResale:checked").length)
                {
                    url += "Resale-";
                    skip = false;
                }
                if (!skip)
                {
                    url += "Flats/";
                }
                
                if ($("#PropertyName").val() != "")
                {
                    url += slug($("#PropertyName").val()) + "/";
                }
                
                //first replace 1/2 with .5
                bhk = $("#PropertyTypeMin :selected").val() + " " + $("#PropertyTypeMax :selected").val();
                bhk = bhk.replace(/ 1\/2/g, '.5');                
                url += slug(bhk) + "/";
                
                url += slug($("#PropertyFlatCostMin :selected").val() + " " + $("#PropertyFlatCostMax :selected").val()) + "_Rates-" + ($("#PropertyAllInclusivePrice:checked").length ? "1" : "0") + "/";

                if (
                    (
                        $("#PropertyFloorMin :selected").val() 
                        != $("#PropertyFloorMin option[selected='selected']").val()
                    )
                    ||
                    (
                        $("#PropertyFloorMax :selected").val() 
                        != $("#PropertyFloorMax option[selected='selected']").val()
                    )
                )
                {
                    //send in request since changed
                    url += slug($("#PropertyFloorMin :selected").val() + " " + $("#PropertyFloorMax :selected").val()) + "_f/";
                }
                
                if (
                    (
                        $("#PropertyBuiltupAreaMin :selected").val() 
                        != $("#PropertyBuiltupAreaMin option[selected='selected']").val()
                    )
                    ||
                    (
                        $("#PropertyBuiltupAreaMax :selected").val() 
                        != $("#PropertyBuiltupAreaMax option[selected='selected']").val()
                    )
                )
                {
                    //send in request since changed
                    url += slug($("#PropertyBuiltupAreaMin :selected").val() + " " + $("#PropertyBuiltupAreaMax :selected").val()) + "_a/";
                }
                
                //ad locality ids
                if ($("#PropertyLocalityId :selected").length)
                {
                    var locality_ids = "";
                    $("#PropertyLocalityId :selected").each(
                        function(index)
                        {
                            if (index != 0)
                            {
                                locality_ids += " ";
                            }
                            
                            locality_ids += $(this).val();
                        }
                    );
                    
                    url += slug(locality_ids);
                }
                
                window.location = url;
                
                return false;
            }
        );
    }
);
