/* Copyright (c)2002 ELR Software Pty Ltd. http://www.elr.com.au   */
/*                                                                 */
/* This program is free software; you may use, redistribute and/or */
/* modify it under the terms of the GNU General Public License as  */
/* published by the Free Software Foundation; either version 2 of  */
/* the License, or (at your option) any later version.             */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
/* See the GNU General Public License for more details.            */
/* http://www.fsf.org/home.html                                    */

Array.prototype.push = function(obj) {this[this.length] = obj;}
Array.prototype.pop  = function() {var obj = this[this.length-1]; if (this.length>0){this.length--}; return(obj);}
var HxStack   = new Array();
var MAX_DEPTH = 3;
var XHEIGHT   = 7.5; /* */
var BASEMEN   = '$m000';
var HOMEIMG   = 'home.gif';
var BACKIMG   = 'back.gif';
var UseIcons  = true;

var MENU = new Array();
for (var i=0; i <9; i++) {
  for (var j=0; j<9 ; j++) {
    for (var k=0; k<9 ; k++) {
      var m = '$m'+i+''+j+''+k;
      MENU[m] = new Array(4);
    }
  }
}

function HotkeyStr(str) {
  return('Hotkey = '+str);
}

function debug(s) {
  /* window.status = s; */
}

function btnClicked() {
  var id = '';
  if (event.type == "keypress") {
    if (window.event.keyCode == 13) {id = event.srcElement.id}
    else {
      var c = String.fromCharCode(window.event.keyCode).toUpperCase();
      var oButtons = document.all.tags('BUTTON');
      var buttons = oButtons.length;
      for (var i=0; i<buttons; i++) {
        if (oButtons[i].title == HotkeyStr(c)) {id = oButtons[i].id}
      }
    }
  }
  else {
    id = (id > '') ? id : event.srcElement.id;
  }
  /* alert(id); */
  if (!WC.WCTogState()) {
    switch (id) {
      case 'back': {
        /* alert(HxStack); */
        id = HxStack.pop(); id = HxStack.pop(); /* sic - we want previous, not current */
        id = (typeof(id) == 'undefined') ? BASEMEN : id;
        break;
      }
      case BASEMEN: {
        break;
      }
    }
    /* alert(id); */
    if (id > '') {
      if (isMenu(id)) {
       /* save menu id in cookie */
        var lastmenuCookie = new Cookie(document.location.pathname+'_lastmenu');
        lastmenuCookie.store(id);
        writeMenu(id);               /* and show new menu */
      }
      else {
        var m = eval('document.all.'+id);
        /* alert(m.ourhref); */
        window.location = m.ourhref; /* or open the required href */
      }
    }
  }
  return(false);
}

function isMenu(s) {
  s = (s > '') ? s : '_';  /* Trap empty strings */
  if (s.match(/^\$m|l[0-9]{3}$/)) {debug(s);return(true)} /* eg $m001 or $l999 */
  else {return(false)}
}

function menuRoller() {
  var elem = event.srcElement;
  /* window.status = elem.id; */
  if (elem.id > '') {
    switch(event.type.toLowerCase()) {
      case 'mouseover': {elem.focus()}
      case 'focus': {
        elem.className = elem.rollclass+ '_';
        break;
      }
      case 'mouseout': {elem.blur()}
      case 'blur': {
        elem.className = elem.rollclass;
        break;
      }
    }
  }
}

function menuTitle (m) {
  var s = '';
  if (m == BASEMEN) {s = 'Home&nbsp;Page'}
  else {
    for (var i=0; i <MAX_DEPTH; i++) {
      for (var j=0; j<9 ; j++) {
        for (var k=0; k<9 ; k++) {
          var x = '$m'+i+''+j+''+k;
          if (''+MENU[x][0] != 'undefined') {
            var hrefs  = MENU[x][1].split('|');
            for (var l=0; l<hrefs.length; l++) {
              if ('$m'+hrefs[l] == m) {
                var links = MENU[x][0].split('|');
                s = links[l];
                break;
              }
            }
          }
        }
      }
    }
  }
  return(s);
}



