(function($) {
  $(function () {
    
    // 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);
			});
		}
    
    // 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);
    }
    
    // Max Age
    // var maxAge = $('#maxAge');
    // for (var age = jQuery.fn.displetform.global_config.options.ageMin;
    // age <= jQuery.fn.displetform.global_config.options.ageMax;
    // age++) {
    //   $('<option value="' + age + '">' + age + '</option>').appendTo(maxAge);
    // }
    
    // Neighborhoods
    var keyword = $('#keyword');
      $.each(jQuery.fn.displetform.global_config.neighborhoods, function () {
      $('<option value="' + this + '">' + this + '</option>').appendTo(keyword);
    });
    
    // Condo Names
    var subdivision = $('#subdivision');
    $.each(jQuery.fn.displetform.global_config.subdivisions, function (k,v) {
      $('<option value="' + v + '">' + k + '</option>').appendTo(subdivision);
    });
			
    // Form Submit
    $('#cta_map_properties a, #quick_search_cta a, #quick_search_thin_ctas .map_search').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.onesourcemetro.com");      
      
      $.each($('.displet'), function () {
        if (this.value != 'none' && this.value != '' && this.value != this.defaultValue) {
        url += this.name + '=' + this.value + '/';
      }
      });

      window.location = url;
      return false;
    });
    
    $('#cta_view_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.onesourcemetro.com/residentials/results");      
      
      $.each($('.displet'), function () {
        if (this.value != 'none' && this.value != '' && this.value != this.defaultValue) {
        url += this.name + '=' + this.value + '&';
      }
      });

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