$(document).ready(function(){
	
	//make sure we're not on a hf domain
	var hasStartedEntry = false;
	
	if(!$(".propertySearch").is(".homepage"))
	{
		$(".propertySearch select").click(function(){
			hasStartedEntry = true;
		});
		
		$("#search_query_auto_complete").autocomplete("/en/1/propertylocation.obyx");
	
		$("#locations_panel a.expander").click(function(e){
		
			e.stopPropagation();
			e.preventDefault();
		
			if($("#uk_regions_container").is(":hidden")) {
		
				$("#uk_regions_container").slideToggle();
				$('#locations_panel .ukcounties').css('background','url(http://asset1.homesandproperty.co.uk/handp/media/uparrow_13213.gif) right 4px no-repeat')

			}
		});
		//initialise the form to show the selected search type field.
		//firefox likes to retain selected checkboxes between refreshes, hence not just selecting 'buy'
		$("fieldset."+ $("form.propertySearch input[name='searchType']:checked").val()).addClass('activePSearch');
		$("fieldset."+ $("form.propertySearch input[name='searchType']:checked").val() + " select").removeAttr("disabled");
		$("fieldset."+ ($("form.propertySearch input[name='searchType']:checked").val() == "rent" ? "buy":"rent") + " select").attr("disabled","disabled");
		if($("form.propertySearch input[name='searchType']:checked").val() == "rent") {
			$("form.propertySearch").attr("action","http://www.homesandproperty.co.uk/lettings");
		}

		//and now wire up change event
		$("form.propertySearch input[name='searchType']").change(function(){
		
			if($("form.propertySearch input[name='searchType']:checked").val() == "rent"){
				$("fieldset.rent").addClass('activePSearch').show();
				$("fieldset.rent select").removeAttr('disabled');
				$("fieldset.buy").removeClass('activePSearch').hide();
				$("fieldset.buy select").attr('disabled','disabled');
				$("form.propertySearch").attr("action","http://www.homesandproperty.co.uk/lettings");
			}
			else{
				$("fieldset.buy").addClass('activePSearch').show();
				$("fieldset.buy select").removeAttr('disabled');
				$("fieldset.rent").removeClass('activePSearch').hide();
				$("fieldset.rent select").attr('disabled','disabled');
				$("form.propertySearch").attr("action","http://www.homesandproperty.co.uk/sales");
			}	
			
		
		}); 



	$(".propertySearchForm").hover(function(){
	//IF function stops this working on the home page where the panel is open
			if($('div#wrapper').is('.homePageMod')){}else{
			     $('.important_search_controls input.btn').css({'position':'absolute','right':'3px','top':'100px','z-index':'10000'});
			     $("fieldset.activePSearch").show();
			     $("div#locations_panel").show();
			}
	
		},function(){
			if(!hasStartedEntry)
			{
		//IF function stops this working on the home page where the panel is open
				if($('div#wrapper').is('.homePageMod')){}else{
					 $("fieldset.activePSearch").hide();
					 $('.important_search_controls input.btn').css({'position':'absolute','right':'4px','top':'24px','z-index':'10000'});
					 $("div#locations_panel").hide();
				}
	        }
		});
	}
});

