/**
 * @author Alex van Niel
 * @version 1.0.1 build 20080616
 */
function clsTreeMenu(p_oMenuNode,p_oArguments){
	this.m_oMenuNode = p_oMenuNode;
	this.m_oCoolTree = p_oArguments.cooljsmenu;
	
}

clsTreeMenu.prototype.buildMenu = function(p_mixSelected){
	var bResult = false;
	var oMenuNode = null;
	var aMenuArray = new Array("",null,null); // new Array();
	var nIndex = 0;

	if( (typeof(p_mixSelected) != 'undefined') && Object.isArray(p_mixSelected) ){
		oMenuNode = this.getNode(p_mixSelected);
	}else if( typeof(p_mixSelected) == 'object' ){
		oMenuNode = p_mixSelected;
	}

	bResult = this.parseMenu(oMenuNode,aMenuArray);

	//if(bResult) bResult = aMenuArray;

	this.m_oCoolTree.getRoot().recreate(aMenuArray);
	RedrawAllTrees();

	return bResult;
}

clsTreeMenu.prototype.selectElement = function( p_aPath,p_nSubPage ){
	var sNodeId = '';
	var oMenuNode = null;
	var oNode = null;
	var sTarget = '';
	var sUrl = '';
	var aPath = new Array();
	var nIndex = 0;

	if( typeof(p_aPath) != 'undefined' ){
		if( Object.isArray(p_aPath) ){
			aPath = p_aPath;
		} else if( typeof(p_aPath) == 'number' ) {
			aPath.push(nNodeId); 
		}

		this.m_oCoolTree.collapseAll();

		for(nIndex = 0; nIndex < aPath.length; nIndex++){
			oMenuNode = this.getNode(aPath.slice(0,nIndex+1));

			if( oMenuNode ){
				nNodeId = oMenuNode.getNodeId();  
				oNode = this.m_oCoolTree.nodeByID(nNodeId);
			}

			if( oNode ){
				g_oJsMenuTree.expandNode(oNode.index,false,true);
				if( nIndex == (aPath.length-1) ){
					sTarget = oNode.getTarget();
					sUrl = oNode.getUrl();
	
					if( typeof(p_nSubPage) != 'undefined' ){
						sUrl += '&subpage='+p_nSubPage;
					}

					if( sUrl.length ){
						if(sTarget.length){
							if(sTarget == '_self'){
								window.location.href = sUrl;
							} else {
								var oTarget = $(sTarget);
								if(oTarget && oTarget.src !== undefined){
									oTarget.src = sUrl;
								} else {
									window.open(sUrl);
								}
							}
						} else {
							window.open(sUrl);
						}
					} else {
						changeFrameContents('iframe_content','content.php');
					}
				}
			}
		}
	}
}

clsTreeMenu.prototype.parseMenu = function(p_oMenuNode,p_aMenuArray){
	var bResult = false;
	var aMenuArray = new Array();
	var aMenuItem = null;
	var sBuffer = '';

	if( p_oMenuNode && p_oMenuNode.hasChildren && p_oMenuNode.hasChildren() ){

		oMenuNode = p_oMenuNode.firstChild();

		while(oMenuNode){
			aMenuItem = new Array();

			aMenuItem.push({id: oMenuNode.getNodeId()});

			sBuffer = '<div align="left" valign="top" class="menu_nodebg">';
			sBuffer += '<div align="left" valign="top" style="cursor: hand; cursor: pointer;">'+oMenuNode.getNodeName()+'</div>';
			sBuffer += '<table cellpadding="0" cellspacing="0" border="0" height="1" width="100%"><tr><td align="left" valign="top" height="1" width="100%" style="background-color: #80a612; height: 1px; width: 100%; valign: top;"></td></tr></table>';
			sBuffer += '</div>';
			aMenuItem.push(sBuffer);

			aMenuItem.push(oMenuNode.getNodeData().url);
			aMenuItem.push(oMenuNode.getNodeData().target);

			if(oMenuNode.hasChildren()) this.parseMenu(oMenuNode,aMenuItem);

			if(Object.isArray(p_aMenuArray)){
				p_aMenuArray.push(aMenuItem);
			} else {
				p_aMenuArray = aMenuItem;
			}

			oMenuNode = p_oMenuNode.nextChild();
		}
		
		bResult = true;
	}
	
	return bResult;
}

clsTreeMenu.prototype.getNodePathByID = function(p_nNodeId){
	var aNodePath = false;

	if( typeof(p_nNodeId) != 'undefined' ){
		aNodePath = this.getNodePathByID_recurse(this.m_oMenuNode,p_nNodeId);
	}

	return aNodePath;
}

clsTreeMenu.prototype.getNodePathByID_recurse = function(p_oParentNode,p_nNodeId,p_aNodePath){
	var oParentNode = p_oParentNode;
	var oChildNode = null;
	var bFound = false;

	if( !Object.isArray(p_aNodePath) ){
		var aNodePath = new Array();
	} else {
		var aNodePath = p_aNodePath.concat(new Array());
	}

	aNodePath.push(0);
	var nIndex = aNodePath.length-1;

	if( typeof(p_nNodeId) != 'undefined' ){
		if( oParentNode && oParentNode.hasChildren() ){
			oChildNode = oParentNode.firstChild();
			while( oChildNode && !bFound ){
				if( oChildNode.getNodeId() == p_nNodeId){
					bFound = true;
					continue;
				}

				mixResult = this.getNodePathByID_recurse(oChildNode,p_nNodeId,aNodePath);
				if( mixResult && Object.isArray(mixResult) ){
					aNodePath = mixResult;
					bFound = true;
					continue;
				}

				oChildNode = oParentNode.nextChild();
				aNodePath[nIndex]++;
			}
		}
	}
	
	return (bFound?aNodePath:false);
}

clsTreeMenu.prototype.getNode = function(p_aPath){
	var oMenuNode = null;

	if( p_aPath ){
		if( Object.isArray(p_aPath) && p_aPath.length ){
			oMenuNode = this.m_oMenuNode;
	
			for(var nIndex = 0;nIndex < p_aPath.length; nIndex++){
				oMenuNode = oMenuNode.getChild(p_aPath[nIndex]);
			}
		} else if( typeof(p_aPath) == 'number' ){
			oMenuNode = this.m_oMenuNode;
			oMenuNode = oMenuNode.getChild(p_aPath)
		}
	}
	
	return oMenuNode;
}
