/*
This code is based on a code example from the article "Javascript navigation - cleaner, not meaner" by Christian Heilmann
URL: http://www.evolt.org/article/Javascript_navigation_cleaner_not_meaner/17/60273/index.html
*/

// If there is enough W3C DOM support for all our show/hide behavior:
// 1. Call the stylesheet that by default hides all toggleable sections 
// 2. Apply the show/hide behavior by calling the initialization function


if(typeof GO == "undefined") var GO = new Object();

GO.ShowHide = function(prefix, setItemActiveClass, setParentActiveClass) {

	/////////////////////////////////////////////////////////////////////////////////
	// Private Members
	/////////////////////////////////////////////////////////////////////////////////
	var that = this;
	var p = prefix;
	var toggle = document.getElementById(p + '-toggle');
	var toggleable = document.getElementById(p + '-toggleable');

	/////////////////////////////////////////////////////////////////////////////////
	// Private Methods
	/////////////////////////////////////////////////////////////////////////////////
	function init() {
		// Hide all toggleable sections with JavaScript for the highly improbable case that CSS is disabled
		// Note that in this case the 'flash of visible content' still will occur
		// For testing purposes you can add the following code to disable CSS: document.getElementsByTagName('link')[0].disabled = true;			
		if(!toggle) return;
		var as = toggle.getElementsByTagName('a');
		for (var i = 0; i < as.length; i++) {
			if(typeof EventManager != 'undefined') {
				EventManager.Add(as[i], 'click', that.show, false);
			}
			else {
				//as[i].onclick = function() { that.show(this); return false; }
			}
		}
	};

	function setActiveClass(s) {
		if(!setItemActiveClass && !setParentActiveClass) {return;}
		var item, parent;
		var toggle = document.getElementById(prefix + '-toggle');
		if(!toggle) return;
		var as = toggle.getElementsByTagName('a');
		for (var i = 0; i < as.length; i++) {
			item = as[i];
			
			if(setItemActiveClass && item.className.match(/\bactive\b/)) {item.className = item.className.replace(/(^|\s)active\b/,'');}
			if(setParentActiveClass) {
				parent = item.parentNode;
				if(parent.className.match(/\bactive\b/)) {parent.className = parent.className.replace(/(^|\s)active\b/,'');}
			}
		}
		item = s
		if(setItemActiveClass) {item.className += (item.className ? ' ' : '') + 'active';}
		if(setParentActiveClass) {
			parent = item.parentNode;
			parent.className += (parent.className ? ' ' : '') + 'active';
		}
	}

	/////////////////////////////////////////////////////////////////////////////////
	// Public Methods
	/////////////////////////////////////////////////////////////////////////////////
	this.show = function(evt) {
		evt = (evt) ? evt : ((window.event) ? window.event : null);
		var s = evt.target ? evt.target : evt.srcElement;
		that.hideAll();
		var id = s.href.match(/#(\w.+)/)[1];
		document.getElementById(id).style.display = 'block';
		setActiveClass(s);
		evt.returnValue = false;
		if(typeof evt.preventDefault != 'undefined') {evt.preventDefault();}
	};

	this.hideAll = function() {
		if(!toggleable) return;
		var divs = toggleable.getElementsByTagName('div')
		for (var i = 0; i < divs.length; i++) {
			if(divs[i].className.match(/(^|\s)toggleable(\s|$)/)) {divs[i].style.display = 'none';}
		}
	};

	init();
	return this;
};

function survey_showhide(open, hide){
//	alert(open);
//	alert(hide);
	document.getElementById(open).style.display = 'block';
	document.getElementById(hide).style.display = 'none';
}
