/*
	gforcesTabs Version 2.0
	Updated March 2010
	written by Matthew Lindley for GForces Web Management
	www.gforces.co.uk
*/

function gforcesTabs( options ) {

	var selectedTab = 0;


	//  Check to see if the URL has the selected tab for selection
	var loc = document.location.toString();

	if ( loc.indexOf( '#' ) > -1 ) {													//  # in the URL

		var divs = $$( 'div#' + loc.split('#')[1] + '.domContent' );					//  Get all content DIVs with the right ID and .domContent class

		if ( divs.length > 0 ) {														//  If any found, set the index (for first set)
			selectedTab = divs[0].previousSiblings().length;
		} else {																		//  Try with <a> tags/tabs

			var tabs = $$( 'a#' + loc.split('#')[1] );									//  Get all content DIVs with the right ID and .domContent class

			if ( tabs.length > 0 ) {
				selectedTab = tabs[0].up(0).previousSiblings().length;
			}

		}

	}

	//  Main options object
	options					= options || {}

	//  Properties
	options.divClass		= options.className || 'domTabs';
	options.selectedTab		= options.selectedTab || selectedTab;						//  Overwrite with manual JavaScript

	//  Callbacks/functions
	options.afterChange 	= options.afterChange || false;
	options.beforeChange 	= options.beforeChange || false;


	//  Main init code
	$$( 'div.' + options.divClass ).each( function ( div ) {							//  Loop <div>s

		var $childDivs = div.select( 'div.domContent' );								//  Store content divs for speed

		div.down(0).childElements()[options.selectedTab].addClassName( 'active' );		//  Activate required tab

		$childDivs.invoke( 'hide' )[options.selectedTab].show();						//  Hide all content, show first

		div.down(0).childElements().each( function ( li, index ) {						//  Loop <li>s

			li.down(0).observe( 'click', function ( ev ) {								//  Monitor onclicks

				ev.stop();																//  Stop the click

				if ( typeof options.beforeChange == 'function' ) {						//  Run function before any changes are made
					options.beforeChange( $( this ) );
				}

				div.down().childElements().invoke( 'removeClassName', 'active' );		//  Remove all class names

				$( this ).up(0).addClassName( 'active' );								//  Set active tab

				$childDivs.invoke( 'hide' )[index].show();								//  Hide all content, show required content

				if ( typeof options.afterChange == 'function' ) {						//  Run function after all changes are made
					options.afterChange( $( this ) );
				}

			});

		});

	});

}
