var slideChanges = 0;
var submit = true;
$(function() {
   renderAutoComplete($("#location"), "/index/cities");
   $("form#filters input").change(function() {
       submitFilters();
   });
   if ($("form#filters").size()>0) {
       var locations = $("#location-element label");
       if (locations.size()>5) {
           var visible = true;
           $("#location-element").append("<a href='#' id='showHideCitys'>Toon minder...</a>").find("#showHideCitys").click(function() {
               if (visible) {
                   locations.slice(5, locations.size()).hide();
                   $(this).text("Toon meer...");
                   visible = false;
               }
               else {
                   locations.show();
                   $(this).text("Toon minder...");
                   visible = true;
               }
               return false;
           }).click();
       }
   }
   if ($("#searchForm, #mainForm").length) {
        $("#searchForm input:not(#location), #searchForm select, #mainForm input, #mainForm select").change(function() {
            requestCount();
        });
        requestCount();
   }
    var pathname = $("form", this).attr("action");
    //var pathname = window.location.pathname;
    $("input:radio[name=type]").change(function() {
            //alert(pathname);
            //window.location = (pathname+'?type='+$(this).val());
            if ($(this).val() == "te koop"){
                    window.location = (pathname+'?type=te+huur');
            } else {
                    window.location = (pathname+'?type=te+koop');
            }
    });

   $("form#mainForm, form#searchForm").jqTransform({imgPath:"jqtransformplugin/img/"});
   $("form").append('<div class="clear MB20"></div>');
   $("dd#sliderAmount-element").append('<div class="clear"></div>');

   $("#filters #Slider-slider, #filters #surface_land-slider").parent("dd").addClass("prijsdd");
	
});
function requestCount() {
   
   var vals = $("form").serialize();
   var url = $("form").attr("target")+"?"+vals+"&count=true";

   if ($("#location").hasClass("blur")) {
        url = url.replace(/&location(\=[^&]*)?(?=&|$)|^location(\=[^&]*)?(&|$)/, "");
   }
   
   $.get(url, null, function(data, status) {
        var count = parseInt(data);
        if(count == 1) {
            $("#submit").val("Toon " + data + " resultaat");
        } else {
            $("#submit").val("Toon " + data + " resultaten");
        }
        $("#submit span span").html($("#submit").attr("value"));
        if (count<1) {
            $("#submit").attr("disabled", "disabled").css({"opacity": .5, "cursor":"default"});
        }
        else {
            $("#submit").removeAttr("disabled").css({"opacity": 1, "cursor":"pointer"});
        }
   });
}
function submitFilters () {
    if ($("form#filters").size()>0 && submit) {
        window.location = $("form#filters").attr("action")+"?"+$("form#filters").serialize();
    }
}
function renderAutoComplete($field, data) {
    
    if (typeof $field.autocomplete !== 'undefined') {
    $field.autocomplete({
        minLength:0,
        source:data,
        select:function(event, ui) {
            $('input.hidden', 'form').val(ui.item.id);
            requestCount();
        }
    });

    $field.bind("blur", function(event) {

        var autocomplete = $(this).data("autocomplete");
        var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i");
        var myInput = $(this);

        autocomplete.widget().children(".ui-menu-item").each(function() {
            //Check if each autocomplete item is a case-insensitive match on the input
            var item = $(this).data("item.autocomplete");
            if (matcher.test(item.label || item.value || item)) {
                //There was a match, lets stop checking
                autocomplete.selectedItem = item;
                return;
            }
        });
        //if there was a match trigger the select event on that match
        //I would recommend matching the label to the input in the select event
        if (autocomplete.selectedItem) {
            autocomplete._trigger("select", event, {
                item: autocomplete.selectedItem
            });
        //there was no match, clear the input
        } else {
            $(this).val('');
        }
    });
    }
} 


