//-----------------------------------------------------------------------------
// topicMenu.js :: used by /kbase
// 2004.11.23 - written by rk7138 for SBC Internet Services
//-----------------------------------------------------------------------------
// -- assumes previewHTML.js is available
// -- assumes switchMenu.js is available
//-----------------------------------------------------------------------------
// getTopicMenuHTML(topicObj, strShowCheckBox, bWrapdiv, givenNumPly, bNoSubCheckBoxes)
//-----------------------------------------------------------------------------
// nodeObj is an array of objects as follows:
// { id:String, title:String}
//
// subtopicObj is an array of objects as follows:
// { id:String, title:String, nodeObj:nodeObj}
//
// topicObj is an array of objects as follows:
// { id:String, title:String, nodeObj:nodeObj, subtopicObj:subtopicObj}
//
// strShowCheckBox if not empty will create checkboxes which
// appears before each entry named per strShowCheckBox.
//
// bNoSubCheckBoxes if true will prevent checkboxes being shown for subtopicObj
// 
// topicMatches must either be null or a list of topicIDs matches for a given documentID
//-----------------------------------------------------------------------------
function getTopicMenuHTML(topicObj, topicMatches, strShowCheckBox, bWrapdiv, givenNumPly, bNoSubCheckBoxes){
	var html = "";
	var numPly = (givenNumPly != null) ? givenNumPly : 0;
	var nodeBase = kbase_assign_topics;
	var instructions = "<div class='BlackSideText'>Description<br/><div class='DefaultGrayText' style='font-weight:normal;'>Topics are shown with a count of their subtopics and a count of their associated documents. Sub-topics are shown with a count of their associated documents. Clicking on a link for a topic or sub-topic will present the interface for managing documents assigned to that element as well as presenting an option to delete it.  Clicking upon a document link will allow management or deletion of it.</div></div><br/>"; 
	
	if (strShowCheckBox != null && strShowCheckBox.length > 0){ nodeBase = kbase_delete_doc; }
	if (numPly == 0 && topicObj == ''){ return $empty(); }
	if (numPly == 0){ html += instructions; }
	if (bWrapdiv){ html += "<div>\n"; }
	
	for (var i = 0; i < topicObj.length; i++){
		itemCount++;
		
		var elObj = topicObj[i];	
		var id = elObj.id;
		var title = elObj.title;
		var nodeObj = elObj.nodeObj;
		var subtopicObj = elObj.subtopicObj;

		var bHasNodes = (nodeObj != null && nodeObj.length > 0) ? true : false;
		var bHasSubs = (typeof subtopicObj != undefined && subtopicObj != null && subtopicObj.length > 0) ? true : false;
		
		var foldId = docPrefix + itemCount;
		var headerId = headerPrefix + foldId;
		var buttonId = buttonPrefix + foldId;
		var listId = listPrefix + foldId;
		
		var nodeId = kbase_manage_topics + id;

		var checkBoxTag = '';
		var checkBoxSpacer = '';
		if (strShowCheckBox != null && strShowCheckBox.length > 0){
			var styleClass = 'topicMenuCheckBox';
			var checkedItem = checkMatchKey(topicMatches, id);
			if (numPly > 0 || i == 0){ styleClass = 'topicMenuCheckBoxFirst'; }
			
			checkBoxTag = "<input class='" + styleClass + "' type='checkbox' name='" + strShowCheckBox + "' value='" + id + "' " + checkedItem + "/>";	
			checkBoxSpacer = "<img class='topicMenuCheckBoxSpacer' src='" + spacerGIF + "' border='0'/>";
		}
		
		if (bNoSubCheckBoxes && numPly > 0){ checkBoxTag = ''; }

		var subTagText = '<i>none</i>';
		var nodeTagText = '<i>none</i>';
		if (bHasNodes){ nodeTagText = "{ " + nodeObj.length + " }"; }
		if (bHasSubs){ subTagText = subtopicObj.length; }
		
		var nesting = '';
		var bLastItem = (i == (topicObj.length - 1)) ? true : false;
		if (numPly == 1){
			subTagText = '';
			var nestSrc = linkSrc_mid;
			if (bLastItem){ nestSrc = linkSrc_end; }
			nesting = checkBoxSpacer + "<img src='" + nestSrc + "' border='0'/>";			
		}
		
		var foldSrc = foldGIF;
		var clickCode = '';
		if (bHasNodes || bHasSubs){ 
			foldSrc = foldGIF; 
			clickCode = "style='cursor: hand;' onclick='toggleList(this);'"; 
		}
		else { 
			foldSrc = nofoldGIF; 
			clickCode = ""; 
		}
				
		title = "<span style='font-weight: normal;'>[ " + subTagText + " " + nodeTagText + " ]</span>&nbsp;" + "<a href='" + nodeId + "'>" + title + "</a>";
		
		html += "	<div id='" + headerId + "'><span>" + nesting + checkBoxTag + "<img id='" + buttonId + "' src='" + foldSrc + "' border='0' class='topicMenuImg' " + clickCode + "><span class='topicMenuTitle' >" + title + "</span></span></div>\n";
		html += "	<div><span>\n";		
		html += "		<div id='" + listId + "' style='display:none;'>\n";

		if (bHasNodes){			
			for (var j = 0; j < nodeObj.length; j++){
				var nodeId = nodeBase + nodeObj[j].id;
				var nodeTitle = "&nbsp;" + nodeObj[j].title;
				var nodeRef = "<span class='topicMenuNodeRef' ><a href='" + nodeId + "'>" + nodeTitle + "</a></span>";
				var linkSrc = linkSrc_mid;
				
				if (nodeObj.length == 1 || j == (nodeObj.length - 1)){ linkSrc = linkSrc_end; }				
				if (bHasSubs){ linkSrc = linkSrc_mid; }

				var nesting = '';
				if (numPly > 0){ 
					if (bLastItem != true){ nestSrc = nestingGIF; }
					else { nestSrc = spacerGIF; }
					nesting = "<img src='" + nestSrc + "' border='0'/'>"; 

					var checkBoxSpacer = '';
					if (strShowCheckBox != null && strShowCheckBox.length > 0){					
						checkBoxSpacer = "<img class='topicMenuCheckBoxSpacer' src='" + spacerGIF + "' border='0'/>";
						if (bNoSubCheckBoxes && numPly > 0){ checkBoxSpacer = ''; }
						nesting = nesting + checkBoxSpacer;
					}
				}
				
				var checkBoxSpacer = '';
				if (strShowCheckBox != null && strShowCheckBox.length > 0){					
					checkBoxSpacer = "<img class='topicMenuCheckBoxSpacer' src='" + spacerGIF + "' border='0'/>";
				}				
				nesting = checkBoxSpacer + nesting;
								
				html += "			<div><span>" + nesting + "<img src='" + linkSrc + "'/><img src='" + nodocGIF + "'/>" + nodeRef + "</span></div>\n";
			}
		}
		if (bHasSubs){
			html += getTopicMenuHTML(subtopicObj, topicMatches, strShowCheckBox, false, (numPly + 1), bNoSubCheckBoxes); 	
		}

		html += "		</div>\n";
		html += "	</span></div>\n";
	}
	
	if (bWrapdiv){ html += "</div>"; }
	
	return html;
}

function $empty (){
	return ("<div><span>NO RECORDS FOUND</span></div>");
}

function checkMatchKey (matchList, givenID){
	for (var q = 0; q < matchList.length; q++){
		if (matchList[q] == givenID){ return "checked"; }
	}
	
	return "";
}
