window.addEvent('domready', function() {
	var preload = new Array();
	var imgs = new Array('styles/images/bg-subnav-li-current.gif', 'images/arrow-up.gif', 'arrow-to-up.gif', 'arrow-down.gif', 'arrow-to-down.gif');
	var i = 0;
	while (imgs[i]) {
		preload[i] = new Image();
		preload[i].src = imgs[i];
		i++;
	}
	
	var browser=navigator.appName;
	var externalFiles = new Array('doc', 'pdf', 'jpg');
	var basePages = new Array('news', 'events');
	var myANCHORS = document.getElementsByTagName('a');
	var baseHREF = document.getElementsByTagName('base')[0].href;
	var current = null;
	
	for (var i=0;i<myANCHORS.length;i++) {
	   if (myANCHORS[i].className == '') {		//Only add a class if there isn't already a class
	   		var path = location.pathname;
		   	if (myANCHORS[i].getAttribute('href') == path || ('/'+myANCHORS[i].getAttribute('href')) == path || myANCHORS[i].getAttribute('href') == location.href) {
			   myANCHORS[i].className = 'current';	//Add selected class to anchors going to the current page
			   current = myANCHORS[i].parentNode.parentNode.parentNode.childNodes[0].nodeValue;
			   
		   	}
			k=0;
			while (externalFiles[k]) {
				if (myANCHORS[i].getAttribute('href').slice(-3) == externalFiles[k] || (myANCHORS[i].getAttribute('href').substr(0,4) == 'http' && myANCHORS[i].getAttribute('href').indexOf(baseHREF)<0)) {	
					myANCHORS[i].target = '_blank';
				}
				k++;
			}
	   }
	}
	
	var list = $$('li.main');
	var i=0;
	list.each(function(el) { 
		var im = new Image();
		var old = el.childNodes[0];
		old.nodeValue;
		if (current == old.nodeValue) current = i;  //This is setting the current open accordion.  We are getting the current parent (li.main) from the anchor function above.
		el.insertBefore(im, old);
		im.src = '/images/arrow-up.gif';
		i++;
	});
	
	if (list[current]) list[current].childNodes[0].src = '/images/arrow-down.gif';
	if (current == null) current = -1;
	
	var accordion = new Accordion('li.main', 'ul.sub', { show:current, opacity: false, alwaysHide:false, onActive: function(toggler, element){
			toggler.setStyle('background', '#fff url(styles/images/bg-subnav-li-current.gif) repeat-x top left;');
			toggler.childNodes[2].setStyle('border-top', 'solid 1px #ccc');
			var n = toggler.childNodes[0].src;
			var p = n.slice(n.lastIndexOf('/')+1, n.length);
			if (p == 'arrow-up.gif' || p == 'arrow-to-up.gif') toggler.childNodes[0].src = '/images/arrow-down.gif';
		},
 
		onBackground: function(toggler, element){
			toggler.setStyle('background', '#fff url(styles/images/bg-subnav-li.gif) repeat-x top left;');
			toggler.childNodes[2].setStyle('border-top', 'none');
			var n = toggler.childNodes[0].src;
			var p = n.slice(n.lastIndexOf('/')+1, n.length);
			if (p == 'arrow-down.gif' || p == 'arrow-to-down.gif') toggler.childNodes[0].src = '/images/arrow-up.gif';
		}
	});
});