/*
	dropdownlink.js
	for pages with links that when click extra info apears below
*/

/*
	It expects a html structure like this:
	
	<dl id="test">
		<dt><a href="#">heading title</a></dt>
		<dd>
			<p>details here....</p>
			<p>details here....</p>
			<p>details here....</p>
		</dd>
		<dt><a href="#">heading title</a></dt>
		<dd>
			<p>details here....</p>
			<p>details here....</p>
			<p>details here....</p>
		</dd>
	</dl>
	
	where "test" is the id passed to the constructer.
*/

function DropDownLink(dlId)
{
	if ( document.getElementsByTagName && document.getElementById )
	{
		if ( typeof(dlId) == 'string' )
			this.dlEl = document.getElementById(dlId);
		else
			this.dlEl = dlId;
			
		if ( this.dlEl )
		{
			this.dtEl = this.dlEl.getElementsByTagName('dt');
			this.ddEl = this.dlEl.getElementsByTagName('dd');
		}
		else
			throw new Error("could not find drop down links");
		
		this.registerLinks();
		this.hideContent();
	}

}

DropDownLink.prototype.toString = function()
{
	return "Drop Down Link id is " + this.dlEl.id;
}

DropDownLink.prototype.registerLinks = function()
{
	for ( var i = 0; i != this.dtEl.length; i++ )
	{
		if ( this.dtEl[i].firstChild != null )
		{
			var obj = this;
			addEvent(this.dtEl[i].firstChild, 'click', function(e) { obj.toggleView.call(obj, e); } );
		}
	}
}

DropDownLink.prototype.hideContent = function()
{
	for ( var i = 0; i != this.ddEl.length; i++ )
		this.ddEl[i].style.display = 'none';
}

DropDownLink.prototype.toggleView = function(e)
{
	var tag = e.target ? e.target : e.srcElement;

	DropDownLink.stopDefaultAction(e);
	
	for ( var i = 0; i != this.dtEl.length; i++ )
	{
		if ( this.dtEl[i] == tag.parentNode )
		{
			if ( this.ddEl[i] )
				this.ddEl[i].style.display == 'block' ? this.ddEl[i].style.display = 'none' : this.ddEl[i].style.display = 'block';
			
			break;
		}
	}
			
}

DropDownLink.stopDefaultAction = function(e)
{
	if ( e.cancelable )
		e.preventDefault(true);
	else
		e.returnValue = false;
}


