$(document).ready(function(){
	$("#contact-us h2").css("cursor","pointer");
	
	//	Set up events
	$("#contact-us h2").hover(
		function(e){
			if(!$(this).hasClass("open")){
				$("#click-message").show();
			}
		},
		function(e){
			$("#click-message").hide();
		}
	);
	
	$("#contact-us h2").click(function(e){
		e.preventDefault();
		if(!$(this).hasClass("open")){
			$.jqURL.loc('index.php?/about_green_star/contact_us');
			// showContact();
			// $(this).addClass("open").css("cursor","auto");
		}
	});
	
	$("#close-contact-form a").click(function(e){
		e.preventDefault();
		$(this).parent().hide();
		contactReset();
		hideContact(true);
	})
	
	$("#contact-reset").click(function(e){
		e.preventDefault();
		contactReset();
	});
	
	$("#contact-form").ajaxForm(
		{
			target:"#hiddenDiv",
			beforeSubmit:contactProgress,
			success:contactMessage
		});
	

	//	Form functions

	function showContact(){
		$("#contact-us").height("auto");
		$("#contact-form,#contact-more,#close-contact-form").show();
	}

	function hideContact(override){
		if($("#contact-msg").is(":hidden") || override == true){
			// $(".home #contact-us").height("260px");
			$("#contact-us").height("auto");
			$("#contact-form,#contact-more,#close-contact-form").hide();
			$("#contact-us h2").removeClass("open").css("cursor","pointer");
		}
	}

	function contactProgress(){
		$("#contact-submit").attr({disabled:"disabled"});
		$("#contact-spinner").show();
	}
	
	function contactReset(){
		$("#contact-form").resetForm();
		$("#contact-us").height("auto");
		$("#contact-msg").fadeOut("slow",showForm).removeClass();
		//hideContact(true);
	}
	
	function showForm(){
		if($(".home #contact-form").is(":hidden")){
			$(".home #contact-form").fadeIn("fast");
		}
	}

	function contactMessage(rtn){
		$("#contact-msg").show();
		$("#contact-spinner").hide();
		$("#contact-submit").attr({disabled:""});
		if(rtn == "success"){
			$("#contact-form").resetForm();
			// $(".home #contact-us").height("260px");
			$("#contact-us").height("auto");
			$("#contact-more, #contact-form").hide();
			$("#contact-msg").addClass("success").removeClass("error").html("<h3>Thank you for your interest in Green Star Builders. Someone will contact you soon.</h3>");
			//setTimeout(contactReset,4000);
		} else {
			var err = $("#hiddenDiv #content ul").html();
			$("#contact-us").height("auto");
			$("#contact-msg").html("<p><strong>There are problems with your form.</strong></p><ul>"+err+"</ul>").addClass("error");
			$("#hiddenDiv").empty();
		}
	}
});
