// Browser detection

var ie4 = (document.all) ? true:false;
var ns4 = (document.layers) ? true:false;
var ns6 = (document.getElementById && !document.all) ? true:false;


// Choose a random number from 1 to max

function pickrandom(max) {
	return Math.ceil(Math.random() * max);
}

/****************************************************************
pop-up window functionality
*****************************************************************/
window.onerror=null;
var win = null;

function newWindow(mypage, myname, w, h, scroll) {
closePopWin();
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize';
win = window.open(mypage, myname, winprops);
}

function closePopWin() {
     if(win != null) {
          if(!win.closed) {
              win.close();
              win = null;
          } else {
              win = null;
          }
     }
}
// Resize functions for Netscape 4

function nsOnResizeEvent() {
	if ((document.size.initWindowWidth != window.innerWidth) || (document.size.initWindowHeight != window.innerHeight)) {
		window.location.href = document.location;
	}
}

function nsOnResizeInit(){
	if (ns4){
		if (typeof document.size == 'undefined'){
			document.size = new Object;
			document.size.initWindowWidth = window.innerWidth;
			document.size.initWindowHeight = window.innerHeight;
			window.onresize = nsOnResizeEvent;
		}
	}
}

nsOnResizeInit();


// Layer show/hide functions

function showLayer(id){
	if (ie4) document.all[id].style.visibility = 'visible';
	else if (ns4) document.layers[id].visibility = 'show';
	else if (ns6) document.getElementById(id).style.visibility = 'visible';
}

function hideLayer(id){
	if (ie4) document.all[id].style.visibility='hidden';
	else if (ns4) document.layers[id].visibility='hide';
	else if (ns6) document.getElementById(id).style.visibility='hidden';
}


// Preload the rollover images

var offstate = new Array();
var onstate = new Array();

function preloadrolloverimages() {
	for (j = 0; j < menus; j++) {
		var index = menuname[j];
		offstate[index] = new Image ();
		offstate[index].src = linkbase + 'images/nav/' + menuname[j] + '_off.gif';
		onstate[index] = new Image ();
		onstate[index].src = linkbase + 'images/nav/' + menuname[j] + '_on.gif';
	}
}


// Rollover activation/deactivation functions

var timerID;

function activate(id) {
	if (id != '') {
		if (timerID) clearTimeout(timerID);
		for (var j = 0; j < menus; j++){
			whichdiv = menuname[j];
			//alert(whichdiv);
			if (id != whichdiv) {
				document.images[whichdiv].src = linkbase + 'images/nav/' + whichdiv + '_off.gif';
				hideLayer('div' + whichdiv);
			} else {
				document.images[whichdiv].src = linkbase + 'images/nav/' + whichdiv + '_on.gif';
				showLayer('div' + whichdiv);
			}
		}
	}
}


function deactivate() { 
	timerID = setTimeout('restoreLayers()',750);
}


function restoreLayers() {
	for (var j = 0; j < menus; j++){
		whichdiv = menuname[j];
		if (activeparent != whichdiv) {
			document.images[whichdiv].src = linkbase + 'images/nav/' + whichdiv + '_off.gif';
			hideLayer('div' + whichdiv);
		} else {
			document.images[whichdiv].src = linkbase + 'images/nav/' + whichdiv + '_on.gif';
			showLayer('div' + whichdiv);
		}
	}
}


// Build a subnavigation div

function buildsubnavdiv(subnav_entries,parentname) {
	var spacerwidth = 0;

	switch(parentname) {
		case 'articles':
			spacerwidth = 100;
			break;
		case 'alternative_energy':
		case 'alternativeenergy':
			spacerwidth = 100;
			break;
		case 'clients':
			spacerwidth = 270;
			break;
		case 'careers':
			spacerwidth = 325;
			break;
		case 'contactus':
			spacerwidth = 340;
			break;
		case 'sitemap':
			spacerwidth = 460;
			break;
		default:
			break;
	}

	var divstart = '<div id="div' + parentname + '"><img src="images/common/spacer.gif" alt="" width="' + spacerwidth + '" height="1" border="0"><span class="subnav">&nbsp;&nbsp;';
	var divend = '</span></div>';
	return divstart + subnav_entries + divend;
}


function buildsubnaventry(parentname,childname,childurl) {
	if (parentdoc == childurl) {
		return childname + '&nbsp;&nbsp;&nbsp;&nbsp;';
	} else {
		return '<a href="' + linkbase + parentname + '/' + childurl + '" onmouseover="clearTimeout(timerID);" onmouseout="deactivate();">' + childname + '</a>&nbsp;&nbsp;&nbsp;&nbsp;';
	}
}


// Create the subnavigation divs

var subnavstring = ''; var menus = 0; var menuname = new Array();

function createsubnavigation() {
	var subnav_entries = '';
	
	for (var parentname in navigation) {
		//alert(parentname);
		for (var childname in navigation[parentname]) {
			//alert(childname);
			subnav_entries += buildsubnaventry(parentname.replace("_",""),childname,navigation[parentname][childname]);
		}
		subnavstring += buildsubnavdiv(subnav_entries,parentname);
		subnav_entries = '';
		menuname[menus] = parentname;
		menus++;
	}
	
	preloadrolloverimages();
}


// Write background image to table named "growthtable"

function writebackground(bgimage) {
	if (!ns4) {
		//eval('growthtable.style.backgroundImage = \'url("' + bgimage + '")\'');
	}
}


// Write the header photo

var bannerphotostring = '';

function createbannerphoto() {
	bannerphotostring = '<img src="' + linkbase + 'images/common/headerphotos/headerphoto' + pickrandom(15) + '.jpg" alt="" width="186" height="253" border="0">';
}

var bannerphotostringpeople = '';

function createbannerphotopeople() {
	bannerphotostringpeople = '<img src="' + linkbase + 'images/common/headerphotos/people/sepia-image-' + pickrandom(10) + '.jpg" alt="" width="186" height="253" border="0">';
}


createsubnavigation();
createbannerphoto();
createbannerphotopeople();
