//-----------------------------------------------------------------------------
// keywordMenu.js :: used by /kbase
// 2004.11.23 - written by rk7138 for SBC Internet Services
//-----------------------------------------------------------------------------
// -- assumes previewHTML.js is available
// -- assumes switchMenu.js is available
//-----------------------------------------------------------------------------
// getKeywordMenuHTML(keywordObj, strShowCheckBox)
//-----------------------------------------------------------------------------
// keywordObj is an array of objects as follows:
// { id:ID, title:String, nodeObj:nodeObj}
//
// nodeObj is an array of objects as follows:
// { id:String, title:String}
//
// strShowCheckBox if not empty will create checkboxes which
// appears before each entry named per strShowCheckBox.
// 
// keywordMatches must either be null or a list of keyword IDs  matches for a given documentID
//-----------------------------------------------------------------------------
function getKeywordMenuHTML(keywordObj, keywordMatches, strShowCheckBox){
	var html = "";
	var instructions = "<div class='BlackSideText'>Description<br/><div class='DefaultGrayText' style='font-weight:normal;'>Keywords are listed with the number of documents to which they are associated.  Clicking upon a keyword link will allow managing documents assigned to the keyword, or deletion of the keyword.  Clicking upon a document link will allow managing or deletion of the document.</div></div><br/>";
	var nodeBase = kbase_assign_keywords;
		
	if (keywordMatches == null){ keywordMatches = [0]; }
	if (strShowCheckBox != null && strShowCheckBox.length > 0){ nodeBase = kbase_delete_doc; }
	if(keywordObj == ''){ return $empty(); }
	
	html += instructions;
	html += "<div>\n";	
	
	for (var i = 0; i < keywordObj.length; i++){
		itemCount ++;
		
		var elObj = keywordObj[i];		
		var id = elObj.id;
		var title = elObj.title;
		var nodeObj = elObj.nodeObj;			

		var foldId = docPrefix + itemCount;				
		var headerId = headerPrefix + foldId;
		var buttonId = buttonPrefix + foldId;
		var listId = listPrefix + foldId;

		var nodeId = kbase_manage_keywords + id;

		var checkBoxTag = '';
		var checkBoxSpacer = '';
		var foldSrc = foldGIF;
		var clickCode = '';
		var bHasNodes = (nodeObj != null && nodeObj.length > 0 && nodeObj != null) ? true : false;		
		
		if (strShowCheckBox != null && strShowCheckBox.length > 0){
			var styleClass = 'topicMenuCheckBox';
			var checkedItem = checkMatchKey(keywordMatches, id);
			if (i == 0){ styleClass = 'topicMenuCheckBoxFirst'; }
			checkBoxTag = "<input class='" + styleClass + "' type='checkbox' name='" + strShowCheckBox + "' value='" + id + "' " + checkedItem + "/>";	
			checkBoxSpacer = "<img class='topicMenuCheckBoxSpacer' src='" + spacerGIF + "' border='0'/>";
		}
					
		if (bHasNodes){
			title = "<span style='font-weight: normal;'>[ " + nodeObj.length + " ]</span>&nbsp;" + "<a href='" + nodeId + "'>" + title + "</a>";
			foldSrc = foldGIF; 
			clickCode = "style='cursor: hand;' onclick='toggleList(this);'"; 
		}
		else { 
			title = "<span style='font-weight: normal;'>[ <i>none</i> ]</span>&nbsp;" + "<a href='" + nodeId + "'>" + title + "</a>";
			foldSrc = nofoldGIF; 
			clickCode = ""; 
		}
		
		html += "	<div id='" + headerId + "'><span>" + checkBoxTag + "<img id='" + buttonId + "' src='" + foldSrc + "' border='0' class='topicMenuImg' " + clickCode + "><span class='topicMenuTitle'>" + title + "</span></span></div>\n";
				
		if (bHasNodes){
			html += "	<div><span>\n";		
			html += "		<div id='" + listId + "' style='display:none;'>\n";	
			

				
			for (var j = 0; j < nodeObj.length; j++){
				var nodeId = nodeBase + nodeObj[j].id;
				var nodeTitle = 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; }	
				html += "			<div><span>" + checkBoxSpacer + "<img src='" + linkSrc + "'/><img src='" + nodocGIF + "'/>" + nodeRef + "</span></div>\n";
			}
			html += "		</div>\n";
			html += "	</span></div>\n";			
		}
	}
	html += "</div>\n";		

	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 "";
}