// JavaScript Document

var bgPos = {
	            pos : null,
					width : null,
					height : null
            };
				
var responsePanelBG = new Image();

function centerHtml( sel, t )
{
	 var p = $("#response");
	 var w = p.outerWidth();
	 var h = p.outerHeight();
	 
	 sel.html( t );
	 var sw = sel.outerWidth();
	 var sh = sel.outerHeight();
	 var x = Math.round( ( w - sw )/ 2 )+"px";
	 var y = Math.round( ( h - sh )/ 2 ) + "px";
	 sel.css( { "top" : y, "left" : "10px" } ).show(); 
}

function showResponse( responseText )
{
	 $("#response").hide();
	 	 
	 var w = $("#response").outerWidth();
	 var h = $("#response").outerHeight();
	 
	 $("#response").css ( {  position : "absolute",
						 	       top : "40px",
									 left: "80px",	
									 "background" : "transparent url('styles/images/responsepanel.png') top left no-repeat"
								 } );
	
	 centerHtml( $("#response .msg"), responseText );
	 //$("#response .msg").html( responseText );
								 
	 $("#responseBack").show();
	 $("#response").show();	 
	 setTimeout( function() { 
						$("#response").fadeOut( "slow" );
						$("#responseBack").fadeOut( "slow", function() { $("#contactform").clearForm() } );
   				 } , 3000 );
}

function testResponse() {
	showResponse("Thank you for your comment.  <br>We personally read each comment and we'll respond soon!");
}

function submitForm()
{
	$("#contactform").ajaxSubmit(
										  {
											  target : '#response',
											  clearForm : false,
											  success : showResponse
										  }
										);
}
function contactusInit()
{
	 var msgMargin = 20;  // must consider the amount of padding in .msg

	 $("#response").hide().append( "<div class='msg'></div>").after( "<div id='responseBack'></div>");	 
	 responsePanelBG.onload = function() {
		  $("#response").css( { "width" : responsePanelBG.width, 
									   "height" : responsePanelBG.height,
										padding : "0px" 
									 } );
		  $("#response .msg").css( { position : "absolute",
										     top : "0px",											  
										     width : (responsePanelBG.width - msgMargin)+"px",
											  padding: "10px",
											  height: "auto",
											  color : "white",
											  "font-weight": "bold"
											} );
	 }
	 responsePanelBG.src = "styles/images/responsepanel.png";
	 
	 bgPos.pos = $(".dataentryform").position();
	 bgPos.width = $(".dataentryform").outerWidth();
	 bgPos.height = $(".dataentryform").outerHeight();
	 
	 //$("#contactform").click( testResponse );
	 
	 $("#responseBack").css( { position : "absolute", 
									   top : bgPos.pos.top, 
										left : bgPos.pos.left, 
										"width" : bgPos.width+"px",
										"height" : bgPos.height+"px",
										display : "none", 
										"background-color" : "#DDDDDD", 
										"opacity" : "0.2" } );	
	 //$("#contactform").ajaxForm();
	 $("#contactform").validate( 
										  {											  
											  rules: {
													  name: "required",
													  comment: "required",
													  returnemail: {
														 required: true,
														 email: true
													  }
													},
											  messages: {
												  name: "Please enter your name",
												  returnemail : {
													 required: "We need your email address to respond",
													 email: "Your email address must be in the format of name@domain.com"
												  }
												},
											   errorClass: "invalid",
												submitHandler: function(form) {
													submitForm();
													return false;
												}
										  }
										);
}

$contentReady.add( contactusInit );