/*  home2.js  v1  R9.3 jrg  2009-04-17  */

function simplePreload() { 
	var args = simplePreload.arguments;
	document.imageArray = new Array(args.length);
	var preloadRoot;
	if(typeof(spRoot) == "undefined") 
		preloadRoot = "";
	else preloadRoot = spRoot;
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = preloadRoot + args[i];
	}
}

simplePreload("agilent/images3/home2/flyout_small.gif",
"agilent/images3/home2/flyout_top2.gif",
"agilent/images3/home2/navsel.gif",
"agilent/images3/home2/navsel_left.gif",
"agilent/images3/home2/navsel_right.gif",
"agilent/images3/home2/navhover.gif",
"agilent/images3/home2/navhover_left.gif",
"agilent/images3/home2/navhover_right.gif",
"agilent/images3/home2/flyout_bg.gif",
"agilent/images3/home2/flyout_bg2.gif",
"agilent/images3/home2/flyoutmenu_bg.gif",
"agilent/images3/home2/flyoutmenu_bg2.gif"
);

var accordionSection = null;
var activeTab = null;
var hashValue = "";
var hashLinks = new Array();
function init() {
	var searchterm = document.getElementById("searchterm");
	if(searchterm.value == "" || searchterm.value == searchterm.defaultValue) {
		searchterm.className += " lightgrey";
	}
	if(document.getElementById("util").offsetWidth <= 700)
		searchterm.style.width = "173px";
	searchterm.onmousedown = searchterm.onkeypress = function() {
		if(this.value == this.defaultValue) {
			this.value = "";
			searchterm.select();
		}
		if(!searchterm.active) {
			searchterm.active = true;
			searchterm.className += " active";
		}
	}
	var corporate = document.getElementById("corporate");
	if(corporate) {
		var sections = corporate.getElementsByTagName("DL");
		for(var i=0; i < sections.length; i++) {
			if(sections[i].className.search(/\bexpandable\b/) == -1) continue;
			if(sections[i].id == "")
				sections[i].id = "accordionSection" + i;
			if(sections[i].className.search(/\bselected\b/) != -1)
				openAccordion = sections[i].id;
			sections[i].onmouseover = function() {
				runAccordion(this.id);
			}
		}
	}
	var defFontSize = fontSize(document.body);
	document.getElementById("tabs").className += " jsenabled";
	var tabs = ["a1675Content","a1721Content","a1691Content","a1678Content","a1692Content","a1693Content","a1690Content"];
	var anchors = ["a1675","a1721","a1691","a1678","a1692","a1693","a1690"];
	var links = document.getElementById("tabnav").getElementsByTagName("LI");
	var hashVal = location.hash.replace("#","");
	for(var i=0; i < links.length; i++) {
		var link = links[i];
		hashLinks[anchors[i]] = link;
		var href = link.getElementsByTagName("A")[0].href;
		link.content = document.getElementById(tabs[i]);
		link.defFontSize = defFontSize;
		var divs = link.content.getElementsByTagName("DIV");
		var panelHeight = link.content.offsetHeight - 18 + "px";
		link.panels = new Array();
		for(var j=0; j < divs.length; j++) {
			if(divs[j].className.search(/\bpanel\b/) != -1) {
				divs[j].style.height = panelHeight;
				link.panels.push(divs[j]);
			}
		}
		if((i == 0 && hashVal == "" ) || hashVal == anchors[i]) {
			link.className += " selected";
			activeTab = link;
			hashValue = hashVal;
		} else {
			link.content.style.display = "none";
		}
		link.onclick = function() {
			activeTab.content.style.display = "none";
			activeTab.className = activeTab.className.replace(/\bselected\b/g,"");
			this.content.style.display = "block";
			this.className += " selected";
			activeTab = this;
		}
	}
	if(navigator.userAgent.indexOf("Opera") == -1) {
		flyoutInit();
	}
	setInterval("selectTab()", 1000);
	flashInit();
	document.title = document.title.replace(/#[^#]*$/,"");
}
function selectTab() {
	var hashVal = location.hash.replace("#","");
	var link = (hashVal != "" ? hashLinks[hashVal] : hashLinks["a1675"]);
	if(hashVal != hashValue) {
		activeTab.content.style.display = "none";
		activeTab.className = activeTab.className.replace(/\bselected\b/g,"");
		link.content.style.display = "block";
		link.className += " selected";
		activeTab = link;
		hashValue = hashVal;
	}
	var tmpFontSize = fontSize(document.body);
	if(link.defFontSize == tmpFontSize) return;
	link.defFontSize = tmpFontSize;
	var panels = link.panels;
	if(navigator.userAgent.indexOf("MSIE 6.0") == -1) {
		for(var j=0; j < panels.length; j++) {
			panels[j].style.height = "auto";
		}
	} else {
		for(var j=0; j < panels.length; j++) {
			panels[j].style.height = "228px";
		}
	}
	var panelHeight = link.content.offsetHeight - 18 + "px";
	for(var j=0; j < panels.length; j++) {
		panels[j].style.height = panelHeight;
	}
}
function fontSize(obj){
	var span = document.createElement('SPAN');
	var atts= {fontSize:'1em',padding:'0',position:'absolute',lineHeight:'1',visibility:'hidden',display:'inline'};
	for(var p in atts){
		span.style[p]= atts[p];
	}
	span.appendChild(document.createTextNode('M'));
	obj.appendChild(span);
	var height = span.offsetHeight;
	obj.removeChild(span);
	return height;
}

if(navigator.userAgent.indexOf("Safari") != -1) {
	var getScrollLeft = function() {
		return document.body.scrollLeft;
	}
} else {
	var getScrollLeft = function() {
		return document.documentElement.scrollLeft;
	}
}
function flyoutInit() {
	var flyoutSections = ["util","corporate","tabs"];
	var defaultFlyoutLeft = [0,62,62];
	if(navigator.userAgent.indexOf("MSIE 7") != -1)
		var leftCorrection = [2,7,-15];
	else
		var leftCorrection = [2,7,2];
	var bgYPosition = ["100%","100%","0"]
	for(var j=0; j < flyoutSections.length; j++) {
		if(!document.getElementById(flyoutSections[j])) continue;
		var sfEls = document.getElementById(flyoutSections[j]).getElementsByTagName("LI");
	
		for (var i=0; i < sfEls.length; i++) {
			if(sfEls[i].className.search(/\bflyout\b/) == -1) continue;
			var divs = sfEls[i].getElementsByTagName("DIV");
			if(divs.length > 1 && divs[1].className.search(/\btwocolumn\b/) != -1)
				var flyout = divs[1];
			else {
				var Uls = sfEls[i].getElementsByTagName("UL");
				if(Uls.length > 0)
					var flyout = Uls[0];
			}
			if(flyout) {
				sfEls[i].flyout = flyout;
				sfEls[i].container = flyoutSections[j];
				sfEls[i].foHolder = divs[0];
				sfEls[i].defLeft = defaultFlyoutLeft[j];
				sfEls[i].leftCorrection = leftCorrection[j];
				var borderLeftFiller = document.createElement("BLOCKQUOTE");
				borderLeftFiller.style.top = bgYPosition[j];
				sfEls[i].appendChild(borderLeftFiller);
				sfEls[i].borderLeftFiller = borderLeftFiller;
				var borderRightFiller = document.createElement("BLOCKQUOTE");
				borderRightFiller.style.top = bgYPosition[j];
				sfEls[i].appendChild(borderRightFiller);
				sfEls[i].borderRightFiller = borderRightFiller;
				var iframe = document.createElement("IFRAME");
				iframe.frameBorder = 0;
				var immediateParent = flyout.parentNode;
				immediateParent.appendChild(iframe);
				sfEls[i].iframe = iframe;
			}
			sfEls[i].onmouseover = function() { flyoutOver(this); }
			sfEls[i].onmouseout = function() { flyoutOut(this); }
		}
	}
}
var activeFlyoutLink = null;
function flyoutOver(obj) {
	if(obj.container == "corporate" && accordionRunning) return;
	obj.foHolder.style.left = "-999999em";
	obj.className += " sfhover sfhover2";
	var viewportWidth = document.documentElement.clientWidth;
	var scrollLeft = getScrollLeft();
	var absRight = obj.defLeft + obj.flyout.offsetWidth;
	var tmp = obj.foHolder.parentNode;
	while(tmp.tagName != "BODY" && tmp.tagName != "HTML") {
		if((tmp.currentStyle && tmp.currentStyle.position != "static") ||
			(document.defaultView && document.defaultView.getComputedStyle(tmp,null).position != "static"))
			absRight += tmp.offsetLeft;
		tmp = tmp.parentNode;
	}
	var newLeft = obj.defLeft;
	if(absRight > viewportWidth + scrollLeft) {
		newLeft = obj.defLeft - (absRight - viewportWidth - scrollLeft) - obj.leftCorrection;
	} else if(absRight - obj.flyout.offsetWidth < scrollLeft) {
		newLeft = obj.defLeft - obj.leftCorrection + scrollLeft - absRight + obj.flyout.offsetWidth;
	}
	obj.foHolder.style.left = newLeft + "px";
	obj.borderLeftFiller.style.width = Math.max(0,newLeft) + "px";
	obj.borderRightFiller.style.left = newLeft + obj.flyout.offsetWidth + "px";
	obj.borderRightFiller.style.width = Math.max(0,obj.offsetWidth - newLeft - obj.flyout.offsetWidth) + "px";
	obj.borderLeftFiller.style.display = "block";
	obj.borderRightFiller.style.display = "block";
	with(obj) {
		iframe.style.width = flyout.offsetWidth + "px";
		iframe.style.height = flyout.offsetHeight + "px";
		iframe.style.left = flyout.offsetLeft + "px";
		iframe.style.zIndex = 1;
	}
	activeFlyoutLink = obj;
}
function flyoutOut(obj) {
	obj.className = obj.className.replace(/\bsfhover\b/g, "");
	obj.className = obj.className.replace(/\bsfhover2\b/g, "");
	obj.borderLeftFiller.style.display = "none";
	obj.borderRightFiller.style.display = "none";
	obj.iframe.style.width = 0;
	obj.iframe.style.height = 0;
	obj.iframe.style.zIndex = -1;
	activeFlyoutLink = null;
}
function unload() {
	if(activeFlyoutLink)
		flyoutOut(activeFlyoutLink);
}
var TimeToSlide = 800;
var openAccordion = '';
var accordionRunning = false;
var opening = null;
var closing = null;
var ContentHeight = null;
function runAccordion(id) {
	if(accordionRunning || openAccordion == id) return;
	document.getElementById("corporate").className += " accordion";
	opening = document.getElementById(id).getElementsByTagName("DD")[0];
	if(opening.style.display != 'block')
		opening.style.display = 'block';
	if(opening.style.height == "") opening.style.height = 0;
	closing = document.getElementById(openAccordion).getElementsByTagName("DD")[0];
	if(closing.style.height == "" || closing.style.height == "auto") closing.style.height = closing.scrollHeight + 'px';
	ContentHeight = Math.max(opening.scrollHeight,closing.scrollHeight);
	openAccordion = id;
	accordionRunning = true;
	setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ")", 60);
}

