var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

function getRef(id) {
  if (isDOM) return document.getElementById(id);
  if (isIE4) return document.all[id];
if (isNS4) return document.layers[id];
}


function loadPage(id,nestref,url) {
    // change the 550 value to make the layer wider or smaller
    content_width = 550;
    if (isNS4) {
	var lyr = (nestref)? eval('document.'+nestref+'.document.'+id) : document.layers[id]
	    lyr.load(url,content_width)
    }
    else if(isIE4) parent.contentFrame.location = url;
    else if(isDOM) document.getElementById('contentFrame').src = url;
}

function showPage(id) {
    if (isIE4) {
	document.all[id].innerHTML = parent.contentFrame.document.body.innerHTML;
    }
    else if(isDOM) {
	document.getElementById(id).innerHTML = window.frames['contentFrame'].document.getElementById('theBody').innerHTML;
    }

}




function getSty(id) {
  return (isNS4 ? getRef(id) : getRef(id).style);
}

var menuArray = new Array();
for (var i = 0; i<10; i++) menuArray[i]=null;

var numOfMenus = 0;

function registerMenu(menuToAdd) {
   menuArray[numOfMenus] = menuToAdd;
   litNow[numOfMenus] = new Array();
   numOfMenus++;
}

// Hide timeout.
var popTimer = 0;

// Array showing highlighted menu items.
var litNow = new Array();

function popOver(i, menuNum, itemNum) {
  hideAllOtherMenus(i);
  var menu = menuArray[i];
  //alert(menu[menuNum][itemNum].target);
  clearTimeout(popTimer);
  hideAllBut(i, menuNum);
  litNow[i] = getTree(i, menuNum, itemNum);
  changeCol(i, litNow[i], true);
  targetNum = menu[menuNum][itemNum].target;
  if (targetNum > 0) {
    thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
    thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);
    testH = thisY+parseInt(menu[targetNum][0].ref.height);
    if (parseInt(navigator.appVersion)>3) {
	if (navigator.appName=="Netscape") {
	    winH = window.innerHeight;
	}
	if (navigator.appName.indexOf("Microsoft")!=-1) {
	    winH = document.body.offsetHeight;
	}
    }
    if (testH>winH) {
	thisY = thisY - parseInt(menu[targetNum][0].ref.height)+parseInt(menu[menuNum][itemNum].ref.height);
    }
    with (menu[targetNum][0].ref) {
      left = parseInt(thisX + menu[targetNum][0].x);
      top = parseInt(thisY + menu[targetNum][0].y);
      visibility = 'visible';
    }
  }
}

function hideAllOtherMenus(i) {
  for (var x = 0; x<numOfMenus; x++) {
    if (x!=i) {
      var menu = menuArray[x];
      for (count = 1; count < menu.length; count++)
        menu[count][0].ref.visibility='hidden';
      changeCol(x,litNow[x],false);
    }
  }
}

function popOut(i, menuNum, itemNum) {
  var menu = menuArray[i];
  if ((menuNum == 0) && !menu[menuNum][itemNum].target)
      hideAllBut(i,0);
  else
      popTimer = setTimeout('hideAll()', 500);
}


function getTree(i, menuNum, itemNum) {
  // Array index is the menu number. The contents are null (if that menu is not a parent)
  // or the item number in that menu that is an ancestor (to light it up).
  var menu = menuArray[i];
  //alert(menu.length);
  itemArray = new Array(menu.length);
  while(1) {
      itemArray[menuNum] = itemNum;
      // If we've reached the top of the hierarchy, return.
      if (menuNum == 0) return itemArray;
      itemNum = menu[menuNum][0].parentItem;
      menuNum = menu[menuNum][0].parentMenu;
  }
}


// Pass an array and a boolean to specify colour change, true = over colour.
function changeCol(i, changeArray, isOver) {
    //  alert("ChangeCol\ni: "+i+"\nchangeArray:"+changeArray+"\nisOver:"+isOver);
  var menu = menuArray[i];
  for (menuCount = 0; menuCount < changeArray.length; menuCount++) {
    if (changeArray[menuCount]) {
      newCol = isOver ? menu[menuCount][0].overCol : menu[menuCount][0].backCol;
      // Change the colours of the div/layer background.
      with (menu[menuCount][changeArray[menuCount]].ref) {
      if (isNS4) bgColor = newCol;
        else backgroundColor = newCol;
      }
    }
  }
}


