﻿/////////////////////////////////////////////////////////////////////////// GLOBALS
var g_oCommentViewerInstance	= null;

var	g_sStatusBarElementId		= "eCellTabContentSpacer";

var	g_sDefaultName				= "Anonymous";
var g_sDefaultComment			= "To comment on this trooker set, type here and click submit.";
var g_sCommentSent				= "Your comment was submitted.";
var g_nUserInfoColumnWidth		= 20;
var g_nMaxCommentsPerPage		= 30;



/////////////////////////////////////////////////////////////////////////// CCOMMENTVIEWER
function CCommentViewer(eParent, sSourceID)
{
	//////////////////////////////////// FIELDS
	this.m_eParent			= eParent;
	this.m_eTBodyComments	= null;
	this.m_eInputNickName	= null;
	this.m_eInputText		= null;
	
	this.m_sSourceID				= sSourceID;
	this.m_aCachedComments			= new Array();
	this.m_oCommentBeingRepliedTo	= null;
	
	
	
	//////////////////////////////////// PUBLIC METHODS
	this.Create = function()
	{
		var eTableMain				= CreateElement(this.m_eParent, "table");
		eTableMain.style.height		= "100%";
		eTableMain.style.width		= "100%";
		eTableMain.cellPadding		= 0;
		eTableMain.cellSpacing		= 0;
		eTableMain.style.tableLayout	= "fixed";
		
		var eTBodyMain				= CreateElement(eTableMain, "tbody");
		var	eRowComments			= CreateElement(eTBodyMain, "tr");
		
		var eCellComments			= CreateElement(eRowComments, "td");
		eCellComments.style.height	= "100%";
		eCellComments.style.width	= "100%";
		
		var eDivComments			= CreateElement(eCellComments, "div");
		eDivComments.style.height	= "100%";
		eDivComments.style.width	= "100%";
		eDivComments.style.overflow	= "auto";
		
		var eTableComments			= CreateElement(eDivComments, "table");
		eTableComments.style.width	= "100%";
		eTableComments.style.borderCollapse	= "collapse";
		eTableComments.cellPadding	= 0;
		eTableComments.cellSpacing	= 0;
		this.m_eTBodyComments		= CreateElement(eTableComments, "tbody");
		
		var eRowInput				= CreateElement(eTBodyMain, "tr");
		var	eCellInput				= CreateElement(eRowInput, "td");
		eCellInput.style.width		= "100%";
		var eTableInput				= CreateElement(eCellInput, "table");
		eTableInput.style.width		= "100%";
		eTableInput.style.borderCollapse	= "collapse";
		eTableInput.cellPadding		= 0;
		eTableInput.cellSpacing		= 0;
		var eTBodyInput				= CreateElement(eTableInput, "tbody");
		var eRowCommentInput		= CreateElement(eTBodyInput, "tr");
		
		
		
		var eCellNickName						= CreateElement(eRowCommentInput, "td", "CellCommentInput");
		this.m_eInputNickName					= CreateElement(eCellNickName, "input");
		this.m_eInputNickName.value				= g_sDefaultName;
		this.m_eInputNickName.type				= "text";
		this.m_eInputNickName.style.border		= "0px";
		this.m_eInputNickName.style.backgroundColor	= "transparent";
		this.m_eInputNickName.onfocus			= this.OnInputFocus;
		this.m_eInputNickName.onblur			= this.OnInputBlur;
		this.m_eInputNickName["CCommentViewer"]	= this;
		this.m_eInputNickName["_Name"]			= "nick";
		
		
		var eCellCommentInput				= CreateElement(eRowCommentInput, "td", "CellCommentInput");
		eCellCommentInput.style.borderLeft	= "1px solid #707070";
		eCellCommentInput.style.width		= "100%";
		
		this.m_eInputText					= CreateElement(eCellCommentInput, "textarea", "InputComment");
		this.m_eInputText.style.border		= "0px";
		this.m_eInputText.value				= g_sDefaultComment;
		this.m_eInputText.style.width		= "100%";
		this.m_eInputText.onfocus			= this.OnInputFocus;
		this.m_eInputText.onblur			= this.OnInputBlur;
		this.m_eInputText.onkeydown			= this.OnKeyDown;
		this.m_eInputText["CCommentViewer"]	= this;
		this.m_eInputText["_Name"]			= "input";
		
		var eCellSubmit				= CreateElement(eRowCommentInput, "td", "CellCommentInput CellCommentSubmit");
		
		var eButtonSubmit				= CreateElement(eCellSubmit, "img");
		eButtonSubmit.src				= "images/Send0.png";
		eButtonSubmit["CCommentViewer"]	= this;
		eButtonSubmit.onclick			= this.OnSubmit;
		eButtonSubmit.onmouseover		= Rollover;
		eButtonSubmit.onmouseout		= Rollover;
		eButtonSubmit.onmousedown		= Rollover;
		eButtonSubmit.onmouseup			= Rollover;
	}
	
	this.LoadComments = function()
	{
		g_oCommentViewerInstance	= this;
		var sURL					= g_sGetAllCommentsURL + this.m_sSourceID + "&random=" + (Math.random() * 999999);
		XMLHTTPRequest(sURL, this.OnDataGet);
	}
	
	this.Refresh = function()
	{
		this.ClearComments();
		this.LoadComments();
	}
	
	this.SubmitComment = function(oComment)
	{
		if (this.m_oCommentBeingRepliedTo)	var sReplyArg	= "&rid=" + this.m_oCommentBeingRepliedTo.m_sId;
		else								var sReplyArg	= "&rid=0";
		
		// Validation
		if (oComment.m_sContent == g_sDefaultComment || oComment.m_sContent == "")
		{
			alert("Please enter a comment before submitting.");
			this.m_eInputText.focus();
			return;
		}
		if (oComment.m_sAuthor == "")
		{
			alert("Please enter a valid nickname before submitting.");
			this.m_eInputNickName.focus();
			return;
		}
		
		var sEscapedMessage	= escape(oComment.m_sContent);
		var sURL	= g_sSubmitCommentURL + "id=" + this.m_sSourceID + "&a=nestedAdd&c=" + sEscapedMessage + "&u=" + oComment.m_sAuthor + "&random=" + (Math.random() * 999999) + sReplyArg;
		XMLHTTPRequest(sURL, this.OnDataSet);
		this.m_eInputText.value = g_sCommentSent;
	}
	
	this.ClearComments = function()
	{
		while (this.m_eTBodyComments.childNodes.length != 0) this.m_eTBodyComments.removeChild(this.m_eTBodyComments.childNodes[0]);
	}
	
	
	
	//////////////////////////////////// EVENT HANDLERS
	this.OnDataGet = function()
	{
		if (g_oXMLHTTPRequestObject.readyState != 4) return;	// Cannot do anything if transmission is not complete
		var oRequestedXML	= g_oXMLHTTPRequestObject.responseXML.firstChild.getElementsByTagName("Table");
		
		// No comments
		if (typeof(oRequestedXML) != "undefined")
		{
			for (var nCommentIndex = 0; nCommentIndex < oRequestedXML.length; nCommentIndex++)
			{
				// Retrieve information
				var oCurrentNode		= oRequestedXML[nCommentIndex];
				var sCurrentAuthor		= GetText(oCurrentNode.getElementsByTagName("AddedBy")[0]);
				var sCurrentMessage		= GetText(oCurrentNode.getElementsByTagName("Comment")[0]);
				var sCurrentId			= GetText(oCurrentNode.getElementsByTagName("PlayListCommentID")[0]);
				var vCurrentDepth		= GetText(oCurrentNode.getElementsByTagName("Depth")[0]);
				
				
				// Prepare data
				if (oCurrentNode.getElementsByTagName("ReplyPlayListCommentID").length != 0) var sReplyToId	= GetText(oCurrentNode.getElementsByTagName("ReplyPlayListCommentID")[0]);
				
				if (oCurrentNode.getElementsByTagName("DateAdded").length != 0)
				{
					var sDate = GetText(oCurrentNode.getElementsByTagName("DateAdded")[0]);
					var sFinalDate = sDate.substr(0, sDate.indexOf("T")) + " " + sDate.substr(sDate.indexOf("T") + 1, 8);
				}
				
				if (vCurrentDepth == "")	vCurrentDepth = null;
				else						vCurrentDepth = parseInt(vCurrentDepth) - 1;
				
				while (sCurrentMessage.indexOf("\n") != (-1)) sCurrentMessage = sCurrentMessage.replace("\n", "<br>");
				
				
				// Create CComment object and parent elements
				var oCurrentComment	= new CComment(g_oCommentViewerInstance, sCurrentAuthor, sCurrentMessage, sFinalDate, sCurrentId);
				var eRowNewComment	= CreateElement(g_oCommentViewerInstance.m_eTBodyComments, "tr");
				var eCellNewComment	= CreateElement(eRowNewComment, "td");
				oCurrentComment.Create(eCellNewComment, nCommentIndex, vCurrentDepth, sReplyToId);
				g_oCommentViewerInstance.m_aCachedComments[sCurrentId] = oCurrentComment;
			}
		}
		
		var eStatusBar			= window.document.getElementById(g_sStatusBarElementId);
		if (typeof(oRequestedXML) != "undefined")	eStatusBar.innerHTML	= oRequestedXML.length + " trookies have commented on this trooker set";
		else										eStatusBar.innerHTML	= "No trookies have commented on this trooker set as of yet";
		
		
		g_oCommentViewerInstance.m_bUpdateOnlyLatest = false;
	}
	
	this.OnDataSet = function()
	{
		if (g_oXMLHTTPRequestObject.readyState != 4) return;	// Cannot do anything if transmission is not complete
		//var oRequestedXML	= g_oXMLHTTPRequestObject.responseXML.firstChild;
		
		g_oCommentViewerInstance.Refresh();
		//g_oCommentViewerInstance.m_eInputText.value = g_sDefaultComment;
		
		if (g_oCommentViewerInstance.m_oCommentBeingRepliedTo) g_oCommentViewerInstance.m_oCommentBeingRepliedTo.Unselect();
	}
	
	this.OnSubmit = function(oEvent)
	{
		if (typeof(oEvent) == "undefined")	oEvent		= window.event;
		if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
		if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
		
		var oInstance	= eSrcElement["CCommentViewer"];
		oInstance.SubmitComment(new CComment(oInstance, oInstance.m_eInputNickName.value, oInstance.m_eInputText.value));
	}
	
	this.OnInputFocus = function(oEvent)
	{
		if (typeof(oEvent) == "undefined")	oEvent		= window.event;
		if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
		if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
		var oInstance	= eSrcElement["CCommentViewer"];
		var sName		= eSrcElement["_Name"];				// We are combining two event handlers in one here. This is to know if the name field is being focused or the comment field
		
		if (sName == "nick")
		{
			if (oInstance.m_eInputNickName.value == g_sDefaultName) oInstance.m_eInputNickName.value = "";
		}
		if (sName == "input")
		{
			if (oInstance.m_eInputText.value == g_sDefaultComment || oInstance.m_eInputText.value == g_sCommentSent) oInstance.m_eInputText.value = "";
		}
	}
	
	this.OnInputBlur = function(oEvent)
	{
		if (typeof(oEvent) == "undefined")	oEvent		= window.event;
		if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
		if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
		var oInstance	= eSrcElement["CCommentViewer"];
		var sName		= eSrcElement["_Name"];				// We are combining two event handlers in one here. This is to know if the name field is losing focus or the comment field is
		
		if (sName == "nick")
		{
			if (oInstance.m_eInputNickName.value == "")	oInstance.m_eInputNickName.value	= g_sDefaultName;
		}
		if (sName == "input")
		{
			if (oInstance.m_eInputText.value == "")		oInstance.m_eInputText.value		= g_sDefaultComment;
		}
	}
	
	this.OnKeyDown = function(oEvent)
	{
		if (typeof(oEvent) == "undefined")	oEvent		= window.event;
		if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
		if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
		var oInstance	= eSrcElement["CCommentViewer"];
		
		if (oEvent.keyCode == 13 && oEvent.ctrlKey)
		{
			oInstance.SubmitComment(new CComment(oInstance, oInstance.m_eInputNickName.value, oInstance.m_eInputText.value));
			oInstance.m_eInputText.value = "";
		}
	}
	
	
	
	//////////////////////////////////// CONSTRUCTOR
	this.Create();
}



