// Functions

// Clear Search Box
$.fn.search = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

// Subnav Dropdowns
$.fn.subnav = function() {
	return this.hover(function() {
		var thisClass = $(this).attr("class");
		$(this).addClass("active");
		if (thisClass && thisClass != 'home' && thisClass != 'company'/* && thisClass != 'contact'*/) {
			var thisFile = "/ajax/load-menu?menu=" + thisClass; // Change path before live
			$(this).append("<div class=\"nav-sub\"></div>");
			$(".nav-sub").show();
			$(".nav-sub").load(thisFile, startRequest);
		}	
	}, function() {
		$(".nav-sub").remove();
		$(this).removeClass("active");
		/*$(".sub-contact").hide();*/
	});
};


// Page actions
$(window).load(function() {
	$(".nav-main li").subnav();
	$(".search-input").search();
});

function startRequest() {
	var options = { 
		// target:        '.nav-sub .contact .main',   // target element(s) to be updated with server response 
		beforeSubmit:  showRequest,  // pre-submit callback 
		success:       showResponse  // post-submit callback 
	};
	// $('.nav-sub form').ajaxForm(options);
	// $('#contactForm').ajaxForm(options);
	jQuery.validator.messages.required = "";
	$("#contactForm").validate({
		submitHandler: function(form) {
			$(form).ajaxSubmit(options);
		}
	});
	$("#supportForm").validate();
};

function showRequest(formData, jqForm, options) { 
	// alert ('hello');
	// $(".nav-sub .contact .main").empty();
	// $(".nav-sub form").empty();
	return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
	alert (responseText);
	$(".nav-sub form").html('<div class="formResponse">' + responseText + '</div>');
	return true;
}