/***************************
* 
* menu.js
* 
* this script generates an object to handle 
* a pop-up-menu
* 
* created by Daniel Kiess;
*         www.kiesswerk.de
*         daniel@kiesswerk.de
* 
* You may use this script in your own sites
* if you do not remove or modify this text.
* If you have any suggestions or bugfixes,
* please report them to the adress above.
* 
***************************/

// sniff the client
var is_ie  = document.all;
var is_dom = ((!is_ie) && (document.getElementById));
var is_nav = ((!is_dom) && (document.layers));

// turn an image on
function imgOn(name, force) {
  if (!force) {
    if ((typeof(activePic) == 'string') && (name == activePic)) return(0);
    if ((typeof(activePic) == 'object') && (getPos(activePic, name) != -1)) return(0);
  }
  var obj = (is_ie) ? document.all[name] : (is_dom) ? document.getElementById(name) : null;
  if (obj == null) {
    if (document.images[name]) obj = document.images[name];
    else {
      for (var l in document.layers)
        if ((l != 'length') && (document.layers[l].document.images[name])) {
          obj = document.layers[l].document.images[name];
        }
    }
    if (obj == null) return(0); // object not found
  }
  var oldSrc = obj.src;
  if (oldSrc && (oldSrc.indexOf('_off.') != -1)) {
    var strg = (oldSrc.indexOf('_off2') != -1) ? '_off2' : '_off';
    var strg1 = oldSrc.substr(0, oldSrc.indexOf(strg));
    var strg2 = oldSrc.substr((oldSrc.indexOf(strg) + strg.length), oldSrc.length);
    var newSrc = strg1 + '_on' + strg2;
    obj.src = newSrc;
    return(1);
  }
  return(0);
}

// turn an image off
function imgOff(name, force) {
  var text = "";
  if (!force) {
    if ((typeof(activePic) == 'string') && (name == activePic)) { return(0); }
    if ((typeof(activePic) == 'object') && (getPos(activePic, name) != -1)) { return(0); }
  }
  var obj = (is_ie) ? document.all[name] : (is_dom) ? document.getElementById(name) : null;
  text += obj + "; ";
  if (obj == null) {
    if (document.images[name]) obj = document.images[name];
    else {
      for (var l in document.layers) {
        if ((l != 'length') && (document.layers[l].document.images[name]))
          obj = document.layers[l].document.images[name];
      }
    }
    if (obj == null) {
      return(0); // object not found
    }
  }
  var oldSrc = obj.src;
//  if (name.indexOf('pus') != -1) alert(oldSrc);
  if (oldSrc && (oldSrc.indexOf('_on.') != -1)) {
    var strg1 = oldSrc.substr(0, oldSrc.indexOf('_on'));
    var strg2 = oldSrc.substr((oldSrc.indexOf('_on') + 3), oldSrc.length);
    var mode = (name.charAt(name.length - 1) == '2');
    if (mode) var newSrc = strg1 + '_off2' + strg2;
      else var newSrc = strg1 + '_off' + strg2;
//    alert('newSrc = ' + newSrc);
    obj.src = newSrc;
    return(1);
  }
  return(0);
}

// create an object to handle layers
function createObj(divName, mode) {
  var obj = null;
  if (is_dom) {
    obj = document.getElementById(divName);
    if ((obj) && (mode)) obj = obj.style;
  } else if (is_ie) {
    obj = document.all[divName];
    if ((obj) && (mode)) obj = obj.style;
  } else if (is_nav) {
    obj = document.layers[divName];
  }
  return(obj)
}

// show a layer
function show(divName) {
  var obj = createObj(divName, 1);
  if (obj) obj.visibility = "visible";
}

// hide a layer
function hide(divName) {
  var obj = createObj(divName, 1);
  if (obj) obj.visibility = "hidden";
}

function showNhide(divName, hideArray) {
  show(divName);
  for (var i in hideArray)
    if (divName != hideArray[i]) hide (hideArray[i]);
}

// create a new (sub-) menu-entry
function createMenu(name, picID, divName) {
  this.name    = name;
  this.id      = picID;
  this.divName = divName;
  
  return(this);
}

// register a new (sub-) menu-entry
function newMenu(name, picID, divName) {
  if (document.images[picID]) {
    var obj = createObj(divName);
    if (obj) {
      this.items[this.items.length] = new createMenu(name, picID, divName);
      if (!this.entries[name]) {
        this.entries[name] = [];
      }
      this.entries[name][picID] = divName;
      return(1);
    } else {
      alert('Layer \'' + divName + '\' is not in current document.\rMenu entry was not created.');
      return(0);
    }
  } else {
    alert('Image \'' + picID + '\' is not in current document.\rMenu entry was not created.');
    return(0);
  }
}

