var commentId;
var lastOriginalComment;
var blogPostCommentMode = "edit";
var rootNode;
var postId;
var commentFormContainer;
var emptyGuid = "00000000-0000-0000-0000-000000000000";
var addBlogPostCommentLink;
var comments;
var postDisplay_body;
var commentElementsDeclared;
var addedClonedComment;

var commentDisplayContainer;
var commentDisplay_body;
var commentDisplay_handle;
var commentDisplay_date;
var commentDisplay_url;
var commentDisplay_time;

var commentFormContainer;
var commentForm_preview;
var commentDisplay_form;
var commentForm_timeStampRow;
var commentForm_previewButtons;
var commentForm_deleteButton
var postDisplay_commentCount;

function addBlogPostComment(comId)
{
	lookupCommentElements();
	
	comments.parentNode.insertBefore(commentFormContainer, comments.nextSibling);
	commentForm_body.style.height = "100px";

	if(commentForm_timeStampRow != null)
		commentForm_timeStampRow.style.display = "none";

	if(commentForm_deleteButton != null)
		commentForm_deleteButton.style.display = "none";

	editBlogPostComment(comId);
}

function editBlogPostComment(comId, comDisplayContainerId)
{
	lookupCommentElements();

	if(commentDisplayContainer != null)
	{
		commentDisplayContainer.style.display = "";
		commentFormContainer.style.display = "none";
	}

	if(comDisplayContainerId != null)
		commentDisplayContainer = document.getElementById(comDisplayContainerId);

	makeActiveComment(comId, commentPreviewDisplay);
	setCommentblogPostCommentMode();
	commentForm_timeStampRow.style.display = blogPostCommentMode == "edit" ? "" : "none";

	if(commentForm_deleteButton != null)
		commentForm_deleteButton.style.display = blogPostCommentMode == "add" ? "none" : "";
	
	commentForm_preview.style.display = "none";
	Library.setOpacity(commentDisplay_form, 1);
	commentDisplay_form.style.display = "";
	
	if(blogPostCommentMode == "edit")
	{
		clearBlogPostCommentsForm();
		CSharper.Net.ScriptService.GetBlogPostComment(commentId, getBlogPostCommentCallback, onError);
	}
	else
	{
		var com = new CSharper.Net.BlogPostComment();
		com.ID = emptyGuid;
		com.TimeStamp = new Date();
		com.Handle = "Anonymous";
		com.Body = "";
		com.Email = "";
		com.Url = "";
		getBlogPostCommentCallback(com);
	}
}

function getBlogPostCommentCallback(comment)
{
	if(comment == null)
	{
		alert("There was an error retrieving this comment from the server");
		cancelEditBlogPostCommentFromPreview();
		return;
	}
	
	//set reference to original comment
	lastOriginalComment = comment;
	hydrateBlogPostCommentForm(comment);
	
	displayBlogPostCommentForm();
}

function displayBlogPostCommentForm()
{
	commentFormContainer.style.position = "absolute";
	commentFormContainer.style.display = "";

	commentFormContainer.style.left = "-5000px";
	commentFormContainer.style.top = "-5000px";
	
	if(blogPostCommentMode == "edit")
		Library.autoSizeScrollBox(commentForm_body, 300, 50);

	var height = Library.getHeightStrict(commentFormContainer);

	if(blogPostCommentMode == "edit")
	{
		commentDisplayContainer.parentNode.insertBefore(commentFormContainer, commentDisplayContainer);
		commentFormContainer.style.height = Library.getHeightStrict(commentDisplayContainer) + "px";
		commentDisplayContainer.style.display = "none";
	}
	else
	{
		commentFormContainer.style.height = "1px";
		commentFormContainer.style.width = commentFormContainer.offsetWidth + "px";
	}
	
	commentFormContainer.style.overflow = "hidden";
	commentFormContainer.style.position = "static";
	hideAddCommentLink();

	var attributes = { height: {to: height }};
	var anim = new YAHOO.util.Anim(commentFormContainer, attributes, 1, YAHOO.util.Easing.easeOut);
	anim.duration = Library.isSaf ? 1.5 : 1;
	anim.onComplete.subscribe(displayBlogPostCommentFormComplete);
	anim.animate();

	if(blogPostCommentMode == "add")
	{
		attributes = {scroll: { to: [0, 10000] }};
		var anim2 = new YAHOO.util.Scroll(document.documentElement, attributes, 1, YAHOO.util.Easing.easeOut);
		anim2.duration = Library.isSaf ? .75 : .5;
		anim2.animate();
	}

	if(!Library.isIe)
	{
		Library.setOpacity(commentFormContainer, 0);
		var attributes = { opacity: {to: 1}  };
		var animOp = new YAHOO.util.Anim(commentFormContainer, attributes, 1, YAHOO.util.Easing.easeOut);
		animOp.duration = 1;
		animOp.animate();
	}
}

