/*
 * Div Group: manages the visibility of group of div objects
 * Copyright (C) 2001 Scott Martin (scott@coffeeblack.org)
 * $Revision: 1.13 $
 *
 * 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 to be managed.
 *	strDefaultName	- (optional) string representing the ID of the default div
 */ 
function DivGroup(aryDivNames, strDefaultName)
{	this.divs = null;
	this.currentDiv = null;
		
	if(aryDivNames && aryDivNames != null && aryDivNames.length > 0)
		for(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)
{	this.defaultDiv = this.getDiv(strDivId);
	if(this.defaultDiv != null && this.currentDiv == null)
		this.show(this.defaultDiv.getId());
}

/*
 * Adds a div object to the set managed by this div group.
 */
DivGroup.prototype.addDiv = function(div)
{	if(!this.divs || this.divs == null)
		this.divs = new Array();
			
	if(div && div != null)		
	{	this.divs[this.divs.length] = div;
		eval("this.divs." + div.getId() + " = div;");
	}
}

/*
 * Gets the div named strDivId, or null if no such div object
 * is being managed by this group.
 */
DivGroup.prototype.getDiv = function(strDivId)
{	return (strDivId && strDivId != null && this.divs != null)
		? this.divs[strDivId] : null;
}

/*
 * Tests whether this div group contains a div object named strDivId.
 */
DivGroup.prototype.containsDiv = function(strDivId)
{	return (strDivId && strDivId != null && this.divs != null)
		? (this.divs[strDivId] && this.divs[strDivId] != null) : false;
}

/*
 * Gets the number of divs managed by this div group
 */
DivGroup.prototype.countDivs = function()
{	return (this.divs != null) ? this.divs.length : 0;
}

/*
 * Gets the div at the supplied index, or null if the index is out of bounds.
 */
DivGroup.prototype.divAt = function(index)
{	return (this.divs != null && index && !isNaN(index) && (index == 0 || (index > 0 && index < this.divs.length)))
		? this.divs[index] : null;
}

/*
 * Gets the array index of the div with the supplied name, or -1 if
 * the div is not managed by this div group.
 */
DivGroup.prototype.indexOf = function(strDivId)
{	if(this.divs != null && strDivId && strDivId != null)
	{	for(var j = 0; j < this.divs.length; j++)
			if(this.divs[j].getId() == strDivId)
				return j;
	}
	
	return -1;
}

/*
 * 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)
{	if(this.containsDiv(divName))
	{	if(this.defaultDiv != null) this.defaultDiv.setVisible(false);
		if(this.currentDiv != null) this.currentDiv.setVisible(false);
		this.currentDiv = this.getDiv(divName);
		this.currentDiv.setVisible(true);
	}
}

/*
 * Hides the div object with the ID supplied, if it is being managed by
 * this group.
 */
DivGroup.prototype.hide = function(divName)
{	if(this.containsDiv(divName))
	{	div = this.getDiv(divName);
		div.setVisible(false);
		if(this.currentDiv != null && div.getId() == this.currentDiv.getId())
			this.currentDiv = null;
	}
}

/*
 * Hides all div objects managed by this group. If a default was defined,
 * it is shown.
 */
DivGroup.prototype.hideAll = function()
{	if(this.divs != null)	
	{	if(this.currentDiv != null)
			this.currentDiv.setVisible(false);					
		else
		{	for(var a = 0; a < this.divs.length; a++)
				this.divs[a].setVisible(false);
		}
		
		if(this.defaultDiv != null) 
		{	this.defaultDiv.setVisible(true);
			this.currentDiv = this.defaultDiv;
		}
		else this.currentDiv = null;
	}
}	

/*
 * Gets a string representation of this div group.
 */
DivGroup.prototype.toString = function()
{	return "[object DivGroup {divs: " + this.divs.toString() + ", defaultDiv: " + 
		((this.defaultDiv != null) ? this.defaultDiv.toString() : "null") + ", currentDiv: " +
			((this.currentDiv != null) ? this.currentDiv.toString() : "null") + "}]";
}