var xOffSet = 34;
var yOffSet = 0;
var CommentWindow;
var ActiveParagraphNumber;
var ActivedID;
var PendingParagraphNumber;
var PendingdID;
var PreviousCommentID;
var NextCommentID;
var windowTimeout;
var Comments;
var CommentSelectorMessage;


var onOn = new Image();
var onOff = new Image();
var offOn = new Image();
var offOff = new Image();
onOn.src = "/stellent/groups/aia/documents/image/btn_on_down.gif";
onOff.src = "/stellent/groups/aia/documents/image/btn_on_up.gif";
offOn.src = "/stellent/groups/aia/documents/image/btn_off_down.gif";
offOff.src = "/stellent/groups/aia/documents/image/btn_off_up.gif";

function validateOnClick(ParagraphNumber){
	var ThisParagraph = $("Paragraph" + ParagraphNumber);
	var CommentsCount = "";

	if(ThisParagraph.text){
		CommentsCount = ThisParagraph.text;
	}
	
	if(ThisParagraph.innerText){
		CommentsCount = ThisParagraph.innerText;
	}
	if(CommentsCount=="0"){
		if($("hIsLoggedIn").value == "false"){
			ThisParagraph.href= $("hRedirectURL").value + "?retUrl=" + window.location;
		}
	}
}

function displayComments(ParagraphNumber,dID){
	if(ParagraphNumber!=ActiveParagraphNumber){
		PendingdID = dID;
		PendingParagraphNumber = ParagraphNumber;
		windowTimeout = window.setTimeout(deleyedShowPopup,400);
	}
}


function deleyedShowPopup(){
		if(CommentWindow!=null){
			hideCommentHard();
		}
		ActivedID = PendingdID;
		ActiveParagraphNumber = PendingParagraphNumber;
		var ActiveParagraph = $("Paragraph" + ActiveParagraphNumber);
		var Parameters = "IdcService=GET_FIRST_COMMENT&dID=" + ActivedID + "&ParagraphNumber=" + ActiveParagraphNumber;
		var PostBackURL = $("hPostBackUrl").value;				
		new Ajax.Request(PostBackURL, {method: "post", parameters: Parameters , onComplete: handleCommentCallback});
}

function copyTotalComments(){
	var Total = 0;
	
	var CommentsDisplay = $("totalCommentsCount");
	var TotalPageComments = $("hTotalComments");
	if(TotalPageComments!=null && TotalPageComments.value !=""){
		Total = TotalPageComments.value;
	}

	if(CommentsDisplay!=null){
		CommentsDisplay.innerHTML = Total;
	}

}



function nextComment(CommentID){
	var Parameters = "IdcService=GET_COMMENT&CommentID=" + CommentID + "&dID=" + ActivedID;
	var PostBackURL = $("hPostBackUrl").value;
	new Ajax.Request(PostBackURL, {method: "post", parameters: Parameters , onComplete: handleCommentCallback});
}

function cancelComments(){
	window.clearTimeout(windowTimeout);	

}

