var expanded_marker_img_src = '../images/navigation_images/marker_expanded.png';
var collapsed_marker_img_src = '../images/navigation_images/marker_collapsed.png';

Event.observe(window, 'load', function() {
	var sections = $$('div.vmux_section');
	var targetLoc = getPageTargetLoc();
	var targetLocAvailable = false;
	var targetSection;
	
	// Set up the sections to accept user actions 
	sections.each(function(section) {		
		Event.observe(section, 'mouseover', cursorChange);
		Event.observe(section, 'mouseout', cursorChange);
		
		if (section.id == targetLoc) {
			targetLocAvailable = true;
			targetSection = section;
		}
		
		var collapsable = {
			imgElem:section.down('img'),
			collapsableElem:section.next()
		};
		section.collapsable = collapsable;
		collapsable.collapsableElem.hide();
		collapsable.collapsableElem.removeClassName('hidden');
		Event.observe(section,'click',toggleBlock);
	});
	
	// finally make the page scroll to the target location, if specified, and 
	// expand that section
	if (targetLocAvailable) {
		// don't need to do the scroll, as both IE and Firefox seem
		// to understand to look for and automatically scroll to the div
		// with the corresponding id 
		//$(targetSection).scrollTop = $(targetSection).offSetTop;
		targetSection.collapsable.collapsableElem.toggle();
		toggleMarker(targetSection.collapsable.collapsableElem,targetSection.collapsable.imgElem);
	}
	
	
});


function toggleBlock(event) {
	this.collapsable.collapsableElem.toggle();
	toggleMarker(this.collapsable.collapsableElem, this.collapsable.imgElem);
	
}

function cursorChange(event) {
	if (event.type == "mouseover") {
		event.element().setStyle({cursor:"pointer"});
	} else {
		event.element().setStyle({cursor:"auto"});
	}
}

function toggleMarker(blockElement, marker) {
			isVisible = blockElement.visible();
			if (isVisible) {
				marker.src = expanded_marker_img_src;
			} else {
				marker.src = collapsed_marker_img_src;
			}
}

function getPageTargetLoc() {
	var hashLocation = window.location.hash.substring(1);
	return hashLocation;
}