function displayBlogPostCommentFormComplete()
{
	commentForm_body.focus();
	commentFormContainer.style.height = null;
}

function hideBlogPostCommentForm()
{
	var height = 1;
	var animateEl;

	if(blogPostCommentMode == "edit")
	{
		commentDisplayContainer.style.position = "absolute";
		commentDisplayContainer.style.top = "-5000px";
		commentDisplayContainer.style.left = "-5000px";
		commentDisplayContainer.style.display = "";

		height = Library.getHeightStrict(commentDisplayContainer);

		commentDisplayContainer.style.height = Library.getHeightStrict(commentFormContainer) + "px";
		commentDisplayContainer.style.overflow = "hidden";
		commentDisplayContainer.style.position = "static";
		commentFormContainer.style.display = "none";
		animateEl = commentDisplayContainer;
	}
	else
		animateEl = commentFormContainer;

	var attributes = { height: {to: height }};
	var anim = new YAHOO.util.Anim(animateEl, attributes, 1, YAHOO.util.Easing.easeOut);
	anim.duration = Library.isSaf ? .75 : .5;
	anim.onComplete.subscribe(hideBlogPostCommentFormComplete);
	anim.animate();
	
	if(blogPostCommentMode == "edit")
	{
		if(!Library.isIe)
		{
			Library.setOpacity(commentDisplayContainer, 0);
			var attributes = { opacity: {to: 1}  };
			var animOp = new YAHOO.util.Anim(commentDisplayContainer, attributes, 1, YAHOO.util.Easing.easeOut);
			animOp.duration = 1;
			animOp.animate();
		}
	}
}

function hideBlogPostCommentFormComplete()
{
	if(blogPostCommentMode == "edit")
	{
		if(commentDisplayContainer != null)
			commentDisplayContainer.style.position = "static";
	}
	
	commentFormContainer.style.display = "none";
	commentForm_preview.style.display = "none";
	commentDisplay_form.style.display = "none";
	
	resetAllHeights();
	showAddCommentLink();
}

function hideAddCommentLink()
{
	addBlogPostCommentLink.style.visibility = "hidden";
}

function showAddCommentLink()
{
	addBlogPostCommentLink.style.visibility = "visible";
}

function cancelEditBlogPostCommentFromForm()
{
	commentDisplay_body.blur();
	hideBlogPostCommentForm();
}

function hydrateBlogPostCommentForm(comment)
{
	commentForm_body.value = comment.Body;
	commentForm_handle.value = comment.Handle;
	commentForm_url.value = comment.Url;
	commentForm_email.value = comment.Email;
	
	commentForm_timeStamp.value = comment.TimeStamp.format("mm/dd/yyyy hh:nn a/p");
}

function dehydrateBlogPostCommentForm()
{
	var comment = new CSharper.Net.BlogPostComment();
	
	comment.ID = commentId;
	comment.Body = commentForm_body.value;
	comment.Handle = commentForm_handle.value;
	comment.Url = commentForm_url.value;
	comment.Email = commentForm_email.value;
	
	if(comment.ID != emptyGuid)
		comment.TimeStamp = new Date(commentForm_timeStamp.value);
	else
		comment.TimeStamp = new Date();
	
	return comment;
}

