/*
Comment Relation Highlight Script 
3rd April 2004 - by Mark Wubben 
 
This script was created especially for Dunstan Orchard to abstract his own comment relation highlight script from the XHTML, in order to validate the XHTML. 
 
Copyright 2004 Mark Wubben
This software is licensed under the CC-GNU LGPL
http://creativecommons.org/licenses/LGPL/2.1/
*/

function insertGuide()
	{
	var parentElement = document.getElementById('cmnts');
	var insertBefore = document.getElementById('cmnts-list');
	var insertElement = document.createElement('p');
	insertElement.className = 'alert';
	insertElement.innerHTML = '<strong>Comment relationships:</strong> Clicking on a comment will highlight it and all those comments that relate to it. A <span style="color: #e07c22;">red border shows the comment in focus</span>, <span style="color: #a3c169;">green borders signify parent comments</span>, <span style="color: #87b3cf;">blue borders signify child comments</span>. <a href="/blog/archive/2003/11/12/comments/" title="Read more about this system">Read more&#8230;</a>';
	parentElement.insertBefore(insertElement, insertBefore);
	}


// using a namespace
var Comments = {
	select : function select(e, oCommentNode)
		{
		var i;
		if (oCommentNode == null)
			{
			//var oCommentNode = e != null ? e.currentTarget : window.event.srcElement;
			var oCommentNode = window.event ? window.event.srcElement : e.currentTarget;
			}
		while (oCommentNode.nodeName.toLowerCase() != "li" && oCommentNode.parentNode != null)
			{ 
			oCommentNode = oCommentNode.parentNode; 
			} 

		var sCommentId = oCommentNode.getAttribute("id");
		var bHasRelations = false;
		if (Comments.tableRelations[sCommentId] != null)
			{
			var oRelations = Comments.getRelations(sCommentId);
			bHasRelations = true;
			}

		// reset previous selected comments
		// do this even if there are no table relations
		if (Comments.sSelectedId != null && Comments.sSelectedId != sCommentId)
			{
			var oSelectedRelations = Comments.getRelations(Comments.sSelectedId);
			// add all relation ids into one big array
			var collLoop = oSelectedRelations.parents.concat(oSelectedRelations.children);
			// loop through the array and reset all the class names
			for (i = 0; i < collLoop.length; i++)
				{
				document.getElementById(collLoop[i]).className = "";
				}
			document.getElementById(Comments.sSelectedId).className = "";
			}

		// check if there are table relations
		if (bHasRelations == false)
			{
			// set class of selected <li>
			oCommentNode.className += " this";

			// assign selected id
			Comments.sSelectedId = sCommentId;
			return;
			}

		// loop through parents and set class
		for (i = 0; i < oRelations.parents.length; i++)
			{
			document.getElementById(oRelations.parents[i]).className = "parent";
			}

		// loop through children and set class
		for (i = 0; i < oRelations.children.length; i++)
			{
			document.getElementById(oRelations.children[i]).className = "child";
			}
		
		// set class of selected <li>
		oCommentNode.className = "this";

		// assign selected id
		Comments.sSelectedId = sCommentId;
		},

	// fetch all relations
	getRelations : function getRelations(sCommentId)
		{
		// create blank arrays so no errors occur if nothing is returned
		var oResult = {parents : [], children : []};
		if (Comments.tableRelations[sCommentId] != null)
			{
			if (Comments.tableRelations[sCommentId].parents != null)
				{
				oResult.parents = Comments.tableRelations[sCommentId].parents;
				}
			if (Comments.tableRelations[sCommentId].children != null)
				{
				oResult.children = Comments.tableRelations[sCommentId].children;
				}
			}
			return oResult;
		},

	// initiate everything
	init : function init()
		{
		var oCommentContainer = document.getElementById("cmnts-list");
		var oChild = oCommentContainer.firstChild;
		// loop through all the children of the <ol>
		do
			{
			if (oChild.nodeType == 1 && oChild.nodeName.toLowerCase() == "li")
				{
				addEvent(oChild, "click", Comments.select);
				addEvent(oChild, "focus", Comments.select);
				}
			oChild = oChild.nextSibling;
			}

		while(oChild != null);
		},

	// reset the selected item
	sSelectedId : null
	}

// load the whole thing
addEvent(window, "load", Comments.init);
addEvent(window, "load", insertGuide);
