/*

	We use this JavaScript file to implement the following features:
	
	- To hide and show the elements below the folder tab navigation on all pages
	  where it is used
	
	- To ensure that links to external sites open in new windows
	
	- To make our dropdown navigation work in Internet Explorer
	
*/


/*

	searchGoogle opens a new window with a google search for 'q'

*/

function searchGoogle( q ) {

	window.open( 'http://www.google.com/search?q=' + q , 'googleSearch' );

}


/*

	changeSearch switches the search form back and forth between a site search and a google search

*/

function changeSearch( to ) {

	var searchForm = document.getElementById('search');
	var searchQ    = document.getElementById('search-q');
	
	if( to == 'google') {
	
		searchForm.onsubmit = function() { searchGoogle( searchQ.value ); return false; }
	
	}
	else {
	
		searchForm.onsubmit = '';
	
	}

}


/*

	getElementsByClassName allows us to select elements based on their class
	
*/

function getElementsByClassName( oElm , strTagName , strClassName ) {

	var arrElements = (strTagName == "*" && document.all)? document.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)

}


/*

	changeDiv swaps out the hidden DIVs on the Ethanol Racing and Plant Support pages

*/

function changeDiv( URL ) {

	if( document.getElementById ) {

		if( URL.search( '#' ) > -1 ) {
			
			var anchor = URL.split( '#' )[1];
			
		}
		else {
		
			var anchor = URL;
		
		}
	
		// hide all 'more' DIVs
		var moreAr = getElementsByClassName( document , 'div' , 'more' );
		
		for( var a = 0 ; a < moreAr.length ; a ++ ) {
			
			moreAr[a].style.display = 'none';
		
		}
		
		// remove selected class from any link LIs
		var selectedLinkAr = getElementsByClassName( document , 'li' , 'selected' );
		
		if( selectedLinkAr.length > 0 ) {
		
			for( var a = 0 ; a < selectedLinkAr.length ; a ++ ) {
			
				selectedLinkAr[a].className = '';
			
			}
		
		}
		
		var showDiv  = document.getElementById( anchor + '-div' );
		var showLink = document.getElementById( anchor + '-link' );
		
		// show new DIV
		showDiv.style.display = 'block';
		
		// set classname to 'selected' of appropriate link LI
		showLink.className = 'selected';
		
	}

}


/*

	changeDiv_bio swaps out the hidden DIVs on the Bio Page

*/

function changeDiv_bio( URL ) {

	if( document.getElementById ) {

		if( URL.search( '#' ) > -1 ) {
			
			var anchor = URL.split( '#' )[1];
			
		}
		else {
		
			var anchor = URL;
		
		}
	
		// hide all 'more' DIVs
		var moreAr = getElementsByClassName( document , 'div' , 'more' );
		
		for( var a = 0 ; a < moreAr.length ; a ++ ) {
			
			moreAr[a].style.display = 'none';
		
		}
		
		// remove selected class from any link LIs
		var selectedLinkAr = getElementsByClassName( document , 'li' , 'selected' );
		
		if( selectedLinkAr.length > 0 ) {
		
			for( var a = 0 ; a < selectedLinkAr.length ; a ++ ) {
			
				selectedLinkAr[a].className = '';
			
			}
		
		}
		
		var showDiv  = document.getElementById( anchor + '-div' );
		//var sho2wLink = document.getElementById( anchor + '-link' );
		
		// show new DIV
		showDiv.style.display = 'block';
		
		// set classname to 'selected' of appropriate link LI
		//showLink.className = 'selected';
		
	}

}


/*

	externalLinks handles opening links to other sites in a new window

*/

function externalLinks() {

	if( document.getElementsByTagName ) {
	
		var linksAr = document.getElementsByTagName( 'a' );
		
		for( var a = 0 ; a < linksAr.length ; a ++ ) {
		
			if(
				linksAr[a].href.length > 0 &&
				linksAr[a].href.search( /icm.integermidwest.com/gi ) == -1 &&
				linksAr[a].href.search( /icminc.com/gi ) == -1 &&
				linksAr[a].href.search( /209.248.163.43/gi ) == -1 ) {
			
				linksAr[a].target = '_blank';
			
			}
			
			if(
				linksAr[a].href.length > 0 &&
				linksAr[a].href.search( /.pdf$/gi ) > -1 ) {
			
				linksAr[a].target = '_blank';
			
			}
			
			if(
				linksAr[a].href.length > 0 &&
				linksAr[a].href.search( /.jpg$/gi ) > -1 ) {
			
				linksAr[a].target = '_blank';
			
			}
		
		}
	
	}
	
}


/*

	pageInit initializes the Ethanol Racing and Plant Support pages

*/

function pageInit() {

	if( document.getElementById ) {

		// if there are any divs with the class 'more' on the page
		var moreAr = getElementsByClassName( document , 'div' , 'more' );
		
		if( moreAr.length > 0 ) {
		
			// remove IDs from all h2s to stop scrolling
			var h2Ar = document.getElementsByTagName( 'h2' );
			
			for( var b = 0 ; b < h2Ar.length ; b ++ ) {
			
				h2Ar[b].id = '';
			
			}
		
			// if there is an anchor in the URL (#)
			var URL = window.location.toString();
			var anchorText = URL.split( '#' )[1];
			
			// show that DIV - otherwise, show the first 'more' DIV
			if( anchorText != undefined ) {
			
				changeDiv( '#' + anchorText );
			
			}
			else {
				
				changeDiv( moreAr[0].id.substr( 0 , moreAr[0].id.length - 4 ) );
			
			}
			
		}
		
	}

}


/*

	startList makes the dropdown menus work in IE
	
*/

startList = function() {

	if ( document.all && document.getElementById ) {
	
		navRoot = document.getElementById( 'navlist' );
		
		for ( i=0 ; i < navRoot.childNodes.length ; i++ ) {
		
			node = navRoot.childNodes[i];
		
			if ( node.nodeName == 'LI' ) {
		
				node.onmouseover = function() {
		
					this.className += ' over';
		  		}
		 
		 		node.onmouseout = function() {
		  
		  			this.className = this.className.replace( ' over', '' );
				}
				
			}
			
		}
		
	}
	
}


/*

	fixNav removes the drop-downs for older versions of Safari

*/

function fixNav() {

	var browser = window.navigator.userAgent;
	
	// if this browser is Safari
	if( browser.search(/Safari/gi) )
	{
	
		var SafariVersion = parseFloat( browser.split( 'Safari/' )[1] );
		
		// if this version of Safari is 1.0 or 1.2
		if( SafariVersion < 312 )
		{
		
			// find all of the top-level #nav LIs
			var navAr = getElementsByClassName( document , 'li' , 'top' );
			
			for( var a = 0 ; a < navAr.length ; a ++ ) {
			
				// find all of the ULs that are children of the top-level #nav LIs
				var ulAr = navAr[a].getElementsByTagName('UL');
				
				for( var b = 0 ; b < ulAr.length ; b ++ ) {
					
					// remove each UL
					navAr[a].removeChild( ulAr[b] );
				
				}
			
			}
		}
	
	}
	
}


/*

	Attach our functions to the onload event
	
*/

window.onload = function() {

	externalLinks();

	pageInit();
	
	startList();
	
	initializePortfolioNav();
	
	if ( typeof( includesGoogleMaps ) != 'undefined' )
		initializeMap();
		
	fixNav();

}