function saveBlogPostCommentFromPreview()
{
	var comment = dehydrateBlogPostCommentForm();
	CSharper.Net.ScriptService.SaveBlogPostComment(comment, postId, saveBlogPostCommentFromPreviewCallback, onError);

	var height = Library.getHeightStrict(commentFormContainer) - Library.getHeightStrict(commentForm_previewButtons);
	var attributes = { height: {to: height}  };
	var animSize = new YAHOO.util.Anim(commentFormContainer, attributes, 1, YAHOO.util.Easing.easeOut);
	animSize.duration = .75;
	animSize.onComplete.subscribe(function(){saveBlogPostCommentFromPreviewCallbackComplete(comment)});
	animSize.animate();
}

function saveBlogPostCommentFromPreviewCallback(comment)
{
}

function saveBlogPostCommentFromPreviewCallbackComplete(comment)
{
	resetAllHeights();
	commentFormContainer.style.display = "none";
	commentForm_preview.style.display = "none";
	commentDisplay_form.style.display = "none";
	
	if(blogPostCommentMode == "add")
	{
		appendNewBlogPostComment(comment);
	}
	else
	{
		assignRelatedCommentElements(commentDisplayContainer);
		hydrateBlogPostCommentDisplay(lastPreviewComment);
		commentDisplayContainer.style.display = "";
	}

	showAddCommentLink();
}

function appendNewBlogPostComment(comment)
{
	var cloneNode = commentPreviewDisplay.cloneNode(true);
	cloneNode.id = "new_div";
	
	comments.appendChild(cloneNode);
	cloneNode.setAttribute("commentId", comment.ID);

	updateCommentCount(1);
	addedClonedComment = cloneNode;
}

function updateCommentCount(toAdd)
{
	var oldCount = parseInt(postDisplay.getAttribute("commentCount"));
	var newCount = oldCount + toAdd;
	var commentText = newCount != 1 ? " comments" : " comment";
	
	postDisplay.setAttribute("commentCount", newCount);
	postDisplay_commentCount.innerHTML = newCount + commentText;

	if(newCount == 0)
		commentsTitle.style.display = "none";
	else
		commentsTitle.style.display = "";
}

function previewBlogPostCommentFromForm()
{
	try
	{
		if(!validateCommentsForm())
			return;
		
		var comment = dehydrateBlogPostCommentForm();
		CSharper.Net.ScriptService.GetBlogPostCommentDisplay(comment, previewBlogPostCommentCallback, onError);
	}
	catch(e)
	{
		alert(e);
	}
}

var lastPreviewComment;
function previewBlogPostCommentCallback(comment)
{
	lastPreviewComment = comment;
	hydrateBlogPostCommentDisplay(comment);
	swapEditDisplayCommentModes("preview");
}

function hydrateBlogPostCommentDisplay(comment)
{
	commentDisplay_body.innerHTML = comment.BodyDisplay;
	commentDisplay_handle.innerHTML = comment.HandleDisplay;
	commentDisplay_date.innerHTML = comment.DateStampLabel;
	commentDisplay_time.innerHTML = comment.TimeStampLabel;
}

function editBlogPostCommentFromPreview()
{
	swapEditDisplayCommentModes("edit");
}

var lastCommentMode;
function swapEditDisplayCommentModes(mode)
{
	var sizeEl;
	var opacEl;
	var animEl;
	
	lastCommentMode = mode;
	commentFormContainer.style.height = Library.getHeightStrict(commentFormContainer) + "px";
	commentForm_preview.style.display = "";
	commentDisplay_form.style.display = "";

	var displayHeight = commentForm_preview.offsetHeight;
	var formHeight = Library.getHeightStrict(commentDisplay_form);
	var formWidth = Library.getWidthStrict(commentDisplay_form);
	var height;
	
	if(mode == "edit")
	{
		commentForm_preview.style.display = "none";
		var diff = formHeight - displayHeight;
		height = Library.getHeightStrict(commentFormContainer) + diff;
		opacEl = commentDisplay_form;
	}
	else
	{
		commentForm_preview.style.width = formWidth + "px";
		commentDisplay_form.style.height = commentDisplay_form.offsetHeight + "px";
		commentDisplay_form.style.display = "none";

		opacEl = commentForm_preview;
		var diff = displayHeight - formHeight;
		height = Library.getHeightStrict(commentFormContainer) + diff;
	}

	if(!Library.isIe)
		Library.setOpacity(commentDisplay_form, 0);

	var attributes = { height: {to: height}  };
	var easeDir = diff > 0 ? YAHOO.util.Easing.easeOut : YAHOO.util.Easing.easeOut;
	var animSize = new YAHOO.util.Anim(commentFormContainer, attributes, 1, easeDir);
	animSize.duration = .75;
	animSize.onComplete.subscribe(swapEditDisplayCommentModesComplete);
	animSize.animate();

	if(!Library.isIe)
	{
		var attributes = { opacity: {to: 1}  };
		var animOp = new YAHOO.util.Anim(opacEl, attributes, 1, YAHOO.util.Easing.easeOut);
		animOp.duration = 1;
		animOp.animate();
	}
}

