/*---------------------------------------------------------------------------------
Maintenance Log:
Date		Responsible		Comments
---------------------------------------------------------------------------------
2006
Mar 24		Kintat KO       Customizations: 
                            - ctlNavTr_initMenu()
                            auto-clicking of menu for each sub
                            page by using var 'ctl_prvClickedNavTrNode' implemented.
                            - ctlNavTr_showMenu()
                            ad hoc (TODO)
2008
Nov 21		Kintat KO       Rev:
							ctlNavTr_showMenu()
---------------------------------------------------------------------------------*/

<!-- Paste this code into an external JavaScript file named: navTree.js  -->

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Travis Beckham :: http://www.squidfingers.com | http://www.podlob.com
version date: 06/02/03 :: If want to use this code, feel free to do so,
but please leave this message intact. (Travis Beckham) */

// Global vars:
// Menu Functions & Properties

var ctlNavTr_activeMenu = null;

// Node Functions
if(!window.Node){
  var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
}

function ctlNavTr_checkNode(node, filter){
  return (filter == null || node.nodeType == Node[filter] || node.nodeName.toUpperCase() == filter.toUpperCase());
}

function ctlNavTr_getChildren(node, filter){
  var result = new Array();
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    if(ctlNavTr_checkNode(children[i], filter)) result[result.length] = children[i];
  }
  return result;
}

function ctlNavTr_getChildrenByElement(node){
  return ctlNavTr_getChildren(node, "ELEMENT_NODE");
}

function ctlNavTr_getFirstChild(node, filter){
  var child;
  var children = node.childNodes;
  for(var i = 0; i < children.length; i++){
    child = children[i];
    if(ctlNavTr_checkNode(child, filter)) return child;
  }
  return null;
}

function ctlNavTr_getFirstChildByText(node){
  return ctlNavTr_getFirstChild(node, "TEXT_NODE");
}

function ctlNavTr_getNextSibling(node, filter){
  for(var sibling = node.nextSibling; sibling != null; sibling = sibling.nextSibling){
    if(ctlNavTr_checkNode(sibling, filter)) return sibling;
  }
  return null;
}
function ctlNavTr_getNextSiblingByElement(node){
        return ctlNavTr_getNextSibling(node, "ELEMENT_NODE");
}

function ctlNavTr_showMenu() {
  if(ctlNavTr_activeMenu){
    ctlNavTr_activeMenu.className = "";
	
	// CTL REVAMP 2008: ad hoc patch:
	// add extra null-chk for nxtSib to guard against case where there
	// is no nxt sib obj.
	var nxtSib = ctlNavTr_getNextSiblingByElement(ctlNavTr_activeMenu);	
	if (nxtSib != null) {
		nxtSib.style.display = "none";
	}
  }
  if(this == ctlNavTr_activeMenu){
    ctlNavTr_activeMenu = null;
  } else {
    this.className = "active";
    
    // CTL logics: 24.mar.2006
    // TODO: menu items without child sub-menues
    // would cause err if without the following
    // condition:
    if (this.outerText.replace(" ", "") != "") {
    
        ctlNavTr_getNextSiblingByElement(this).style.display = "block";
    
    }
    
    ctlNavTr_activeMenu = this;
  }
  return false;
}

// -----------------------------------------------------------------------
// Initialize pg's menu based on an included html-<ul>-based menubar 
// struc.
// CTL Customization ctktko 24.mar.2006
// CTL Note: 1) No param can be passed in, to suit for the syntax
//           of assigning a functor (but not a func call statement with
//           actual param vals) to to event handler 'window.onload'.
//           2) ctl_prvClickedNavTrNode must be set via global js as an assignment 
//           of literal str value somewhere **prior** to exuecting ctlNavTr_initMenu().
//           3) Clicking a menu item is restricted to some top-lvl menu 
//           item only (TODO)
// -----------------------------------------------------------------------
function ctlNavTr_initMenu(){
  var menus, menu, text, a, i;
  var strNTag = null;
  var nodeToClick = null;          // CTL customization
  menus = ctlNavTr_getChildrenByElement(document.getElementById("navTreeNodes"));
  for(i = 0; i < menus.length; i++){
    menu = menus[i];
    text = ctlNavTr_getFirstChildByText(menu);    
    if (text == null) {
	    // Skip
    }
    else {        
	    a = document.createElement("a");
	    menu.replaceChild(a, text);
	    a.appendChild(text);
	    a.href = "#";	    
	    a.onclick = ctlNavTr_showMenu;
	    a.onfocus = function(){this.blur()};
	    // CTL logics:
	    if (typeof(ctl_prvClickedNavTrNode) == "string") {
    	    // ctl_prvClickedNavTrNode is defined: try to fix a node for clicking:
    	    if (text.nodeValue.replace(" ", "") != "") {
        	    strNTag = text.nodeValue;
    	    } else {
        	    // WORKS ONLY IN IE4+:
        	    // Menu item without  child sub-menus don't have nodeValue
        	    // So chk for its own text instead.        	    
        	    strNTag = menu.outerHTML;
    	    }
    	    if (strNTag.indexOf(ctl_prvClickedNavTrNode) > -1) {        	    
        	    ctlNavTr_nodeToClick = a;
    	    }    	    
	    } else {
    	    // NOP: ctl_prvClickedNavTrNode not defined.
	    }
    } 
  }
  if (ctlNavTr_nodeToClick != null) {    
    ctlNavTr_nodeToClick.click();
  }
}

/* 
[07.Nov.2008]
This call must be included to activate the nav tree:
Precond: ctl_addLoadEvent's def already included in the pg.
*/
ctl_addLoadEvent(ctlNavTr_initMenu);