function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

$(function() {

	// browser fixes
	if ($.browser.mozilla) {
		$('#name').css({"min-width":"298px","width":"298px","max-width":"298px",});
		$('#email').css({"min-width":"298px","width":"298px","max-width":"298px",});
		$('#commentField').css({"min-width":"298px","width":"298px","max-width":"298px",});
		//postCancel
		$('#postCancel').css({"left":"222px"});
	}
	
	if ($.browser.safari) {
		$('#name').css({"min-width":"296px","width":"296px","max-width":"296px",});
		$('#email').css({"min-width":"296px","width":"296px","max-width":"296px",});
		$('#commentField').css({"min-width":"294px","width":"294px","max-width":"294px",});
	}


	var comment = '';
	var name = '';
	var email = '';
	var padHeightOpen = "";
	var padHeightClose = "";
	
	if ($.browser.safari) {
		padHeightOpen = "2px";
		padHeightClose = "0px";
	}
	if ($.browser.mozilla) {
		padHeightOpen = "10px";
		padHeightClose = "0px";
	}
		
	
	$('#post input').css('height','0px;');


	function commentForm() {
	
		$('#placeHolder').hide();
		$('#commentField').show();
		
		// grow
		$(this).parent().animate({ height: "114px" }, 500, function() {
			
			$(this).addClass("post_open");
			
			/* $(this).animate({"padding-bottom" : padHeightOpen}, 500); */
			
			if (comment !== '') {
				$(this).children('#commentField').val(comment);
			}
			else {
				$(this).children('#commentField').val('comment...');
			}
			
			if (name !== '') {
				$(this).children('#name').val(name);
			}
			else {
				$(this).children('#name').val('name');
			}
			
			if (email !== '') {
				$(this).children('#email').val(email);
			}
			else {
				$(this).children('#email').val('email (hidden) required');
			}
		});
		
		$(this).parent().children('#commentField').animate({ height: "65px" }, 500, function() {
				$('#post #buttons').show();
				$(this).css('max-height','65px');
		});
			
		$(this).parent().children('input').animate({ "height": "13px", "min-height": "13px", "max-height": "13px" }, 500, function() {});
		
		$(this).parent().children('input:eq(0)').focus();
		
	
	};


	$('#placeHolder').bind('click', commentForm);
	$('#placeHolder').bind('focus', commentForm); 
	
	
/*

	$('#placeHolder').focus(function(e) {
	
		commentForm(e);
	
	});
	
	
*/
	$('#postCancel').click(function() {
		
		$('#buttons').hide();
	
		name = $('#name').val();
		email = $('#email').val();
		comment = $('#commentField').val();
		$('#name, #email, #commentField').val('');
	
		$('#name, #email').animate({ "height": "0px", "min-height": "0px", "max-height": "0px" }, 430, function() {
			$(this).hide();
		});
		
		$('#commentField').animate({ height: "13px" }, 500, function() {
			$(this).hide();
			$('#placeHolder').show();
		});
		//alert(padHeight);
		$('#post').animate({ "height" : "15px"/* , "padding-bottom" : padHeightClose */ }, 515, function() {});
		
		$('#post').removeClass("post_open");
		
	});
	
	function saveComment(name, email, comments, blog_id) {
		$.post("../process.php", { name: name, email: email, comments: comments, blog_id: blog_id, p: 'blog_comment_process.php' }, function(data) {
			// Success!
			if (data > 1) {
				//alert('Success! '+data);
				
				// load last comment
				$('#lastComment').load("../process.php",{blog_comments_id: data, p: 'blog_last_comment.php'}, function(html) {
					$('#commentsSection').append(html);//$(this).html());
				});
				
				// Add 1 to Comment Count
				var commentsCount = parseFloat($('#commentsCount').text());
				$('#commentsCount').text((commentsCount + 1));
				
				// clear comment box
				$('#buttons').hide();
	
				name = '';
				email = '';
				comment = '';
				$('#name, #email, #commentField').val('');
	
				$('#name, #email').animate({ "height": "0px", "min-height": "0px", "max-height": "0px" }, 430, function() {
					$(this).hide();
				});
		
				$('#commentField').animate({ height: "13px" }, 500, function() {
					$(this).hide();
					$('#placeHolder').show();
				});
		
		
				$('#post').animate({ height: "15px"/* , "padding-bottom" : padHeightClose */ }, 515, function() {});
				$('#post').removeClass("post_open");
						
			}
			else if (data == 0) {
				alert('Your comment failed to post. Please try again.');
			}
		} );
	};
	
	$('#postComment').click(function() {
		
		// check if required is met
		var email = $("#email").val();
		if (isValidEmailAddress(email)) {
			saveComment($('#name').val(), $('#email').val(), $('#commentField').val(), $('#blog_id').val());
			$('#email').css({'background-color':'#ffffff'});
		}
		else {
			$('#email').css({'background-color':'#ffd3ba'});
		}
	
		
	});
	
	$('#post').submit(function() {
		
		// check if required is meet
		var email = $("#email").val();
		if (isValidEmailAddress(email)) {
			saveComment($('#name').val(), $('#email').val(), $('#commentField').val(), $('#blog_id').val());
			$('#email').css({'background-color':'#ffffff'});
		}
		else {
			$('#email').css({'background-color':'#ffd3ba'});
		}
	
		return false;
		
	});

});