// register a new Image for a menu
function newEntry(menuName) {
  
  // is menuName already registered?
  var divName = null;
  for (var i in this.items) {
    if (this.items[i].name == menuName) {
      // ok, found the menu
      divName = this.items[i].divName;
    }
  }
  
  if (divName == null) {
    alert('newEntry: the menu "' + menuName + '" was not registered yet.');
    return(0);
  }
  
  var args = newEntry.arguments;
  // repeat registration for each image given (skip the first one)
  
  for (var a = 1; a < args.length; a++) {
    // is picID already registered somewhere?
    var picID = args[a];
    for (var i in this.entries) {
      for (var j in this.entries[i]) {
        if (j == picID) {
          alert('image "' + picID + '" is already registered in menu "' + j + '"!');
          return(0);
        }
      }
    }
    
    // submenu already registered?
    if (!this.entries[menuName]) {
      // no, register it
      this.entries[menuName] = [];
    }
    // register the image
    this.entries[menuName][picID] = divName;
  }
  
  return(1);
}

// activate the menu
function activate() {
  if (is_nav) document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
  this.oldMouseOver = document.onmouseover;
  this.oldMouseOut  = document.onmouseout;
  // teo
  document.onmouseover = mouseOver; //eval(this.obj + '.mouseOver;');
  document.onmouseout  = mouseOut; //eval(this.obj + '.mouseOut;');
}

// deactivate the menu
function deactivate() {
  document.onmouseover = this.oldMouseOver;
  document.onmouseout  = this.oldMouseOut;
}
function findImg(x, y) {
  for (var ls in document.layers) {
    if (ls != "length") {
      var l = document.layers[ls];
      for (var i in l.document.images) {
        if ((l.visibility == "show") && (l.document.images[i].x == x) && (l.document.images[i].y == y))
          return(i);
      }
    }
  }
  for (var i in document.images) {
    if (i != 'length') {
      if ((document.images[i].x == x) && (document.images[i].y == y))
        return(document.images[i].name);
    }
  }
  return(null);
}

// check for mouseOver
function mouseOver(e) {
  // received a mouseover
  // now let's get the source of the event
  var id = null;
  if ((is_nav) && (e) && (e.target)) {
    if (e.target.id) id = e.target.id;
    else if (((e.target.x) || (e.target.x == 0)) && ((e.target.y) || (e.target.y == 0))) {
      // found link; ist there an image with the same coords?
      id = findImg(e.target.x, e.target.y);
    }
  }
  else if ((is_ie) && (event) && (event.toElement) && (event.toElement.id)) {
    id = event.toElement.id;
  }
  else if ((is_dom) && (e) && (e.target)) {
    if (e.target.name) id = e.target.name;
    if (e.target.id) id = e.target.id;
  }
  
  // ok, source (or null) is now in id
  // do we have this id registered?
  if (id != null) {
    // check only if we found an id
    for (var i = 0; i < menuCount; i++) {
      // call all the menus to check this id
      var evl = 'menuObj' + i + '.menuAction(\'' + id + '\', 0);'
//alert(evl);
      eval(evl);
    }
  }
  return(false);
}

// check for mouseOut
function mouseOut(e) {
  // received a mouseout
  // now let's get the source of the event
  var id = null;
  if ((is_nav) && (e) && (e.target)) {
    if (e.target.id) id = e.target.id;
    else if (((e.target.x) || (e.target.x == 0)) && ((e.target.y) || (e.target.y == 0))) {
      // found link; ist there an image with the same coords?
      id = findImg(e.target.x, e.target.y);
    }
  }
  else if ((is_ie) && (event) && (event.fromElement) && (event.fromElement.id)) {
    id = event.fromElement.id;
  }
  else if ((is_dom) && (e) && (e.target)) {
    if (e.target.name) id = e.target.name;
    if (e.target.id) id = e.target.id;
  }
  if (id != null) {
//alert(id);
    // check only if we found an id
    for (var i = 0; i < menuCount; i++) {
      // call all the menus to check this id
      var evl = 'menuObj' + i + '.menuAction(\'' + id + '\', 1);'
      eval(evl);
    }
  }
}

// find a DivName
function getDivName(id) {
  // search the items
  for (var i in this.items) {
    if (this.items[i].name == id) {
      return(this.items[i].divName);
    }
  }
  
  // search the entries
  for (var i in this.entries) {
    for (var j in this.entries[i]) {
      if (i == id) return(this.entries[i][j]);
    }
  }
  return(null);
}

function menuAction(id, mode) {
  // nothing found yet
  var done = false;
//  window.status += id + ': ' + mode + '; ';
  
  // don't do anything, if id is not regitered in this menu
  if (this.isValidID(id)) {
//    window.status += '; v: ' + id;
    
    var menu4ID = this.findMenu4ID(id);
    if ((menu4ID != this.currentMenu)){
      // we stepped into another menu or we leaved
        this.clearTimeOut();
      if (menu4ID == null) {
        // we leaved
        this.startTimeOut();
      } else {
        if (!mode) {
          if (this.currentMenu != null) hide(this.getDivName(this.currentMenu));
          show(this.getDivName(menu4ID));
          this.currentMenu = menu4ID;
        }
      }
    } else {
      if (/*(menu4ID == null) && (this.currentMenu != null) ||*/ (mode)) {
        this.startTimeOut();
      }
      if (!mode) {
        this.clearTimeOut();
      }
    }
  }
  else {
    if ((this.currentMenu != null) && (mode)) this.startTimeOut();
//    window.status += '; nv: ' + id;
  }
  
  // if we didn't find a regiestered item, it's probably an image
  if (!done) {
    if (mode){
      imgOff(id);
    } else{
      imgOn(id);
    }
  }
  
}

