var menus = new Array;
var menustruct = new Array();
var bodyref;
var offsetleft = 5;
var offsettop = 20;
var suboffsettop = 30;
var shadowoffset = 7;
var shadowcast = 4;
var toobj;

// Config preferences
var basetab = "    "; // Adjust to suit starting point of code indentation
var showimages = false;  // flag to disable loading of images into menu
var imgdir = "menuimages/";  // base directory for images
var imgheight = "24px";   // height of menu images

var navcolouroff = "#CC9933";  // background colour of main menu
var navcolouron = "#996633";  // highlighted background colour of main menu
var navtextoff = "white";   // text colour of main menu
var navtexton = "white";   // highlighted text colour of main menu

var mnucolouroff = "#CC9933";  // background colour of sub menus
var mnucolouron = "#996633";  // highlighted background colour of sub menus
var mnutextoff = "white";   // text colour of sub menus
var mnutexton = "white";   // highlighted text colour of sub menus
var mnuborder = "silver outset 1pt";   // border colour for sub menus


function GetMenus() {

  bodyref = document.body;
  bodyref.style.width = "95%";
  LoadArray();
  bodyref.innerHTML = bodyref.innerHTML + BuildMenuHTML();
  SetStyle();

}


function LoadArray() {

  // Main Menu Bar
  xmenu = new Menu(0, 'menubar');
  xmenu.addItem(new MenuItem('home', 'Home', 'index.asp'));
  xmenu.addItem(new MenuItem('about', 'About Us', null, null, 'mnuabout'));
  xmenu.addItem(new MenuItem('contact', 'Contact Us', 'info.asp?key=Contact_Us'));
  xmenu.addItem(new MenuItem('vision', 'Vision', 'info.asp?key=Vision'));
  xmenu.addItem(new MenuItem('servs', 'Services', null, null, 'mnuservs'));
  CreateMenu();


  // Level 1 menus
  xmenu = new Menu(1, 'mnuabout');
  xmenu.addItem(new MenuItem('aboutus', 'About Us', 'info.asp?key=About_Us', null));
  xmenu.addItem(new MenuItem(null, null, null, null, null, true));
  xmenu.addItem(new MenuItem('lawyers', 'Lawyers', null, null, 'sublawyers'));
  CreateMenu();

  xmenu = new Menu(1, 'mnuservs');
  xmenu.addItem(new MenuItem('corporate', 'Corporate And Property Law', 'info.asp?key=Corporate_And_Property_Law', null));
  xmenu.addItem(new MenuItem('litigation', 'Litigation', 'info.asp?key=Litigation', null));
  xmenu.addItem(new MenuItem('licenses', 'Licenses', 'info.asp?key=Licences', null));
  xmenu.addItem(new MenuItem('translation', 'Notary And Translation', 'info.asp?key=Notary_And_Translation', null));
  xmenu.addItem(new MenuItem('visa', 'Visa-Tax-Accounting', 'info.asp?key=Visa-Tax-Accounting', null));
  CreateMenu();


  // Level 2 menus
  xmenu = new Menu(2, 'sublawyers');
  xmenu.addItem(new MenuItem('chai', 'Reungchai Watcharasin', 'info.asp?key=Reungchai_Watcharasin', null));
  xmenu.addItem(new MenuItem('apple', 'Chamaiporn Chorchan', 'info.asp?key=Chamaiporn_Chorchan', null));
  CreateMenu();

}


function BuildMenuHTML() {
  var ptr;
  var str = "";
  var checklevel = 1;
  var shadowname = "";

  for (ptr = 0; ptr < menustruct.length; ptr ++) {
    if (menustruct[ptr].level == checklevel) {
      switch (menustruct[ptr].level) {
        case 1: shadowname = "mnushadow";
                break;
        case 2: shadowname = "submnushadow";
                break;
        case 3: shadowname = "childmnushadow";
                break;
        default: shadowname = "";
      }
      if (shadowname != "") {
        str += basetab + "<div id=" + shadowname + " class=shadow style=display:none>\n";
        str += basetab + "</div>\n";
      }
      checklevel ++;
    }
    if (menustruct[ptr].level == 0) {
      str += BuildNavMenu(menustruct[ptr]);
    }
    else {
      str += BuildSubMenu(menustruct[ptr]);
    }
  }
  return str;
}


