/** 
 * NavItem class
 * @author Craig Barber
 * This class encapsulates the functionality of collapsible nav items.
 */

/**
 * This constructs a new NavItem object. NOTE: The img and div element being specified must already exist, as this constructor could modify them depending upon
 * the values passed as parameters.
 * @param boolean isToggledOn Whether or not the nav item is toggled on by default. If specified as false, this function will auto-matically toggle the nav item to the off state.
 * @param string divId The id of the div this nav item corresponds to.
 * @param string titleImgName The name of the title image for this name item.
 * @param string onTitleImgSrc The src of the img this nav item uses for it's on state.
 * @param string offTitleImgSrc The src of the img this nav item uses for it's off state.
 */
function NavItem( isToggledOn, divId, titleImgName, onTitleImgSrc, offTitleImgSrc )
{
	this.isToggledOn = isToggledOn;
	this.divId = divId;
	this.titleImgName = titleImgName;
	this.onTitleImgSrc = onTitleImgSrc;
	this.offTitleImgSrc = offTitleImgSrc;
	// if the user specified an off toggle state by default, toggle the nav item off
	if ( !isToggledOn )
		this.toggle( false );
}

/**
 * This function toggles the state of the nav item. If the nav item is currently on, it
 * will hide it's corresponding div, and switch it's image to the off title image, otherwise
 * it will reveal it's div and switch it's image to the on title image.
 * @param boolean isToggleOn (optional) If specified, this function will ignore the current state of the nav item and force a toggle on or off depending upon the
 * boolean state of this param.
 * @return void
 */
NavItem.prototype.toggle = function( isToggleOn )
{
	// retrieve the corresponding div
	var div = document.getElementById( this.divId );
	// if the div could not be retrieved throw an exception
	if( div == null )
		throw "Failed to retrieve nav item's div, id: " + this.divId;
	// if the isToggleOn param is specified as null or if this nav item is currently switched off, hide it's div and switch it's image to the off title image
	if ( ( isToggleOn != null && !isToggleOn ) || ( isToggleOn == null && this.isToggledOn ) )
	{
		div.style.display = 'none';
		document.images[this.titleImgName].src = this.offTitleImgSrc;
	}
	// otherwise display it's div and switch it's image to the on title image
	else
	{
		div.style.display = 'block';
		document.images[this.titleImgName].src = this.onTitleImgSrc;
	}
	
	// toggle this nav item's toggle state
	if ( isToggleOn != null )
		this.isToggledOn = isToggleOn;
	else
		this.isToggledOn = !this.isToggledOn;
}