/* Copyright (C) 2002-2008 by Home of the Brave
   Web http://home.of.the.brave.de
   E-Mail info@brave.de */
/* $Revision: 1.16 $ $Date: 2008/02/27 17:18:56 $ */


/*
 * Display blocks with CSS class "hideable" as tabs.
 * dependencies: BodyEvents
 * owner: Simon.Leidig@brave.de
 *
 */

document.write('<style type="text/css">.hideable {display:none;}</style>');

var TabBlocks = {
	LabelTags:	['legend','h1','h2','h3'],
	Anchors:	{},
	Show:		[] // to be set from outside
};
TabBlocks.jump = function (a) {
	var t; if ( a.hash && a.hash.length > 1 &&
		( t = TabBlocks.Anchors[a.hash.substr(1)] )
	) t.show(true);
}
TabBlocks.Tab = function (block,tabs,linknode,statenode,id,key) {
	this.Block	= block;
	this.Tabs	= tabs;
	this.State	= statenode;
	this.ID		= id;
	this.Switch	= document.createElement('a');
	this.Switch.href = '#';
	this.Switch.className = 'closed';
	this.Switch.innerHTML = '&#160;';
	for (var i in TabBlocks.LabelTags) {
		var h = block.getElementsByTagName(TabBlocks.LabelTags[i]);
		if (h[0] && h[0].childNodes.length > 0) {
			this.Switch.innerHTML = h[0].innerHTML;
			if (h.length == 1) h[0].parentNode.removeChild(h[0]);
			break;
		}
	}
	linknode.appendChild(this.Switch);
	var sep = document.createElement('span');
	sep.className = 'separator';
	sep.appendChild(document.createTextNode(' '));
	linknode.appendChild(sep);
	var s = this;
	this.Switch.onclick = function () {s.show(); return false};
	if (key != null) this.Switch.setAttribute('accesskey',key);
}
TabBlocks.Tab.prototype.show = function (jump) {
	for (var i in this.Tabs) {
		var t = this.Tabs[i];
		if (t == this) {
			t.Switch.className = 'open';
			t.Block.style.display = 'block';
			t.State.value = t.ID;
		} else {
			t.Switch.className = 'closed';
			t.Block.style.display = 'none';
		}
	}
	if (jump) this.Switch.scrollIntoView();
}
BodyEvents.addListener('load',function () {
	var links; var state; var tabs; var units = []; var key = 0;
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) {
		var n = divs[i];
		if ( n.className &&
			 n.className.match(/\bhideable\b/) &&
			 n.childNodes.length > 0
		) {
			var p = n.previousSibling;
			if (!( p &&
				p.className &&
				p.className.match(/\btab_view\b/)
			)) {
				units.push( tabs = [] );
				links = document.createElement('div');
				links.className = 'tab_links';
				n.parentNode.insertBefore(links,n);
				state = document.createElement('input');
				state.type = 'hidden';
				state.name = 'tab_blocks_show';
				n.parentNode.insertBefore(state,n);
			}
			n.className =
			n.className.replace(/\bhideable\b/,'tab_view');
			var tab = new TabBlocks.Tab(
				n, tabs, links, state,
				(units.length - 1)+','+tabs.length,
				n.className.match(/\bcreate_accesskey\b/) &&
				key < 9 ? ++key : null
			);
			tabs.push(tab);
			var a = n.firstChild;
			if (a && a.nodeName.toLowerCase() == 'a' && a.name && a.name.length)
				TabBlocks.Anchors[a.name] = tab;
		}
	}
	for (var i in units) units[i][0].show();
	var s;
	if (TabBlocks.Show.length == 2) {
		if ( TabBlocks.Show[0] < units.length &&
			( s = units[TabBlocks.Show[0]][TabBlocks.Show[1]] )
		) s.show();
	} else TabBlocks.jump(location);
} );