function swapEditDisplayCommentModesComplete()
{
	resetAllHeights();
}

function cancelEditBlogPostCommentFromPreview()
{
	commentDisplay_body.blur();
	hideBlogPostCommentForm();

	if(lastOriginalComment != null && blogPostCommentMode == "edit")
		hydrateBlogPostCommentDisplay(lastOriginalComment);
}

function resetAllHeights()
{
	commentForm_preview.style.height = null;
	commentDisplay_form.style.height = null;
	commentFormContainer.style.height = null;
}

function deleteBlogPostComment()
{
	if(lastOriginalComment == null)
		return;

	var attributes = { height: {to: 0 }};
	var anim = new YAHOO.util.Anim(commentFormContainer, attributes, 1, YAHOO.util.Easing.easeOut);
	anim.duration = Library.isSaf ? 1.5 : 1;
	anim.onComplete.subscribe(hideBlogPostCommentFormComplete);
	anim.animate();

	CSharper.Net.ScriptService.DeleteBlogPostComment(commentId, deleteBlogPostCommentCallback, onError);
}

function deleteBlogPostCommentCallback(success)
{
	if(!success)
	{
		alert("There was an error deleting the comment");
		return;
	}
	
	commentDisplayContainer.parentNode.removeChild(commentDisplayContainer);
	updateCommentCount(-1);
}

function makeActiveComment(id, root)
{
	postId = postDisplay_body.parentNode.getAttribute("postId");
	commentId = id;
	rootNode = root;
	assignRelatedCommentElements(root);
}

function assignRelatedCommentElements(root)
{
	commentDisplay_body = getWithin(root, "commentDisplay_body");
	commentDisplay_handle = getWithin(root, "commentDisplay_handle");
	commentDisplay_date = getWithin(root, "commentDisplay_date");
	commentDisplay_time = getWithin(root, "commentDisplay_time");	
}

function clearBlogPostCommentsForm()
{
	commentForm_body.value = "";
	commentForm_handle.value = "Anonymous";
	commentForm_email.value = "";
	commentForm_url.value = "";
	commentForm_timeStamp.value = "";
}

function setCommentblogPostCommentMode()
{
	if(commentId == emptyGuid)
		blogPostCommentMode = "add";
	else
		blogPostCommentMode = "edit";
}

function getWithin(root, nodeId)
{
	if(root.id == nodeId)
		return root;

	if(root.firstChild == null)
		return;
	
	for(var i = 0; i < root.childNodes.length; i++)
	{
		var obj = getWithin(root.childNodes[i], nodeId);
		if(obj != null)
			return obj;
	}
		
	return null;
}

function validateCommentsForm()
{
	var validated = Page_ClientValidate("comment");
	return validated;
}

function onError(arg)
{
	alert("Error: " + arg);
}

function lookupCommentElements()
{
	if(commentElementsDeclared)
		return;
	
	commentFormContainer = document.getElementById("commentFormContainer");
	commentForm_timeStampRow = document.getElementById("commentForm_timeStampRow");
	commentForm_preview = document.getElementById("commentForm_preview");
	commentForm_previewButtons = document.getElementById("commentForm_previewButtons");

	postDisplay_commentCount = document.getElementById("postDisplay_commentCount");
	postDisplay_body = document.getElementById("postDisplay_body");
	commentDisplay_form = document.getElementById("commentDisplay_form");
	addBlogPostCommentLink = document.getElementById("addBlogPostCommentLink");

	commentElementsDeclared = true;
}
