var ie5 		= false;
var pre_w3c 	= false;
var isStnd 		= document.getElementById ? true : false; // Standards compliant
var isIE 		= document.all ? true : false; // IE 4/5
var isNN4 		= document.layers ? true : false; // NN 4.x
var DHTML 		= (isStnd || isIE || isNN4); // Any of the above
var isMac 		= (navigator.appVersion.indexOf( "Macintosh" ) != -1) ? true : false;
var isSafari 	= (navigator.appVersion.indexOf( "Safari" ) != -1) ? true : false;

var version = navigator.appVersion;

// Page and Window Vars
var adjusted = 0;
var adjID = 0;
var pageName = "";
var newWindow = null;

var menuArr = ["space","air","ground","home","about","news","jobs","sitemap","login","submit","contact"];

/* Staged links */
function rewriteStagedLinks(){
	var myDomain = document.domain;
	if(document.getElementsByTagName && (myDomain.indexOf('cakefarm') !== -1)){
		var links = document.getElementsByTagName("a");  
		for (var eleLink=0; eleLink < links.length; eleLink ++) {
			if(links[eleLink].href.indexOf("https") !== -1) {
				var l = links[eleLink].href.replace("https", "http");
				links[eleLink].href = l;
			}	
		}
	}
	return true;
}
/* End Staged links */
 
// Get browser information ... 
 if (navigator.appName.indexOf("Netscape") != -1) {
    var subVersion = version.substring(0,3)
    var subVersion_p = parseFloat(subVersion)
     if (subVersion_p < 5)  {
		pre_w3c = true;
		isNS = false;
    }  
  }   
 if (navigator.appName.indexOf("Explorer") != -1) {
    var subVersion = version.substring(22,25)
    var subVersion_p = parseFloat(subVersion)
     if (subVersion_p < 5)  {
	   pre_w3c = true;
	   isNS = false;
	 }
	 if ((subVersion_p == 5) || (subVersion_p == 5.5))  {
	   ie5 = true;
	 }
  }
  
 /*
	util:implCheck use to check implementation on standards compliant browsers.
	Requires: none
	Returns: boolean
	Example: implCheck("DOM","2.0");
*/
function implCheck(feature, version){
	if (document.implementation &&
		  document.implementation.hasFeature &&
		  document.implementation.hasFeature(feature, version))
	   alert(feature + " version " + version + " support available");
	else
	   alert(feature + " version " + version + " NOT available");
}

//initLoad = function() {if(!pre_w3c){adjustLayout();setActions();if(isIE && !(isMac && ie5)) addHovers("mn");}}
  
function initPage(){
	if(!pre_w3c){
		adjustLayout();
		//setActions();
		if(isIE && !(isMac && ie5)) addHoversNew(); // addHovers("mn");
	}
}

// GetPageFileName returns name of current file
function getPageFileName(){
	var f = document.location.href;
	var ix  = f.lastIndexOf('/');
	if ( ix > -1 ) f = f.substring( ix + 1 );
	var ix  = f.indexOf('?');
	if ( ix > -1 ) f = f.substring(0, ix);
	if(f==""){
		return "index.php";
	}else{
		return f;
	}
	//var page  = parseInt( ( f.substring( f.indexOf('Page') + 4 ) ), 10 );
}

function getFileName(path){
	var ix  = path.lastIndexOf('\\');
	if ( ix > -1 ) path = path.substring( ix + 1 );
	//alert("Path is " + path);
	if(path==""){
		return null;
	}else{
		return path;
	}
}

function addHoversX(startID){
	var navRoot = document.getElementById(startID);
	for (i=0; i<navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName=="LI"){
			if(node.id=="spacetab"){
				//alert("Spacetab");
				node.onmouseover=function() {
					this.className+=" spacehover";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" spacehover", "");
				}
				processNested("spacetab","spacehover");
				
			}else if(node.id=="airtab"){
				node.onmouseover=function() {
					this.className+=" airhover";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" airhover", "");
				}
				processNested("airtab","airhover");
				
			}else if(node.id=="groundtab"){
				node.onmouseover=function() {
					this.className+=" groundhover";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" groundhover", "");
				}
				processNested("groundtab","groundhover");
			}
		}
	}
}

