/*
	Expandable Item

	last updated 2010-03-11
*/

function ExpandClass() {

	this.lastShownExpandableItemIDNames = [];
	
	// expand/collapse a section and adjust the twistie image accordingly
	this.toggleSectionAndTwistie = function(sectionID, twistieID, twistieImgSrcCollapsed, twistieImgSrcExpanded)
	{
	    if (Browser.getStyleProperty(sectionID, "display")=="none")
	    {
	        Browser.displayElement(sectionID, "block");
	        Browser.getObject(twistieID).src=twistieImgSrcExpanded;
	    }
	    else
	    {
	        Browser.displayElement(sectionID, "none");
	        Browser.getObject(twistieID).src=twistieImgSrcCollapsed;
	    }
	}
	
	// expand the given item, collapsing the previously-expanded item in the same group
	this.expandItem = function(groupName,itemIDSuffix,doNotCollapseShown) {
		var lastShownExpandableItemIDName = this.lastShownExpandableItemIDNames[groupName];
		if (lastShownExpandableItemIDName!=groupName+itemIDSuffix) {
			// collapse current item, expand new item,
			if (lastShownExpandableItemIDName) Browser.setStyleProperty(lastShownExpandableItemIDName,"display","none");
			lastShownExpandableItemIDName = groupName+itemIDSuffix;
			Browser.setStyleProperty(lastShownExpandableItemIDName,"display","block");
			this.lastShownExpandableItemIDNames[groupName] = lastShownExpandableItemIDName;
		} else {
			// collapse or expand the item that was previously expanded or collapsed
			// i.e. the user has clicked on the same item twice in a row
			var currentState = Browser.getStyleProperty(lastShownExpandableItemIDName,"display");
			if (currentState==null || currentState=="none") {
				Browser.setStyleProperty(lastShownExpandableItemIDName,"display","block");
			} else if (!doNotCollapseShown) {
				Browser.setStyleProperty(lastShownExpandableItemIDName,"display","none");
			}
		}
	}
	
	// mark this item has started in a expanded state
	this.itemStartsExpanded = function(groupName,itemIDSuffix) {
		var expandableItemIDName = groupName+itemIDSuffix;
		Browser.setStyleProperty(expandableItemIDName,"display","block");
		this.lastShownExpandableItemIDNames[groupName] = expandableItemIDName;
	}
	
	// collapse all items in a group
	this.collapseItems = function(groupName) {
		var lastShownExpandableItemIDName = this.lastShownExpandableItemIDNames[groupName];
		if (lastShownExpandableItemIDName) {
			Browser.setStyleProperty(lastShownExpandableItemIDName,"display","none");
			this.lastShownExpandableItemIDNames[groupName] = null;
		}
	}
	
	return(this);
}

var Expand = new ExpandClass();

