/* JS controlling the submenu */

// helper functions
getElById = function(id)	{ return document.getElementById(id); }
submenuItems = function()	{ return getElById('submenu').getElementsByTagName('li'); }
String.prototype.trim = function() { return this.replace(/^\s*|\s*$/g,''); }


function setupMenu() {
	var menu = submenuItems();
	
	for (var i in menu)
		menu[i].onclick = function() {
			var menuItems = submenuItems();

			for (var i=0; i < menuItems.length; i++) {
				// hide all content sections
				var id = 'content-' + menuItems[i].innerHTML.trim().toLowerCase()
				getElById(id).style.display = 'none';
				
				// apply unselected styling to all submenu boxes
				menuItems[i].style.paddingBottom = '10px';
				menuItems[i].style.borderBottom = '1px solid #cecece';
				menuItems[i].style.backgroundColor = '#f1f1f1';
			}			

			// Hack to get rid of the photo sidebar when viewing flickr photos
			try {
				var pix = getElById('pix');
				if (this.innerHTML.trim() == 'Photos')
					pix.style.display = 'none';
				else
					pix.style.display = 'block';
			} catch(e) { }

			
			// show requested content section
			var id = 'content-' + this.innerHTML.toLowerCase().trim();
			getElById(id).style.display = 'block';
			
			// set style of selected submenu box
			this.style.paddingBottom = '11px';
			this.style.borderBottom = 'none';
			this.style.backgroundColor = '#ffffff';
			
			window.location.hash = '#' + this.innerHTML.toLowerCase().trim();
		}

		// Move to the right subsection of the page.
		var section = window.location.hash.replace('#', '');
		section = section.substr(0, 1).toUpperCase() + section.substr(1);
		if (section.length > 1)
			$('#submenu li:contains(' + section + ')').click();
}