var oTabboxes = {

	mInit : function () { // init
	
		var aDivs = document.getElementsByTagName ( "div" );
		var i = -1; while ( ++i < aDivs.length ) {
			if ( aDivs.item ( i ).className == "tabbox" ) {
				oTabboxes.mSetup ( aDivs.item ( i ));
			}
		}
			
		// select tab whose ID is specified in location hash
		var sHash = document.location.hash;
		if ( sHash != "" ) {
			var sId = sHash.split ( "#" )[ 1 ];
			try {
				document.getElementById ( sId ).mSelect ();
			}
			catch ( noSuchElementException ) {}
		}
	},
	mSetup : function ( oTabbox ) { // setup

		// retrieve
		var oTabs = oTabbox.firstChild;
		var oTabpanels = oTabbox.lastChild;

		// locate
		if ( oTabs.nodeType == Node.TEXT_NODE ) oTabs = oTabs.nextSibling;
		if ( oTabpanels.nodeType == Node.TEXT_NODE ) oTabpanels = oTabpanels.previousSibling;

		// collect
		oTabbox.aTabs = oTabs.getElementsByTagName ( "a" );
		oTabbox.aTabpanels = [];
		var i = -1; while ( ++i < oTabpanels.childNodes.length ) {
			var oNode = oTabpanels.childNodes.item ( i );
			if ( oNode.className == "tabpanel" ) {
				oTabbox.aTabpanels.push ( oNode );
			}
		}
		
		// check
		if ( oTabbox.aTabs.length != oTabbox.aTabpanels.length ) {
			alert ( "tabs != tabpanels" );
			return;
		}

		// public interface	
		oTabbox.ontabselect 	= null;
		oTabbox.selectedIndex 	= 0;
		oTabbox.tabs 			= oTabbox.aTabs;
		oTabbox.tabpanels 		= oTabbox.aTabpanels;
	
		// build
		i = -1; while ( ++i < oTabbox.aTabs.length ) {
			var oTab = oTabbox.aTabs.item ( i );
			oTab.oTabbox = oTabbox;
			oTab.mSelect = oTabboxes.mSelect;
			oTab.onclick = oTab.mSelect;
			oTab.label = oTab.firstChild.nodeValue;
			if ( oTab.className == "selected" ) oTab.mSelect ( true ); 	
		}
	},
	mSelect : function ( bInitializing ) { // show hide tabpanels

		var i = -1; while ( ++i < this.oTabbox.aTabs.length ) {
			var bSelect = this.oTabbox.aTabs [ i ] == this;
			this.oTabbox.aTabs [ i ].className = bSelect ? "selected" : "";
			this.oTabbox.aTabpanels [ i ].className = bSelect ? "tabpanelinitialized selected" : "tabpanelinitialized";
			
			// fire synthetic event
			if ( bSelect ) { 
				this.oTabbox.selectedIndex = i;
				if ( this.oTabbox.ontabselect ) this.oTabbox.ontabselect ();
			}
		}
		oLayout.mToolbar ();
		return false;
	}
}

// initialize
oGod.mOnload ( oTabboxes.mInit );