// ****************************************************************************
// INDEX PAGE TOP MENU JAVASCRIPT
// This JavaScript only applies to the "Master Menu" on the index page of the 
// website. The only part you will have to modify to change menu links
// is found above the "End of Editable Code" message below.
//
// To Add/Remove a top level menu item, edit the first line "var_master". The 
// text in all-capitals are IDs for the Master Menu link list in the index page html.
// So you will have to make sure that the IDs here and the IDs on the html page match.
// There are currently 7 main menu items, numbered 0-6. If you want to have more or 
// less you will have to make sure that you keep the sequential order, and make sure
// that var _menu (found on the last line of the editable area) reflects the total
// link count.
//
// To Add/Remove secondary level menu items, use var_menu1 or var_menu2 as an
// example of proper form. Note that the NSubMenu array items are separated by comas,
// and the last NSubMenu in the Array has two closing brakets and a semi-colon ));
//
// Notice that the presented text of the link and the url of the link are 
// within each Array item.
// ****************************************************************************

var _master = new Array('HOME', 'INFORMATION', 'ADVICE', 'LINKS', 'ACTION', 'ABOUT', 'CONTACT');

// Home Menu
var _menu0 = new Array();

// Information Menu
var _menu1 = new Array(new NSubMenu('Latest Newsletter', 'documents/Newsletter-Spring-2010.pdf'), 
                       new NSubMenu('Newsletter Archive', 'newsletters.htm'),
					   new NSubMenu('Datalink', 'datalink.htm'),
                       new NSubMenu('Welcome', 'WelcomeToNW.htm'));
// Advice Menu
var _menu2 = new Array(new NSubMenu('Distraction Burglaries',  'ColdCalling.htm'),
                       new NSubMenu('House Alarms',      'HouseAlarms.htm'),
                       new NSubMenu('Summer Break', 'SummerBreak.htm'),
					   new NSubMenu('Safer Homes', 'saferhomes.htm'));

// Links Menu
var _menu3 = new Array(new NSubMenu('', ''));

// Action Menu
var _menu4 = new Array(new NSubMenu('', ''));

// Sponsors Menu
var _menu5 = new Array(new NSubMenu('', ''));

// Contact Menu
var _menu6 = new Array(new NSubMenu('', ''));

// Master
var _menu = new Array(_menu0, _menu1, _menu2, _menu3, _menu4, _menu5, _menu6);

// ****************************************************************************
// End of Editable Code
// ****************************************************************************

// ----------------------------------------------------------------------------
// NSubMenu
// ----------------------------------------------------------------------------

function NSubMenu(text, url)
{
  this.text = text;
  this.url  = url;
}

// ----------------------------------------------------------------------------
// Window Handlers
// ----------------------------------------------------------------------------

document.onmousemove = function(event)
{
  var mouse = getMousePosition(event);
  
  var main_menu = document.getElementById('navigation');
  var content = document.getElementById('content');
  
  var pos = 0;
  var clear_menu = false;

  // Test Top
  pos = getPosition(main_menu);
  if (mouse.y < pos.top) {
    clear_menu = true;
  }  
  
  // Test Left
  if (mouse.x < pos.left) {
    clear_menu = true;
  }
  
  // Test Right
  if (mouse.x > pos.left + main_menu.offsetWidth) {
    clear_menu = true;
  }
  
  // Test Bottom
  pos = getPosition(content);
  if (mouse.y > pos.top - 2) {
    clear_menu = true;
  }

  if (clear_menu) {  
    resetMenus();
  } 
}

// ----------------------------------------------------------------------------
// Functions
// ----------------------------------------------------------------------------

function getMousePosition(event)
{
  var e = event || window.event;
  var mouse = new Object();
  if (e.pageX || e.pageY) {
    // this doesn't work on IE6!! (works on FF,Moz,Opera7)
    mouse.x = e.pageX;
    mouse.y = e.pageY;
  } else if (e.clientX || e.clientY) {
    // works on IE6,FF,Moz,Opera7
    mouse.x = e.clientX + document.body.scrollLeft;
    mouse.y = e.clientY + document.body.scrollTop;
  }
  return mouse;
}

function getPosition(obj)
{
  var curleft = curtop = 0;
  
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  
  var result = new Object();
  result.left = curleft;
  result.top  = curtop;
  
  return result;
}

function onMouseOver(index)
{
  resetMenus();
  
  // Add SubMenu Items
  var ul = document.getElementById('sub_navigation');
  for (var i = 0; i < _menu[index].length; i++) {
    var li = document.createElement('li');
    if (i == _menu[index].length - 1) {
      li.className = 'last';
    }
    
    var a = document.createElement('a');
    a.href = _menu[index][i].url;
    a.innerHTML = _menu[index][i].text;
    li.appendChild(a);
    ul.appendChild(li);
  }
}

function onMouseOut(event)
{
  var e = event || window.event;
  var target = e.target || e.srcElement;
  var mouse = getMousePosition(event);
  var pos = getPosition(target);
    
  var ul = document.getElementById('sub_navigation');
  
  if (mouse.y >= pos.top) {
    if (target.id == 'HOME') {
      // Test Left
      if (mouse.x >= pos.left) {
        //target.className = 'Selected';
      } else {
        removeElements(ul);
      }
    } else if (target.id == 'CONTACT') {
      // Test Right
      if (mouse.x <= pos.left + target.offsetWidth) {
        target.className = 'Selected';
      } else {
        removeElements(ul);
      }
    } else {
      target.className = 'Selected';
    }
  }
}

function removeElements(element)
{
  var o = element.firstChild;
  while (o) {
    var next = o.nextSibling;
    element.removeChild(o);
    o = next;
  }
}

function resetMenus()
{
  // Update class of Main Menu
  var main_menu = document.getElementById('navigation');
  var o = main_menu.firstChild;
  while (o) {
    var next = o.nextSibling;
    if (o.nodeName == 'LI') {
      o.firstChild.className = 'Normal';
    }
    o = next;
  }
  
  // Remove SubMenu Items
  var ul = document.getElementById('sub_navigation');
  removeElements(ul);
}