//On page load begin headline scrolling
Event.observe(window, 'load', function() {
	initSideMenu();
}); 



/*initializeSideMenu: zips up side menu nested unordered lists 
	1. Select the nested unordored lists within the container
	2. Iterate over each unordered list
		a. add expansion button
		b. hide the list
*/
function initSideMenu() {
	var nestedLists = $$('#leftnavcontainer ul ul');
	nestedLists.each(function(list) {
		list.up().insert({top:"<span onclick=\"expandSideMenuItem('"+list.identify()+"')\"><img src=\"/images/button-nav-plus-sm.png\" alt=\"+\" width=\"15\" height=\"15\" \/></span>"});
		list.hide();
	});
	
	//locate the current page so we can highlight that!
	var pageLinks = $$('#leftnavcontainer ul a');
	pageLinks.each(function (link){
		if(location.href.toLowerCase().include(link.readAttribute("href").toLowerCase())){
			link.addClassName('selected');
			linkAncestors = link.ancestors();
			linkAncestors.each(function (ancestor){
				childElements = ancestor.childElements();
				if(childElements.indexOf(ancestor.down('span',0)) > -1 && childElements.indexOf(ancestor.down('ul',0))){
					ancestor.down('span',0).replace("<span onclick=\"contractSideMenuItem('"+ancestor.down('ul',0).identify()+"')\"><img src=\"/images/button-nav-minus-sm.png\" alt=\"+\" width=\"15\" height=\"15\" \/></span>");
					ancestor.down('ul',0).show();
				}
			});
		}
	});
}

/*
expandSideMenuItem: expand the menu and changes the image to allow for contracting the menu
	1. select the list
	2. show the list 
	3. change the image

*/	
function expandSideMenuItem(id) {
	var queue = Effect.Queues.get(id);
	//If there is nothing in the queue we can do our function
	if(queue.size() == 0){ 
		Effect.BlindDown(id, { duration: 0.2, queue: { limit: 1, scope: id} });
		$(id).previous("span").replace("<span onclick=\"contractSideMenuItem('"+id+"')\"><img src=\"/images/button-nav-minus-sm.png\" alt=\"+\" width=\"15\" height=\"15\" \/></span>");
	}
}
	
/*
contractSideMenuItem: contract the menu and changes the image to allow for expanding the menu
	1. select the list
	2. hide the list 
	3. change the image

*/	
function contractSideMenuItem(id) {
	var queue = Effect.Queues.get(id);
	//If there is nothing in the queue we can do our function
	if(queue.size() == 0){ 
	     Effect.BlindUp(id, { duration: 0.2,  queue: { limit: 1, scope: id} });
	 	$(id).previous("span").replace("<span onclick=\"expandSideMenuItem('"+id+"')\"><img src=\"/images/button-nav-plus-sm.png\" alt=\"+\" width=\"15\" height=\"15\" \/></span>");
	}
}

function debugNav(){
	$$('.debuggingInfo').each(function(item){
		 item.show(); 
	 });
}
