var tnodestyle = 'tree_folder';
var node_menu = 1;
var node_page = 2;

var image_node_page = '';
var image_node_page_highlight = '';
var image_node_folder_open = '';
var image_node_folder_closed = '';
var image_node_folder_highlight = '';


var node_menu_class = 'folder';
var node_page_class = 'page';

var node_menu_class_cell = 'folder_cell';
var node_page_class_cell = 'page_cell';

var useindent = true;

var menu_cururl = '';

var indentwidth = 16;

function shownodetree(node, width) {
  var res = '<table  cellpadding="1" cellspacing="1" border="0" width="'+ width +'">'+
              node.show() +
            '</table>'
  document.write(res);           
  node.makevisible(node, node.id);
//  document.write('<textarea cols=80 rows=10>'+res+'</textarea>');          
}


function tnode(id, parentid, type, title, url, priority, target) {
  var res = new Array()
  res.id = id;
  res.parentid = parentid;
  res.parent = null;
  res.type = type;
  res.title = title;
  if (url != '' && url.toLowerCase().indexOf('http://') == -1 && url.toLowerCase().indexOf('javascript:') == -1) {
    if (url.substring(0,1) == '/') url = url.substring(1);
    url = getbasehttp + url;
  }
  res.url = url;
  res.show = tnode_show;
  res.showindent = tnode_showindent;
  res.displaytext = tnode_displaytext;
  res.find = tnode_find;
  res.add = tnode_add;
  res.childrenvisible = false;
  res.makevisible = tnode_makevisible;
  res.topnode = null;
  res.cellclass = tnode_cellclass;
  if (isundefined(target))
    res.target = ''
  else
    res.target = target;
  res.priority = priority;
  res.topmenuname = topmenuname;
  return res;
}

function tnode_find(id) {
  if (this.id == id) return this;
  var startpos = this.url.length-id.length;
  if (startpos < 0) startpos = 0;
  if (id != '' && this.url.toLowerCase().substring(startpos, startpos+id.length) == id.toLowerCase()) return this;
  if (this.url.toLowerCase() == id.toLowerCase()) return this;
  var res = null
  for (var i = 0; i < this.length; i++) {
    if (this[i].id == id) return this[i];
    res = this[i].find(id);
    if (res != null) return res;
  }
  return null;
}

function comparepriority(a,b) { 
  if (a.priority != b.priority) return -(a.priority - b.priority);
  if (a.name == b.name) return 0;
  if (a.name < b.name) return -1;
  return 1;
}

function tnode_add( node ) {
  res = this.find(node.parentid);
  if (res != null) {
    node.parent = res;
    node.topnode = res.topnode;
    if (node.target == '') {
      node.target = this.target
    }
    res[res.length] = node;
    res.sort(comparepriority);
  }
}

function tnode_cellclass() {
  if (this.type == node_menu)
    return node_menu_class_cell
  return node_page_class_cell
}
 
function showimage( url, id) {
  if (url == '') return '';
  return '<img src="' + url + '" id="mi'+id+'" name="mi'+id+'" border=0 align="absmiddle" hspace=3>';
}
 
function tnode_show() {
  if (this.parentid != '') 
    var indent = this.parent.showindent(this)
  else
    var indent = '';
  
  if (this.title != '') {
    if (this.type == node_menu && this.url == '') 
      var res = '<tr id="menu'+topmenuname+this.id+'" style="display:none" onClick="togglemenu('+topmenuname+', \''+ this.id +'\');return false;">'+
                  '<td valign="middle" class="'+ this.cellclass() +'" nowrap>'+
                    indent + 
                    this.displaytext() + 
                  '</td>'+
                '</tr>';
    else    
      var res = '<tr id="menu'+topmenuname+this.id+'" style="display:none">'+
                  '<td valign="middle" class="'+ this.cellclass() +'" nowrap>'+
                    indent + 
                    this.displaytext() + 
                  '</td>'+
                '</tr>';
  } else {
    var res = '<tr id="menu'+topmenuname+this.id+'" style="display:none">'+
                '<td></td>'+
              '</tr>';
  }
  for (var i = 0; i < this.length; i++) {
    res += this[i].show();
  }
  return res;          
}

