// Stuff to do when the page loads
window.addEvent('domready', function () {
  // set up side navigation menus
  $$('.menucontent').addClass('hide');
  $$('.submenucontent').addClass('hide');
  $$('.menu').addEvent('click', function() { toggleMenu(this.id); return false; });
  $$('.submenu').addEvent('click', function() { toggleSubMenu(this.id); return false; });

  // open menus (if neccessary) and highlight current page
  // parse file name to get id's - therefore, filenames must match id attributes
  var page = document.location.href;
  var filename = page.slice(page.lastIndexOf("/")+1, page.lastIndexOf("."));
  var parts = filename.split('-');

  setCurrent(parts[0]);  // main navigation tab
  
  // check for menus in side subnavigation
  if ( parts[1] ) {
    // more than one part, so in a subnav
    if ( parts[2] ) {
      // more than 2 parts, so a menu is open
      toggleMenu(parts[0]+parts[1]);
      if ( parts[3] ) {
        // more than 3 parts, so on a submenu item
	toggleSubMenu(parts[0]+parts[1]+parts[2]);
	setCurrent(parts[0]+parts[1]+parts[2]+parts[3]);
      } else {
        // only 3 parts, so on a single-level menu item
	setCurrent(parts[0]+parts[1]+parts[2]);
      }
    } else {
      // only 2 parts, so on a single page on the subnav; no menu open
      setCurrent(parts[0]+parts[1]);
    }
  } else {
    // only one part = main splash page for a tab
    if ( $('subnavigation') ) {
      // if there is a subnav, highlight the current page
      setCurrent(parts[0]+'index');
    }
  }

  // set up collapsing sections if applicable
  if ( $$('.sectionHeader').length > 0 ) {
    makeCollapsed('sectionHeader', 'sectionContent');
  }

  // set up contact slider if applicable
  if ( $('contactback') ) {
    $('contactback').slide('hide');
    
    $('contactlink').addEvent('mouseover', function () { $('contactback').slide(); return false; });
    $('contactlink').addEvent('click', function () { $('contactback').slide(); return false; });
  }  // end contactback

  // fix page dimensions
  setContentDims();
});

// fix page dimensions on resize also
window.addEvent('resize', function() { setContentDims(); });

// add a trim function to the String class
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

// create an accordion effect from page headings and content
function makeAccordion(linkClass, contentClass) {
  var myAccordion = new Accordion($$('.'+linkClass), $$('.'+contentClass), { 
    alwaysHide: 'true',
    display: -1,
    show: -1,
    onActive: function(toggler, element) { toggler.addClass('current'); },
    onBackground: function(toggler, element) { toggler.removeClass('current'); }
  });

  setContentDims();  // fix page dimensions for IE6
}

// similar to accordion, but allows multiple sections open at once
function makeCollapsed(linkClass, contentClass) {
  var headers = $$('.'+linkClass);
  var content = $$('.'+contentClass);

  // hide all content for initial view
  content.addClass('hide');
  
  // add click behavior to headers
  headers.each( function(header, index) {
    header.getParent().addClass('menuclosed');
    
    header.getParent().addEvent('click', function() {
      content[index].toggleClass('hide');
      header.getParent().toggleClass('menuclosed');
      header.getParent().toggleClass('menuopen');
      return false;
    });
  });

  // prevent clicks in the content area from closing the section
  // only the header should trigger closing
  // this is for structures like <li class=link>Click here<p class="content">Stuff to show</p></li>
  content.addEvent('click', function(event) {
    event.stopPropagation();
  });
    
  // add functionality to Show All and Hide All links
  if ($('showhide')) {
    $('show').addEvent('click', function() {
      content.removeClass('hide');
      headers.getParent().removeClass('menuclosed');
      headers.getParent().addClass('menuopen');
      return false;
    });
    
    $('hide').addEvent('click', function() {
      content.addClass('hide');
      headers.getParent().removeClass('menuopen');
      headers.getParent().addClass('menuclosed');
      return false;
    });
    
    // showhide section is hidden unless Javascript is enabled
    // without JS, nothing is hidden anyway
    $('showhide').removeClass('hide');
  }
}

// easy aliases to set current and hidden status
function setCurrent(itemId) {
  $(itemId).addClass('current');
}

function removeCurrent(itemId) {
  $(itemId).removeClass('current');
}

function showItem(theItem) {
  $(theItem).removeClass('hide');
}

