// Hide all the items past the maximum.  This is only meant to be called once (on loading of the document)
function initHideLists() {
	
	var maxListItems = 6;
	var content = document.getElementById("content");
	var lists = content.getElementsByTagName("ul");   // Get all the lists
	
	// Go through each list
	for(var i = 0; i < lists.length; i++){
		
		// Hide the extraneous entries in the current list
		var useText = "less...";
		var isShown = "1";
		if((new String(window.location)).indexOf(lists[i].getAttribute("id")) == -1) {
			hideList(lists[i], maxListItems);
			useText = "more...";
			isShown = "0";
		}
		
		// Create and add the Show/Hide link
		if(lists[i].getElementsByTagName("li").length > maxListItems){
			
			// Create the link and it's properties
			var listID     = lists[i].getAttribute("id");
			var toggleLink = document.createElement("a");
			toggleLink.setAttribute("href", 'javascript:toggleList("' + listID + '", "' + maxListItems + '")');
			toggleLink.setAttribute("id", listID + "-toggle");
			toggleLink.setAttribute("shown", isShown);  // Whether the list is visible or not.  Defaults to 0
			Lib.addClass(toggleLink, "toggle-link");
			
			// Create the link's text
			toggleLinkText = document.createTextNode(useText);
			toggleLink.appendChild(toggleLinkText);
			
			// Define the initial onclick behavior for the link
			lists[i].parentNode.insertBefore(toggleLink, lists[i].nextSibling);
		}
	}
	
}

// Shows/hides the list when clicked on, and updates the link text and attributes accordingly
function toggleList(listID, maxListItems) {
	
	// Get the list and it's link
	var list       = document.getElementById(listID);
	var toggleLink = document.getElementById(listID + "-toggle");
	
	if(toggleLink.getAttribute("shown") < 1){
		// If the list is currently hidden, show it, change the link to say "Hide",
		// and set the "shown" status to show that it's shown.
		showList(list, maxListItems);
		toggleLink.childNodes[0].nodeValue = "less...";
		toggleLink.setAttribute("shown", "1");
	}
	else{
		// If the list is currently shown, hide it, change the link to say "Show",
		// and set the "shown" status to show that it's hidden.
		hideList(list, maxListItems);
		toggleLink.childNodes[0].nodeValue = "more...";
		toggleLink.setAttribute("shown", "0");
	}
}

// Hide the extraneous list items in a specific list
function hideList(listObj, maxListItems) {
	
	// Get all the list items in the list
	var listItems = listObj.getElementsByTagName("li");
	
	// If the list has more items than allowed, start iterating through the list
	for(var i = 0; i < listItems.length; i++){
		if(i > (maxListItems - 1)){
			hideElement(listItems[i]);
		}
	}
}

// Show the extraneous list items in a specific list
function showList(listObj, maxListItems) {
	
	// Get all the lsit items
	var listItems = listObj.getElementsByTagName("li");
	
	// Iterate through the list items
	for(var i = 0; i < listItems.length; i++){
		showElement(listItems[i]);
	}
	
}

// Hide an element
function hideElement(element) {
	Lib.addClass(element, "hide");
}

// Un-hide an element
function showElement(element) {
	Lib.removeClass(element, "hide");
}

Lib.addEvent(window, "load", initHideLists);