function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        if (vars[key]!=null) {
          vars[key] = vars[key] + ',' + value;
        } else {
          vars[key] = value;	
        }
        
    });
    return vars;
}

function searchListingsMini(){
        document.forms['searchListingsMini'].submit();
    }
    
function makeSublist(parent,child,isSubselectOptional,childVal)
{
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));

	var parentValue = $('#'+parent).attr('value');
	$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

	childVal = (typeof childVal == "undefined")? "" : childVal ;
	$("#"+child).val(childVal).attr('selected','selected');

	$('#'+parent).change(function(){
		var parentValue = $('#'+parent).attr('value');
		$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
		if(isSubselectOptional) $('#'+child).prepend("<option value='' selected='selected'> -- Select -- </option>");

		$('#'+child).trigger("change");
		$('#'+child).focus();
	});
}

$(document).ready(function()
{
  $("#amenities").hide();
  $("#bedsbathsspacer").hide();

  $("#bedclose").click(function() {
    $("#beds").click();
  });

  $("#bathclose").click(function() {
    $("#baths").click();
  });

  $("#amenities_switch").click(function() {
    if($("#amenities_switch").html() == "Search By Amenities") {
      $("#amenities_switch").html("Hide Amenities");
      $("#amenities_switch_icon").attr("src", "/themes/pangeare-2/images/amenities.minus.png");

      if($("#bedoptions").is(":visible")) {
        $("#bedoptions").hide();
        $("#bedsbathsspacer").hide();
       }

      if($("#bathoptions").is(":visible")) {
        $("#bathoptions").hide();
        $("#bedsbathsspacer").hide();
      }

      $("#amenities").slideToggle();
    } else {
      $("#amenities_switch").html("Search By Amenities");
      $("#amenities_switch_icon").attr("src", "/themes/pangeare-2/images/amenities.plus.png");
      $("#amenities").slideToggle();
    }
  });    

  $("#amenities_switch_icon").click(function() {
    if($("#amenities_switch").html() == "Search By Amenities") {
      $("#amenities_switch").html("Hide Amenities");
      $("#amenities_switch_icon").attr("src", "/themes/pangeare-2/images/amenities.minus.png");

      if($("#bedoptions").is(":visible")) {
        $("#bedoptions").hide();
        $("#bedsbathsspacer").hide();
       }

      if($("#bathoptions").is(":visible")) {
        $("#bathoptions").hide();
        $("#bedsbathsspacer").hide();
      }

      $("#amenities").slideToggle();
    } else {
      $("#amenities_switch").html("Search By Amenities");
      $("#amenities_switch_icon").attr("src", "/themes/pangeare-2/images/amenities.plus.png");
      $("#amenities").slideToggle();
    }
  });

	// Setup Beds and Baths
  $("#beds").click(function() {
    if($("#bedoptions").is(":visible")) {
      $("#bedoptions").hide();
      if(!$("#bathoptions").is(":visible")) {
        $("#bedsbathsspacer").slideToggle();
        }

      var allVals = [];
     $('#bedoptions :checked').each(function() {
       allVals.push($(this).attr("text"));
     });
      $('#nbrofbeds').text(allVals.join(','));
      if (allVals.length == 0) { $('#nbrofbeds').text("Any")} 

    } else {
      if($("#amenities").is(":visible")) {
        $("#amenities").hide();
        $("#amenities_switch").html("Search By Amenities");
        $("#amenities_switch_icon").attr("src", "/themes/pangeare-2/images/amenities.plus.png");
      }
      if(!$("#bathoptions").is(":visible")) {
        $("#bedsbathsspacer").slideToggle();
      }
      $("#bedoptions").show();
    }
  });

  $("#baths").click(function() {
    if($("#bathoptions").is(":visible")) {
      $("#bathoptions").hide();
      if(!$("#bedoptions").is(":visible")) {
        $("#bedsbathsspacer").slideToggle();
        }

      var allVals = [];
     $('#bathoptions :checked').each(function() {
       allVals.push($(this).attr("text"));
     });
      $('#nbrofbaths').text(allVals.join(','));
      if (allVals.length == 0) { $('#nbrofbaths').text("Any")} 

    } else {
      if($("#amenities").is(":visible")) {
        $("#amenities").hide();
        $("#amenities_switch").html("Search By Amenities");
        $("#amenities_switch_icon").attr("src", "/themes/pangeare-2/images/amenities.plus.png");
      }
      if(!$("#bedoptions").is(":visible")) {
        $("#bedsbathsspacer").slideToggle();
        }
      $("#bathoptions").show();
    }
  });
	

	// Link State to Properties and Neighborhoods
	makeSublist('state','property', false, '');	
	makeSublist('state','neighborhood', false, '1');	

        state_request =  "";
    	property_request =  "";
        neighborhood_request =  "";
        rentmax_request =  "";

	// Populate form from request params
    if (getUrlVars()['state']!=null) { 
    	$("#state").val(getUrlVars()['state']);
    	$('#state').change();
    	state_request =  getUrlVars()['state'];
    	}

    if (getUrlVars()['neighborhood']!=null) { 
    	$("#neighborhood").val(unescape((getUrlVars()['neighborhood']+"").replace(/\+/g, " "))); 
	    neighborhood_request = unescape((getUrlVars()['neighborhood']+"").replace(/\+/g, " "));
    	}

    if (getUrlVars()['property']!=null) { 
    	$("#property").val(unescape((getUrlVars()['property']+"").replace(/\+/g, " "))); 
	    property_request = unescape((getUrlVars()['property']+"").replace(/\+/g, " "));
    }

    if (getUrlVars()['rentmax']!=null) { 
    	$("#rentmax").val(getUrlVars()['rentmax']);
    	rentmax_request =  getUrlVars()['rentmax'];
    }

    if (getUrlVars()['select_beds']!=null) { 
    	$('#bedoptions input[type="checkbox"]').each(function() {
          if (jQuery.inArray($(this).val(), getUrlVars()['select_beds'].split(',')) > -1) { 
            $(this).attr("checked", "true");
          }
        });
    	$('#nbrofbeds').text(getUrlVars()['select_beds'])
    }
    if (getUrlVars()['select_baths']!=null) { 
    	$('#bathoptions input[type="checkbox"]').each(function() {
          if (jQuery.inArray($(this).val(), getUrlVars()['select_baths'].split(',')) > -1) { 
            $(this).attr("checked", "true");
          }
        });
    	$('#nbrofbaths').text(getUrlVars()['select_baths'])
    }
    if (getUrlVars()['select_amenities']!=null) { 
    	$('#amenities ul li input[type="checkbox"]').each(function() {
          if (jQuery.inArray($(this).val(), getUrlVars()['select_amenities'].split(',')) > -1) { 
            $(this).attr("checked", "true");
          }
        });
    }

});



