//
// Copyright(c) 2005 Gammakurve Corporation. All rights reserved.
//
// Permission granted to use this file as part of the 
// Seagull PHP application framework provided this copyright notice
// remains unchanged.
//

var LastSubMenu = null;
var lasty = 0;
var LastHLMenu = null;
var MenuFlyBy = false;
var menuList = new Object();
var graphicMenu = false;

// API: add to html
// Setup the event stuff for a menu
function InitMenu(menuID, flyBy)
{
  if (typeof flyBy == 'undefined') {
    flyBy = true;
  }

  MenuFlyBy = flyBy;

  var container = '#'+menuID;
  var body = 'body';
  var anchors = '#'+menuID+' a';
  var lists = '#'+menuID+' li';

  menuList[menuID] = true;

  var myrules = new Object();
  myrules[body] = function (element) { 
    element.onclick = GKML_HideMenuHandler; 
  };

  myrules[container] = function (element) { 
    element.onclick = GKML_HideMenuHandler; 
  };

  myrules[anchors] = function(element) { 
    element.onclick = GKML_DispatchEvent;
    element.onmouseover = GKML_DispatchEvent;
    element.onmouseout = GKML_DispatchEvent;
  };

  myrules[lists] = function(element) {
      element.onclick = GKML_DispatchEvent;
      element.onmouseover = GKML_DispatchEvent;
      element.onmouseout = GKML_DispatchEvent;
  };

  Behaviour.register(myrules);
  Behaviour.apply();

  GKML_SetupMenu(menuID);
}

function GKML_MenuExpander(elt, e, flyBy)
{
  if (!flyBy) {
    return false;
  }

  var nav = GKML_getNavNode(elt);
  if (nav == null) {
    return false;
  }

  var id = nav.id;

  if (typeof menuList[id] != 'undefined' ) {
    var nodes = elt.parentNode.childNodes;

    if (elt.parentNode.parentNode.parentNode == nav) {
      var ott = getXY(elt.parentNode.parentNode);
      var ol = elt.parentNode.offsetLeft;
      var o = getXY(elt.offsetParent);
      var ot = getXY(nav);

      if (o == null || ot == null) { return false; }

      // Hack for detecting IE
      if ((o.x-ot.x) == 0 && ol == 0) {
        var xo = 1;
      } else {
        var xo = o.x - ot.x;
      }

      var yo = (nav.offsetHeight - 1);
      var topLevel = true;

      GKML_HideMenu(0);
      lasty = 0;
    } else {
      var o = getXY(elt.offsetParent);
      var ot = getXY(elt);
      var xo = elt.parentNode.offsetWidth;

      if (o == null || ot == null) { return false; }

      // Hack for detecting IE
      if ((ot.y-o.y) == 0 && lasty != 0) {
        yo = 1;
      } else {
        var yo = ot.y - o.y;
      }

      lasty = yo;
      var topLevel = false;
    }

    for (var i = 0; i < nodes.length; i++) {
      if (nodes[i].tagName == 'UL') {
        if (nodes[i].style.display != 'block') {
          GKML_HideMenu(0);
          LastSubMenu = nodes[i];
          GKML_ShowMenu(0);
          if (xo > 0) {
            nodes[i].style.left = (xo) + 'px';
          }

          if (yo > 0) {
            nodes[i].style.top = (yo) + 'px';
          }

          if (!topLevel) {
            if (xo < nodes[i].offsetWidth) {
              nodes[i].style.left = (nodes[i].offsetWidth-2) + 'px';
            }
          }
        } else {
          GKML_HideMenu(0);
          LastSubMenu = nodes[i];
          GKML_ShowMenu(0);
        }

        e.cancelBubble = true;
        if (e.stopPropagation) {
          e.stopPropagation();
        }

        return false;
      }
    }
  }

  return true;
}

function GKML_SetupMenu(id)
{
  var elt = document.getElementById(id);
  if (elt == null) {
    return false;
  }

  var p = null;
  for (var k = 0; (p = elt.getElementsByTagName("A")[k]); k++) {
    var elt2 = p;
    while (elt2) {
      if (elt2.tagName == 'UL') {
        var pp = p.parentNode;
        if (pp.parentNode.parentNode.id == id) {
           pp.innerHTML = pp.innerHTML + '<IMG src="'+SGL_JS_WEBROOT+'/themes/'+SGL_JS_THEME+'/images/menu/menu_arrow_yellow2.gif" />';
        } else {
          pp.style.backgroundImage = 'url("'+SGL_JS_WEBROOT+'/themes/'+SGL_JS_THEME+'/images/menu/menu_arrow_yellow1.gif")';
          pp.style.backgroundPosition = '98% 50%';
          pp.style.backgroundRepeat = 'no-repeat';
        }
      }

      elt2 = elt2.nextSibling;
    }
  }

  return true;
}