function hideAllBut(i, menuNum) {
  var menu = menuArray[i];
  var keepMenus = getTree(i, menuNum, 1);
  for (count = 0; count < menu.length; count++)
    if (!keepMenus[count])
      menu[count][0].ref.visibility = 'hidden';
  changeCol(i,litNow[i],false);
}


function hideAll() {
  for (var i = 0; i< numOfMenus; i++) {
    var menu = menuArray[i];
    var keepMenus = getTree(i, 0, 1);
    for (count = 0; count < menu.length; count++)
      if (!keepMenus[count])
        menu[count][0].ref.visibility = 'hidden';
      changeCol(i,litNow[i],false);
  }
}

// *** MENU CONSTRUCTION FUNCTIONS ***

function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass) {
  // True or false - a vertical menu?
  this.isVert = isVert;
  // The popout indicator used (if any) for this menu.
  this.popInd = popInd
  // Position and size settings.
  this.x = x;
  this.y = y;
  this.width = width;
  // Colours of menu and items.
  this.overCol = overCol;
  this.backCol = backCol;
  // The stylesheet class used for item borders and the text within items.
  this.borderClass = borderClass;
  this.textClass = textClass;
  // Parent menu and item numbers, indexed later.
  this.parentMenu = null;
  this.parentItem = null;
  // Reference to the object's style properties (set later).
  this.ref = null;
}

function Item(text, href, frame, length, spacing, target) {
  this.text = text;
  this.href = href;
  this.frame = frame;
  this.length = length;
  this.spacing = spacing;
  this.target = target;
  // Reference to the object's style properties (set later).
  this.ref = null;
}

