/******************************************************************************
 *
 *                   INDIGEN SOLUTIONS CODE PROPERTY
 *       The present javascript code is property of Indigen Solutions. This 
 *     code can only be used inside Internet/Intranet web sites located on 
 *  *web servers*, as the outcome of a licensed Indigen Solutions application 
 *  only. Any unauthorized use, reverse-engineering, alteration, transmission, 
 * transformation, facsimile, or copying of any means (electronic or not) is 
 *     strictly prohibited and will be prosecuted. Removal of the present 
 *              copyright notice is strictly prohibited
 *         Copyright (c) 2004 Indigen Solutions. All Rights Reserved.
 *
 * RCS Id   $Id: topology.js,v 1.6 2006/04/06 10:11:26 indigen Exp $
 * RCS Revision                 $Revision: 1.6 $
 * RCS Check in date            $Date: 2006/04/06 10:11:26 $
 * 
 ******************************************************************************/

/**
 * @todo
 */

/* Constants. */

var TOPOLOGY_NODE     = 0;
var TOPOLOGY_ROOTNODE = 1;

/* Class. */

function Topology(objid) {
  // Attributes.
  this.objid      = objid;
  this.nodes      = new Array();
  this.roots      = new Array();
  this.root       = null;
  this.rootid     = 0;
  // Tree construction methods.
  this.d = Topology_d;
  this.c = Topology_c;
  // Methods.
  this.toHTML = Topology_toHTML;
}

/* Methods. */

function Topology_d(objid, objtype, type, name, accessible) {
  var node = {objid:objid, objtype:objtype, type:type, name:name, accessible:accessible};
  node.nbChildren = 0;
  node.children = new Array();
  node.nbParents = 0;
  node.parents = new Array();
  node.toHTML = TopologyNode_toHTML;
  this.nodes[objid] = node;
  return node;
}

function Topology_c(graph) {
  for ( var objid in this.nodes )
    if ( this.nodes[objid].type == TOPOLOGY_ROOTNODE )
      this.roots.push(this.nodes[objid]);
  this.root = this.roots[0];
  this.rootid = this.root.objid;
  if ( (graph == undefined) || (graph == null) || (graph == "") )
    return;
  var array = graph.split(",");
  for ( var i = 0; i < array.length; ) {
    var parentid = parseInt(array[i++]);
    var childid = parseInt(array[i++]);
    if ( (parentid == null) || (childid == null) )
      return ;
    var parent = this.nodes[parentid];
    var child = this.nodes[childid];
    if ( (parent == null) || (child == null) )
      continue;
    parent.children[parent.nbChildren++] = child;
    child.parents[child.nbParents++] = parent;
  }
}

function Topology_toHTML() {
    return this.root.toHTML(0);
}

function TopologyNode_toHTML(depth) {
  var str = "";
  for ( var i = 0; i < depth; i++ )
    str += "&nbsp;&nbsp;&nbsp;&nbsp;";
  str += "<b>" + this.name + "</b> (" + this.objid + ")<br>";
  for ( var i = 0; i < this.nbChildren; i++ )
    str += this.children[i].toHTML(depth + 1);
  return str;
}