function tableRows(m) {
  /*  window.status = menuTitle(m); */
  var s = ''; var btnid = ''; /* var l = ''; */
  var h = Math.round((window.screen.availHeight/XHEIGHT));
  var j = 1;
  var links  = MENU[m][0].split('|');
  var hrefs  = MENU[m][1].split('|');
  var titles = MENU[m][2].split('|');
  var icons  = MENU[m][3].split('|');
  var icon   = '';
  var homeimg  = HOMEIMG;
  var homealt  = 'Go home';
  var showback = 'visible';
  if (m == BASEMEN) {
    homeimg = LOGOIMG;
    homealt = LOGOALT;
    showback = 'none';
    s  = '<tr><td width="25%" id="'+BASEMEN+'" class="logo" onclick="history.go(-1)" tabindex="-1" height="'+(1.1*h)+'px">';
  }
  else {
    s  = '<tr><td width="25%"><button id="'+BASEMEN+'" class="home" rollclass="home" style="height:'+(1.1*h)+'px" ';
    s += ' onclick="btnClicked()" onmouseover="menuRoller()" onmouseout="menuRoller()" onfocus="menuRoller()" onblur="menuRoller()" accesskey="h" tabindex="1" title="'+HotkeyStr('ALT-H')+'">';
  }
  s += '<img hspace="0" border="0" height="60" width="80" align="middle" src="'+homeimg+'" alt="'+homealt+'">'+WC.WCButtonFix("Home")+'</button></td>';
  s += '<td width="25%"><button id="back" class="back" rollclass="back" style="display:'+showback+'" style="height:'+(1.1*h)+'px" ';
  s += 'onclick="btnClicked()" onmouseover="menuRoller()"  onmouseout="menuRoller()" onfocus="menuRoller()" onblur="menuRoller()" accesskey="b" tabindex="2" title="'+HotkeyStr('ALT-B')+'">';
  s += '<img hspace="0" border="0" height="60" width="80" align="middle" alt="Go back" src="'+BACKIMG+'">'+WC.WCButtonFix("Back")+'</button></td>';
  s += '<td colspan="2" width="50" class="menutitle">'+menuTitle(m)+'</td></tr><tr>';
  if (links.length > 0) {
    h = Math.round(window.screen.availHeight/XHEIGHT);
    for (var i=0; i <links.length; i++) {
      hrefs[i]  = (''+hrefs[i]  == 'undefined') ? '' : hrefs[i];
      titles[i] = (''+titles[i] == 'undefined') ? '' : titles[i];
      icons[i]  = (''+icons[i]  == 'undefined') ? '' : icons[i];
      icons[i]  = (''+icons[i]  == '') ? DEFAULTICON : icons[i];
      btnid = (parseInt(hrefs[i]) >= 0) ? '$m'+hrefs[i] : hrefs[i];
      btnid = isMenu(btnid) ? btnid : 'menu'+i;
      s += '<td colspan="2" width="50%"><button id="'+btnid+'" class="menu'+i+'" rollclass="menu'+i+'" ourhref="'+hrefs[i]+'" ourtitle="'+titles[i]+'" style="height:'+h+'px" ';
      s += 'onclick="btnClicked()" onmouseover="menuRoller()" onmouseout="menuRoller()" onfocus="menuRoller()" onblur="menuRoller()" tabindex="i+2" title="'+HotkeyStr(i+1)+'">';
      if (UseIcons) {
        s += '<img class="menuicon" height="50" width="50" border="0" hspace="5" vspace="0" src="'+icons[i]+'" alt="'+HotkeyStr(i+1)+'">';
      }
      s += (i+1)+'. '+WC.WCButtonFix(links[i])+'</button></td>';
      if (j%2==0) {s += '</tr><tr>'};
      j++;
    }
    s += '</tr>'
  }
   return(s);
}

function refreshIconImages() {
  if (UseIcons) {
    var oImages = document.all.tags('IMG');
    var images = oImages.length;
    for (var i=0; i<images; i++) {
      if (oImages[i].className = 'menuicon') {oImages[i].src = oImages[i].src}
    }
  }
}

function writeMenu(m) {
  /* alert(m); */
  /* recover last menu saved in cookie (if any) */
  var lastmenuCookie = new Cookie(document.location.pathname+'_lastmenu');
  m = (lastmenuCookie.get()) ? lastmenuCookie.get() : BASEMEN;
  m = (m > '') ? m : BASEMEN;
  if (m != BASEMEN) {HxStack.push(m);}
  var s  = '<table id="pagetable" border="0" cellpadding="0" cellspacing="15"><tbody>';
      s += tableRows(m);
      s += '</tbody></table>';
  if (document.all) {
    document.all.pagetablediv.innerHTML = s;
    /* IE sometimes doesn't place the icon images! - try this */
    refreshIconImages();
  }
}

document.onkeypress = btnClicked; /* Enable keyboard handler */