function writeMenus(i)
{

  var menu = menuArray[i];
  if (!isDOM && !isIE4 && !isNS4) return;

  for (currMenu = 0; currMenu < menu.length; currMenu++) with (menu[currMenu][0]) {
    // Variable for holding HTML for items and positions of next item.
      var str = '', itemX = 0, itemY = 0, menuHeight=0;

    // Remember, items start from 1 in the array (0 is menu object itself, above).
    // Also use properties of each item nested in the other with() for construction.
    for (currItem = 1; currItem < menu[currMenu].length; currItem++) with (menu[currMenu][currItem]) {
      var itemID = i+'menu' + currMenu + 'item' + currItem;

      // The width and height of the menu item - dependent on orientation!
      var w = (isVert ? width : length);
      var h = (isVert ? length : width);
      if (isVert) menuHeight+=h;

      // do some checking to make sure the menu fits on the screen (BUGGY)
      //if ((itemY+h) > self.height) itemY = itemY-h;
      //itemX = self.width;


      // Create a div or layer text string with appropriate styles/properties.
      // Thanks to Paul Maden (www.paulmaden.com) for helping debug this in IE4, apparently
      // the width must be a miniumum of 3 for it to work in that browser.
      if (isDOM || isIE4)
	{
        str += '<div id="' + itemID + '" style="position: absolute; left: ' + itemX + '; top: ' + itemY + '; width: ' + w + '; height: ' + h + '; visibility: inherit; ';
        if (backCol) str += 'background: ' + backCol + '; ';
        str += '" ';
      }
      if (isNS4)
	{

        str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' +  w + '" height="' + h + '" visibility="inherit" ';
        if (backCol) str += 'bgcolor="' + backCol + '" ';
      }
      if (borderClass) str += 'class="' + borderClass + '" ';

      // Add mouseover handlers and finish div/layer.


      if (href=='')
      {
      str += 'onMouseOver="popOver(' +i+',' + currMenu + ',' + currItem + ')" onMouseOut="popOut('+i+',' + currMenu + ',' + currItem + ')">';
      }
      else
      {
      str += 'onClick="location.href=\'' + href + '\'" onMouseOver="popOver(' +i+',' + currMenu + ',' + currItem + ')" onMouseOut="popOut('+i+',' + currMenu + ',' + currItem + ')">';
      }

      // Add contents of item (default: table with link inside).
      // In IE/NS6+, add padding if there's a border to emulate NS4's layer padding.
      // If a target frame is specified, also add that to the <a> tag.
   
      str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="' + (!isNS4 && borderClass ? 3 : 0) + '"><tr><td align="left" height="' + (h - 7) + '">';
      if (href=='')
      {
      	str += '<font class="' + textClass + '">'+text+'</font>';
      }
      else
      {
      	 str+= '<a class="' + textClass + '" href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') + text + '</a>';

 //if(textClass == "itemText2")
 //{
 //	alert("str = " + str);
 //}
      }
      str+='</td>';

      if (target > 0) {

        // Set target's parents to this menu item.
        menu[target][0].parentMenu = currMenu;
        menu[target][0].parentItem = currItem;

        // Add a popout indicator.
        if (popInd) str += '<td class="' + textClass + '" align="right">' + popInd + '</td>';
      }
      str += '</tr></table>' + (isNS4 ? '</layer>' : '</div>');
      if (isVert){
	  itemY += length + spacing;
	  menuHeight+=spacing;
      }
      else itemX += length + spacing;

    }
    if (isDOM) {
      var newDiv = document.createElement('div');
      document.getElementsByTagName('body').item(0).appendChild(newDiv);
      newDiv.innerHTML = str;
      ref = newDiv.style;
      ref.position = 'absolute';
      ref.visibility = 'hidden';
      if (isVert) ref.height = menuHeight;
    }

    // Insert a div tag to the end of the BODY with menu HTML in place for IE4.
    if (isIE4) {
	strHTML = '<div id=i+"menu' + currMenu + 'div" ' + 'style="position: absolute; visibility: hidden;';
	if (isVert) strHTML += ' height: '+menuHeight+';';
	strHTML+='>' + str + '</div>';
	document.body.insertAdjacentHTML('beforeEnd', strHTML);
	ref = getSty(i+'menu' + currMenu + 'div');
    }

    // In NS4, create a reference to a new layer and write the items to it.
    if (isNS4) {
	//alert(str);
      ref = new Layer(0);
      ref.document.write(str);
      ref.document.close();
    }

   for (currItem = 1; currItem < menu[currMenu].length; currItem++) {
      itemName = i+'menu' + currMenu + 'item' + currItem;
      if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
      if (isNS4) {
	  menu[currMenu][currItem].ref = ref.document.layers[currItem-1];
	  //alert(getSty(itemName));
      }
    }

  }

  with(menu[0][0]) {
    ref.left = x;
    ref.top = y;
    ref.visibility = 'visible';
  }
}


var popOldWidth = window.innerWidth;
nsResizeHandler = new Function('if (popOldWidth != window.innerWidth) location.reload()');

// This is a quick snippet that captures all clicks on the document and hides the menus
// every time you click. Use if you want.

if (isNS4) document.captureEvents(Event.CLICK);
document.onclick = clickHandle;

function clickHandle(evt)
{
 if (isNS4) document.routeEvent(evt);
 hideAll();
}

function buildMenus() {
 for (var i = 0; i<numOfMenus; i++) {
      writeMenus(i);
      //alert("Writing menu "+i);
  }
  //writeMenus(menuArray[0]);
}


var menu1 = new Array();
menu1[0] =  new Array();
menu1[0][0] = new Menu(true,'<IMG SRC="images/greenarrow.gif">',0,82,150,'#ffffff','#dcdcdc','','itemText');
menu1[0][1] = new Item('<font color="#000000">Home</font>','home.html','',20,5,0);
menu1[0][2] = new Item('<font color="#000000">About Tri-Mont</font>','','',20,5,1);
menu1[0][3] = new Item('<font color="#000000">Clients</font>','','',20,5,3);
menu1[0][4] = new Item('<font color="#000000">Work Experience</font>','','',20,5,4);
menu1[0][5] = new Item('<font color="#000000">Construction Photos</font>','constructionphotos.html','',20,5,0);
menu1[0][6] = new Item('<font color="#000000">Employment</font>','employment.html','',20,5,0);
menu1[0][7] = new Item('<font color="#000000">Contact Us</font>','contact.html','',20,5,0);
menu1[0][8] = new Item('<font color="#000000">Directions</font>','','',20,5,5);

