/*
 * Div Group: manages the visibility of group of div objects
 * $Id: divgroup.js,v 1.21 2003/10/24 04:19:43 scott Exp $
 * Copyright (C) 2001-2003 Scott Martin (scott@coffeeblack.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, it is available from the Free Software
 * Foundation, Inc. at http://www.gnu.org/copyleft/lesser.html or in writing at
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
 * Creates a new div group to manage a set of Div objects. The visibilities of
 * the Div objects managed by a div group are mutually exclusive, that is,
 * only one can be visible at any time. Optionally, a default div can be
 * specified that will be shown when no other div object managed by this div
 * group is visible.
 * 
 * Parameters:
 *	aryDivNames	- An array of strings representing the IDs of the divs
 * 		in the group.
 *	strDefaultName	- (optional) string representing the ID of the default div
 *
 * dependencies: Div, Map
 */ 
function DivGroup(aryDivNames, strDefaultName)
{	this.divs = null;
	this.currentDiv = null;

	if(aryDivNames && aryDivNames != null && aryDivNames.length > 0)
		for(var a = 0; a < aryDivNames.length; a++)
			this.addDiv(new Div(aryDivNames[a]));

	this.defaultDiv = this.getDiv(strDefaultName);
	this.hideAll();

	return this;
}

/*
 * Gets this div group's default div as an object of type Div,
 * or null if none is defined.
 */
DivGroup.prototype.getDefault = function()
{	return this.defaultDiv;
}

/*
 * Gets this div currently being shown by this div group, or null
 * if all are hidden.
 */
DivGroup.prototype.getCurrent = function()
{	return this.currentDiv;
}

/*
 * Sets this div group's default div. The default div will be shown
 * when no other div in this group is visible.
 */
DivGroup.prototype.setDefault = function(strDivId)
{	with(this)
	{	defaultDiv = getDiv(strDivId);
		if(defaultDiv != null && currentDiv == null)
			show(defaultDiv.id);
	}
}

/*
 * Adds a div object to the set managed by this div group.
 * If a div with the same ID as the one to be added is already
 * contained, it is replaced.
 */
DivGroup.prototype.addDiv = function(div)
{	with(this)	
	{	if(!divs || divs == null)
			divs = new Map();

		if(div && div != null)		
			divs.put(div.id, div);
	}
}

/*
 * Gets the div named strDivId, or null if no such div object
 * is being managed by this group.
 */
DivGroup.prototype.getDiv = function(strDivId)
{	with(this)
	{	return(strDivId && strDivId != null && divs != null)
			? divs.get(strDivId) : null;
	}
}

/*
 * Gets the number of divs managed by this div group
 */
DivGroup.prototype.countDivs = function()
{	with(this)
	{	return (divs == null) ? 0 : divs.size();
	}
}

/*
 * Tests whether this div group contains a div object named strDivId.
 */
DivGroup.prototype.containsDiv = function(strDivId)
{	with(this)
	{	return (strDivId && strDivId != null && divs != null
			&& divs.containsKey(strDivId));
	}
}

/*
 * Shows the div object with the ID supplied, if it is being managed by
 * this group. Upon showing the div in question, all others are hidden.
 */
DivGroup.prototype.show = function(divName)
{	with(this)	
	{	if(containsDiv(divName))
		{	if(defaultDiv != null) defaultDiv.setVisible(false);
			if(currentDiv != null) currentDiv.setVisible(false);
			currentDiv = getDiv(divName);
			currentDiv.setVisible(true);
		}
	}
}

/*
 * Hides the div object with the ID supplied, if it is being managed by
 * this group.
 */
DivGroup.prototype.hide = function(divName)
{	with(this)	
	{	if(containsDiv(divName))
		{	var div = getDiv(divName);
			div.setVisible(false);
			if(currentDiv != null && div.id == currentDiv.id)
				currentDiv = null;
		}
	}
}

/*
 * Hides all div objects managed by this group. If a default was defined,
 * it is shown.
 */
DivGroup.prototype.hideAll = function()
{	with(this)
	{	if(divs != null)
		{	if(currentDiv != null)
				currentDiv.setVisible(false);
			else
			{	var values = divs.values();
				for(var i = 0; i < values.length; i++)
					values[i].setVisible(false);
			}

			if(defaultDiv != null) 
			{	defaultDiv.setVisible(true);
				currentDiv = defaultDiv;
			}
			else currentDiv = null;
		}
	}
}	

/*
 * Gets a string representation of this div group.
 */
DivGroup.prototype.toString = function()
{	return "[object DivGroup]";
}
