// original Javascript by Angus Turnbull Apr 2001
// modified by Andrew Schultz -- May 2002

var isDOM=false;
var isIE4=false;
var isNS4=false;
if (document.getElementById) {
   isDOM=true;
   px='px';
} else if (document.all) {
   isIE4 = true;
   px='px';
} else if (document.layers) {
   isNS4 = true;
   px='';
}

var currLMenu;

function go(href, menuDiv)
{
   if (menuDiv)
   {
      if (currLMenu) currLMenu.visibility='hidden';
      currLMenu = getSty(menuDiv);
      currLMenu.visibility=isNS4?'show':'visible';
   }
    else if (href && (href != "#"))
      document.location=href;
}

function getRef(id) {
   if (isDOM) return document.getElementById(id);
   if (isIE4) return document.all[id];
   if (isNS4) return document.layers[id];
   return null;
}

function getSty(id) {
   return (isNS4 ? getRef(id) : getRef(id).style);
} 

var popTimer = 0;
var litNow = new Array();
function popOver(menuNum, itemNum) {
   clearTimeout(popTimer);
   hideAllBut(menuNum);
   litNow = getTree(menuNum, itemNum);
   changeCol(litNow, true);
   var targetNum = menu[menuNum][itemNum].target;
   if (targetNum > 0) {
      var thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
      var thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);
      menu[targetNum][0].ref.left = parseInt(thisX + menu[targetNum][0].x)+px;
      menu[targetNum][0].ref.top = parseInt(thisY + menu[targetNum][0].y)+px;
      menu[targetNum][0].ref.visibility = 'visible';
   }
}
function popOut(menuNum, itemNum) {
   if ((menuNum == 0) && !menu[menuNum][itemNum].target)
      hideAllBut(0)
   else
      popTimer = setTimeout('hideAllBut(0)', 500);
}
function getTree(menuNum, itemNum) {

   var itemArray = new Array(menu.length);
   for (var i=0; i < itemArray.length; i++) {
      itemArray[i] = 0;
   }

   while(1) {
      itemArray[menuNum] = itemNum;
      if (menuNum == 0) return itemArray;
      itemNum = menu[menuNum][0].parentItem;
      menuNum = menu[menuNum][0].parentMenu;
   }
   return itemArray;
}

function changeCol(changeArray, isOver) {
   var newCol;
   for (var menuCount = 0; menuCount < changeArray.length; menuCount++) {
      if (changeArray[menuCount]) {
         newCol = isOver ? menu[menuCount][0].overCol : menu[menuCount][0].backCol;
         if (isNS4) menu[menuCount][changeArray[menuCount]].ref.bgColor = newCol;
         else menu[menuCount][changeArray[menuCount]].ref.backgroundColor = newCol;
      }
   }
}

function hideAllBut(menuNum) {
   var keepMenus = getTree(menuNum, 1);
   for (var count = 0; count < menu.length; count++)
      if (!keepMenus[count])
         menu[count][0].ref.visibility = 'hidden';
   changeCol(litNow, false);
}

// *** MENU CONSTRUCTION FUNCTIONS ***

function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass) {
   this.isVert = isVert;
   this.popInd = popInd
   this.x = x;
   this.y = y;
   this.width = width;
   this.overCol = overCol;
   this.backCol = backCol;
   this.borderClass = borderClass;
   this.textClass = textClass;
   this.parentMenu = null;
   this.parentItem = null;
   this.ref = null;
}

function Item(text, href, frame, length, spacing, target, menuDiv) {
   this.text = text;
   this.href = href;
   this.frame = frame;
   this.length = length;
   this.spacing = spacing;
   this.target = target;
   this.menuDiv = menuDiv;
   this.ref = null;
}

var winWidth;

