var browser_type = '';
if(document.layers){ browser_type=1; }//NN4
if(document.all){ browser_type=2; }//IE
if(!document.all && document.getElementById){ browser_type=3; }//NN6

var hidetimer = null;
function clear_timer(){
  if(hidetimer)
    clearTimeout(hidetimer);
  hidetimer = null;
}
function timer_menu(){
  clear_timer();
  hidetimer = setTimeout("hide_all();",500);
}

var submenu_count=0;

function hide_all(){
  for(i=0;i<submenu_count;i++)
    hide_menu('submenu'+i);
  clear_timer();
}

function open_menu(el,menuname){
  hide_all();
  show_menu(el,menuname);
}

function set_visibility(menuname,vis,coord){
  str = vis?'visible':'hidden';
  if(browser_type == 1){
    document.layers[menuname].visibility = str;
  }
  else if(browser_type == 2){
    e = document.all[menuname];
    if(coord){
      e.style.left = coord.x+'px';
      e.style.top = coord.y+'px';
    }
    e.style.visibility = str;
  }
  else if(browser_type == 3){
    e = document.getElementById(menuname);
    if(coord){
      e.style.left = coord.x+'px';
      e.style.top = coord.y+'px';
    }
    e.style.visibility = str;
  }
}

function show_menu(el,menuname){
  coord = {x:0,y:el.offsetHeight};
  ee = el;
  while(ee){
    coord.x += ee.offsetLeft;
    coord.y += ee.offsetTop;
    ee = ee.offsetParent;
  }
  set_visibility(menuname,true,coord);
}

function hide_menu(menuname){ set_visibility(menuname,false,false); }

function hilite(el){
  clear_timer();
  el.className = 'hilite';
}
function unhilite(el){
  el.className = '';
  timer_menu();
}

// menus
function Menu(){
  this.subMenus = new Array();
}

Menu.prototype.addSubMenu =
function(name,href,items) {
  this.subMenus[this.subMenus.length] = {name: name,href:href,items: items,id: submenu_count++};
}

Menu.prototype.append =
function(papa) {
  var tableMain = document.createElement('table');add_child(papa,tableMain);
  tableMain.className = 'js-menu left';
  tableMain.cellPadding = 0; tableMain.cellSpacing = 0;
  var table = document.createElement('tbody');add_child(tableMain,table);
  var tr = document.createElement('tr');add_child(table,tr);
  
  for(var i=0;i<this.subMenus.length;i++){
    var td = document.createElement('td');add_child(tr,td);
    var l = document.createElement('a'); add_child(td,l);
    
    l.setAttribute('onmouseover',"open_menu(this,'submenu"+this.subMenus[i].id+"');");
    l.onmouseover = new Function("open_menu(this,'submenu"+this.subMenus[i].id+"');");
    l.setAttribute('onmouseout',"timer_menu();");
    l.onmouseout = new Function("timer_menu();");
    l.href = this.subMenus[i].href;
    l.appendChild(create_text(this.subMenus[i].name));
  }

  // submenu invisible tables creation
  for(var i=0;i<this.subMenus.length;i++){
    var divMain = document.createElement('div');add_child(papa,divMain);
    divMain.setAttribute('id','submenu'+this.subMenus[i].id);
    divMain.style.position = 'absolute';
    divMain.style.visibility = 'hidden';
    divMain.style.zIndex = 100;
    
    tableMain = document.createElement('table');add_child(divMain,tableMain);
    tableMain.className = 'js-menu js-submenu left';
    tableMain.cellPadding = 0; tableMain.cellSpacing = 0;
    table = document.createElement('tbody');add_child(tableMain,table);
        
    if(this.subMenus[i].items){
      for(var j=0;j<this.subMenus[i].items.length;j+=2){
        var name = this.subMenus[i].items[j];
        var href = this.subMenus[i].items[j+1];
        
        tr = document.createElement('tr');add_child(table,tr);
        var td = document.createElement('td');add_child(tr,td);
        td.setAttribute('onmouseover',"hilite(this);");
        td.onmouseover = new Function("hilite(this);");
        td.setAttribute('onmouseout',"unhilite(this);");
        td.onmouseout = new Function("unhilite(this);");
        
        var l = document.createElement('a'); add_child(td,l);
        l.href = href;
        l.style.display = 'block';
        l.innerHTML = name;
      }
    }
    else{
      // document.write('<tr><td>empty</td></tr>');
    }
  }
}

///////////////////////////////////
// Additional functions
///////////////////////////////////
var nl="\n";
var qu='"';
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));

// dynamic elements
// remove child
function remove_child(parent_id,child_id){
  var papa = document.getElementById(parent_id);
  var forRemove = document.getElementById(child_id);
  papa.removeChild(forRemove);
}
// move child up
function child_up(parent_id,child_id){
  var papa = document.getElementById(parent_id);
  var children = new Array();
  for(var i=0;i<papa.childNodes.length;i++){
    var child = papa.childNodes.item(i);
    if(child.id == child_id){
      if(i==0) return;
      var tmp = children[i-1];
      children[i-1] = papa.childNodes.item(i);
      children[i] = tmp;
    }
    else
      children[i] = papa.childNodes.item(i);
  }
  for(var i=0;i<children.length;i++){
    papa.removeChild(papa.childNodes.item(0));
  }
  for(var i=0;i<children.length;i++){
    papa.appendChild(children[i]);
  }
}
// move child down
function child_down(parent_id,child_id){
  var papa = document.getElementById(parent_id);
  var children = new Array();
  for(var i=papa.childNodes.length-1;i>=0;i--){
    var child = papa.childNodes.item(i);
    if(child.id == child_id){
      if(i==papa.childNodes.length-1) return;
      var tmp = children[i+1];
      children[i+1] = papa.childNodes.item(i)
      children[i] = tmp;
    }
    else
      children[i] = papa.childNodes.item(i);
  }
  for(var i=0;i<children.length;i++){
    papa.removeChild(papa.childNodes.item(0));
  }
  for(var i=0;i<children.length;i++){
    papa.appendChild(children[i]);
  }
}
function create_text(str){
  return document.createTextNode(str)
}
function add_child(p,c){
  p.appendChild(c);
  return p;
}
function create_label_row(label,el){
  var tr = document.createElement('tr');
  var td = document.createElement('td');
  td.className == 'right nowrap w1';
  td.appendChild(create_text(label+' : '));
  tr.appendChild(td);

  td = document.createElement('td');
  td.appendChild(el);
  tr.appendChild(td);
  return tr;
}
function create_field_row(label,name,value){
  var inp = document.createElement('input');
  inp.setAttribute('type','text');
  inp.setAttribute('name',name);
  if(value) inp.setAttribute('value',value);
  inp.setAttribute('size','60');
  inp.className = 'w100 textbox';

  return create_label_row(label,inp);
}
function create_flat_row(el){
  var tr = document.createElement('tr');
  var td = document.createElement('td');
  td.setAttribute('colspan','2');
  td.colSpan = 2;
  td.appendChild(el);
  tr.appendChild(td);
  return tr;
}

function create_jslink(text,js){
  var l = document.createElement('a');
  l.setAttribute('onclick',js);
  l.onclick = new Function(js);
  l.appendChild(create_text(text));
  return l;
}
