/*
Update: 14 Oct 2008 (suki) ~ detect 'newsletter signup' option selection and show first name and first name field
*/
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_requireName = "Whoops. We'll need your name (First Name &amp; Last Name).";
var commentform_success = "Thanks! We'll get you back to you pronto.";
var commentform_already_subscribed = "You're already on our list";
var commentform_subscribe_success = "You've been subscribed to our mailing list!";

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 = "/comment.php";
var commentform_subcriber_url = "/subscriber.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.find("select")[0]).trigger("change");
			
			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;
			}
			
			if (document.frmSendComment.comment_subject.value != "newsletter_signup") {
				//validate message
				v = commentform.find("form").find("#comment_message").val();
				if (v == '') {
					commentform.find(".formMessage").find("ul").append("<li>"+commentform_requireMessage+"</li>");
					valid = false;
				}
			} else {
				if (document.frmSendComment.first_name.value == "" || document.frmSendComment.last_name.value == "") {
					commentform.find(".formMessage").find("ul").append("<li>"+commentform_requireName+"</li>");
					valid = false;
				}
			}
			
			if (!valid) {
				//
				commentform.find(".formMessage").fadeIn();
			} else {
				//ajax send
				var data = "";
				var url = "";
				var subscribe = false;
				if (document.frmSendComment.comment_subject.value == "newsletter_signup") {
					subscribe = true;
					url = commentform_subcriber_url;
					
					data = "subject="+URLEncode(document.frmSendComment.comment_subject.value);
					data = data + "&first_name="+URLEncode(document.frmSendComment.first_name.value);
					data = data + "&last_name="+URLEncode(document.frmSendComment.last_name.value);
					data = data + "&email="+URLEncode(document.frmSendComment.comment_email.value);
					
				} else {
					url = commentform_submit_url;
					
					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: url,
					data: data,
					type: "GET",
					success: function(msg) {
						if (msg == "ALREADY SUBSCRIBED") {
							commentform.find(".formMessage").find("ul").append("<li>"+commentform_already_subscribed+"</li>");
							commentform.find(".formMessage").fadeIn();
						} else {
							if (subscribe) {
								commentform.find(".formMessage").find("ul").html("<li class=\"success\">"+commentform_subscribe_success+"</li>");
							} else {
								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;
		});
	});
	//-
	
	//detect newsletter signup selection
	$("#sendCommentForm form select[name='comment_subject']").bind("click change", function(){
		if (this.value == "newsletter_signup") {
			var frm = $(this).parents("form");
			$(".message", frm).hide();
			$(".firstName", frm).show();
			$(".lastName", frm).show();
		} else {
			var frm = $(this).parents("form");
			$(".message", frm).show();
			$(".firstName", frm).hide();
			$(".lastName", frm).hide();
		}
	});
	
});