function writeMenus() {
   if (!isDOM && !isIE4 && !isNS4) return;
   var currMenu, currItem;
   var  itemName;
   for (currMenu = 0; currMenu < menu.length; currMenu++) {
      var thisMenu=menu[currMenu][0];
      var str = '', itemX = 0, itemY = 0;
      if (currMenu==0) {
         if (window.innerWidth) winWidth=window.innerWidth;
          else winWidth=document.body.clientWidth;
         if (winWidth>900) itemX=(winWidth-900)/2+150;
          else if (winWidth<750) itemX=0;
          else itemX = winWidth-750;
      }

      for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
         var itemID = 'menu' + currMenu + 'item' + currItem;
         var thisItem = menu[currMenu][currItem];

         var w = (thisMenu.isVert ? thisMenu.width : thisItem.length);
         var h = (thisMenu.isVert ? thisItem.length : thisMenu.width);

         if (isDOM || isIE4) {
            str += '<div id="' + itemID + '" style="z-index: 5; position: absolute; left: ' + itemX + 'px; top: ' + itemY + 'px; width: ' + w + 'px; height: ' + h + 'px; visibility: shown; ';
            if (thisMenu.backCol) str += 'background: ' + thisMenu.backCol + '; ';
            str += '" ';
         }
         if (isNS4) {
            str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' +  w + '" height="' + h + '" visibility="inherit" ';
            if (thisMenu.backCol) str += 'bgcolor="' + thisMenu.backCol + '" ';
         }
         if (thisMenu.borderClass) str += 'class="' + thisMenu.borderClass + '" ';

         str += 'onMouseOver="popOver(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')">\n';


         str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="' + (!isNS4 && thisMenu.borderClass ? 3 : 0) + '">\n<tr><td valign=top align="left" height="' + (h - 7) + '">' + '<a class="' + thisMenu.textClass + '" href="JavaScript:go('+"'" + thisItem.href + "','"+thisItem.menuDiv+ "'"+');"' + (thisItem.frame ? ' target="' + thisItem.frame + '">' : '>') + thisItem.text + '</a></td>';
         if (thisItem.target > 0) {

            menu[thisItem.target][0].parentMenu = currMenu;
            menu[thisItem.target][0].parentItem = currItem;

            if (thisMenu.popInd) str += '<td class="' + thisMenu.textClass + '" align="right">' + thisMenu.popInd + '</td>';
         }
         str += '</tr></table>' + (isNS4 ? '</layer>\n' : '</div>\n');
         if (thisMenu.isVert) itemY += thisItem.length + thisItem.spacing;
         else itemX += thisItem.length + thisItem.spacing;
      }
      if (isDOM) {
         var newDiv = document.createElement('div');
         document.getElementsByTagName('body').item(0).appendChild(newDiv);
         newDiv.className="menuHead";
         newDiv.innerHTML = str;
         thisMenu.ref = newDiv.style;
         thisMenu.ref.position = 'absolute';
         thisMenu.ref.left="0px";
         thisMenu.ref.top="0px";
         thisMenu.ref.visibility = 'hidden';
      }

      if (isIE4) {
         document.body.insertAdjacentHTML('beforeEnd', '<div id="menu' + currMenu + 'div" ' + 'style="position: absolute; top: 0; left: 0; visibility: hidden">' + str + '</div>');
         thisMenu.ref = getSty('menu' + currMenu + 'div');
      }

      if (isNS4) {
         thisMenu.ref = new Layer(0);
         thisMenu.ref.document.write(str);
         thisMenu.ref.document.close();
      }

      for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
         itemName = 'menu' + currMenu + 'item' + currItem;
         if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
         if (isNS4) menu[currMenu][currItem].ref = thisMenu.ref.document[itemName];
      }
   }
   menu[0][0].ref.left = menu[0][0].x+px;
   menu[0][0].ref.top = menu[0][0].y+px;
   menu[0][0].ref.visibility = 'visible';
}

var menu = new Array();

var defOver = '#ff0000', defBack = '#b00000';

var defLength = 30;
var i=0, j=0;

