$(document).ready(function() {

	$.validator.addMethod("spamBot",		   				   
		function(value, element) {
			if(element.value == '') {
				return true;
			}
			else {
				return false;
			}
		}
	);
	
	// contact pane
	function paneOpen() {	
		
		$.each($.browser, function(i) {
   			if($.browser.msie){
				$("#content").hide();
      			$("#contact").show(); 
   			}else{
				$("#content").fadeOut(600);
     			$("#contact").fadeIn(600);	
   			}
 		});			
	}
						   
	function paneClose() {
		
		$.each($.browser, function(i) {
   			if($.browser.msie){
      			$("#contact").hide(); 
				$("#content").show();
   			}else{
     			$("#contact").fadeOut(600);
				$("#content").fadeIn("slow");
   			}
 		});			
	}	

	$(".contact-link").click(function() {
		
		if($(this).hasClass("active")) {	
			$(".contact-link").removeClass("active");
			paneClose();				
		}
		else {	
			$(".contact-link").addClass("active")
			paneOpen();	
		}
		
		return false;		

	});
	
	// remove broken links
	$('img').error(function () {
		$(this).remove();
	});

	// external sites in a new window
	$("a[rel=external]").attr('target', '_blank');
	
	// textarea width varies from browser to browser
	$.each($.browser, function(i) {
   		if($.browser.msie){
			$("#form_comments").css('width','168px');
   		}
		if ($.browser.webkit) {
			$("#form_comments").css('width','164px');
   		}
		if ($.browser.mozilla) {
			$("#form_comments").css('width','168px');
		}
 	});	
// ---------------------------------------------------
//  Sets focus on first text input
// ---------------------------------------------------
	 $("input.text:first:visible").focus();	  

// ---------------------------------------------------
// The Submit Form Controls including validation
// ---------------------------------------------------
	$("#submit").click(
		function() {		
// 			---------------------------------------------------
// 			Validation method
// 			---------------------------------------------------
			$("#form_contact").validate({
					rules: {
						form_from_firstname: "required",
						form_from_lastname: "required",
						form_from_email: {
							required: true,
							email: true
						},
						form_address: {
							spamBot: true
						}
					},
					messages: {
						form_from_firstname: "Please enter you first name",
						form_from_lastname: "Please enter you last name",
						form_from_email: "Please enter a valid email address",
						form_address: "Please accurately total the numbers to prove you are not a spambot"
					},				
							
							
					submitHandler: function(form) {
// 						---------------------------------------------------
// 						Conditional Statements based on return value of message
// 						--------------------------------------------------- 
	
						var dataString = 
								'form_from_firstname='+$('#form_from_firstname').val()+'&'+
								'form_from_lastname='+$('#form_from_lastname').val()+'&'+
								'form_title='+$('#form_title').val()+'&'+
								'form_company='+$('#form_company').val()+'&'+
								'form_from_email='+$('#form_from_email').val()+'&'+
								'form_telephone='+$('#form_telephone').val()+'&'+								
								'form_comments='+$('#form_comments').val(); 

// 						---------------------------------------------------
// 						This is the actual form post and closing message on screen
// 						--------------------------------------------------- 											
						$.ajax({
							  type: "POST",
							  url: "includes/process-ajax.php",
							  data: dataString,
							  success: function() {
									$('#form_wrap').fadeOut('fast', function() {
									$('#message').fadeIn('fast');
									$('#message').html("<div id='formStatus'><h4>Thank you. Your email has been sent.</h4></div>").fadeIn('fast').fadeOut(5000, function() {
											$("#form_contact").clearForm();
											$("#form_wrap").fadeIn('fast');
									});
								});
							}
						});						 
//					End of Success parameter
					}	
//				End Of Validate
			});
//			return false;	
		}); 

	
// ---------------------------------------------------
// Clear form data jquery plugin (being used after submitted
// and closing message. Before the user clicks the e-mail button again,
// the form data is cleared.
// ---------------------------------------------------
$.fn.clearForm = function() {
	return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form')
		return $(':input',this).clearForm();
	if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = '';
	else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
	else if (tag == 'select')
		this.selectedIndex = -1;
	});
};	
//End of Main Function
	
});


Cufon.replace('h2');
Cufon.replace('#content p');