/////////////////////////////////////////////////////////////////////////// CCOMMENT
function CComment(oParentCommentViewer, sAuthor, sContent, sDate, sId)
{
	//////////////////////////////////// FIELDS
	this.m_sAuthor		= sAuthor;
	this.m_sContent		= sContent;
	this.m_sDate		= sDate;
	this.m_sId			= sId;
	
	this.m_eParent;
	this.m_nDepth;
	this.m_nReplyId;
	this.m_sStripedClass;
	
	this.m_oParentCommentViewer = oParentCommentViewer;
	
	this.m_eCellAuthor;
	this.m_eCellMessage;
	this.m_eCellTimeStamp;
	
	this.m_bSelectedForReply = false;
	
	
	
	//////////////////////////////////// PUBLIC METHODS
	this.Create = function(eParent, nStripingIndex, nDepth, nReplyId)
	{
		this.m_eParent	= eParent;
		this.m_nDepth	= nDepth;
		this.m_nReplyId	= nReplyId;
		
		if (nDepth > 0) var sReplyText	= "'s reply";
		else			var sReplyText	= "";
		
		if ((nStripingIndex % 2) == 1)	var bOdd = true;
		else							var bOdd = false;
		this.m_sStripedClass	= bOdd ? "RowOdd" : "RowEven";
		
		
		// Create elements
		this.m_eTableContainer						= CreateElement(eParent, "table");
		this.m_eTableContainer.style.width			= "100%";
		this.m_eTableContainer.cellPadding			= 0;
		this.m_eTableContainer.cellSpacing			= 0;
		this.m_eTableContainer.style.borderCollapse	= "collapse";
		var eTBodyContainer					= CreateElement(this.m_eTableContainer, "tbody");
		var eRowContainer					= CreateElement(eTBodyContainer, "tr");
		
		var eCellIndent						= CreateElement(eRowContainer, "td");
		if (nDepth)
		{
			eCellIndent.style.width			= nDepth * (g_nUserInfoColumnWidth + 7);
			eCellIndent.innerHTML			= "&nbsp;";
		}
		
		var eCellContainer					= CreateElement(eRowContainer, "td");
		this.m_eTable						= CreateElement(eCellContainer, "table", "CellComment");
		if (nDepth) this.m_eTable.className += "Nested";
		this.m_eTable.className				+= " " + this.m_sStripedClass;
		this.m_eTable.style.width			= "100%";
		this.m_eTable.cellPadding			= 0;
		this.m_eTable.cellSpacing			= 0;
		this.m_eTable.style.borderCollapse	= "collapse";
		var eTBody							= CreateElement(this.m_eTable, "tbody");
		var eRowHeader						= CreateElement(eTBody, "tr");
		
		this.m_eCellAuthor					= CreateElement(eRowHeader, "td", "CellAuthor");
		var eNoBrAuthor						= CreateElement(this.m_eCellAuthor, "nobr");
		var eSpanAuthor						= CreateElement(eNoBrAuthor, "span", null, this.m_sAuthor);
		if (this.m_sAuthor.toLowerCase() == g_sDefaultName.toLowerCase()) eSpanAuthor.style.fontStyle = "italic";
		var eSpanReplyText					= CreateElement(eNoBrAuthor, "span", null, sReplyText);
		eSpanReplyText.style.fontStyle		= "italic";
		
		this.m_eCellTimeStamp				= CreateElement(eRowHeader, "td", "CellTimeStamp");
		var eNoBrTimeStamp					= CreateElement(this.m_eCellTimeStamp, "nobr", null, this.m_sDate);
		
		// Depth limit is 4 (0 based index), also ignore older comments that lack depth attributes
		if (this.m_nDepth < 4 || this.m_nDepth == null)
		{
			var eCellReply						= CreateElement(eRowHeader, "td");
			eCellReply.style.width				= "100%";
			var eSpanBracket1					= CreateElement(eCellReply, "span", "SpanReplyBracket", "[");
			this.m_eSpanReply					= CreateElement(eCellReply, "span", "SpanReply", "Reply");
			var eSpanBracket2					= CreateElement(eCellReply, "span", "SpanReplyBracket", "]");
			this.m_eSpanReply["CComment"]		= this;
			this.m_eSpanReply.onclick			= this.OnReply;
		}
		else
		{
			var eCellReply						= CreateElement(eRowHeader, "td");
			eCellReply.style.width				= "100%";
		}
		
		var eRowMessage						= CreateElement(eTBody, "tr");
		this.m_eCellMessage					= CreateElement(eRowMessage, "td", "CellMessage");
		this.m_eCellMessage.innerHTML		= this.m_sContent;
		this.m_eCellMessage.colSpan			= 3;
		this.m_eCellMessage.scrollIntoView(false);
	}
	
	this.SelectForReply = function()
	{
		if (this.m_bSelectedForReply)
		{
			this.Unselect();
			return;
		}
		
		if (this.m_oParentCommentViewer.m_oCommentBeingRepliedTo) this.m_oParentCommentViewer.m_oCommentBeingRepliedTo.Unselect();	
		
		this.m_eTable.className			= "RowSelected CellComment";
		if (this.m_nDepth) this.m_eTable.className += "Nested";
		
		this.m_eCellAuthor.className	= "CellAuthorSelected";
		this.m_eSpanReply.className		= "SpanReplySelected";
		this.m_eSpanReply.innerHTML		= "Cancel";
		this.m_oParentCommentViewer.m_oCommentBeingRepliedTo = this;
		this.m_oParentCommentViewer.m_eInputText.focus();
		this.m_oParentCommentViewer.m_eInputText.parentNode.className	= "CellCommentInputActive";
		this.m_oParentCommentViewer.m_eInputText.className				= "InputCommentActive";
		this.m_bSelectedForReply		= true;
	}
	
	this.Unselect = function()
	{
		this.m_eTable.className			= this.m_sStripedClass + " CellComment";
		if (this.m_nDepth) this.m_eTable.className += "Nested";
		
		this.m_eCellAuthor.className	= "CellAuthor";
		this.m_eSpanReply.className		= "SpanReply";
		this.m_eSpanReply.innerHTML		= "Reply";
		this.m_oParentCommentViewer.m_eInputText.parentNode.className	= "CellCommentInput";
		this.m_oParentCommentViewer.m_eInputText.className				= "InputComment";
		
		this.m_oParentCommentViewer.m_oCommentBeingRepliedTo = null;
		this.m_bSelectedForReply		= false;
	}
	
	
	
	//////////////////////////////////// EVENTS
	this.OnDataSet = function()
	{
		if (g_oXMLHTTPRequestObject.readyState != 4) return;	// Cannot do anything if transmission is not complete
		
		// Should be checking for possible errors here
		
		g_oCommentViewerInstance.Refresh();
	}
	
	this.OnReply = function(oEvent)
	{
		if (typeof(oEvent) == "undefined")	oEvent		= window.event;
		if (oEvent.srcElement)				eSrcElement	= oEvent.srcElement;
		if (oEvent.originalTarget)			eSrcElement	= oEvent.originalTarget;
		var oInstance	= eSrcElement["CComment"];
		
		oInstance.SelectForReply();
	}
}