function isValidID(id) {
//  alert('s');
  var valid = false;
  for (var i in this.items) {
    valid |= (this.items[i].id == id);
//    alert((this.items[i].id == id) + '; ' + this.items[i].id + ' == ' + id + '; ' + valid);
  }
  for (var i in this.entries) {
    valid |= (typeof(this.entries[i][id]) != 'undefined');
//    alert(this.entries[i][id] + '; ' + valid);
  }
//  alert('e: ' + valid);
  return(valid);
}

function menuOut(id) {
  // check all items stored
  for (var i in this.items) {
    if (this.items[i].id == id) {
      // ok, found the correct id
      this.timeOutID = setTimeout('hide(' + this.items[i].divName + ');', this.timeOut);
    }
  }
}

function clearTimeOut() {
  if (this.timeOutID != -1) {
    // clear the timeOut and reset it
    clearTimeout(this.timeOutID);
    this.timeOutID = -1;
//    window.status += 'geloescht; ';
  }
}

function startTimeOut() {
  if (this.timeOutID != -1) this.clearTimeOut();
  this.timeOutID = setTimeout(this.obj + '.closeMenu();', this.timeOut);
//  window.status += 'gestartet; ';
}

function closeMenu() {
  if (this.currentMenu != null) {
    var divName = this.getDivName(this.currentMenu);
    if (divName != null) hide(divName);
    this.currentMenu = null;
  }
}

function findMenu4ID(id) {
  // search the items (seems to be stupid ;), but necessary)
  for (var i in this.items) {
    if (this.items[i].id == id) {
      return(id);
    }
  }
  
  // search the entries
  for (var i in this.entries) {
//    alert(i + ': ' + this.entries[i][id]);
    if (this.entries[i][id]) {
      return(i); // return the menu of the id
    }
  }
  
  return(null);
}

function menu(name) {
  // "private" properties
  this.name         = name;
  this.items        = []; // collect the menus
  this.entries      = []; // collect the images in each menu
  this.currentMenu  = null;
  this.oldMouseOver = null;
  this.oldMouseOut  = null;
  this.timeOut      = 500; // msec
  this.timeOutID    = -1;
  
  // "public" methods
  this.newMenu      = newMenu;
  this.newEntry     = newEntry;
  this.activate     = activate;
  this.deactivate   = deactivate;

  // "private" methods
  this.menuAction   = menuAction;
  this.mouseOver    = mouseOver;
  this.mouseOut     = mouseOut;
  this.getDivName   = getDivName;
  this.clearTimeOut = clearTimeOut;
  this.startTimeOut = startTimeOut;
  this.findMenu4ID  = findMenu4ID;
  this.closeMenu    = closeMenu;
  this.isValidID    = isValidID;
  
  // create the object
  this.obj = 'menuObj' + menuCount++;
  eval(this.obj + ' = this;');
}

menuCount = 0;

function createHaeberleMenu() {
  // create menu for the haeberle-website
  haeberleMenu = new menu('haeberle');
  haeberleMenu.newMenu('u', 'u', 'uLayer');
  haeberleMenu.newEntry('u', 'zuf', 'gesch', 'qm', 'ref', 'pres');
  haeberleMenu.newMenu('d', 'd', 'dLayer');
  haeberleMenu.newEntry('d', 'schweiss', 'schneid', 'bau');
  haeberleMenu.newMenu('p', 'p', 'pLayer');
  haeberleMenu.newEntry('p', 'cnc', 'hand', 'vorricht', 'einprod');
  haeberleMenu.activate();
}

function getPos(arr, el) {
  for (var i in arr) {
    if (arr[i] == el) return(i);
  }
  return(-1);
}

function imgsOn(obj, b) {
  if (!b) {
    activePic = obj;
  }
  if (typeof(obj) == 'object') {
    for (var i in obj) {
      imgsOn(obj[i], 1);
    }
  }
  if (typeof(obj) == 'string') {
    imgOn(obj, 1);
  }
}

function showProps(obj) {
  //alert("starting to analyze...");
  if (!obj) {
    alert("Kein Objekt!");
    return(false);
  }
  if (typeof obj != "object") {
    alert(obj);
    return(false);
  }
  var count = 0;
  var Text = "";
  for (var i in obj) {
    Text += i  + " = " + obj[i] + "\n";
    count++; 
    if (count == 15) {
      ok = confirm(Text);
      if (!ok) return(false);
      count = 0;
      Text = "";
    }
  }
  //alert(Text);
  return(true);
}
