$(document).ready(function () {

/*
	if ($.browser.name == "msie") {
		alert("saa");
	}
*/
var commentBoxHeight = "";
if ($.browser.name !== "msie") {
	// browser fixes
	if ($.browser.name == "firefox" && $.os.name == "mac") {
		$('#post #name').css({"min-width":"298px","width":"298px","max-width":"298px"});
		$('#post #emailblog').css({"min-width":"298px","width":"298px","max-width":"298px"});
		$('#post #commentField').css({"min-width":"298px","width":"298px","max-width":"298px"});
		//postCancel postComment
		$('#post #postCancel').css({"clear":"all","left":"234px"});
		$('#post #postComment').css({"position":"relative","left":"0px"});
				
		if (loggedIn == 1) {
			commentBoxHeight = "80px";
		}
		else {
			commentBoxHeight = "114px";
		}
		$('#pay').css({"margin-left":"0px"});
	}

	if ($.browser.name == "safari" && $.os.name == "mac") {
		$('#post #name').css({"min-width":"296px","width":"296px","max-width":"296px"});
		$('#post #emailblog').css({"min-width":"296px","width":"296px","max-width":"296px"});
		$('#post #commentField').css({"min-width":"294px","width":"294px","max-width":"294px"});
		$('#post #postCancel').css({"clear":"all","left":"236px"});
		$('#post #postComment').css({"position":"relative","left":"0px"});
		
				
		if (loggedIn == 1) {
			commentBoxHeight = "80px";
		}
		else {
			commentBoxHeight = "114px";
		}
		$('#pay').css({"margin-left":"0px"});
	}

	if ($.browser.name == "chrome") { // && $.os.name == "mac"
		$('#pay').css({"margin-left":"0px"})
	}
	
	
}
else {
	//commentBoxHeight = "120px";
	$('#post #name').css({"min-width":"296px","width":"296px","max-width":"296px"});
	$('#post #emailblog').css({"min-width":"296px","width":"296px","max-width":"296px"});
	$('#post #commentField').css({"min-width":"296px","width":"296px","max-width":"296px"});
	$('#post #postCancel').css({"position":"relative","clear":"all","left":"257px","top":"-15px"});
	$('#post #postComment').css({"position":"relative","left":"0px"});
	
	$('form#login_form').css({"text-align":"left"});
	$('form#login_form span').css({"text-align":"left"});
	$('form#login_form input').css({"min-width":"180px","width":"180px","max-width":"180px"});
	$('form#login_form .send').css({"min-width":"60px","width":"60px","max-width":"60px"});
	$('.dialogClose').css({"position":"relative","top":"-32px","left":"10px","margin":"0px"});	
				
	if (loggedIn == 1) {
		commentBoxHeight = "80px";
	}
	else {
		commentBoxHeight = "118px";
	}
	$('#pay').css({"margin-left":"0px"});
	
}
	if ($.browser.name == "firefox" && $.os.name == "win") {
		$('#post #name').css({"position":"relative","min-width":"299px","width":"299px","max-width":"299px","left":"1px"});
		$('#post #emailblog').css({"position":"relative","min-width":"299px","width":"299px","max-width":"299px","left":"1px"});
		$('#post #commentField').css({"position":"relative","min-width":"299px","width":"299px","max-width":"299px","left":"1px"});
		//postCancel postComment
		$('#post #postCancel').css({"clear":"all","left":"230px"});
		$('#post #postComment').css({"position":"relative","left":"1px"});
		//$('#post input')
		
		$('form#login_form').css({"text-align":"left"});
		$('form#login_form span').css({"text-align":"left"});
		$('form#login_form input').css({"min-width":"180px","width":"180px","max-width":"180px"});
		$('form#login_form .send').css({"min-width":"60px","width":"60px","max-width":"60px"});
		$('.dialogClose').css({"position":"relative","top":"-32px","left":"10px","margin":"0px"});	
			
		if (loggedIn == 1) {
			commentBoxHeight = "79px";
		}
		else {
			commentBoxHeight = "114px";
		}
	}
		
	if ($.browser.name == "safari" && $.os.name == "win") {
		$('#post #name').css({"min-width":"296px","width":"296px","max-width":"296px"});
		$('#post #emailblog').css({"min-width":"296px","width":"296px","max-width":"296px"});
		$('#post #commentField').css({"min-width":"298px","width":"298px","max-width":"298px"});
		$('#post #postCancel').css({"clear":"all","left":"236px"});
		$('#post #postComment').css({"position":"relative","left":"0px"});
				
		if (loggedIn == 1) {
			commentBoxHeight = "78px";
		}
		else {
			commentBoxHeight = "112px";
		}
		
		$('#pay').css({"margin-left":"0px"});
	}
	
	
	if ($.browser.name == "chrome" && $.os.name == "win") { // && $.os.name == "mac"
		$('#pay').css({"margin-left":"0px"})
	}
	



	var comment = "";
	var name = "";
	var email = "";
	var padHeightOpen = "";
	var padHeightClose = "";
	

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



	function commentForm() {
		//alert("Win 1");
		$('#placeHolder').hide();
		$('#commentField').show();
		
		// grow
		$(this).parent().animate({ height: commentBoxHeight }, 500, function() {
			
			$(this).addClass("post_open");
			
			//* $(this).animate({"padding-bottom" : padHeightOpen}, 500); 
			
			if (comment !== '') {
				$(this).children('#commentField').val(comment).select();
				$(this).children('#commentField').select();
			}
			else {
				$(this).children('#commentField').val('comment...');
			}
			
			if (name !== '') {
				$(this).children('#name').val(name);
			}
			else {
				$(this).children('#name').val('name');
			}
			
			if (email !== '') {
				$(this).children('#emailblog').val(email);
			}
			else {
				$(this).children('#emailblog').val('email (hidden) required');
			}
		});
		
		$(this).parent().children('#commentField').animate({ height: "65px" }, 500, function() {
				$('#post #buttons').show();
				$(this).css('max-height','65px');
		});
		
		if ($.browser.name == "firefox" && $.os.name == "win") {
			$(this).parent().children('input').animate({ "height": "19px", "min-height": "19px", "max-height": "19px" }, 500, function() {});
		}
		else {
			$(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); 
// END - MSIE patch	
	
	$('#postCancel').click(function() {
		
		$('#buttons').hide();
	
		name = $('#name').val();
		email = $('#emailblog').val();
		comment = $('#commentField').val();
		$('#name, #emailblog, #commentField').val('');
	
		$('#name, #emailblog').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(pPg, { name: name, email: email, comments: comments, blog_id: blog_id, p: 'blog_comment_process.php' }, function(data) {
		$.post(pPg, { comments: comments, blog_id: blog_id, p: 'blog_comment_process', token: token, email: email, name:name }, function(data) {
			// Success!
			if (data > 1) {
				//alert('Success! '+data);
				
				// load last comment
				$('#lastComment').load(pPg,{blog_comments_id: data, p: 'blog_last_comment', token: token}, 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, #emailblog, #commentField').val('');
	
				$('#name, #emailblog').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.');
			}
		} );
	};
	
	function checkComment() {
		if ($('#commentField').val().length > 0) {
			return true;
		}
		else {
			return false;
		}
	}
	
	$('#postComment').click(function() {
		if (loggedIn == 0) {
			// check if required is met
			//var email = $("#emailblog").val();
			if (isValidEmailAddress("#emailblog")) {
				saveComment($('#name').val(), $('#emailblog').val(), $('#commentField').val(), $('#blog_id').val());
				//$('#emailblog').css({'background-color':'#ffffff'});
			}/*

			else {
				$('#emailblog').css({'background-color':'#ffd3ba'});
			}
*/
		}
		else {
			if (checkComment()) {
				saveComment($('#name').val(), $('#emailblog').val(), $('#commentField').val(), $('#blog_id').val());
			}
		}
	
		
	});
	
	$('#post').submit(function() {
		if (loggedIn == 0) {
			// check if required is meet
			//var email = $("#emailblog").val();
			if (isValidEmailAddress("#emailblog")) {
				saveComment($('#name').val(), $('#emailblog').val(), $('#commentField').val(), $('#blog_id').val());
				//$('#emailblog').css({'background-color':'#ffffff'});
			}/*

			else {
				$('#emailblog').css({'background-color':'#ffd3ba'});
			}
*/
		}
		else {
			if (checkComment()) {
				saveComment($('#name').val(), $('#emailblog').val(), $('#commentField').val(), $('#blog_id').val());
			}
		}
	
		return false;
		
	});

});