menu1[1] =  new Array();
menu1[1][0] = new Menu(true,'<IMG SRC="images/greenarrow.gif">',150,0,200,'#ffffff','#dcdcdc','itemBorder','itemText');
menu1[1][1] = new Item('<font color="#000000">Our Employees</font>','employees.html','',20,5,0);
menu1[1][2] = new Item('<font color="#000000">Specialized Tools</font>','','',20,5,2);

menu1[2] =  new Array();
menu1[2][0] = new Menu(true,'<IMG SRC="images/greenarrow.gif">',150,0,200,'#ffffff','#dcdcdc','itemBorder','itemText');
menu1[2][1] = new Item('<font color="#000000">Energy & Mass Balance</font>','massbalance.html','',20,4,0);
menu1[2][2] = new Item('<font color="#000000">Piping Network Analysis</font>','pipingnetworkanalysis.html','',20,4,0);
menu1[2][3] = new Item('<font color="#000000">Load Analysis</font>','loadanalysis.html','',20,4,0);
menu1[2][4] = new Item('<font color="#000000">3-Tier Cost Estimate</font>','costestimate.html','',20,4,0);
menu1[2][5] = new Item('<font color="#000000">Spreadsheets</font>','spreadsheets.html','',20,4,0);
menu1[2][6] = new Item('<font color="#000000">Pipe Stress Analysis</font>','pipestressanalysis.html','',20,4,0);
menu1[2][7] = new Item('<font color="#000000">Structural Steel Analysis</font>','structuralsteelanalysis.html','',20,4,0);
menu1[2][8] = new Item('<font color="#000000">Equipment Specifications</font>','equipmentspecs.html','',20,4,0);
menu1[2][9] = new Item('<font color="#000000">Construction Specifications</font>','constructionspecs.html','',20,4,0);
menu1[2][10] = new Item('<font color="#000000">Technical Library</font>','techlibrary.html','',20,4,0);

menu1[3] =  new Array();
menu1[3][0] = new Menu(true,'<IMG SRC="images/greenarrow.gif">',150,0,200,'#ffffff','#dcdcdc','itemBorder','itemText');
menu1[3][1] = new Item('<font color="#000000">Energy Developers</font>','energydevelopers.html','',20,4,0);
menu1[3][2] = new Item('<font color="#000000">Utilities</font>','utilities.html','',20,4,0);
menu1[3][3] = new Item('<font color="#000000">Bulk Fuel</font>','bulkfuel.html','',20,4,0);
menu1[3][4] = new Item('<font color="#000000">Industrial</font>','industrial.html','',20,4,0);
menu1[3][5] = new Item('<font color="#000000">Government</font>','government.html','',20,4,0);
menu1[3][6] = new Item('<font color="#000000">Institutions</font>','institutions.html','',20,4,0);

menu1[4] =  new Array();
menu1[4][0] = new Menu(true,'<IMG SRC="images/greenarrow.gif">',150,0,200,'#ffffff','#dcdcdc','itemBorder','itemText');
menu1[4][1] = new Item('<font color="#000000">Past Projects</font>','pastprojects.html','',20,4,0);
menu1[4][2] = new Item('<font color="#000000">Sample Drawings</font>','sampledrawings.html','',20,4,0);

menu1[5] =  new Array();
menu1[5][0] = new Menu(true,'<IMG SRC="images/greenarrow.gif">',150,0,200,'#ffffff','#dcdcdc','itemBorder','itemText');
menu1[5][1] = new Item('<font color="#000000">Plain Text</font>','textdirections.html','',20,4,0);
menu1[5][2] = new Item('<font color="#000000">Maps</font>','maps.html','',20,4,0);
registerMenu(menu1);



function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}



// declare global variable name
var newWindow = null
function makeNewWindow(url,windowname) {
	// check if window already exists
	if (!newWindow || newWindow.closed) {
    //    store new window object in global variable
	newWindow = window.open (url,windowname,"width=600,height=600,status=yes,scrollbars,resizable,toolbar,titlebar,menubar, location")
		} else {
newWindow.focus()}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		XLW_home_xlwdoc_over = newImage("http://www.softartisans.com/lib/softartisans/XLW-home-xlwdoc_over.gif");
		xlwdoc_over = newImage("http://www.softartisans.com/lib/softartisansxlwdoc-over.gif");
		preloadFlag = true;
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}