$(document).ready(function(){
	
	$('#alert').show().animate({ opacity: 0.2 }, 500).animate({ opacity: 1.0 }, 1000);
	
	$("input[name^='clear_'], .header-search").focus(function(){
		if($(this).val()==$(this).attr('title')){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val()==''){
			$(this).val($(this).attr('title'));
		}
	});
	
	$('.advanced-button').click(function(e){
		e.preventDefault();
		$('.advanced-options').slideToggle();
	});
	
	//vehicle select form
	$("#vs_make").change(function(){
		$("#vs_model").html("<option value=''></option>");	
		$.post("/ajax/vs.php", {make: $(this).val(), action: "model"}, function(j){
			$("#vs_model").html(j).focus();
		});
	});

	$('#vs_model').change(function(){
		$("#vs_location").html("<option value=''>All Locations</option>");		
		$.post("/ajax/vs.php", {make : $('#vs_make').val(), model : $(this).val(), action: "location"}, function(j){
			$("#vs_location").html(j);
		});
	});
		
	//required form
	$('.check-form').live("submit", function(){
		var error = true;
		var field = null;
		var i = $(this).attr('id');
		$('#'+i+' .required, #'+i+' .email').each(function(i){
			if($(this).val()==''){
				$(this).css("border", "1px solid #cc0000");
				$(this).next(".required-error").remove();
				$(this).after("<div class='required-error'></div>");
				if(field==null){ field = $(this) };
				error = false;
			}else{
				$(this).css("border", "1px solid #999999");
				$(this).next(".required-error").remove();
			}
		});
		
		if(!error){ alert("Please complete all the required fields"); field.focus(); }
		return error;
	});		
	
	//directions popout
	$('.map-popout').click(function(e){
		e.preventDefault();
		if($('#map_popout').length==0){
			$(this).before('<div id="map_popout"><div class="top">&nbsp;</div><div class="bg"><iframe frameborder="0" width="310" height="250" id="popout_iframe" src="/iframe'+ $(this).attr('href') +'"></iframe></div><div class="bottom">&nbsp;</div></div>');
		}else{
			$('#map_popout').remove();
		}
	});

	$('#options_order').change(function(){
		document.location = '/finder_options.php?order='+$(this).val();
	});
	$('#options_show').change(function(){
		document.location = '/finder_options.php?display='+$(this).val();
	});	
});
