/***********************************************
 Electric Farm ASA 2005
 Created: 17.3.2005 Tor Haugen
 Last modified: 17.3.2005 Tor Haugen
************************************************/

// The currently visible elements for each toggleGroup
var currentElements = new Array();

//toggle div display none/block
function toggleVisible(id, button, toggleGroup)
{
	var target = document.getElementById(id);
	var targetButton = button;
	var current = null;
	var currentButton = null;
	
	// Find the toggleButton for the element, if any
	var tbid = target.getAttribute("toggleButton");
	if (tbid != null && tbid.length > 0)
		targetButton = document.getElementById(tbid);
	
	// If toggleGroup specified, get the currently visible element in the group (if any)
	if (typeof(toggleGroup) == "string")
	{
		current = currentElements[toggleGroup];
		if (current != null)
		{
			currentButton = currentElements[toggleGroup + "_tb"];
		}
	}
	
	if (target.style.display == "none")
	{
		// If another element in the toggle group is visible, hide it
		if (current != null)
			current.style.display = "none";
		if (currentButton != null)
			removeClass(currentButton, "selected");
		
		// Show the element
		target.style.display = "block";
		if (targetButton != null)
			addClass(targetButton, "selected");
		
		// Set as current element in the toggle group
		if (typeof(toggleGroup) == "string")
		{
			currentElements[toggleGroup] = target;
			currentElements[toggleGroup + "_tb"] = targetButton;
		}
	}
	else
	{
		// Hide the element
		target.style.display = "none";
		if (targetButton != null)
			removeClass(targetButton, "selected");
		
		// Reset current element in the toggle group
		if (typeof(toggleGroup) == "string")
		{
			currentElements[toggleGroup] = null;
			currentElements[toggleGroup + "_tb"] = null;
		}
	}

	// Stop handling here
	if (typeof(event) != "undefined")
		event.cancelBubble = true;
}


// Adds a class to the specified element's class name
function addClass(elem, className)
{
	if (elem.className.length > 0)
		elem.className += " " + className;
    else
    	elem.className += className;
}

// Removes a class from the specified element's class name
function removeClass(elem, className)
{
	var pos = elem.className.lastIndexOf(className);
	if (pos > -1)
	{
		if (pos == 0)
			elem.className = elem.className.substr(className.length + 1);
		else
			elem.className = elem.className.substr(0, pos) + elem.className.substr(pos + className.length);
	}
}
