/*
 * Starfleet Legacy Alliance - slasims.com
 * 2006 Website Redesign
 * Copyright © 2006 Ben Mitchell
 * 
 * Good day to you, wanderer of code
 * Welcome to this design's humble abode.
 * Explore a bit and stay a while
 * And compare your browser to this file.
 * Courtesy always comes off best
 * In a place of learning when a guest.
 * Look around and touch and feel
 * But please do not dare to steal.
 *
 */

/* shifts to a two- or three-column layout depending on window width */
function checkColumnLayout() {
  var bodyElements = document.getElementsByTagName("body");
  var bodyElement = null;
  if(bodyElements.length > 0) {
    bodyElement = bodyElements[0];
  }
  /* based on width and height detection code from QuirksMode.org */  
  var x;
  if (self.innerHeight) { // all except Explorer
    x = self.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    // Explorer 6 Strict Mode
    x = document.documentElement.clientWidth;
  }
  else if (document.body) { // other Explorers
    x = document.body.clientWidth;
  }
  
  if(bodyElement) {
    if(x < 1004) {
      bodyElement.className = "twocol";
    } else {
      bodyElement.className = "threecol";
    }
  }
  
}

/* menu class definition */
function MenuStructure(menuLink, menuList, menuSection) {
  this.link = menuLink;
  this.list = menuList;
  this.section = menuSection;
}

/* active menu holder -- which menu is expanded? */
var activeMenu = null;
var menusList = new Array();

/* sets up the folding menu system */
function setUpMenus() {
  var menuElement = document.getElementById("menu_menu");
  
  /* recurse through the #menu_menu tree to find child menus */
  var node = menuElement.firstChild;
  while(node) {
    if(node.tagName && node.tagName.toLowerCase() == "li") {
      
      /* if there is a child list, set the fold */
      var childNode = node.firstChild;
      var linkNode = null;
      var listNode = null;
      while(childNode) {

        if(childNode.tagName) {
          if(childNode.tagName.toLowerCase() == "ul") {
            listNode = childNode;
          } else if(childNode.tagName.toLowerCase() == "a") {
            linkNode = childNode;
          }
        }
        
        if(childNode.nextSibling) {
          childNode = childNode.nextSibling;
        } else {
          break;
        }
      }
      
      /* if both the menu link and submenu exist, set up the handlers */
      if(linkNode && listNode && node.id) {
        // linkNode.href = "";
        linkNode.onclick = foldUnfold;
        menusList[menusList.length] = new MenuStructure(linkNode, listNode, node.id);
      }
      
    }
      
    /* continue walking the tree */
    if(node.nextSibling) {
      node = node.nextSibling;
    } else {
      break;
    }
  }
  
  var currentSection = "home";
  var body = document.getElementsByTagName("body");
  var bodyNode = null;
  if(body.length > 0) {
    bodyNode = body[0];
  }
  if(bodyNode && bodyNode.id) {
    var bodyId = bodyNode.id;
    var splitBodyId = bodyId.split("_");
    currentSection = splitBodyId[splitBodyId.length - 1];
  }
  
  var activatedSection = false;
  var homeMenu = null;
  
  for(var menuIndex = 0; menuIndex < menusList.length; menuIndex++) {
    var currentMenu = menusList[menuIndex];
    
    if(currentMenu.section.toLowerCase() == currentSection) {
      activeMenu = currentMenu;
      activatedSection = true;
      currentMenu.list.className = "visible";
    } else if(currentMenu.section.toLowerCase() == "home") {
      homeMenu = currentMenu;
    } else {
      currentMenu.list.className = "hidden";
    }
  }
  
  if(activatedSection == false) {
    if(homeMenu) {
      activeMenu = homeMenu;
      homeMenu.list.className = "visible";
    }
  }
}

/* shows and hides the link's submenu */
function foldUnfold() {  
  if(activeMenu && activeMenu.link == this) {
    activeMenu.list.className = "hidden";
    activeMenu = null;
  } else {
    for(var menuIndex = 0; menuIndex < menusList.length; menuIndex++) {
      var currentMenu = menusList[menuIndex];
      if(currentMenu.link == this) {
        if(activeMenu) {
          activeMenu.list.className = "hidden";
        }
        currentMenu.list.className = "visible";
        activeMenu = currentMenu;
      }
    }
  }
  
  return false;
}

/* adds a classname of "dark" to alternating table rows for coloring effects */
function setUpTables() {
  var tables = document.getElementsByTagName("tbody");
  if(tables) {
    
    /* iterate over each table on the page */
    var tableIndex;
    for(tableIndex = 0; tableIndex < tables.length; tableIndex++) {
      
      /* iterate over the table's rows */
      var isDark = 1;
      var children = tables[tableIndex].childNodes;
      var childIndex;
      for(childIndex = 0; childIndex < children.length; childIndex++) {
        
        if(children[childIndex] && children[childIndex].tagName && children[childIndex].tagName.toLowerCase() == "tr") {
          if(isDark == 1) {
            isDark = 0;
            if(children[childIndex].className == "") {
              children[childIndex].className = "dark";
            } else {
              children[childIndex].className += " dark";
            }
          } else {
            isDark = 1;
          }
        }
        
      }
      
    }
    
  }
}

/* sets up the page once the window is loaded */
function runSetupScripts() {
  /* set up folding menus */
  setUpMenus();
  
  /* check the column layout when resizing has completed */
  window.onresize = checkColumnLayout;
  
  /* check the column layout now too */
  checkColumnLayout();
  
  /* alternate the colors on tables -- using only classes, of course */
  setUpTables();
}

/* DOM support check -- do nothing if these DOM methods are not supported */
if(document.createElement && document.getElementById) {
  /* use a different event listener for each modern browser */
  if(typeof window.addEventListener != "undefined") {
    // for Gecko, KHTML, and WebKit
    window.addEventListener("load", runSetupScripts, false);
  } else if(typeof document.addEventListener != "undefined") {
    // for Opera 7
    document.addEventListener("load", runSetupScripts, false);
  } else if(typeof window.attachEvent != "undefined") {
    // for IE on Windows
    window.attachEvent("onload", runSetupScripts);
  }
}