function BuildNavMenu(obj) {
  var ptr;
  var buf = "";
  var xclass = "";
  var mouseover = "";
  var mouseout = "";
  var mouseclick;

  xclass = "menubar";
  buf += basetab + "<table id=" + obj.id + " class=" + xclass + " onmouseover=\"togglemenu(1);\" onmouseout=\"togglemenu(0);\">\n";
  buf += basetab + "  <tr>\n";

  for (ptr = 0; ptr < obj.items.length; ptr ++) {
    mouseover = " onmouseover=\"showmenu('" + obj.items[ptr].id + "', '" + obj.items[ptr].submenu + "', 0); highlight('" + obj.items[ptr].id + "', 1);\"";
    mouseout = " onmouseout=\"highlight('" + obj.items[ptr].id + "', 0);\"";
    mouseclick = "";
    if (obj.items[ptr].command != null) {
      if (obj.items[ptr].popup == true) {
        mouseclick = " onclick=\"popupdynamic('" + obj.items[ptr].command + "', " + obj.items[ptr].height + ", " + obj.items[ptr].width + ", '" + obj.items[ptr].showtoolbar + "', '" + obj.items[ptr].scrollbars + "');\"";
      }
      else {
        mouseclick = " onclick=\"gotourl('" + obj.items[ptr].command + "');\"";
      }
    }
    buf += basetab + "    <td class=" + xclass + " id=" + obj.items[ptr].id + mouseover + mouseout + mouseclick + ">" + obj.items[ptr].caption + "</td>\n";
  }
  buf += basetab + "  </tr>\n";
  buf += basetab + "</table>\n";

  return buf;

}


function BuildSubMenu(obj) {
  var ptr;
  var buf = "";
  var xclass = "";
  var mouseover = "";
  var mouseout = "";
  var mouseclick;
  var cellpad;
  var colspan = 3;
  
  if (showimages == false) colspan = 2;

  switch (obj.level) {
    case 1: xclass = "menu";
            break;
    case 2: xclass = "submenu";
            break;
    case 3: xclass = "childmenu";
            break;
  }
  cellpad = " style=display:none cellpadding=4 cellspacing=0 border=0";
  buf += basetab + "<table id=" + obj.id + " class=" + xclass + cellpad + " onmouseover=\"togglemenu(1);\" onmouseout=\"togglemenu(0);\">\n";

  for (ptr = 0; ptr < obj.items.length; ptr ++) {
    if (obj.items[ptr].separator == true) {
      buf += "  <tr class=divider><td colspan=" + colspan + "><hr></td></tr>\n";
    }
    else {
      if (obj.items[ptr].submenu == null) {
        switch (obj.level) {
          case 1: mouseover = " onmouseover=\"hideallsubmenus(); highlight('" + obj.items[ptr].id + "', 1);\"";
                  break;
          case 2: mouseover = " onmouseover=\"hideallchildmenus(); highlight('" + obj.items[ptr].id + "', 1);\"";
                  break;
          case 3: mouseover = " onmouseover=\"highlight('" + obj.items[ptr].id + "', 1);\"";
                  break;
        }
      }
      else {
        mouseover = " onmouseover=\"showmenu('" + obj.items[ptr].id + "', '" + obj.items[ptr].submenu + "', " + obj.level + "); highlight('" + obj.items[ptr].id + "', 1);\"";
      }
      mouseout = " onmouseout=\"highlight('" + obj.items[ptr].id + "', 0);\"";
      mouseclick = "";
      if (obj.items[ptr].command != null) {
        if (obj.items[ptr].popup==true) {
          mouseclick = " onclick=\"popupdynamic('" + obj.items[ptr].command + "', " + obj.items[ptr].height + ", " + obj.items[ptr].width + ", '" + obj.items[ptr].showtoolbar + "', '" + obj.items[ptr].scrollbars + "');\"";
        }
        else {
          mouseclick = " onclick=\"gotourl('" + obj.items[ptr].command + "');\"";
        }
      }
      buf += basetab + "  <tr  id=" + obj.items[ptr].id + mouseover + mouseout + mouseclick + ">\n";
      if (showimages == true) buf += basetab + "    <td><img src=" + imgdir + obj.items[ptr].image + " class=menuimg></td>\n";
      if (obj.items[ptr].submenu == null) {
        buf += basetab + "    <td colspan=2>" + obj.items[ptr].caption + "&#160; &#160;</td>\n";
      }
      else {
        buf += basetab + "    <td>" + obj.items[ptr].caption + "&#160; &#160;</td>\n";
        buf += basetab + "    <td class=submenu><img src=" + imgdir + "right-arrow.png class=menuimg></td>\n";
      }

      buf += basetab + "  </tr>\n";
    }
  }

  buf += basetab + "</table>\n";

  return buf;
}


