/////////////////////////////////////////////////////////////////////////////
// Function : siteMap
// Comments : 
/////////////////////////////////////////////////////////////////////////////

function SiteMap(strShowHome, columns, doDebug)
{
	try{
		this.m_showHome   = false;
		this.m_debug = false;
		this.m_columns = 3;  // default columns
		this.m_columnWidth = 33; //default percent based on columns
		
		if (columns && !isNaN(parseInt(columns, 10)))
			this.m_columns = parseInt(columns, 10);
				
		this.m_columnWidth = parseInt(100/this.m_columns, 10);
			
		if (doDebug)
			this.m_debug=true;
		
		this.m_NavPath    = g_navNode_Path;
				
		SiteMap.prototype.display = siteMap_display;
		SiteMap.prototype.buildSiteMap = siteMap_buildSiteMap;
		SiteMap.prototype.displayNode = siteMap_displayNode;
	
		if (strShowHome == 'true')
			this.m_showHome = true;
	}catch(e){
		g_errorHandler('SiteMap',e);
	}
}
	
function siteMap_display (node)
{
	var siteMapArray = this.buildSiteMap(node);
	var out = new Array();
	var i = 0;
	out[i++] = '<table style="width:100%;" cellspacing="0" cellpadding="0" border="' + (this.m_debug ? 1 : 0) + '"><tr valign="top">';
	for (var x=0;x<siteMapArray.length;x++){
		out[i++] = '<td';
		if (x < this.m_columns - 1)
			out[i++] = ' style="width: ' + this.m_columnWidth + '%;"';
		out[i++] = '>' + siteMapArray[x] + '</td>';
	}
	out[i++] = '</tr></table>';
	
	if (this.m_debug)
		alert(out.join("\n"));
		
	document.write(out.join("\n"));
	
}

function siteMap_buildSiteMap(node)
{
	try{
		var columnCounter = 0;
		var ds = new Array();
		
		for (var i=0;i<this.m_columns;i++)
			ds[i]='';
			
		if (this.m_showHome)	   
		{				
			ds[columnCounter] = '<div class="nodeLevel_0"><a href="' + node.m_href + '">' + node.m_label + '</a></div>';		
		}	
		if (node.m_subNodes && node.m_subNodes.length > 0){
			for (var x=0;x<node.m_subNodes.length;x++){
				// skip admin-only sections (unless admin)
				if (!g_isAdmin && node.m_subNodes[x].cp_AdminOnly == 'TRUE')
					continue;
					
				ds[columnCounter] += this.displayNode(node.m_subNodes[x]);
				columnCounter++;
				if (columnCounter == this.m_columns)
					columnCounter = 0;
			}
		}
		/*if (this.m_debug) {
			for (var y=0;y<this.m_columns;y++)
				alert(ds[y]);
		}*/
		
		return ds;
	}catch(e){
		g_errorHandler("siteMap_buildSiteMap", e);
	}
}

function siteMap_displayNode(theNode){
	try {
		var theNodeLevelNum = parseInt(theNode.m_level - 1);
		var debugNodeLevelNum = '';
		if (parseInt(theNode.m_level - 1) > 6) theNodeLevelNum = 6; 
			
		var result = '<a href="' + theNode.m_href + '">' + theNode.m_label + '</a>';
		  	if (theNodeLevelNum < 1 && theNode.cp_SectionDescription != '' && theNode.cp_SectionDescription != undefined)
		  		result += '<br /><span class="sectionDescription">' + theNode.cp_SectionDescription + '</span>';
		  		
		  	
		  	
			if (theNode.m_subNodes.length>0){
			  for (var x=0;x<theNode.m_subNodes.length;x++){
				result += this.displayNode(theNode.m_subNodes[x]);						  
			  }		
			  
		  }
	}catch(e){
		g_errorHandler("siteMap_displayNode", e);
	}finally{	
		if (this.m_debug) {
			debugNodeLevelNum = theNodeLevelNum + ' ';	
		}
		return '<div class="nodeLevel_' + theNodeLevelNum + '">' + debugNodeLevelNum + result + '</div>\n';
	}
}