function handleCommentCallback(originalResponse){
		var HasComments = true;
		var CommentsList;
		var ThisCommentID;
		var UserName;
		var InsertDate;
		var TotalComments=0;
		var TotalDocumentComments=0;
		var CommentNumber;
		var CurrentCommentID;


		if(originalResponse.responseXML ==null){
			HasComments = false;
		}else{
			if(originalResponse.responseXML.getElementsByTagName("TotalComments")[0]!=null){
				TotalDocumentComments = originalResponse.responseXML.getElementsByTagName("TotalComments")[0].firstChild.nodeValue;
			}
			CommentsList = originalResponse.responseXML.getElementsByTagName("Comment");
			if(CommentsList.length==0){
				HasComments = false;
			}else{
				var CommentData = originalResponse.responseXML.getElementsByTagName("CommentData")[0];
				if(CommentData==null){
					HasComments = false;
				}else{
					ThisCommentID = CommentData.getElementsByTagName("CommentID")[0].firstChild.nodeValue;
					UserName = CommentData.getElementsByTagName("UserName")[0].firstChild.nodeValue;
					Comments = CommentData.getElementsByTagName("Comments")[0].firstChild.nodeValue;
					InsertDate = CommentData.getElementsByTagName("InsertDate")[0].firstChild.nodeValue;
					TotalComments = CommentsList.length;
					PreviousCommentID=null;
					CurrentCommentID=null;
					NextCommentID=null;
					for (var i=0;i<CommentsList.length ;i++ )
					{
						CurrentCommentID=CommentsList[i].getElementsByTagName("CommentID")[0].firstChild.nodeValue;
						if(ThisCommentID==CurrentCommentID){
							CommentNumber=i+1;
							if(i==CommentsList.length-1){
								NextCommentID=CurrentCommentID;
							}
							if(CommentsList[i+1]!=null){
								NextCommentID=CommentsList[i+1].getElementsByTagName("CommentID")[0].firstChild.nodeValue;
							}
							break;
						}
						PreviousCommentID = CurrentCommentID;
					}
					if(PreviousCommentID==null){
						PreviousCommentID = CurrentCommentID;
					}
					
					if (HasComments) {
						CommentSelectorMessage = "Comments " + CommentNumber + " of " + TotalComments;
					}

					Comments="<br/><strong>By: </strong><a href=\"#\">" + UserName + "</a><br/><strong>On: </strong><a href=\"#\">" + InsertDate + "</a><br/><br/>" + Comments.replace(/(\r\n|[\r\n])/g, "<br />") + "<br/><br/>";
				}
			}

		}
		var LoggedIn = $("hIsLoggedIn").value;
		if(CommentWindow==null){
			if(LoggedIn == "true" ||HasComments){
				showPopupWindow();
			}
		}
		$("totalCommentsCount").innerHTML = TotalDocumentComments;
		$("Paragraph" + ActiveParagraphNumber).innerHTML = TotalComments;

		if(HasComments){
			Effect.Fade($("lyr1"),{duration:.3,afterFinish: showCommentsAfterFade});
		}else{
			if(LoggedIn == "true"){
				newComment();
			}
		}
}


function showCommentsAfterFade(Obj){
		$("lyr1").innerHTML = Comments;
		$("commentSelector").innerHTML = CommentSelectorMessage;
		Effect.Appear($("lyr1"),{duration:.3,afterFinish: initScrollingAfterFade});
}

function initScrollingAfterFade(Obj){
		initScrollLayer();
		$("lyr1").style.visibility = "visible";}



function newComment(){
		if($("hIsLoggedIn").value == "false"){
			window.location.href= $("hRedirectURL").value + "?retUrl=" + window.location;
		}else{
			Effect.Fade($("lyr1"),{duration:.3,afterFinish: newCommentafterFade});
		}
}


function newCommentafterFade(Obj){
			$("lyr1").innerHTML = "<p><strong>Enter your comments below:</strong></p><textarea id=\"txtNewComment\" name=\"txtNewComment\" WRAP=\"HARD\" style=\"width:220px; height:170px;\"></textarea><br/><div style=\"padding-top:5px;\"><a href=\"Javascript:;\" onclick=\"Javascript:submitComment();\"><img src=\"/stellent/groups/aia/documents/image/comment_btn_save.gif\"/></a></div>";
			initScrollLayer();
			$("lyr1").style.visibility = "visible";
			Effect.Appear($("lyr1"),{duration:.3});
			if ($("commentAddBtn") != null) { $("commentAddBtn").style.visibility = "hidden"; } 
}



function submitComment(){
			var dDocName = $("hdDocName").value;
			var Comments = $("txtNewComment").value;
			if(Comments==null || Comments.length == 0) {
				alert("Comments are required");
			} else if (Comments.length > 1024) {
				alert("Comments should be less than 1024 characters.");
			} else {
				if ($("commentAddBtn") != null) { $("commentAddBtn").style.visibility = "visible"; }

				var Parameters = "IdcService=INSERT_MARGIN_COMMENT_XML&dDocName=" + dDocName + "&dID=" + ActivedID + "&ParagraphNumber=" + ActiveParagraphNumber + "&Comments=" + Comments;
				var PostBackURL = $("hPostBackUrl").value;
				new Ajax.Request(PostBackURL, {method: "post", parameters: Parameters , onSuccess: handleCommentCallback});
			}

}



