var tnodestyle = 'tree_folder';
var jsnode_menu = 1;
var jsnode_page = 2;
var jsnode_topmenu = 3;
var jsimage_node_page = '';
var jsimage_node_folder_open = '';
var jsimage_node_folder_closed = '';

var jsnode_topmenu_class = 'topfolder';
var jsnode_topmenu_class_cell = 'topfolder_cell'
var jsnode_menu_class = 'folder';
var jsnode_page_class = 'page';

var jsnode_menu_class_cell = 'folder_cell';
var jsnode_page_class_cell = 'page_cell';

var jsnode_submenu_table = 'submenu_table';

var jsnode_width = 140;
var jssubmenunode_width = jsnode_width;

var jscurmenu = '';
var jstopmenu = new deflist();
var topmenu_suffix = '';

var usesubmenuimages = true;

var hidemenustimer = null;
var hidemenusonmove = false;

function showjsnodetree(node, width, submenuwidth) {
  jsnode_width = asint(width);
  jssubmenunode_width = asint(submenuwidth);
  var res = '<table class="jsmenuinside" cellpadding="1" cellspacing="1" border="0" width="'+node.menuwidth+'">'+
              node.showtopmenu() +
            '</table>';
  document.write(res);           
  node.createmenucontainers();
  jsshowmenu(node);          
}


function tjsnode(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;
  res.url = url;
  res.show = tjsnode_show;
  res.showchildren = tjsnode_showchildren
  res.find = tjsnode_find;
  res.add = tjsnode_add;
  res.displaytext = tjsnode_displaytext;
  res.topnode = null;
  res.cellclass = tjsnode_cellclass;
  res.priority = priority;
  res.level = 0;
  res.showtopmenu = tjsnode_showtopmenu;
  res.createmenucontainers = tjsnode_createmenucontainers;
  res.hidecontainertree = tjsnode_hidecontainertree;
  res.hideall = tjsnode_hideall;
  res.showparents =tjsnode_showparents;
  res.jstopmenuname = '';
  res.menuwidth = '100%';
  res.target = target;
  return res;
}

function tjsnode_hidecontainertree() {
  var curcontainer = getElement('container'+this.id);
  curcontainer.style.visibility = 'hidden';
  if (this.parent != null) this.parent.hidecontainertree();
}

function tjsnode_createmenucontainers() {
  if (this.type == jsnode_menu) {
    var res = '';
    res = '<span class="jsmenuinside" id="container'+this.id+'" style="position:absolute; left:0px; top:0px; visibility:none; z-index:'+ (this.level + 1)+'"></span>';
    document.write(res);
    for (var i = 0; i < this.length; i++) {
      this[i].createmenucontainers();
    }
  }
}


function tjsnode_find(id) {
  if (this.id == id) 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.title == b.title) {
   if(a.cpIndex==b.cpIndex) return 0;
   return (a.cpIndex-b.cpIndex);
  }
  if (a.title < b.title) return -1;
  return 1;
}

function tjsnode_add( node ) {
  res = this.find(node.parentid);
  node.parent = res;
  node.topnode = res.topnode;
  if (node.target == '') node.target = this.target;
  node.level = res.level + 1;
  node.jstopmenuname = res.jstopmenuname;
  res[res.length] = node;
  for(var i=0;i<res.length;i++) res[i].cpIndex = i;
  res.sort(comparepriority);
}

