// Expander Class
// Used to make one HTML element (e.g., a <legend>) expand/contract (show/hide) another (e.g., a <div>)
// expander_ref - id name of the element that will handle the onclick event which triggers the toggle
// expandee_ref - id name of the element that will be shown/hiddne
// persist - bool - if true, the expander will use cookies to store the expanded/contracted state
//					(and will automatically restore the cookie and the expandee's state when the expander is instantiated)
// retract_now - bool - if true, the expandee_ref element will be contracted (overridden by cookies)
function Expander(expander_ref, expandee_ref, persist, retract_now)
{
	// Do the constructor-like things
	var expander = getElement(expander_ref);
	var expandee = getElement(expandee_ref);
	
	// Preserve the original contents of the retractor
	expander.orig_text = expander.innerHTML;	// Preserve the original contents of the retractor
	expander.expandee = expandee;			// Give the retractor a reference to what it's expanding
	expander.persist = (persist) ? persist : false; // Tell it to use cookies or not
	
	// Put the [-] image in the expander by default
	expander.innerHTML = "<img src='images/buttons/minus.gif' alt='' border='0' /> " + expander.orig_text;
	
	// Give the retractor some totally sweet functions
	expander.onclick = Toggle;
	expander.Expand = Expand;
	expander.Contract = Contract;
	
	// If the user wants to persist the state with cookies...
	if(persist)
	{
		// Create the new cookie object
		expander.cookie = new Cookie(document, "expander_" + expander_ref);
		expander.cookie.load(); // Load it
		
		// Test to see if the cookie says this expandee should be expanded
		// Only match true and false values so as to avoid overriding the initial 'persist' parameter the first time
		// this object is created
		if(expander.cookie.expanded == "true")
			expander.Expand();
		else if(expander.cookie.expanded == "false" || retract_now)
			expander.Contract();
	}
	else
	{
		// Finally, if the user wants to contract this right away, do it
		if(retract_now)
			expander.Contract();
		else
			expander.Expand();
	}
			
	// Basic onclick function - will show the retractee div if it's hidden, will hide it if it's show
	// All this (and more) is done by subsequent calls to Expand and Contract
	function Toggle()
	{
		var current_state = (document.getElementById) ? this.expandee.style.display : this.expandee.display;
		
		// Show it if it's hidden
		if(current_state == 'none')
			this.Expand();
		else // Hide it if it's shown
			this.Contract();
	}
	
	// Expands (shows) the retractee div. Also puts a spiffy [+] gif before the retractor's text
	function Expand()
	{
		this.innerHTML = '<img src="images/buttons/minus.gif" border="0" alt="" /> ' + this.orig_text;
		
		if(this.persist)
		{
			this.cookie.expanded = true;
			this.cookie.store();
		}
			
		if(document.getElementById)
			this.expandee.style.display = "block";
		else
			this.expandee.display = "block";
	}
	
	// Contracts (hides) a retractee div. Also puts a [-] gif before the retractor's text
	function Contract()
	{
		this.innerHTML = '<img src="images/buttons/plus.gif" border="0" alt="" /> ' + this.orig_text;
		
		if(this.persist)
		{
			this.cookie.expanded = false;
			this.cookie.store();
		}

		if(document.getElementById)
			this.expandee.style.display = "none";
		else
			this.expandee.display = "none";
	}
	
	// Retrieves an HTMLElement reference given an id
	function getElement(id)
	{
		if(document.getElementById)
			return document.getElementById(id);
		return document.layers[id];
	}
}