//=======================================================================//
//                                                                       //
//  Macrobject Software Code Library                                     //
//  Copyright (c) 2004-2007 Macrobject Software, All Rights Reserved     //
//  http://www.macrobject.com                                            //
//                                                                       //
//  Warning!!!                                                           //
//      The library can only be used with web help system                //
//      created by CHM-2-Web Pro.                                        //
//                                                                       //
//=======================================================================//

function moCssTree(ps, ts, tl) {

  this.ps = ps;
  this.ts = ts;
  this.tl = tl;
  
  this.clsCollapsed = "Collapsed";
  this.clsExpanded  = "Expanded";
  this.clsTopic     = "Topic";
  this.clsCurrent   = "Current";
  this.emptyImg     = "images/~.gif";

  this.curNode   = null;
  this.panel     = null;
  this.nodes     = null;

  this.getObject = function(id){
   return document.getElementById(id);
  }

  this.expand = function(node) {
    this.createChildren(node.mo_id); 
    if(node.className == this.clsCollapsed) node.className = this.clsExpanded;
  }
  
  this.toggleNode = function(node) {
    this.createChildren(node.mo_id); 
    node.className = node.className == this.clsCollapsed ? this.clsExpanded : this.clsCollapsed;
  }

  this.focus = function(node) {
    if ( this.curNode ) this.curNode.className = "";
    node.className = this.clsCurrent;
    this.curNode = node;
  }
  
  this.nodeClick = function(e, node) {
    if (node) this.toggleNode(node);
      
    var o = null;
    if (window.event) {
      e.cancelBubble = true;
      o = e.srcElement;
    }
    else if (e.stopPropagation) {
      e.stopPropagation();
      o = e.target;
      while(o.nodeType != o.ELEMENT_NODE) o = o.parentNode;
    }
    if (o.tagName.toLowerCase() == "a") {
      this.focus(o);
      if(moTop) moTop.curPageIndex = parseInt(o.mo_id);
    } 
  }

  
  this.locate = function(index) {
    if (!this.nodes[index]) {
      var parent = this.findParentIndex(index);
      this.locate(parent);
      this.createChildren(parent);
    }


    var node = this.nodes[index].getElementsByTagName('a')[0];
    this.focus(node);

    while(node != this.panel) { 
      node = node.parentNode;
      if (node.tagName.toLowerCase() == 'li') this.expand(node);
    }
  }

  this.createObject = function(parent, tagName) {
    var o = document.createElement(tagName);
    parent.appendChild(o);
    return o;
  }

  this.createTree = function(panelId) {
    this.nodes = [];
    this.panel = this.getObject(panelId);
    this.createChildren(-1);
  }
  
  this.createChildren = function(index) {
    index = parseInt(index);
    if (this.nodes[index+1]) return;
    
    var root;
    
    var childLevel;
    if (index < 0) {
      root = this.panel;
      childLevel = 1;
    }
    else {
      root = this.nodes[index];
      childLevel = tl[index]+1;
    }

    root = this.createObject(root, 'UL');
      
    var i = index;
    while(tl[i+1] >= childLevel) {
      i++;
      var nextLevel = i+1 < tl.length ? tl[i+1] : -1;
      if(tl[i] == childLevel) {
        var li = this.createObject(root, 'LI');
        li.mo_id = i;
        this.nodes[i] = li;
        if(childLevel < nextLevel) {
          li.className = this.clsCollapsed;
          li.setAttribute('onclick', document.all ? function(){eval("ct.nodeClick(event, this)");} : 'ct.nodeClick(event, this);');
        }
        else {
          li.className = this.clsTopic;
          li.setAttribute('onclick', document.all ? function(){eval("ct.nodeClick(event, null)");} : 'ct.nodeClick(event, null);');
        }

        var img = this.createObject(li, 'IMG');
        img.className = 'Icon';
        img.src       = 'images/o.gif';
        var link  = this.createObject(li, 'A');
        if(ps[i] == '#' || ps[i] == 'javascript:void(0)')
          link.href   = ps[i];
        else
        link.href     = 'topics/' + ps[i];
        link.target   = 'content';
        link.mo_id    = i;
        link.innerHTML= ts[i];
      }
    }
  }
  
  this.findParentIndex = function(index) {
    var level = tl[index];
    while(tl[index] >= level && index > 0) index--;
    return index;
  }
 
}

var ct = new moCssTree(ps, ts, tl);
