var imageRotator = (function () {
	return {
		init: function () {
			var imageRotatorDiv = getElementsByClassName('image-rotator','div',document);
			imageRotatorDiv = imageRotatorDiv[0];
			var imageRotatorImage = imageRotatorDiv.getElementsByTagName('img')[0];
			
			var numberofImages = imageRotator.imageSources.length;
			var randomNumber = imageRotatorImage.src = Math.floor(Math.random()*numberofImages);
			imageRotatorImage.src='/images/photos/iowa/'+imageRotator.imageSources[randomNumber];
			imageRotatorImage.className="iowa-image-"+randomNumber;
		},
		imageSources: ["iowa-brown-hills.jpg", "iowa-cattle.jpg", "iowa-cityscape.jpg", "iowa-crops.jpg", "iowa-fields.jpg", "iowa-foliage.jpg", "iowa-grass.jpg", "iowa-hay.jpg", "iowa-hills.jpg", "iowa-historic-building.jpg", "iowa-silos.jpg", "iowa-sunset.jpg", "iowa-turbines.jpg", "iowa-wetlands-at-night.jpg", "iowa-wetlands.jpg"]
	}
})();


var ie6HoverThingy = (function () {
	return {
		init: function () {
			mainNavigation = getElementsByClassName('main-navigation','ul',document);
			mainNavigationLIs = mainNavigation[0].getElementsByTagName('li');
			for (var i = 0; i < mainNavigationLIs.length; i++) {
				mainNavigationLIs[i].onmouseover = function () {
					this.className = this.className + ' hover';
				}

				mainNavigationLIs[i].onmouseout = function () {
					this.className = this.className.replace(' hover','');
				}

			}
		}
	}
})();

window.ondom = function(fn){
	window.__ondom_functionArray.push(fn);
};
(function(){
	window.__ondom_functionArray = [];
	function _runFunctions(){
		/*for (var i in window.__ondom_functionArray){
			(window.__ondom_functionArray[i])();
		}*/
		// The above code was inadvertently calling commonspot functions that weren't ready
		// I have updated it to directly call the intended functions
		// Lisa Backer, Fig Leaf Software - 6/15/09
		imageRotator.init();
		ie6HoverThingy.init();
	};
	var _khtml = /(WebKit|khtml)/i.test(navigator.userAgent);
	if(document.addEventListener && !_khtml){
		document.addEventListener("DOMContentLoaded", _runFunctions, false);
	}else if(_khtml){
		var _timer = setInterval(function(){
			if(/loaded|complete/.test(document.readyState)){
				clearInterval(_timer);
				_runFunctions();
			}
		}, 10);
	}else{
		document.write("<script id=__ie_ondom defer src=javascript:void(0)><\/script>");
		var script = document.getElementById("__ie_ondom");
		script.onreadystatechange = function(){
			if(this.readyState == "complete"){
				_runFunctions();
			}
		};
	}
})();

window.ondom(function(){
	// see comment above where these are called
	imageRotator.init();
	ie6HoverThingy.init();
});


function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}


function selectURL(nexturl) {
 	if (nexturl != "") {
		  window.top.location.href = nexturl;
	 }
}