// navigation tree for www.hansky.com

function NavTree(txt) {
    if (arguments.length > 0) {
	this.text = txt;
    }
    // this.text = "Product";
    this.nodes = [];
    this.allnodes = {};	// save all nodes under this tree, use qualified name as key
    return this;
}

NavTree.prototype.add = function(node) {
    this.nodes[this.nodes.length] = node;
    this.allnodes[node.name] = node;
    node.tree = this;
}

NavTree.prototype.setExpand = function(name) {
    var node = this.getNode(name);
    if (node) {
	node.isExpanded = true;
    }
}

NavTree.prototype.setSelected = function(name) {
    var node = this.getNode(name);
    if (node) {
	node.isSelected = true;
    }
}

NavTree.prototype.getNode = function(name) {
    return this.allnodes[name];
}

NavTree.prototype.toString = function() {
    var str = '<h3>' + this.text + '</h3>\n';
    for (var i = 0; i < this.nodes.length; i++) {
	str += this.nodes[i].toString();
    }
    // alert(str);
    return str;
}

function NavTreeNode(name, text, url, style, popup) {
    this.nodes = [];
    this.name = name;
    this.text = text;
    if (url.charAt(0) == ':') {
	this.url = '/cn/dokuwiki/doku.php?id=' + url;
    } else {
    	this.url = url;
    }
    this.style = style;
    this.popup = popup;
    
    this.id = NavTreeHandler.getId();
    this.isExpanded = false;
    this.isSelected = false;
    NavTreeHandler.all[this.id] = this;
    this.parentNode = null;
    this.tree = null;	// hold a reference of this tree.

    return this;
}

NavTreeNode.prototype.add = function(node) {
    this.nodes[this.nodes.length] = node;
    node.parentNode = this;
    node.tree = this.tree;
    this.tree.allnodes[node.getQualifiedName()] = node;
}

NavTreeNode.prototype.getQualifiedName = function() {
    var str = "";
    if (this.parentNode == null) {
	return this.name;
    }
    str = this.parentNode.getQualifiedName() + "." + this.name;
    return str;
}

NavTreeNode.prototype.overMenu = function() {
    document.getElementById(this.id + '-img').src = NavTreeHandler.arrowBlueIcon;
}

NavTreeNode.prototype.leaveMenu = function() {
    document.getElementById(this.id + '-img').src = getIcon(this);
}

NavTreeNode.prototype.toString = function() {
    var str = '<a href="' + this.url + '"'; 
    if (this.isSelected) {
	str += ' class="right_menu_selected"';
    }
    if(this.popup) {
	str = str + ' target="_blank">';
    } else {
	str = str + '>';
    }

    // print the indent, only support one-level indent.
    var foo = this.parentNode;
    if (foo != null) {
	str = str + '<span>' + this.text + '</span>';
    } else {
	str = str + this.text;
    }

    str = str + '</a>\n';

    if (this.isExpanded) {
	for (var i = 0; i < this.nodes.length; i++) {
	    str += this.nodes[i].toString();
	}
    }
    return str;
}

function getIcon(node) {
    if (node.isSelected) {
		 if (node.style == "session") {
			 return NavTreeHandler.arrowWhiteIcon;
		 } else {
			return NavTreeHandler.arrowBlueIcon;
		 }
    } else {
	return NavTreeHandler.spaceIcon;
    }
}

var NavTreeHandler = {
    idCounter : 0,
    idPrefix  : "hansky-navtree-object-",
    all       : {},	// hash keyed by id.
    // nodes     : {},	// hash keyed by qualified name.
    getId     : function() { return this.idPrefix + this.idCounter++; },
    overMenu  : function (oItem) { this.all[oItem.id].overMenu(); },
    leaveMenu  : function (oItem) { this.all[oItem.id].leaveMenu(); },
    arrowBlueIcon : '/cn/img/arrow_b.gif',
	arrowWhiteIcon : '/cn/img/arrow_w.gif',
    spaceIcon : '/cn/img/1x1.gif'
};
