

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function checkDOM() {
	// checks that the DOM exists
	if (!document.getElementById || !document.getElementsByTagName) return false;
}


sfHover = function() {
	checkDOM();
	if (document.getElementById("navigation")) {
		var sfEls = document.getElementById("navigation").getElementsByTagName("LI");

		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);  // only loads in IE


function addClass(element, value) {
    if (!element.className) {
        element.className = value;
    } else {
        var newClassName = element.className;
        newClassName += " ";
        newClassName += value;
        element.className = newClassName;
    }
}


function resizeMenu() {
	checkDOM();
	if (!document.getElementById('navigation')) return false;
	var navigation = document.getElementById('navigation');
	
	// width of navigation bar (minus padding)
	var availableWidth = 890;
	
	// initialise the variable
	var actualWidth = 0;

	// get all the child elements
	var topLevelNav = navigation.getElementsByTagName('ul')[0].childNodes;
	
	// create an array for the navigation items	
	var navItems = new Array();
	
	// look for elements that are list items and add them to the array
	for (var i=0; i<topLevelNav.length; i++) {
		if (topLevelNav[i].nodeName == "LI") {
			navItems.push(topLevelNav[i]);
			
			// find the width of each nav item and add them to actualWidth
			actualWidth += topLevelNav[i].offsetWidth;
		} 
	}
	
	var secondLevelNav = navigation.getElementsByTagName('ul');
	
	// work out how much space we have leftover ...
	itemPadding = (availableWidth - actualWidth) / (navItems.length);

	// and divide it by two so we can apply the padding on both sides of the list item
	itemPaddingHalf = Math.max(10,Math.floor(itemPadding / 2));
	
	
	// go through the navItems array and add padding on both sides 
	for (var i=0; i<navItems.length; i++) {
		thisItem = navItems[i];
		thisItem.style.paddingLeft = itemPaddingHalf + "px";
		thisItem.style.paddingRight = itemPaddingHalf + "px";
	}

	// shift the second level nav ul back the same number of pixels so that they line up with the top level nav item
	for (var i=1; i < secondLevelNav.length; i++) {
		secondLevelNav[i].style.marginLeft = -itemPaddingHalf + "px";
	}
	
}
	
function addLastClass() {
	checkDOM();
	if (!document.getElementById('navigation')) return false;

	var navigation = document.getElementById('navigation');
	
	// get all the child elements
	var topLevelNav = navigation.getElementsByTagName('ul')[0].childNodes;
	
	// create an array for the navigation items
	var navItems = new Array();
	
	// look for elements that are list items and add them to the array
	for (var i=0; i<topLevelNav.length; i++) {
		if (topLevelNav[i].nodeName == "LI") {
			navItems.push(topLevelNav[i]);
		}
	}
	
	// find the last item in the array...
	var lastItem = navItems[navItems.length-1];

	// ... and give it a class name
	addClass(lastItem, "last");
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function linkEntireDiv() {
	checkDOM();
	
	// create array and populate with featureinner classes
	var innerBoxes = new Array();
	if (!getElementsByClass('featureinner linkedbox')) return false;
	
	var innerBoxes = getElementsByClass('featureinner linkedbox');
	
	for (var i=0; i < innerBoxes.length; i++) {

		// find all links inside innerBoxes
		var links = innerBoxes[i].getElementsByTagName('A');
	
        if (links != null && links.length > 0 ) {
	
		    // grab the first link
		    var linkHref = links[0].getAttribute('href');
		
		    // apply the link to the enclosing div
		    innerBoxes[i].setAttribute('onclick', 'goToLink("' + linkHref + '")');
		    innerBoxes[i].style.cursor = "pointer";
        }
	};
}

function goToLink(href) {
	window.location = href;
}
	
	
	// Prototype function that checks that the DOM is ready, and triggers the following functions

	// document.observe('dom:loaded', function() {
	// 	resizeMenu();
	// 	addLastClass();
	// 	linkEntireDiv();
	// });

function initSIFR() {
	 if(typeof sIFR == "function" && sIFR.UA.bHasTransparencySupport){
		sIFR.replaceElement(named({sSelector:"#page-home .featurebox h3", sFlashSrc:"/export/sites/default/resources/sifr/agbookstencil.swf", sColor:"#FFFFFF", sBgColor:"#000000", nPaddingTop:3, nPaddingLeft:20, sWmode:"transparent", sCase:"upper",sFlashVars:"offsetTop=3"}));
		sIFR.replaceElement(named({sSelector:"#pageheading h2", sFlashSrc:"/export/sites/default/resources/sifr/agbookstencil.swf", sColor:"#FFFFFF", sBgColor:"#000000", nPaddingTop:16, nPaddingLeft:20, sWmode:"transparent", sCase:"upper",sFlashVars:"offsetTop=0"}));
	};
	 sIFR.removeDecoyClasses();
}

	
// addEvent(window, 'DOMContentLoaded', initScripts);

window.onload = function() {
	initScripts();
}

function initScripts() {
	resizeMenu();
	addLastClass();
	disableRightClick();
	linkEntireDiv();
	initSIFR();
}

function disableRightClick() {
  var isNS = (navigator.appName == "Netscape") ? 1 : 0;
  if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
  function mischandler(){
   return false;
 }
  function mousehandler(e){
  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
    if((eventbutton==2)||(eventbutton==3)) return false;
 }
 document.oncontextmenu = mischandler;
 document.onmousedown = mousehandler;
 document.onmouseup = mousehandler;
}