function showmenu(parentctrl, childctrl, xlevel) {

  switch (xlevel) {
    case 0: hideallmenus();
            break;

    case 1: hideallsubmenus();
            break;

    case 2: hideallchildmenus();
            break;
  }

  xchild = null;
  if (childctrl != null) xchild = document.getElementById(childctrl);

  if (xchild) {
    if (xchild.style.display != "") {
      xparent = document.getElementById(parentctrl);
      switch (xlevel) {
        case 0: xchild.style.top = xparent.offsetTop + offsettop;
                xchild.style.width = "auto";
                xchild.style.left = xparent.offsetLeft + xparent.offsetParent.offsetLeft;
                xshadow = document.getElementById("mnushadow");
                break;

        case 1: xchild.style.top = xparent.offsetTop + suboffsettop;
                xchild.style.width = "auto";
                xchild.style.left = xparent.offsetParent.offsetLeft + xparent.offsetWidth;;
                xshadow = document.getElementById("submnushadow");
                break;

        case 2: xchild.style.top = xparent.offsetTop + suboffsettop;
                xchild.style.width = "auto";
                xchild.style.left = xparent.offsetParent.offsetLeft + xparent.offsetWidth;;
                xshadow = document.getElementById("childmnushadow");
                break;
      }

      xchild.style.display = "";

      // If the menu goes past the bottom of the body, move it up
      if ((xchild.offsetTop + xchild.offsetHeight) > (bodyref.offsetHeight - 4)) {
        xchild.style.top = bodyref.offsetHeight - xchild.offsetHeight - 10;
      }

      // if the menu overruns the page then move it in or flip it round
      if ((xchild.offsetLeft + xchild.offsetWidth) > (bodyref.offsetWidth - (xchild.offsetWidth / 3))) {
        if (xparent.offsetParent.offsetLeft<50) {
          xchild.style.left = bodyref.offsetWidth - xchild.offsetWidth - 24;
        }
        else {
          xchild.style.left = xparent.offsetParent.offsetLeft - xchild.offsetWidth + offsetleft;
        }
      }

      // If the menu is too far left, make the left at 0
      if (xchild.offsetLeft < 0) {
        xchild.style.left = 0;
      }

      xshadow.style.top = xchild.offsetTop + shadowoffset;
      xshadow.style.left = xchild.offsetLeft + shadowoffset;
      xshadow.style.height = xchild.offsetHeight - shadowcast;
      xshadow.style.width = xchild.offsetWidth - shadowcast;
      xshadow.style.display = ""
    }
  }
}

function hideallmenus() {
  var i;
  
  xmenu = document.getElementsByTagName('table');
  if(xmenu.length>0) {
    for (i=0; i<xmenu.length; i++) {
      if (xmenu[i].className=="menu" || xmenu[i].className=="submenu"){
        xmenu[i].style.display = "none";
      }
    }
  }
  xshadow = document.getElementById('mnushadow');
  if (xshadow) xshadow.style.display = "none";
  xshadow = document.getElementById('submnushadow');
  if (xshadow) xshadow.style.display = "none";
  xshadow = document.getElementById('childmnushadow');
  if (xshadow) xshadow.style.display = "none";
}

function hideallsubmenus() {
  var i;

  xmenu = document.getElementsByTagName('table');
  if(xmenu.length>0) {
    for (i=0; i<xmenu.length; i++) {
      if (xmenu[i].className=="submenu"){
        xmenu[i].style.display = "none";
      }
    }
  }
  xshadow = document.getElementById('submnushadow');
  if (xshadow) xshadow.style.display = "none";
}

function hideallchildmenus() {
  var i;

  xmenu = document.getElementsByTagName('table');
  if(xmenu.length>0) {
    for (i=0; i<xmenu.length; i++) {
      if (xmenu[i].className=="childmenu"){
        xmenu[i].style.display = "none";
      }
    }
  }
  xshadow = document.getElementById('childmnushadow');
  if (xshadow) xshadow.style.display = "none";
}


