addEvent(window, 'load', InnerPageHelper_init, false);

function InnerPageHelper_init(){
	
	////////////////
	// Activities //
	////////////////
	
	//// Find the activities list. Hide all second level items.
	//// Add event handler to open second level items on click.

	// Get array of uls in the Activity Holder div
	var uls = $('ActivityHolder').getElementsByTagName('ul');

	// Make sure there are actually some in there. It's possible to switch the activities
	// off in which case we may get javascript errors.
	if(uls.length > 0){
		// Take the first list item as the main activity list
		// Note that the uls array will contain ALL the lists in the Activity holder
		// not just the the top level one. We don't need any but the first.
		var myList = uls[0];
		
		// Get a list of the li elements in the main activity list and loop through them.
		var lis = myList.getElementsByTagName('li');
		for(var i = 0; i < lis.length; i++){
			
			// Check if this li contains a ul.
			var uls = lis[i].getElementsByTagName('ul');
			if(uls.length > 0){

				
				// Get the link for the main item
				var aList = lis[i].getElementsByTagName('a');
				
				var curLink = aList[0].href;

				// Change the href for the link (so it toggles the menu instead of going to the page)
				aList[0].setAttribute('href','javascript:void(0);');

				// Add the event handler
				addEvent(aList[0], 'click', InnerPageHelper_toggleActivityList, false);

				// If the current page is this entry OR a subpage of it, don't close it.
				var pattern = new RegExp("^" + curLink + "");

				if(pattern.test(document.location.href)){
					addClass(aList[0], 'Active');
				}else{
					// Hide the list
					uls[0].style.display = "none";
				}

			}
		} // end loop through lis
		
	} // end check uls.length

	// Highlight the current page	
	var aList = $('ActivityHolder').getElementsByTagName('a');
	for(var i = 0; i < aList.length; i++){
		if(aList[i].href == document.location.href){
			addClass(aList[i],'Active');
		}
	}


} // end InnerPageHelper_init

function InnerPageHelper_toggleActivityList(e){
	if(!e) var e = window.event;
	if(e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;

	//alert(targ.href);
	uls = targ.parentNode.getElementsByTagName('ul');
	if(uls.length > 0){

		if(uls[0].style.display == "block"){
			uls[0].style.display = "none";
			removeClass(targ, 'Active');
		}else{
			// Close all then open this one.
			InnerPageHelper_ActivityList_closeAll();
			uls[0].style.display = "block";
			addClass(targ, 'Active');
		}
	}
} // end InnerPageHelper_toggleActivityList

function InnerPageHelper_ActivityList_closeAll(){

	var uls = $('ActivityHolder').getElementsByTagName('ul');
	var myList = uls[0];
	var lis = myList.getElementsByTagName('li');
	for(var i = 0; i < lis.length; i++){
		var uls = lis[i].getElementsByTagName('ul');	
		if(uls.length > 0){
			removeClass(lis[i].firstChild, 'Active');
			uls[0].style.display = "none";
		}
	}
} // end InnerPageHelper_ActivityList_closeAll


function addClass(target, classValue){
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	if(!pattern.test(target.className)){
		if(target.className == ""){
		target.className = classValue;
		}else{
		target.className += " " + classValue;
		}
	}
	return true;
} // end addClass

function removeClass(target, classValue){
	var removedClass = target.className;
	var pattern = new RegExp("(^| )" + classValue + "( |$)");
	removedClass = removedClass.replace(pattern, "$1");
	removedClass = removedClass.replace(/ $/, "");
	target.className = removedClass;
	return true;
} // end removeClass