(function($) {
  $(function () {
    // Property Type
    //     var property_type = $('#property_style');
    //     $.each(jQuery.fn.displetform.global_config.property_types, function (k,v) {
    //  $('<option value="' + v + '">' + k + '</option>')
    //    .appendTo(property_type);
    // });
    // Area
    var area = $('#area');
    if (jQuery.fn.displetform.global_config.areas != undefined
			&& jQuery.fn.displetform.global_config.areas != null
			&& jQuery.fn.displetform.global_config.areas.length != 0
			&& jQuery.fn.displetform.global_config.areas != '') {

			$.each(jQuery.fn.displetform.global_config.areas, function (k,v) {
				$('<option value="' + v + '">' + k + '</option>').appendTo(area);
			});
		}
    
    // School District
    // var school_district = $('#school_district');
    //   $.each(jQuery.fn.displetform.global_config.districts, function () {
    //   $('<option value="' + this + '">' + this + '</option>').appendTo(school_district);
    // });
    
    // Min Price
    var minListPrice = $('#minListPrice');
    for (var price = jQuery.fn.displetform.global_config.options.priceMin;
    price <= jQuery.fn.displetform.global_config.options.priceMax;
    price += jQuery.fn.displetform.global_config.options.priceIncrement) {
      $('<option value="' + (price / 1000) + '">' + (price / 1000) + ',000</option>').appendTo(minListPrice);
    }
    
    // Max Price
    var maxListPrice = $('#maxListPrice');
    for (var price = jQuery.fn.displetform.global_config.options.priceMin;
    price <= jQuery.fn.displetform.global_config.options.priceMax;
    price += jQuery.fn.displetform.global_config.options.priceIncrement) {
      $('<option value="' + (price / 1000) + '">' + (price / 1000) + ',000</option>').appendTo(maxListPrice);
    }
    
    // Min Bedrooms
    var minBedrooms = $('#minBedrooms');
    for (var beds = jQuery.fn.displetform.global_config.options.bedsMin;
    beds <= jQuery.fn.displetform.global_config.options.bedsMax;
    beds++) {
      $('<option value="' + beds + '">' + beds + '+</option>').appendTo(minBedrooms);
    }
    
    // Min Square Feet
    var minSquareFeet = $('#minSquareFeet');
    $.each(jQuery.fn.displetform.global_config.minsq, function () {
      $('<option value="' + this + '">' + this + '</option>').appendTo(minSquareFeet);
    });
    
    // Min Bathrooms
    var minBathrooms = $('#minBathrooms');
    for (var baths = jQuery.fn.displetform.global_config.options.bathsMin;
    baths <= jQuery.fn.displetform.global_config.options.bathsMax;
    baths++) {
      $('<option value="' + baths + '">' + baths + '+</option>').appendTo(minBathrooms);
    }
    
			
    // Form Submit
    $('#cta_map_properties a').click(function() {
      var url = (function (url) {
        if (url.lastIndexOf('/') != url.length) {
          url += '/';
        }
        url += '#';
        var property_values = "";
        $.each($('.property_style:checked'), function() {
          property_values += $(this).val() + ",";
        });
        if (property_values != "") {
          url += "property_style[]=" + property_values + "/";
        }
        return url;
      })("http://search.kinanbeck.com");      
      
      $.each($('.displet'), function () {
        if (this.value != 'none' && this.value != '' && this.value != 'Subdivision' && this.value != 'Zip Code' && this.value != 'Keyword') {
        url += this.name + '=' + this.value + '/';
      }
      });

      window.location = url;
      return false;
    });
    
    $('#cta_property_list a').click(function() {
      var url = (function (url) {
        if (url.lastIndexOf('/') != url.length) {
          url += '/';
        }
        url += '?';
        var property_values = "";
        $.each($('.property_style:checked'), function() {
          property_values += $(this).val() + ",";
        });
        if (property_values != "") {
          url += "property_style[]=" + property_values + "&";
        }
        return url;
      })("http://search.kinanbeck.com/residentials/results");      
      
      $.each($('.displet'), function () {
        if (this.value != 'none' && this.value != '' && this.value != 'Subdivision' && this.value != 'Zip Code' && this.value != 'Keyword') {
        url += this.name + '=' + this.value + '&';
      }
      });

      window.location = url;
      return false;
    });
  });
})(jQuery);