function tnode_displaytext() {
  if (this.type == node_menu && this.url == '')
    return '<a href="javascript:togglemenu('+this.topmenuname+', \''+ this.id +'\');return false;">' + showimage( image_node_folder_closed, this.id) + '</a>' + 
           '<a href="javascript:togglemenu('+this.topmenuname+', \''+ this.id +'\');return false;" class="'+ node_menu_class +'">' + this.title + '</a>'
  else if(this.url != '' && this.type == node_menu){
    if (this.target != '')
      var t = ' target="' + this.target + '" '
    else
      var t = '';
      
    var curclassname = node_page_class;
    var isCurrent = false;
    if (menu_cururl.toLowerCase().indexOf('referencepageid=' + this.id.toLowerCase()) != -1) {
      curclassname += '_current';
      isCurrent = true;
    } else {
      if (menu_cururl.toLowerCase() == this.url.toLowerCase()) {
        curclassname += '_current';
        isCurrent = true;
      } else {
        var m = menu_cururl.toLowerCase().split('?')[0];
        var s = m.length - this.url.length;
        if (m.substring(s) == this.url.toLowerCase() && this.type != node_menu) {
          curclassname += '_current';
          isCurrent = true;
        }
      }
    }
    
    return '<table  cellpadding="0" cellspacing="0" border="0" '+ ifthen(isCurrent,'class="current"','') +'>'+
             '<tr>'+
               '<td class="currentpagefolder_icon" onmouseover="mover_ChangeIcon(\'mi'+ this.id +'\',\' '+this.type +' \');" onmouseout="mout_ChangeIcon(\'mi'+ this.id +'\',\' '+this.type +' \');">'+
                 '<a '+ ifthen(isCurrent,'class="currentpagefolder_icon_current"','') + t +'href="'+ this.url +'">' + showimage( ifthen(isCurrent,image_node_folder_open,image_node_folder_closed), this.id ) + '</a>' + 
               '</td>'+
               '<td '+ ifthen(isCurrent,'class="currentpagefolder"','') +'>'+
                 '<a '+ t +'href="'+ this.url +'" class="'+ curclassname +'" onmouseover="mover_ChangeIcon(\'mi'+ this.id +'\',\' '+this.type +' \');" onmouseout="mout_ChangeIcon(\'mi'+ this.id +'\',\' '+this.type +' \');">' + this.title + '</a>' +
               '</td>'+
             '</tr>'+
           '</table>'
           
           
  }
  else {
    if (this.target != '')
      var t = ' target="' + this.target + '" '
    else
      var t = '';
      
    var curclassname = node_page_class;
    var isCurrent = false;
    if (menu_cururl.toLowerCase().indexOf('referencepageid=' + this.id.toLowerCase()) != -1) {
      curclassname += '_current';
      isCurrent = true;
    } else {
      if (menu_cururl.toLowerCase() == this.url.toLowerCase()) {
        curclassname += '_current';
        isCurrent = true;
      } else {
        var m = menu_cururl.toLowerCase().split('?')[0];
        var s = m.length - this.url.length;
        if (m.substring(s) == this.url.toLowerCase() && this.type != node_menu) {
          curclassname += '_current';
          isCurrent = true;
        }
      }
    }
    
    return '<table  cellpadding="0" cellspacing="0" border="0" '+ ifthen(isCurrent,'class="current"','') +'>'+
             '<tr>'+
               '<td '+ ifthen(isCurrent,'class="currentpagefolder_icon"','') +' onmouseover="mover_ChangeIcon(\'mi'+ this.id +'\',\' '+this.type +' \');" onmouseout="mout_ChangeIcon(\'mi'+ this.id +'\',\' '+this.type +' \');">'+
                 '<a '+ ifthen(isCurrent,'class="currentpagefolder_icon_current"','') + t +'href="'+ this.url +'">' + showimage( ifthen(isCurrent && image_node_page_highlight!="",image_node_page_highlight,image_node_page) , this.id ) + '</a>' + 
               '</td>'+
               '<td '+ ifthen(isCurrent,'class="currentpagefolder"','') +' >'+
                 '<a '+ t +'href="'+ this.url +'" class="'+ curclassname +'" onmouseover="mover_ChangeIcon(\'mi'+ this.id +'\',\''+ this.type+'\');" onmouseout="mout_ChangeIcon(\'mi'+ this.id +'\',\' '+this.type +' \');">' + this.title + '</a>' +
               '</td>'+
             '</tr>'+
           '</table>'
           
           
  }
}