menu[i] = new Array();
menu[i][j] = new Menu(false, '', 5, 0, 35, '#ff0000', '#b00000', '', 'itemText'); j++;
menu[i][j] = new Item('  Home', '/', '', 50, 15, j, 'homeDiv'); j++;
menu[i][j] = new Item('  News', '/news/', '', 55, 15, j, 'newsDiv'); j++;
menu[i][j] = new Item('  Dept Info', '/dept_info/', '', 40, 15, j, 'deptinfoDiv'); j++;
menu[i][j] = new Item('  Current Students', '/current_students/', '', 65, 15, j, 'currentDiv'); j++;
menu[i][j] = new Item('  Prospective Students', '/prospective_students/', '', 80, 15, j, 'prospectDiv'); j++;
menu[i][j] = new Item('  Faculty & Staff', '/faculty_staff/', '', 60, 15, j, 'facstaffDiv'); j++;
menu[i][j] = new Item('  Research', '/research/', '', 65, 15, j, 'researchDiv'); j++;
menu[i][j] = new Item('  Alumni', '/alumni/', '', 55, 15, j, 'alumniDiv'); j++;
menu[i][j] = new Item('  Industry', '/industry/', '', 60, 15, j, 'industryDiv'); j++;

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 160, defOver, defBack, 'itemBorder', 'itemText', '');
menu[i][j++] = new Item('Front Page', '/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Mission Statement', '/mission.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Educational Objectives', '/CBE_PEO.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Net Links', '/net_link.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('NC State','http://www.ncsu.edu','',defLength, 0, 0, '');
menu[i][j++] = new Item('NCSU Engineering','http://www.engr.ncsu.edu','',defLength, 0, 0, '');
menu[i][j++] = new Item('Annual Report', '/CBE_Annual_Report_2008-2009.pdf', '', defLength, 0, 0, ''); 

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 130, defOver, defBack, 'itemBorder', 'itemText', '');
menu[i][j++] = new Item('Seminars', '/news/seminar.php', '', defLength, 0, 0, '');
menu[i][j++] = new Item('COE News', 'http://www.engr.ncsu.edu/news/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('NCSU News', 'http://www.ncsu.edu/ncsu/univ_relations/release.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Pictures', '/photos/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('ChE Grad News', '/news/grad_news/', '', defLength, 0, 0, '');

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 125, defOver, defBack, 'itemBorder', 'itemText');
menu[i][j++] = new Item('Contact Info', '/dept_info/contact_information.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Courses', '/dept_info/courses.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Overview', '/dept_info/overview.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Intro to NCSU', '/dept_info/ncsu.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Publications', '/publications/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Campus Location', '/dept_info/campus_location.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Raleigh, NC', '/dept_info/raleigh.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Virtual Tour', '/dept_info/tour.ram', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Dept History', '/history/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('EBI Floor Plans', '/dept_info/EBIFloorPlans.pdf', '', defLength, 0, 0, '');

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 105, defOver, defBack, 'itemBorder', 'itemText', '');
menu[i][j++] = new Item('Course Pages', 'http://courses.ncsu.edu/che.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Graduate', '/current_students/graduate_students.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Post Docs', '/current_students/postdocs.php', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Societies', '/current_students/societies.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Undergrad', '/current_students/undergrad_students.html', '', defLength, 0, 0, '');

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 150, defOver, defBack, 'itemBorder', 'itemText');
menu[i][j++] = new Item('Graduate', '/prospective_students/graduate_students.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Undergrad', '/prospective_students/undergrad_students.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Distance Ed', 'http://engineeringonline.ncsu.edu/EOL_DEGREES/MSCHE.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Engineering Online', 'http://engineeringonline.ncsu.edu/', '', defLength, 0, 0, '');

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 60, defOver, defBack, 'itemBorder', 'itemText', '');
menu[i][j++] = new Item('Faculty', '/faculty_staff/faculty.php', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Staff', '/faculty_staff/staff.php', '', defLength, 0, 0, '');

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 70, defOver, defBack, 'itemBorder', 'itemText', '');
menu[i][j++] = new Item('Current', '/research/current_research.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Centers', '/research/centers.html', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Projects', '/publications/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('REU', '/reu/', '', defLength, 0, 0, '');

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 175, defOver, defBack, 'itemBorder', 'itemText', '');
menu[i][j++] = new Item('Newsletters', '/publications/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Brick Order Form', '/alumni/CBE_BrickOrderForm.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Brick Proofs', '/alumni/CBE_BrickProofs.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Distinguished Alumni', '/alumni/CBE_Achievements.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Endowed Fellowships', '/alumni/CBE_Fellowships.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Endowed Scholarships', '/alumni/CBE_Scholarships.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Frank Seely Society', '/alumni/CBE_FrankSeelySociety.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Giving Society Members', '/alumni/CBE_GivingSocietyMembers.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Giving Societies', '/alumni/CBE_LifetimeGivingSocieties.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Named Rooms', '/alumni/CBE_NamedRooms.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Engineering Foundation', 'http://www.engr.ncsu.edu/ncef/', '', defLength, 0, 0, '');

j=0; i++;
menu[i] = new Array();
menu[i][j++] = new Menu(true, '>', 0, 35, 170, defOver, defBack, 'itemBorder', 'itemText', '');
menu[i][j++] = new Item('Scholarships', '/industry/CorporateScholarships.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Sponsors', '/industry/CorporateSponsors.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('Professorships', '/industry/Professorships.pdf', '', defLength, 0, 0, '');
menu[i][j++] = new Item('NCSU Eng Foundation', 'http://www.engr.ncsu.edu/ncef/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('NCSU Co-op Ed', 'http://www2.ncsu.edu/ncsu/co-op_ed/', '', defLength, 0, 0, '');
menu[i][j++] = new Item('NCSU Career Center', 'http://www.ncsu.edu/career/', '', defLength, 0, 0, '');

// *** OPTIONAL CODE FROM HERE DOWN ***

// var popOldWidth = window.innerWidth;
//nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');
function ResizeHandler () {
   var newWidth;
   if (window.innerWidth) newWidth=window.innerWidth;
    else newWidth=document.body.clientWidth;
   if (winWidth != newWidth) {
      winWidth=newWidth;
      if (!isDOM && !isIE4) location.reload();
      hideAllBut(0);
      var thisMenu, thisItem;
      for (currMenu = 0; currMenu < menu.length; currMenu++) {
         thisMenu = menu[currMenu][0];
         var str = '', itemX = 0;
         if (currMenu==0) {
            if (winWidth>900) itemX=(winWidth-900)/2+150;
             else if (winWidth<750) itemX=0;
             else itemX = winWidth-750;
         }
         for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
            thisItem = menu[currMenu][currItem];
            var itemID = 'menu' + currMenu + 'item' + currItem;
            document.getElementById(itemID).style.left=itemX+px;
            if (thisMenu.isVert) itemY += thisItem.length + thisItem.spacing;
             else itemX += thisItem.length + thisItem.spacing;
         }
      }
   }
}


if (isNS4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;

function clickHandle(evt) {
 if (isNS4) document.routeEvent(evt);
 hideAllBut(0);
}
