//////////////////////////////////////////
//                                      //
//              ROOT NODE               //
//                                      //
//////////////////////////////////////////

RootNode.TREENODE_TAG_NAME = "treenode";

function RootNode () 
{
  this.nodes = new Array();  
}

RootNode.prototype.setTreeJavascriptCode = function(code)
{
  this.treeJavascriptCode = code;
}

RootNode.prototype.getTreeJavascriptCode = function()
{
  return this.treeJavascriptCode;
}


RootNode.prototype.addChildNode = function(node) 
{
  this.nodes[this.nodes.length] = node;
}

RootNode.prototype.getChildNode = function(index)
{
  return this.nodes[index];
}

RootNode.prototype.getChildNodeCount = function()
{
  return this.nodes.length;
}

RootNode.prototype.getDrawElem = function()
{
  if(this.drawElem == null)
  {
    this.drawElem = System.getTreePanelElem();
  }
  return this.drawElem;
}

RootNode.prototype.findNode = function(id)
{
  for(var i=0; i<this.nodes.length; i++)
  {    
    if(this.nodes[i].id == id)
    {
      return this.nodes[i];
    }
    if(this.nodes[i].nodes.length > 0) 
    {
      return this.nodes[i].findNode(id);
    }
  }
  return null;
}



//////////////////////////////////////////
//                                      //
//                 NODE                 //
//                                      //
//////////////////////////////////////////

Node.TAG_DISPLAY = "display";
Node.TAG_TITLE = "title";
Node.TAG_TREENODE_CHILDS = "treenode_childs";
Node.TAG_DIR = "dir";
Node.TAG_PAGES = "pages";
Node.TAG_PAGE = "page";
Node.TAG_LINKS = "links";
Node.TAG_HTML = "html";
Node.TAG_DOC = "doc";
Node.TAG_XLS = "xls";

Node.uniqueId = 0;

function Node (display, title, pages, childs, dirPath) 
{
  this.id = Node.createId();
  this.display = display;
  this.title = title;
  this.pages = pages;
  this.childs = childs;
  this.dirPath = dirPath;
  this.drawElem = null;
  this.nodes = new Array();
}

Node.prototype.findNode = function(id)
{
  for(var i=0; i<this.nodes.length; i++)
  {    
    if(this.nodes[i].id == id)
    {
      return this.nodes[i];
    }
    if(this.nodes[i].nodes.length > 0) 
    {
      return this.nodes[i].findNode(id);
    }
  }
  return null;
}

Node.createId = function() 
{
  return Node.uniqueId++;
}


Node.prototype.addChildNode = function(node) 
{
  this.nodes[this.nodes.length] = node;
  node.parent = this;
}

Node.prototype.getChildNode = function(index)
{
  return this.nodes[index];
}

Node.prototype.getChildNodeCount = function()
{
  return this.nodes.length;
}


Node.prototype.getHtmlPath = function(index)
{
  var pageTagList = this.pages;
  var linksTag = pageTagList[index].getElementsByTagName(Node.TAG_LINKS);  
  return this.getLinksPath(linksTag, Node.TAG_HTML);  
}

Node.prototype.getDocPath = function(index)
{
  var pageTagList = this.pages; 
  var linksTag = pageTagList[index].getElementsByTagName(Node.TAG_LINKS);
  return this.getLinksPath(linksTag, Node.TAG_DOC);
}

Node.prototype.getXlsPath = function(index)
{
  var pageTagList = this.pages; 
  var linksTag = pageTagList[index].getElementsByTagName(Node.TAG_LINKS);
  return this.getLinksPath(linksTag, Node.TAG_XLS);
}

Node.prototype.getLinksPath = function(linksTag, tagName)
{
  var htmlTag = linksTag[0].getElementsByTagName(tagName);
  var htmlPath = XmlTools.getLanguageNodeValue(htmlTag[0]); 
  return (htmlPath == "") ? htmlPath : (this.dirPath + htmlPath);
}


Node.createNodeTree = function (directories, childNode) 
{ 
  for(var i=0;i<directories.length; i++)
  {
    var dirTag = directories[i]; 
    var dirPath = XmlTools.getNodeValue(dirTag, -1);  
    var filePath = dirPath + Content.PROPERTIES_XML_FILE_NAME;
        
    var xmlDoc = XmlTools.importXML(filePath);
    var structureElements = xmlDoc.getElementsByTagName(System.STRUCTURE_TAG_NAME);

    if(XmlTools.isXMLFormatValid(structureElements, filePath))
    {
      var treenodeTag = structureElements[0].getElementsByTagName(RootNode.TREENODE_TAG_NAME); 
      
      var displayTag = treenodeTag[0].getElementsByTagName(Node.TAG_DISPLAY);
      var display = (displayTag[0] == null) || (displayTag[0] != null && XmlTools.getNodeValue(displayTag[0]) == "true")
      var titleTag = treenodeTag[0].getElementsByTagName(Node.TAG_TITLE);
      var title = XmlTools.getLanguageNodeValue(titleTag[0]);      
      var pagesTag = treenodeTag[0].getElementsByTagName(Node.TAG_PAGES);
      
      var pageTagList = null;
      if(pagesTag[0] != null)
      {
        pageTagList = pagesTag[0].getElementsByTagName(Node.TAG_PAGE);          
      }  
      var pages = pageTagList;
      
      var treenodeChildsTag = treenodeTag[0].getElementsByTagName(Node.TAG_TREENODE_CHILDS);
      var childs = null;
      if(treenodeChildsTag[0] != null)
      {
        var dirList = treenodeChildsTag[0].getElementsByTagName(Node.TAG_DIR); 
        var childs = dirList;
      }
    
      var node = new Node(display, title, pages, childs, dirPath);
      childNode.addChildNode(node);
      
      //alert(title + ";  " + filePath + ";  ")
      
      if(childs != null && childs.length > 0)
      {
        Node.createNodeTree(childs, node);
      }          
    }           
  }
}