function showPopupWindow(){
			var ActiveParagraph = $("Paragraph" + ActiveParagraphNumber);
			var x = findPosX(ActiveParagraph) + xOffSet; 
			var y = findPosY(ActiveParagraph) + yOffSet;
			var CommentWindowHTML = $("hCommentsHTML").value;
			CommentWindow = Builder.node("div",{className:"commentContainer"});
			new Insertion.Top(CommentWindow,CommentWindowHTML);
			Element.setStyle(CommentWindow, {position: "absolute"});
			CommentWindow.style.top = y + "px";
			CommentWindow.style.left = x + "px";
			CommentWindow.style.display = "none";
			$("masterDiv").appendChild(CommentWindow);	
			new Effect.Appear(CommentWindow,{duration:.3});
			ActiveParagraph.className = "commentCountActive";
			Event.observe(window, "resize", handleResize);
}

function handleResize(){
		if(CommentWindow!=null){
			var ActiveParagraph = $("Paragraph" + ActiveParagraphNumber);
			var x = findPosX(ActiveParagraph) + xOffSet; 
			var y = findPosY(ActiveParagraph) + yOffSet;
			CommentWindow.style.top = y + "px";
			CommentWindow.style.left = x + "px";
		}
}

function hideCommentWindowAfterFade(Obj){
		CommentWindow.remove();
		CommentWindow = null;
		Event.stopObserving(window, "resize", handleResize);
}

function normalizeActiveBubble(Obj){
		var ActiveParagraphs = document.getElementsByClassName("commentCountActive");
		for(var i = 0; i<ActiveParagraphs.length; i++){
			ActiveParagraphs[i].style.backgroundImage = "";
			ActiveParagraphs[i].className = "commentCount";
		}
}

function hideCommentHard() {
		var ActiveParagraph = $("Paragraph" + ActiveParagraphNumber);
		CommentWindow.remove();
		CommentWindow = null;
		ActiveParagraph.style.backgroundImage = "";
		ActiveParagraph.className = "commentCount";
		ActiveParagraph = null;
		Event.stopObserving(window, "resize", handleResize);
		clearValues();
}

function hideCommentSoft() {
		var ActiveParagraph = $("Paragraph" + ActiveParagraphNumber);
		new Effect.Fade(CommentWindow,{duration:.3, afterFinish: hideCommentWindowAfterFade});
		normalizeActiveBubble();
		clearValues();						
}
function clearValues(){
		ActiveParagraphNumber = null;
		ActivedID = null;
		PreviousCommentID = null;
		NextCommentID = null;
}

function ToggleMarginComments(){

		var Status = $("margincommentstatus").value;
		if(Status=="1"){
			var ActiveParagraphs = document.getElementsByClassName("commentCountActive");
			for(var i = 0; i<ActiveParagraphs.length; i++){
				ActiveParagraphs[i].style.backgroundImage = "";
				ActiveParagraphs[i].className = "commentCount";
			}
			if(CommentWindow!=null){
				CommentWindow.remove();
				CommentWindow=null;
			}
		}
		var CommentBubbles = $("pageContentLeft").getElementsByClassName("commentCount");
		for(var i=0;i<CommentBubbles.length;i++){
			if(Status=="1"){
				new Effect.Fade(CommentBubbles[i],{duration:.3});
			}else{
				new Effect.Appear(CommentBubbles[i],{duration:.3});
			}
		}
		
		if(Status=="1"){
			$("margincommentsmenuon").src = onOff.src;
			$("margincommentsmenuoff").src = offOn.src;
			$("margincommentstatus").value = 0;
			clearValues();
		}else{
			$("margincommentsmenuon").src = onOn.src;
			$("margincommentsmenuoff").src = offOff.src;
			$("margincommentstatus").value = 1;
		}
}



function callCommentWindowHTMLCache(){
		var URL = "/stellent/fragments/ek_margin_comments_menu/comment.html";
		new Ajax.Request(URL, {method: "get", onSuccess: cacheWindowHTML});

}

function cacheWindowHTML(originalResponse){
		$("hCommentsHTML").value = originalResponse.responseText;
}

function findPosX(obj) {
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
}

function findPosY(obj) {
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curTop += obj.y;
		return curtop;
}

