
var found_stretcher = false;

function hasClass(element, class_name)
{
	return (
		element.className &&
		element.className.match(new RegExp('\\b' + class_name + '\\b'))
	) ? true : false;
}


Behaviour.register({
	//
	// these behaviours are used to add various types of
	// accordions to articles
	//
	'#article div.stretcher' : function(el)
	{
		//
		// none of the stretchers are wrapped in innoccuous divs
		// that are required by scriptaculous to function
		// properly, and weren't required by moo.fx.  lets
		// wrap everything in a div so the writers don't have
		// to go back and edit all the stretcher content...
		//
		var container = document.createElement('div');
		var container_class = document.createAttribute('class');
		container_class.nodeValue = 'stretcher-container';
		container.setAttributeNode(container_class);
		container.style.hasLayout = false;
		// container.style.zIndex = el.style.zIndex;

		while (el.firstChild)
		{
			container.appendChild(el.removeChild(el.firstChild));
		}
		el.appendChild(container);
	},
	'#article h3.display' : function(el)
	{
		// find the next element, which should be div.stretcher
		// based on thewatchmakerproject.com/journal/329/finding-html-elements-using-javascript-nextsibling-and-previoussibling (the comments by Mark Dalgleish)
		var elem = el.nextSibling;
		while (elem.nodeType != 1/* && elem.hasClass && !elem.hasClass('stretcher')*/)
		{
			elem = elem.nextSibling;
		}


		var group_mode = 'linked';
		var node_mode = 'normal';

		if (hasClass(el.parentNode, 'all-closed'))
		{
			node_mode = 'closed';
		}
		if (hasClass(el.parentNode, 'many-open'))
		{
			group_mode = 'unlinked';
		}
		if (hasClass(el, 'open'))
		{
			node_mode = 'open';
		}

		// at this point we should have div.stretcher
		if (!found_stretcher)
		{
			found_stretcher = true;

			if (node_mode == 'closed')
			{
				elem.style.display = 'none';
			}
		}
		else if (node_mode != 'open')
		{
			elem.style.display = 'none';
		}
		// alert(elem.innerHTML);


		el.onclick = function()
		{
			// find all 'stretchers'
			var f = $$('#article div.stretcher');

			if (group_mode == 'linked')
			{
				for (var i = 0; i < f.length; ++i)
				{
					// if the stretcher doesn't equal elem, hide it
					if (f[i] != elem && f[i].style.display != 'none')
					{
						new Effect.SlideUp(f[i], {duration: 0.5});
						// new Effect.Fade(f[i], {duration: 0.5});
						// f[i].style.display = 'none';
					}
					else if (f[i] == elem && f[i].style.display == 'none') // if it does, show it
					{
						// alert("trying to show " + this.id);
						new Effect.SlideDown(
							f[i] ,
							{duration: 0.5});
						// new Effect.Appear(f[i], {duration: 0.5});
						// f[i].style.display = 'block';
					}
					// f[i].style.zIndex = 1;
				}
			}
			else if (group_mode = 'unlinked')
			{
				for (var i = 0; i < f.length; ++i)
				{
					//
					// if we're at the current stretcher.
					//
					if (f[i] == elem && f[i].style.display != 'none')
					{
						new Effect.SlideUp(f[i], {duration: 0.5});
						// new Effect.Fade(f[i], {duration: 0.5});
						// f[i].style.display = 'none';
					}
					else if (f[i] == elem && f[i].style.display == 'none') // if it does, show it
					{
						// alert("trying to show " + this.id);
						new Effect.SlideDown(
							f[i] ,
							{duration: 0.5});
						// new Effect.Appear(f[i], {duration: 0.5});
						// f[i].style.display = 'block';
					}
					// f[i].style.zIndex = 1;
				}
			}
		}
	}
});