/***********************************************************************************
 * University of Wisconsin Platteville
 *
 * A container of element IDs that switches visibility between them.
 *
 * To use this class, you need to create the object, add items with the IDs of the
 * elements you wish to toggle between.  If the link that called the toggle method
 * has an id of link_(itemID), it will automatically be bolded when the item is
 * selected and normal when it is unselected.
 *
 * SAMPLE:
 * <script type="text/javascript">
 * var t = new Toggler();
 * t.addItem("test1");
 * t.addItem("test2");
 * t.addItem("test3");
 * </script>
 *
 * <a id="link_test1" href='javascript:t.toggle("test1")'>Test1</a><br />
 * <a id="link_test2" href='javascript:t.toggle("test2")'>Test2</a><br />
 * <a id="link_test3" href='javascript:t.toggle("test3")'>Test3</a><br />
 *
 * <div id="test1">Test 1 content</div><br />
 * <div id="test2">Test 2 content</div><br />
 * <div id="test3">Test 3 content</div>
 *
 * @author Adam Nowicki
 * @copyright 2005-2006 ResNet Development
 */

function Toggler()
{
	var EXCEPTION_INVALID_ITEM = "Toggler: Invalid Item";
	
	var link_prefix = "link";
	
	var default_item = null;
	
	var current_item = null;
	
	/**
	 * A vector containing the IDs of elements
	 */
	var items = new Vector();
	
	/**
	 * Add the element id to the item list.
	 *
	 * @param string Element ID
	 * @access public
	 * @author Adam Nowicki
	 */
	this.addItem = function(item)
	{
		items.push(item);
	}
	
	/**
	 * Remove the provided element id from the list
	 *
	 * @param string Element ID
	 * @access public
	 * @author Adam Nowicki
	 */
	this.removeItem = function(item)
	{
		items.remove( items.search(item) );
	}
	
	this.defaultItem = function(item)
	{
		default_item = item;
	}
	
	/**
	 * Toggles the provided element id visible and the rest invisible
	 *
	 * @param string Element ID
	 * @access public
	 * @author Adam Nowicki
	 */
	this.toggle = function(item, action)
	{
		var itemId = items.search(item);
		
		if ( itemId == -1 )
			throw EXCEPTION_INVALID_ITEM;
		
		for ( var i = 0; i < items.getSize(); i++ )
		{
			var curItem = items.get(i);
			
			if ( i == itemId )
			{
				if ( document.getElementById(curItem).style.display == "block" )
				{
					document.getElementById(curItem).style.display = "none";
					
					var link = document.getElementById(link_prefix+"_"+curItem);
					if ( link )
						link.style.fontWeight = "normal";
				}
				else
				{	
					var link = document.getElementById(link_prefix+"_"+curItem);
					if ( link )
						link.style.fontWeight = "bold";
					
					var element = document.getElementById(curItem);
					Toggler.fading_item = element;
					Toggler.fading_increment = 5;
					Toggler.fading_delay = 2;
					if ( isIE() )
						element.style.filter = "alpha(opacity=25)";
					else
						element.style.opacity = .25;
					element.style.display = "block";
					
					setTimeout(Toggler.fadeIn, Toggler.fading_delay);
					
					if ( isFunction(action) )
						action();
				}
			}
			else
			{
				document.getElementById(curItem).style.display = "none";
				
				var link = document.getElementById(link_prefix+"_"+curItem);
				if ( link )
					link.style.fontWeight = "normal";
			}
		}
		
		// Check to see if the user is toggling off the currently displayed item
		if ( current_item == item && default_item != null && item != default_item )
			this.toggle(default_item);	
		else
			current_item = item;
	}
	
	/**
	 * Toggles the styles of the items in this class. The item specified gets the
	 * its style.property set to mainStyle while all other items style is
	 * changed to otherStyle.
	 */
	this.toggleStyle = function(item, property, mainStyle, otherStyle)
	{
		var itemId = items.search(item);
		
		if ( itemId == -1 )
			throw EXCEPTION_INVALID_ITEM;
		
		for ( var i = 0; i < items.getSize(); i++ )
		{
			var curItem = items.get(i);
			
			if ( i == itemId )
			{	
				var element = document.getElementById(item);
				if ( element )
					eval("element.style."+property+" = '"+mainStyle+"'");
			}
			else
			{
				var element = document.getElementById(curItem);
				if ( element )
					eval("element.style."+property+" = '"+otherStyle+"'");
			}
		}
	}
	
	/**
	 * Clears all of the element IDs.
	 *
	 * @access public
	 * @author Adam Nowicki
	 */
	this.clear = function()
	{
		items.clear();
	}
}

Toggler.fadeIn = function()
{
	var element = Toggler.fading_item;
	var value = element.style.opacity * 100 + Toggler.fading_increment;
	if ( isIE() )
	{
		element.style.filter = "alpha(opacity="+value+")";
	}
	else
	{
		element.style.opacity = value / 100;
	}
	if ( value <= 100 )
		setTimeout(Toggler.fadeIn, Toggler.fading_delay);
	else
		return;
}