function hideItem(theItem) {
  $(theItem).addClass('hide');
}

/*
// close all items where the id matches the given regex
function closeAllItems(typeRegEx) {
  var allObjs = $$('div');
  
  for ( var i = 0; i < allObjs.length; i++ ) {
    if ( typeRegEx.test(allObjs[i].id) ) {
      $(allObjs[i]).addClass('hide');

      var menuItem = $(allObjs[i].id.replace(typeRegEx, ""));
      if (menuItem) {
        $(menuItem).removeClass('current');
      }
    }
  }
}

// show all items where the id matches the given regex
function openAllItems(typeRegEx) {
  var allObjs = $$('div');
  
  for ( var i = 0; i < allObjs.length; i++ ) {
    if ( typeRegEx.test(allObjs[i].id) ) {
      showItem(allObjs[i]);
      
      var menuItem = $(allObjs[i].id.replace(typeRegEx, ""));
      if (menuItem) {
        setCurrent(menuItem.id);
      }
    }
  }
}

// toggle showing or hiding an item
// itemId - the id of the menu link of the item to show/hide
// itemType - the suffix added to the menu id to give the id of the item to show/hide
// closeAll - boolean: true = close all other items of the same type; false = ignore other items' states
function toggleItem(itemId, itemType, closeAll) {
  var menuItem = $(itemId);
  var targetItem = $(itemId + itemType);
  
  if (!targetItem)
    return false;
    
  if (targetItem.className.match("hide")) {
    if (closeAll) {
      closeAllItems(new RegExp(itemType+'$'));
    }
    showItem(targetItem);
    if (menuItem) {
      setCurrent(menuItem.id);
    }
  } else {
    hideItem(targetItem);
    removeCurrent(menuItem.id);
  }
}
*/

// open or close a menu section; if opening, close all other menu sections
// (accordion effect - is it possible to just use accordions for the menus?)
function toggleMenu(itemId) {
  var menuItem = $(itemId);
  var targetItem = $('menu' + itemId);

  if (!menuItem || !targetItem)
    return false;
    
  if (targetItem.className.match("hide")) {
    // menu was closed - close all other menus, and open this one
    $$('.menucontent').addClass('hide');
    $$('.menu').removeClass('current');
    showItem(targetItem);
    setCurrent(menuItem.id);
  } else {
    hideItem(targetItem)
    removeCurrent(menuItem.id);
  }
}

// open or close a submenu section; if opening, close all other submenu sections
// (accordion effect - is it possible to just use accordions for the menus?)
function toggleSubMenu(itemId) {
  var menuItem = $(itemId);
  var targetItem = $('submenu' + itemId);

  if (!menuItem || !targetItem)
    return false;
    
  if (targetItem.className.match("hide")) {
    // submenu was closed - close all other submenus, and open this one
    $$('.submenucontent').addClass('hide');
    $$('.submenu').removeClass('current');
    showItem(targetItem);
    setCurrent(menuItem.id);
  } else {
    hideItem(targetItem)
    removeCurrent(menuItem.id);
  }
}

/* Dynamic page sizing functions */
function getClientWidth() {
  var myWidth = 0;
  if ( typeof( window.innerWidth ) == 'number' ) {
    // Non-IE
    myWidth = window.innerWidth;
  } else if ( document.documentElement && document.documentElement.clientWidth ) {
    // IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else {
    myWidth = document.body.clientWidth;
  }
  
  return myWidth;
}

function getClientHeight() {
  var myHeight = 0;
  if ( typeof( window.innerHeight ) == 'number' ) {
    // Non-IE
    myHeight = window.innerHeight;
  } else if ( document.documentElement && document.documentElement.clientHeight ) {
    // IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else {
    myHeight = document.body.clientHeight;
  }
  
  return myHeight;
}

function setContentWidth() {
  if (!document.getElementById('container'))
    return false;
  var myWidth = getClientWidth();
  document.getElementById('container').style.width = myWidth < 950 ? '950px' : myWidth > 1000 ? '1000px' : (myWidth + 'px');
}

function setContentHeight() {
  if (!document.getElementById('contentcontainer'))
    return false;
  var myHeight = document.getElementById('contentcontainer').clientHeight;
  document.getElementById('contentleft').style.height = myHeight;
  document.getElementById('contentright').style.height = myHeight;
}

function setContentDims() {
  setContentHeight();
  setContentWidth();
}