// IE functions to add hovers to css-powered-buttons
function addHovers(startID){
	var navRoot = document.getElementById(startID);
	for(var i=0; i< menuArr.length;++i){
		var node = xGetElementById(menuArr[i]+"tab");
		node.onmouseover=function() {
			// Note: element id must be elem+"tab"
			var tab = this.id.replace("tab","");
			this.className+=" "+tab+"hover";
		}
		node.onmouseout=function() {
			// Note: element id must be elem+"tab"
			var tab = this.id.replace("tab","");
			this.className=this.className.replace(" "+tab+"hover", "");
		}
		processNested(menuArr[i]+"tab",menuArr[i]+"hover");
	}
}

function processNested(n,nn){
	var els = document.getElementById(n).getElementsByTagName("LI");
	for (var i=0; i<els.length; i++) {
		els[i].onmouseover=function() {
			this.className+=" "+nn;
		}
		els[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" "+nn+"\\b"), "");
		}
	}
}


// Page / DOM and Style adjustment functions
// 'xFunctions' requires LGPL licensed X-Library from Michael Foster

function selectField(f){
	var ex = "document.f1." + f + ".focus()";eval(ex);
	var ex = "document.f1." + f + ".select()";eval(ex);
}

function setActions(){
	var el = getObj("f1");
	if(el != null){
		el.obj.srch.onfocus = function () { selectField('srch');}
		el.obj.srch.onmouseover = function () { selectField('srch');}	
	}
}

function setUploadActions(){
	if(isStnd && !isMac && !ie5){
		// Upload buttons
		a = document.getElementById("ulbtn");
		if(a != null){
			a.onmouseover = function () { MM_swapImage('ulbtn','','../../images/upload_btn_gray.gif',1);}
			a.onmouseout = function () { MM_swapImgRestore();}
		}
		a = document.getElementById("cmbtn");
		if(a != null){
			a.onmouseover = function () { MM_swapImage('cmbtn','','../../images/end_upload_btn_gray.gif',1);}
			a.onmouseout = function () { MM_swapImgRestore();}
		}
	}
	//MM_preloadImages('../../images/upload_btn_gray.gif','../../images/end_upload_btn_gray.gif');
}

// Layout adjustment functions
function adjustLayoutResize(){
	if(!printing){
		if(adjID) clearTimeout(adjID);
		adjusted = false;
		adjustLayout();
	} else {
		clearTimeout(adjID);
	}
}

function adjustLayout(){
	
	if(adjusted<2){

		pageName = getPageFileName();
		if(pageName == "service_areas.php" || pageName == "about.php") adjustSets(); // Fix set heights less than 140px
		
		var adj = 0;
		var margins = 39;
		var corimg = (!isIE) ? 160 : 160; // 10/20/04: Changed from isSafari to !isIE.
										  // Safari needs 214 adjustment to level1 height
		var maxheight = 0;
		var targetheight = 270;
		
		var l1 = xHeight("level1");
		var l2 = xHeight("level2");
		var l3 = xHeight("level3");
		var c  = xHeight("content");
		maxheight = Math.max(l1,Math.max(l2,Math.max(l3,Math.max(c,targetheight))));
		 
		// Sitemap columns
		var n1 = xHeight("n1");
		var n2 = xHeight("n2");
		var n3 = xHeight("n3");
		var n4 = xHeight("n4");
		if(n1) {
			//alert("Site map");
			nheight = Math.max(l1,Math.max(n1,Math.max(n2,Math.max(n3,n4)))) + 25;
			maxheight = Math.max(maxheight,nheight);
			xHeight("n1", maxheight);
			xHeight("n2", maxheight);
			xHeight("n3", maxheight);
			xHeight("n4", maxheight);
		}
		
		
		lspot  = xHeight("leftspot"); // left blue box home
		if(lspot) {
			var boxset  = xHeight("topleftbox"); // left blue box home
			maxheight = Math.max(Math.max(lspot,boxset),maxheight); 
			if(!isIE) maxheight = maxheight+40; // TODO: Find better fix for overhang on ff
			xHeight("leftspot", maxheight);
		}
		var col  = xHeight("col"); // career middle col
		if(col) {maxheight = Math.max(lspot,maxheight); xHeight("col", maxheight);}
		
		xHeight("level1", maxheight);
		xHeight("level2", maxheight);
		
		if(!l2) {
			// only adjust content, ridge if level2 isn't present
			xHeight("content", maxheight); 
			xHeight("ridge", maxheight);
		}
		if((pageName != "project.php") && (pageName != "project.php#")) xHeight("level3", maxheight-2);
		
		adjID = setTimeout('adjustLayout()', 60);
		adjusted += 1;
	} else {
		//adjust the page twice
		if(adjusted > 2){
			adjusted = 0;
			if(adjID) clearTimeout(adjID);
		}
	}
} 