function tjsnode_cellclass() {
  if (this.type == jsnode_menu)
    return ifthen(this.parentid == '', jsnode_topmenu_class_cell, jsnode_menu_class_cell)
  return jsnode_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 tjsnode_showsubmenuicon () {
}

function tjsnode_showtopmenu() {
  if (this.title != '') {
    if (this.type == jsnode_menu) 
      var res = '<td id="' + this.jstopmenuname + this.id+'" valign="middle" class="'+ this.cellclass() +'" nowrap onMouseOver="showmenucontent('+this.jstopmenuname+', \''+ this.id +'\');highlightcell(' + this.jstopmenuname+',\''+this.id+'\')" width='+ jsnode_width +' onMouseOut="highlightcellstop(' + this.jstopmenuname+',\''+this.id+'\')">'+
                  this.displaytext() + 
                '</td>';
    else    
      var res = '<td id="' + this.jstopmenuname + this.id+'"  valign="middle" class="'+ this.cellclass() +'" nowrap onMouseOver="showmenucontent('+ this.jstopmenuname+', \''+ this.id +'\');highlightcell(' + this.jstopmenuname+',\''+this.id+'\')" width='+ jsnode_width +' onMouseOut="highlightcellstop(' + this.jstopmenuname+',\''+this.id+'\')">'+
                  this.displaytext() + 
                '</td>';
  } else {
    var res = '<tr>'
    for (var i = 0; i < this.length; i++) {
      res += this[i].showtopmenu();
    }
    res += '</tr>';
  }
  return res;          
}

function tjsnode_showchildren() {
  var res = ''
  for (var i = 0; i < this.length; i++) {
    res += this[i].show();
  }
  return res;          
}

function tjsnode_show() {
  if (this.title != '') {
    if (this.type == jsnode_menu) 
      // Vis undermenu
      var res = '<tr id="' + this.jstopmenuname + this.id+'" >'+
                  '<td onClick="clickcell('+ this.jstopmenuname+', \''+ this.id +'\')" valign="middle" class="'+ this.cellclass() +'" nowrap onMouseOver="showmenucontent('+this.jstopmenuname+', \''+ this.id +'\');highlightitem(\'' + this.jstopmenuname+this.id+'\',jsnode_menu)" width='+ jssubmenunode_width +' onMouseOut="highlightstop(\'' + this.jstopmenuname+this.id+'\',jsnode_menu)">'+
                    this.displaytext() + 
                  '</td>'+
                '</tr>';
    else    
      var res = '<tr id="' + this.jstopmenuname +this.id+'">'+
                  '<td onClick="clickcell('+ this.jstopmenuname+', \''+ this.id +'\')" valign="middle" class="'+ this.cellclass() +'" nowrap width='+ jssubmenunode_width +'  onMouseOver="showmenucontent('+this.jstopmenuname+', \''+ this.id +'\');highlightitem(\'' + this.jstopmenuname+this.id+'\',jsnode_page)" onMouseOut="highlightstop(\'' + this.jstopmenuname+this.id+'\',jsnode_page)">'+
                    this.displaytext() + 
                  '</td>'+
                '</tr>';
  }

  return res;          
}

function tjsnode_displaytext() {
  if (this.type == jsnode_menu) {
    var ref = this.url;
    if (ref != '') ref = ' href="'+ref+'" '
    if (this.length > 0 ) {
      return '<table  cellpadding="0" cellspacing="0" border="0" width="100%">'+
               '<tr>'+
                 '<td width="100%">'+
                   '<a '+ ref +'class="'+ ifthen(this.parentid=='',jsnode_topmenu_class,jsnode_menu_class) +'">' + this.title + ifthen(this.parentid == '', topmenu_suffix, '') + '</a>'+
                 '</td>'+
                 ifthen((usesubmenuimages && this.parentid !=''),'<td align="right" width="16">'+
                   '<a class="'+ jsnode_menu_class +'">' + 
                     '<img src="'+getbasehttp+'images/jssubmenu.gif" border=0>'+ 
                   '</a>'+
                 '</td>','')+
               '</tr>'+
             '</table>'
    } else {
      return '<a '+ ref +'class="'+ jsnode_menu_class +'">' + this.title + '</a>'
    }       
  } else {
    if (this.target != '')
      var t = ' target="' + this.target + '" '
    else
      var t = '';
    return '<a '+ t +'href="'+ this.url +'">' + showimage( jsimage_node_page, this.id ) + '</a>' + 
           '<a '+ t +'href="'+ this.url +'" class="'+ jsnode_page_class +'">' + this.title + '</a>'
  }
}

function jsshowmenu(curmenu) {
  curmenu.childrenvisible = true;
  if (curmenu.id != '') {
    var cur = (document.getElementById)?document.getElementById(curmenu.jstopmenuname+curmenu.id):eval(curmenu.jstopmenuname+curmenu.id);
    cur.style.display = 'block';
    //openmenuimage(curmenu.id)
  }
  for (var i = 0; i < curmenu.length; i++) {
    var cur = (document.getElementById)?document.getElementById(curmenu.jstopmenuname+curmenu[i].id):eval(curmenu.jstopmenuname+curmenu[i].id);
    if(curmenu[i].type!=jsnode_menu) cur.style.display = 'block';
  }
}

function tjsnode_hideall() {
  if (this.type == jsnode_menu) {
    var cur = getElement('container'+this.id);
    cur.style.visibility = 'hidden';
    for (var i = 0; i < this.length; i++) {
      this[i].hideall();
    }
  }
}

function tjsnode_showparents() {
  if (this.type == jsnode_menu) {
    var curcontainer = getElement('container'+this.id);
    curcontainer.style.visibility = 'visible';
  }
  if (this.parent != null) this.parent.showparents();
}

function clickcell(topnode, id) {
  var curnode   = topnode.find(id);
  if (curnode.type == jsnode_page || curnode.url != '') {
    if (curnode.target == '' || isundefined(curnode.target))
      location.href = curnode.url
    else {
      if (curnode.url.indexOf('javascript:') == -1)
        window.open( curnode.url, curnode.target )
      else {
        eval(curnode.url.substring(11));
      }
    }    
  }
}


function showmenucontent(topnode, id) {
  var curnode   = topnode.find(id);
  topnode.hideall();
  curnode.showparents();
  document.onclick= hidemenus;
  if(hidemenusonmove==true) {
    document.onmouseout = hidemenusonmoveout;
    document.onmouseover = hidemenusonmovein;
  }

  if (curnode.type == jsnode_menu) {
    var curcontainer = getElement('container'+id);
    curcontainer.style.visibility = 'visible';
    var curmenuheight = getElement(curnode.jstopmenuname+id).offsetHeight;
    if (curnode.level == 1) {
      curcontainer.style.left = asint(getRealLeft( curnode.jstopmenuname+id));
      curcontainer.style.top =  asint(getRealTop( curnode.jstopmenuname+id)) + curmenuheight;
    } else {
      curcontainer.style.left = asint(getRealLeft( curnode.jstopmenuname+id)) + getElement(curnode.jstopmenuname+id).clientWidth;
      curcontainer.style.top =  asint(getRealTop( curnode.jstopmenuname+id))-1;
    }
    width = 0;
    width = jssubmenunode_width; //getElement(curnode.jstopmenuname+id).clientWidth
    var res = '<table cellpadding="1" cellspacing="1" border="0" width="'+ width +'" class="'+jsnode_submenu_table+'">'+
                curnode.showchildren() + 
              '</table>';
    curcontainer.innerHTML = res;
  }                           
}

function highlightcell(topnode, nodeid) {
  var obj = getElement(topnode.topnode + nodeid);
  var node = topnode.find(nodeid)
  obj.className = node.cellclass() + '_highlight';
}

function highlightcellstop(topnode, nodeid) {
  var obj = getElement(topnode.topnode + nodeid);
  var node = topnode.find(nodeid)
  obj.className = node.cellclass();
}

function highlightitem(id, type) {
  var obj = getElement(id);
  var children = obj.children;
  if(!children) children = obj.childNodes;
  for (var i = 0; i < children.length; i++) {
    if (type == jsnode_menu_class_cell)
      children[i].className = jsnode_menu_class_cell + '_highlight'
    else
      children[i].className = jsnode_page_class_cell + '_highlight'
  }
}

function highlightstop(id, type) {
  var obj = getElement(id);
  var children = obj.children;
  if(!children) children = obj.childNodes;
  for (var i = 0; i < children.length; i++) {
    if (type == jsnode_menu_class_cell)
      children[i].className = jsnode_menu_class_cell;
    else
      children[i].className = jsnode_page_class_cell;
  }
}

function hidemenus() {
  for (var i = 0; i < jstopmenu.length; i++) {
    jstopmenu[i].hideall();
  }
  document.onmouseout=null;
  document.onmouseover=null;
  document.onclick=null;
}

function hidemenusonmovein(e) {
  hidemenussettimer(e, true);
  return false;
}
function hidemenusonmoveout(e) {
  hidemenussettimer(e, false);
  return false;
}

function hidemenussettimer(e, moveIn) {
  if(!e) e=window.event;
  var targ;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;
  var insidemenu = false;
  while(targ) {
    if(targ.className=="jsmenuinside") {
      insidemenu=true;
      break;
    }
    targ=targ.parentNode;
  }
  if(insidemenu) {
    if(moveIn && hidemenustimer) {
      window.clearTimeout(hidemenustimer);
      hidemenustimer = null;
    } else if(!moveIn && !hidemenustimer) {
      hidemenustimer = window.setTimeout(hidemenus, 100);
    }
  } else {
    if(moveIn && !hidemenustimer) {
      hidemenustimer = window.setTimeout(hidemenus, 100);
    }
  }
  if(e.preventDefault) e.preventDefault();
}