function GKML_HideMenuHandler(e)
{
  if (typeof e == 'undefined') e = window.event;

  if (e.srcElement) {
    var target = e.srcElement;
  } else if (e.target) {
    var target = e.target;
  }

//   if (target.tagName != 'BODY') {
//     if (typeof menuList[target.id] == 'undefined' ) {
//       return true;
//     }
//   }

  lasty = 0;
  if (LastHLMenu != null) { 
    if (graphicMenu) {
      LastHLMenu.parentNode.style.backgroundPosition = '0px 2px';
      LastHLMenu.parentNode.style.backgroundImage = 'url("'+SGL_JS_WEBROOT+'/themes/'+SGL_JS_THEME+'/images/menu/tabc.gif")';
    }
    LastHLMenu = null;
  }

  return GKML_HideMenu(0);
}

function GKML_HideMenu(levels)
{
  var lvl = levels;

  cssAction = 'none';

  var thisO = LastSubMenu;

  while (thisO) {
    var id = thisO.parentNode.id;
    if (typeof menuList[id] != 'undefined' ) {
      break;
    }

    if (thisO.tagName == 'UL') {
      if (levels > 0) {
        lvl--;
        if (lvl >= 0) {
          thisO.style.display = cssAction;
        } else {
          LastSubMenu = thisO;
          return true;
        }
      } else {
        thisO.style.display = cssAction;
      }
    }

    thisO = thisO.parentNode;
  }

  LastSubMenu = null;
  return true;
}

function GKML_ShowMenu(levels)
{
  var lvl = levels;

  cssAction = 'block';

  var thisO = LastSubMenu;

  while (thisO) {
    var id = thisO.parentNode.id;
    if (typeof menuList[id] != 'undefined' ) {
      break;
    }

    if (thisO.tagName == 'UL') {
      if (levels > 0) {
        lvl--;
        if (lvl >= 0) {
          thisO.style.display = cssAction;
        } else {
          LastSubMenu = thisO;
          return true;
        }
      } else {
        thisO.style.display = cssAction;
      }
    }

    thisO = thisO.parentNode;
  }

  return true;
}

function GKML_MenuHighlight(target, e)
{
  var nav = GKML_getNavNode(target);
  if (nav == null) {
    return false;
  }

  var id = nav.id;

  if (typeof menuList[id] != 'undefined' ) {
    if (e.type == 'mouseout') {
      if (LastHLMenu != null) {
        if (target.parentNode == this) {
	  if (graphicMenu) {
	    LastHLMenu.parentNode.style.backgroundPosition = '0px 2px';
	    LastHLMenu.parentNode.style.backgroundImage = 'url("'+SGL_JS_WEBROOT+'/themes/'+SGL_JS_THEME+'/images/menu/tabc.gif")';
	  }
          LastHLMenu = null;
        }
      }
    }

    if (target.parentNode.parentNode.parentNode == nav) {
      if (e.type == 'mouseover') {
        if (LastHLMenu != null) { 
	  if (graphicMenu) {
	    LastHLMenu.parentNode.style.backgroundPosition = '0px 2px';
	    LastHLMenu.parentNode.style.backgroundImage = 'url("'+SGL_JS_WEBROOT+'/themes/'+SGL_JS_THEME+'/images/menu/tabc.gif")';
	  }
        }
        LastHLMenu = target;
	if (graphicMenu) {
	  LastHLMenu.parentNode.style.backgroundImage = 'url("'+SGL_JS_WEBROOT+'/themes/'+SGL_JS_THEME+'/images/menu/tabb.gif")';
	  LastHLMenu.parentNode.style.backgroundPosition = '0px 0px';
	}
        return GKML_MenuExpander(target, e, MenuFlyBy);
      }
    } else {
      if (e.type == 'mouseover') {
        var res = GKML_MenuExpander(target, e, MenuFlyBy);
        if (res) {
          GKML_HideMenu(0);
          LastSubMenu = target.parentNode;
          GKML_ShowMenu(0);
        }
      }
    }
  }

  return false;
}

function GKML_DispatchEvent(e)
{
  if (typeof e == 'undefined') e = window.event;
  e.cancelBubble = true;
  if (e.stopPropagation) {
    e.stopPropagation();
  }

  if (e.srcElement) {
    var target = e.srcElement;
  } else if (e.target) {
    var target = e.target;
  }

  if (target.tagName == 'A') {
    var p = target;
    var direct = true;
  } else {
    var direct = false;
    var p = this.getElementsByTagName("A")[0];
    if (p == null || typeof p == 'undefined' || p.tagName != 'A') {
      return false;
    }
  }

  switch (e.type) {
  case 'click':
    var res = GKML_MenuExpander(p, e, true);
    if (res && target == p) {
      GKML_HideMenu(0);
      return true;
    }
    return res;
  case 'mouseover':
    if (target == p && LastSubMenu != null) {
      return false;
    }
    return GKML_MenuHighlight(p, e);
  case 'mouseout':
  default:
    return false;
  }
}

// Get the navigation bar node
function GKML_getNavNode(o)
{
  var thisO = o;
  while (thisO) {
    var id = thisO.id;
    if (typeof menuList[id] != 'undefined' ) {
      return thisO;
    }

    thisO = thisO.parentNode;
  }

  return null;
}
