var commentform_submitError = "Fail to submit data, please try again.";
var commentform_invalidEmail = "Invalid Email Address.";
var commentform_requireEmail = "Whoops. We'll need your email.";
var commentform_requireMessage = "Whoops. We'll need your message.";
var commentform_success = "Thanks! We'll get you back to you pronto.";

var commentform_show_speed = 1000;
var commentform_show_easing = "easeOutExpo";
var commentform_hide_speed = 800;
var commentform_hide_easing = "easeInExpo";

var commentform_success_autohide = 2500;

var commentform_submit_url = "contact.php";

$(document).ready(function(){
	//comment form
	$("#sendcomment").children("a.showForm").each(function() {
		$(this).click(function() {
			var commentform = $("#sendCommentForm");
			//hide form warning message
			commentform.find(".formMessage").css("display", "none");
			
			//clear form
			commentform.find("form").find("#comment_email").val("");
			commentform.find("form").find("#comment_message").val("");
			//clear dropdown value
			sv = commentform.find("select").find("option:eq(0)").val();
			sh = commentform.find("select").find("option:eq(0)").html();
			commentform.find("select").val(sv);
			commentform.find(".loewy_dropdown_result").find("span").html(sh);
			
			
			commentform.css("display", "block");
			commentform.find(".sendCommentFormContainer").animate({
				height: "show"
			}, commentform_show_speed, commentform_show_easing);
			
			return false;
		});
	});
	
	$("#sendCommentForm").find("form").find("#comment_cancel").each(function() {
		$(this).click( function() {
			
			$("#sendCommentForm").find(".sendCommentFormContainer").animate({
				height: "hide"
			}, commentform_hide_speed, commentform_hide_easing, function() { $("#sendCommentForm").css("display", "none"); });
			
			return false;
		});
	});
	
	$("#sendCommentForm").find("form").find("#comment_submit").each(function() {
		$(this).click( function() {
			var commentform = $("#sendCommentForm");
			valid = true;
			//validate email
			v = commentform.find("form").find("#comment_email").val();
			commentform.find(".formMessage").find("ul").html("");
			if (v == '') {
				commentform.find(".formMessage").find("ul").append("<li>"+commentform_requireEmail+"</li>");
				
				valid = false;
			} else if (!validEmail(v)) {
				commentform.find(".formMessage").find("ul").append("<li>"+commentform_invalidEmail+"</li>");
				//commentform.find(".formMessage").fadeIn();
				valid = false;
			}
			//validate message
			v = commentform.find("form").find("#comment_message").val();
			if (v == '') {
				commentform.find(".formMessage").find("ul").append("<li>"+commentform_requireMessage+"</li>");
				//commentform.find(".formMessage").fadeIn();
				valid = false;
			} 
			if (!valid) {
				//
				commentform.find(".formMessage").fadeIn();
			} else {
				//ajax send
				data = "subject="+URLEncode(document.frmSendComment.comment_subject.value);
				data = data + "&message="+URLEncode(document.frmSendComment.comment_message.value);
				data = data + "&email="+URLEncode(document.frmSendComment.comment_email.value);
				$.ajax({
					url: commentform_submit_url,
					data: data,
					type: "GET",
					success: function(msg) {
						commentform.find(".formMessage").find("ul").html("<li class=\"success\">"+commentform_success+"</li>");
						commentform.find(".formMessage").fadeIn();
						window.setTimeout( function() {
							$("#sendCommentForm").find(".sendCommentFormContainer").animate({
								height: "hide"
								}, commentform_hide_speed, commentform_hide_easing, function() { $("#sendCommentForm").css("display", "none"); 
							});
						}, commentform_success_autohide);
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						$("#sendCommentForm").find(".formMessage").find("ul").html("<li>"+commentform_submitError+"</li>");
					}
				});				
			}
			
			return false;
		});
	});
	//-
});