function adjustSets(){
	var els = document.getElementById("ridge").getElementsByTagName("div");
	for (var i=0; i<els.length; i++) {
		if(els[i].className == "set"){
			var setH = xHeightObj(els[i]);
			if(setH < 140) xHeightObj(els[i], 140);
		}
	}
}

// Open window functions

function openWindow(page,width,height,sbars) {
	if (newWindow != null){
		if(navigator.appName.indexOf("Netscape")>=0){
			if(newWindow.name){
		 		newWindow.close();
			}
		}else{
			newWindow.close();
		}
	}
	newWindow = window.open(page, "newWin", "width=" + width + ",height=" + height + ",screenX=0,screenY=0,top=0,left=0,status=yes,toolbar=no,menubar=no,location=no,scrollbars=" + sbars + ",resizable=yes");
	//newWindow.location.href = page;
	if (newWindow.opener == null) newWindow.opener = window;

}

/* Note: MacIE5.2.3 takes relative links from the location of this script file,
note the location of the calling file */
function zoom(page,orientation) {
	//alert("Loc: " + document.location.href + "\n" + "Page: " + page);
	if(isIE && isMac) page = getBackPaths(page);
		//alert(page);
	if (newWindow != null){
		if(navigator.appName.indexOf("Netscape")>=0){
			if(newWindow.name){
		 		newWindow.close();
			}
		}else{
			newWindow.close();
		}
	}
	if(orientation == "landscape"){
		height = 560;
		width = 730;
	} else if(orientation == "portrait"){
		height = 710;
		width = 550;
	} else if(orientation == "pass"){
		height = 360;
		width = 500;
	}
	newWindow = window.open(page, "newWin", "width=" + width + ",height=" + height + ",screenX=0,screenY=0,top=0,left=0,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	//newWindow.location.href = page;
	if (newWindow.opener == null) newWindow.opener = window;

}

function getBackPaths(url) {
	var idx = url.indexOf('?');
	
	// Fix page
	var newpage = url.substring(0,idx) + "?";
	/*var backidx = page.indexOf('../../');
	if(backidx > -1) {
		var newpage =  "../../" + newpage.substring(backidx+6, newpage.length) + "?";
	} else {
		var newpage = "../" + newpage + "?";;
	}*/
	
	// Fix parameters
	if (idx != -1){
		var pairs = url.substring(idx+1, url.length).split('&');
		for (var i=0; i<pairs.length; i++){
			nameVal = pairs[i].split('=');
			var backidx = nameVal[1].lastIndexOf('../../');
			if(backidx > -1) {
				var newval = "back2" + nameVal[1].substring(backidx+6, nameVal[1].length);
			} else {
				var newval = nameVal[1];
			}
			//params[nameVal[0]] = newval;
			newpage = newpage + nameVal[0] + "=" + newval + "&";
		}
		newpage = newpage.substring(0, newpage.length-1);
	}
	return newpage;
} 

function openWindow_nobars(page,width,height,sbars) {
	if (newWindow != null){
		if(navigator.appName.indexOf("Netscape")>=0){
			if(newWindow.name){
		 		newWindow.close();
			}
		}else{
			newWindow.close();
		}
	}
	newWindow = window.open("", "newWin", "width=" + width + ",height=" + height + ",screenX=0,screenY=0,top=0,left=0,status=yes,toolbar=no,menubar=no,location=no,scrollbars=" + sbars + ",resizable=yes");
	newWindow.location.href = page;
	if (newWindow.opener == null) newWindow.opener = window;

}

/*
	Utility:getObj creates crossbrowser layer object.
	Requires: layer name
	Returns: self with obj and style reference
	Error checking: TODO check presence of 'objName' layer
*/
function getObj(objName) { 
	if (isStnd) { 
		this.obj = document.getElementById(objName); 
		this.style = document.getElementById(objName).style; 
	} else if (isIE) { 
		//alert("Document.all");
		this.obj = document.all[objName]; 
		this.style = document.all[objName].style; 
	} else if (isNN4) { 
		//this.obj = document.layers[objName]; 
		//this.style = document.layers[objName]; 
		this.obj = xLayer(objName);
		this.style = xLayer(objName);
	} else {
		// Other browsers
		
	}
} 

function getElementWidth(Elem) {
	
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	
	xPos = elem.offsetWidth;

	return xPos;
}