function animate(lastTick, timeLeft) {
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	if(timeLeft <= elapsedTicks) {
		opening.style.height = 'auto';
		closing.style.display = 'none';
		closing.style.height = 0;
		opening.parentNode.className += " selected";
		var tmp = closing.parentNode;
		tmp.className = tmp.className.replace(/selected/g,"");
		tmp = document.getElementById("corporate");
		tmp.className = tmp.className.replace(/accordion/g,"");
		accordionRunning = false;
		return;
	}
	timeLeft -= elapsedTicks;
	var newHeight = Math.round(timeLeft/TimeToSlide * ContentHeight);
	if(opening.scrollHeight > closing.scrollHeight) {
		initHeight = parseInt(opening.style.height);
		opening.style.height = ContentHeight - newHeight + 'px';
		closing.style.height = Math.max(0,parseInt(closing.style.height) - ContentHeight + newHeight + initHeight) + 'px';
	} else {
		initHeight = parseInt(closing.style.height);
		opening.style.height = Math.min(opening.scrollHeight,parseInt(opening.style.height) + initHeight - newHeight) + 'px';
		closing.style.height = newHeight + 'px';
	}
	setTimeout("animate(" + curTick + "," + timeLeft + ")", 60);
}
function validate(form) {
	var inputError = false, selectError = false;
	if(form.searchterm.value == "" || form.searchterm.value == form.searchterm.defaultValue) {
		inputError = true;
		form.searchterm.value = form.searchterm.defaultValue;
		form.searchterm.className = form.searchterm.className.replace(/\active\b/g,"");
		form.searchterm.active = false;
	}
	if(form.neighborhood.value == "") {
		selectError = true;
	}
	if(inputError && selectError) {
		form.searchterm.className = form.searchterm.className.replace(/\berror\b/g,"");
		form.neighborhood.parentNode.className = form.neighborhood.parentNode.className.replace(/\berror\b/g,"");
		return false;
	} else if(inputError) {
		form.searchterm.select();
		form.searchterm.className += " error";
		form.neighborhood.parentNode.className = form.neighborhood.parentNode.className.replace(/\berror\b/g,"");
		return false;
	} else if(selectError) {
		form.searchterm.className = form.searchterm.className.replace(/\berror\b/g,"");
		form.neighborhood.parentNode.className += " error";
		return false;
	} else {
		form.searchterm.className = form.searchterm.className.replace(/\berror\b/g,"");
		form.neighborhood.parentNode.className = form.neighborhood.parentNode.className.replace(/\berror\b/g,"");
	}
	return true;
}
function addEvent(obj, type, fn) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
		obj.attachEvent( "on"+type, fn );
}
function removeEvent(obj, type, fn) {
	if (obj.detachEvent)
		obj.detachEvent( "on"+type, fn );
	else if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
}
addEvent(window,"load", init);
addEvent(window,"unload", unload);