function highlight(id, onoff) {
  xctrl = document.getElementById(id);

  if (onoff==1) {
    if (xctrl.className == "menubar") {
      xctrl.style.background = navcolouron;
      xctrl.style.color = navtexton;
      xctrl.style.cursor = "pointer"
    }
    else {
      xctrl.style.background = mnucolouron;
      xctrl.style.color = mnutexton;
      xctrl.style.cursor = ""
    }
  }
  else {
    if (xctrl.className == "menubar") {
      xctrl.style.background = navcolouroff;
      xctrl.style.color = navtextoff;
      xctrl.style.cursor = "pointer"
    }
    else {
      xctrl.style.background = mnucolouroff;
      xctrl.style.color = mnutextoff;
      xctrl.style.cursor = ""
    }
  }
}


function togglemenu(onoff) {
  if (onoff==1) {
    clearTimeout(toobj);
  }
  else {
    toobj = setTimeout ("hideallmenus()", 300);
  }
}

function CreateMenu() {

  menustruct[menustruct.length] = menus[0];

}

function MenuRegister(item) {
  menus[menus.length] = item;
}

function MenuItem(id, caption, command, image, submenu, separator, popup, height, width, scrollbars, showtoolbar) {
  // these become the tr and td tags
  this.id = id;
  this.caption = caption;
  this.command = command;
  this.image = image;
  this.submenu = submenu;
  this.separator = (separator==true) ? true : false;
  this.popup = (popup==true) ? true : false;
  this.height = height;
  this.width = width;
  this.scrollbars = (scrollbars==true) ? 'yes' : 'no';
  this.showtoolbar = (showtoolbar==true) ? 'yes' : 'no';
  MenuRegister(this);
}


function Menu(level, id) // this becomes the table tag
{
  menus = new Array();
  this.items = new Array();
  this.level = level;
  this.id = id;
  MenuRegister(this);
  this.update = true;
}

function MenuAddItem(item)
{
  this.items[this.items.length] = item;
}

Menu.prototype.addItem = MenuAddItem;

function SetStyle() {
 
  var i;
  var xmenu = document.getElementsByTagName('table');

  if(xmenu.length>0) {
    for (i=0; i<xmenu.length; i++) {
      switch (xmenu[i].className) {
        case "menubar":   xmenu[i].style.background = navcolouroff;
                          xmenu[i].style.color = navtextoff;
                          xmenu[i].style.border = mnuborder;
                          break;

        case "menu":      xmenu[i].style.background = mnucolouroff;
                          xmenu[i].style.color = mnutextoff;
                          xmenu[i].style.border = mnuborder;
                          break;

        case "submenu":   xmenu[i].style.background = mnucolouroff;
                          xmenu[i].style.color = mnutextoff;
                          xmenu[i].style.border = mnuborder;
                          break;

        case "childmenu": xmenu[i].style.background = mnucolouroff;
                          xmenu[i].style.color = mnutextoff;
                          xmenu[i].style.border = mnuborder;
                          break;
      }
    }
  }

  if (navigator.appName.indexOf("Microsoft")==-1) {
    SetLeft();
  }
  
  var ximg = document.getElementsByTagName('img');

  if(ximg.length>0) {
    for (i=0; i<ximg.length; i++) {
      if (ximg[i].className == "menuimg") ximg[i].style.height = imgheight;
    }
  }

}

function SetLeft() {
  // set the left property to zero if not ie
  var ptr;

  ptr = document.getElementById('menubar');
  if (ptr) ptr.style.left="0px";

}

function gotourl(url, popup) {

  if (popup==1) {
    window.open(url);
  }
  else {
    if (navigator.appName.indexOf("Microsoft")!=-1) {
      window.navigate(url);
    }
    else {
      window.location = url;
    }
  }
  hideallmenus();
}

function popupdynamic(url, height, width, menu, canscroll) {
  x = Math.random() * 50;
  y = Math.random() * 50;
  window.open(url, "", "top = " + y + ", left = " + x + ", height = " + height + ", width = " + width + ", menubar = no, resizable = yes, toolbar = " + menu + ", location = " + menu + ", status = no, scrollbars = " + canscroll);
}