function tnode_showindent(child) {
  if (!useindent) return '';
  var res = '';
  if (this.parent != null) res += this.parent.showindent(this);
  if (this.title != '') 
    return res + '<img src="/images/trans.gif" width='+ indentwidth + ' height=1 align="left">';
  return '';
}

function showmenu(curmenu) {
  curmenu.childrenvisible = true;
  if (curmenu.id != '') {
    //MBL 20051025 var cur = eval('menu'+curmenu.topmenuname+curmenu.id);
    var cur = document.getElementById('menu'+curmenu.topmenuname+curmenu.id);
    cur.style.display = 'block';
    //openmenuimage(curmenu.id)
  }
  for (var i = 0; i < curmenu.length; i++) {
    //MBL 20051025 var cur = eval('menu'+curmenu[i].topmenuname+curmenu[i].id);
    var cur = document.getElementById('menu'+curmenu[i].topmenuname+curmenu[i].id);
    cur.style.display = 'block';
  }
}

function hidemenu(curmenu) {
  //closemenuimage(curmenu.id)
  curmenu.childrenvisible = false;
  for (var i = 0; i < curmenu.length; i++) {
    //MBL 20051025 var cur = eval('menu'+curmenu[i].topmenuname+curmenu[i].id);
    var cur = document.getElementById('menu'+curmenu[i].topmenuname+curmenu[i].id);
    cur.style.display = 'none';
    hidemenu(curmenu[i]);
  }
}

function togglemenu(topnode, id) {
  var res = topnode.find(id);
  //MBL 20051025 var img = document.images('mi'+id);
  var img = document.getElementById('mi'+id);
  if (res.childrenvisible) {
    hidemenu(res);
    if (img != null) img.src = image_node_folder_closed;
  } else {
    showmenu(res);
    if (img != null) img.src = image_node_folder_open;
  }
}

function tnode_makevisible(topnode, id) {
  var cur = topnode.find(id);
  var counter = 0
  while (cur != null && counter < 10) {
    showmenu(cur);
    cur = topnode.find(cur.parentid);
    counter++;
  }
}


function mover_ChangeIcon(id,type)
{
  if (image_node_folder_highlight!="" || image_node_page_highlight!=""){
    var menu = node_menu;
  //  alert(1);
    var img = document.getElementById(id);
    if (type == menu){ //menu mouse over
      img.src = image_node_folder_highlight;
      //img.src = image_node_folder_open;
    } else { //page mouse over
      img.src = image_node_page_highlight;
      //img.src = image_node_page;
    } 
  }
/*  return;*/
}

function mout_ChangeIcon(id,type)
{
  if (image_node_folder_highlight!="" || image_node_page_highlight!=""){
    var menu = node_menu;
  //  alert(1);
    //alert(id);
    var img = document.getElementById(id);
    if (type == menu){//menu mouse out
      if (img.parentNode.className == 'currentpagefolder_icon_current'){
        img.src = image_node_folder_open;
      }else{    
        img.src = image_node_folder_closed;
      }
    } else { //page mouse out
      if (img.parentNode.className == 'currentpagefolder_icon_current'){
        img.src = image_node_page_highlight;
      } else {
        img.src = image_node_page;
      }      
    }
  }
/*  return;*/
}

