//The code assumes that the number of links and the number of content boxes are the same

//window.onload = load;
var myAccordion;
	
window.onload = function(){ //safari cannot get style if window isnt fully loaded
		
    var stretchers = $$('div.contentarea');
	stretchers.each(function(item){
		item.setStyles({'height': '0', 'overflow': 'hidden'});
	});
	var togglers = $$('li.toggler');
	
	
	togglers.each(function(toggler, i){
		//toggler.defaultColor = toggler.getStyle('background-color');
	});

	myAccordion = new Fx.Accordion(togglers, stretchers, { opacity: true, start: false, wait: false, transition: Fx.Transitions.quadOut });
	
	//anchors
	function checkHash(){
		var found = false;
		$$('li.toggler a').each(function(link, i){
			if (window.location.hash.test(link.hash)){
				myAccordion.showThisHideOpen(i);
				found = true;
			}
		});
		return found;
	}

	myAccordion.showThisHideOpen(0);
		
};
	
try {
	Window.disableImageCache();
}catch(e){}

var navTimer;
var currentIndex = 0;
function AccordionDisplay(i)
{
    clearTimeout(navTimer);
    currentIndex = i;
    navTimer = setTimeout('doTimer()', 300);

}

function doTimer(i)
{
    myAccordion.display(currentIndex)
}

function clearTimer()
{
    clearTimeout(navTimer);
}

var currentLink = ""; //current menu item id
var links = new Array(); //menu item ids
var currentEffect;



//setup event handlers and ids
function load() {
	var i, j = 0;

	//setup event handlers for all menu items
	for(i = 0; i < document.getElementById("nav").childNodes.length; i++) {
		if(document.getElementById("nav").childNodes[i].nodeType == 1) {
			links[links.length] = document.getElementById("nav").childNodes[i].id;
			document.getElementById("nav").childNodes[i].onmouseover = changeContent;
		}
	}

	//give content divs ids with "Content" appended to the end of the menu item's id
	for(i = 0; i < document.getElementById("changingContent").childNodes.length; i++) {
		if(document.getElementById("changingContent").childNodes[i].nodeType == 1) {
			document.getElementById("changingContent").childNodes[i].id = links[j] + "Content";
			j++;
		}
	}
	
	
	/*
	var contentBlocks =  $$('div.homepageContent');
    alert(typeof(contentBlocks));
	contentBlocks.each(function(item){
		alert(typeof(item));
		item.setStyles({'opacity': '0', 'overflow': 'hidden'});
	});*/
	//update content
	changeContent("");
}

//update content
function changeContent(e) {
	//first load
	if(currentLink == "") {
		currentLink = links[0];
		//document.getElementById(links[0] + "Content").style.display = "block";
		currentEffect = new Fx.Style(links[0] + "Content", 'height', {duration: 500});
        currentEffect.start(0,380); //will close the element if open, and vice-versa.
		//highlightItem(links[0], true);
	}
	//subsequent loads
	else {
	    var oldEffect = currentEffect;
		oldEffect.hide();
		if(window.event) { //if ie
			e = window.event;
		}
		
		var srcEl = e.srcElement ? e.srcElement : e.target; //accomadate for ie and w3c
		
		//if an anchor tag is used in the code, go up one level to the li element to get the id
		if(srcEl.id == "") {
			if(e.srcElement) {
				srcEl = e.srcElement.parentNode;
			}
			else if(e.target) {
				srcEl = e.target.parentNode;
			}
		}
		
		
		currentEffect = new Fx.Style(srcEl.id + "Content", 'height', {duration: 500});
        currentEffect.start(0,380); //will close the element if open, and vice-versa.
		
		//hide previous content
		//document.getElementById(currentLink + "Content").style.display = "none";
		//highlightItem(currentLink, false);
		//show selected content
		//document.getElementById(srcEl.id + "Content").style.display = "block";
		//highlightItem(srcEl.id, true);
		
		currentLink = srcEl.id;
	}
}

//change css class of menu items
function highlightItem(id, on) {
	if(on) {
		document.getElementById(id).className = "on";
	}
	else {
		document.getElementById(id).className = "off";
	}
}