// *** Page initialization ***


// Set flags for managing visual focus on search box
var searchIsFocused = false;
var searchIsClicked = false;


// Attach event handlers for page loading and resizing
AttachEvent(window,'load',initializeSearch,false);
AttachEvent(window,'load',initializePoppers,false);
AttachEvent(window,'load',setHeight,false);
AttachEvent(window,'resize',setHeight,false);







// *** Function definitions ***


// Visually focuses (brightens up) the search box
function focusSearch() 
{
	searchIsFocused = true;
	var objQuery=document.getElementById("searchquery");
	var objButton=document.getElementById("searchbutton");
	var objBox=document.getElementById("searchbox");
	if(objQuery) {
		changeStyle(objQuery,"focused");
		if(searchIsClicked) {
			if(objQuery.value=="Search") objQuery.value = "";
			objQuery.focus();
			if(objBox) objBox.style.border="1px solid #6C0";
		}
	}
	if(objButton) changeStyle(objButton,"focused");
}

// Visually focuses (brightens up) the search box AND forces it remain bright for longer period (assumes intentional user attention due to user's clicking in the search box)
function clickSearch() 
{
	searchIsClicked = true;
	focusSearch();
}

// Visually unfocuses (dims down) the search box
function blurSearch()
{
	if(!searchIsFocused)
	{
		var objQuery=document.getElementById("searchquery");
		var objButton=document.getElementById("searchbutton");
		var objBox=document.getElementById("searchbox");
		if(objQuery) {
		  if(!searchIsClicked || (objQuery.value==""))  {
			changeStyle(objQuery,"unfocused");
			objQuery.value="Search";
			if(objButton) changeStyle(objButton,"unfocused");
			if(objBox) objBox.style.border="1px solid #FFF";
			searchIsFocused = false;
			searchIsClicked = false;
			objQuery.blur();
		  }
		}
	}
}

// Depending on global flags that have been set, sets a timeout for unfocusing the search box
function startBlurSearch()
{
	searchIsFocused = false;
	if(searchIsClicked) {
		setTimeout('blurSearch()',10000);	// a longer delay, assuming intentional user attention due to user's clicking in the search box
	} else {
		setTimeout('blurSearch()',200);	// a bit of delay to avoid UI flicker/jitter from tiny mouse movements
	}
}	

// Sets a flag that prohibits the search box from becoming unfocused
function stopBlur() {
	searchIsFocused = true;
}

// Attaches event handlers to search box
function initializeSearch() 
{
	var objSearchBox=document.getElementById("searchbox");
	if(objSearchBox) {
		AttachEvent(objSearchBox,'mouseover',focusSearch,false);
		AttachEvent(objSearchBox,'click',clickSearch,false);
		AttachEvent(objSearchBox,'mouseout',startBlurSearch,false);
	}
	var objQuery=document.getElementById("searchquery");
	if(objQuery) objQuery.value = "Search";
}

// Applies the CSS class newClass to the object objToChange
function changeStyle(objToChange,newClass) 
{
	objToChange.className=newClass;
}

// Sets initial height of translucent content and left side scrims -- overcomes browser bugs in setting these heights
function setHeight() {  
	var objSidescrim = document.getElementById("leftscrim");
	var objLeftnavscrim = document.getElementById("leftnavscrim");
	var objContent = document.getElementById("content");
	var objLeftcolumn = document.getElementById("leftcolumn");
	var objContentscrim = document.getElementById("contentscrim");
	var contentHeight;
	var sidescrimHeight;
	var winHeight = getWinHeight();
	
	if(objContent) {
		// if the div with id "content" exists, set all heights by it
		contentHeight = objContent.offsetHeight - 90;	// allow for footer whitespace below
		sidescrimHeight = objContent.offsetHeight + 195;  // allow for header whitespace above
	} else if(objLeftcolumn) {
		// if the div with id "content" does NOT exist BUT the div with id "leftcolumn" DOES exist, set sidescrim height by leftcolumn
		sidescrimHeight = objLeftcolumn.offsetHeight + 320;
	}
		
	if (winHeight > sidescrimHeight) sidescrimHeight = winHeight;

	if(objSidescrim) {
		objSidescrim.style.height = sidescrimHeight + "px";
	}
	if(objContentscrim) {
		objContentscrim.style.height = contentHeight + "px";
	}
	if(objLeftnavscrim) {
		objLeftnavscrim.style.height = contentHeight + "px";
	}
}

// Returns the integer height of the visible area of the browser window, in pixels
function getWinHeight() {  
	var intHeight;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		intHeight = window.innerHeight;
	} else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		intHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		intHeight = document.body.clientHeight;
	}			
	return intHeight;
}

// Parses the document for any links of class "popup", and adds onclick event listeners to pop up the links into new browser windows rather than into the current one.
function initializePoppers() {
	var arrPoppers = getElementsByClassName(document, "a", "popup");
	var i;
	for(i=0;i<arrPoppers.length;i++){
		arrPoppers[i].target = "_blank";
	}
}

// Returns an array of objects of specified type ("tag") that have the specified CSS class ("searchClass")
// e.g., arrSidebarDIVSinContent = getElementsByClass("content","sidebar","div");
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

// Submit the form whose id matches the argument
function submitForm(strFormID){
	var objForm = document.getElementById(strFormID);
	if(objForm){
		objForm.submit();	
	}
}



// *** The following code is copyright 2003 by Gavin Kistner, gavin@refinery.com. It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt

// Cross browser attach event function. For 'evt' pass a string value with the leading "on" omitted e.g. AttachEvent(window,'load',MyFunctionNameWithoutParenthesis,false);
function AttachEvent(obj,evt,fnc,useCapture){ 
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

// Fixes for browsers like NS4 or IE5Mac which don't support either attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){ 
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){ 
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}