var gTabs = Class.create({

	initialize: function ( options ) {

		//  Loop <div>s
		$$('div.domtab').each( function ( div ) {

			//  Set an id if one isn't present
			div.identify();

			//  Activate first tab
			div.down().firstDescendant().addClassName( 'active' );

			//  Hide all content
			$$( '#' + div.id + ' div.tabset_content' ).invoke( 'hide' );

			//  Show first content
			$$( '#' + div.id + ' div.tabset_content' )[0].show();

			/***********************************************************************
			*  Set onclicks for tabs
			***********************************************************************/

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

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

					//  Stop link working
					Event.stop ( ev );

					//  Do beforeChange() function?
					if ( ( options ) && ( typeof options.beforeChange == 'function' ) ) {
						options.beforeChange();
					}

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

					//  Set active tab
					ev.element().up().addClassName( 'active' );

					//  Hide all content
					$$( '#' + div.id + ' div.tabset_content' ).invoke( 'hide' );

					//  Show content selected
					$$( '#' + div.id + ' div.tabset_content' )[index].show();

					//  Do afterChange() function?
					if ( ( options ) && ( typeof options.afterChange == 'function' ) ) {
						options.afterChange();
					}

				});

			});

			/***********************************************************************
			*  Create "next" buttons
			***********************************************************************/

			$$( 'div#' + div.id + ' a.gTabsNext' ).each( function ( el ) {

				el.observe( 'click', function ( ev ) {

					Event.stop( ev );

					if ( ( options ) && ( typeof options.beforeChange == 'function' ) ) {
						options.beforeChange();
					}

					tabs = div.down().childElements();

					activeTab = tabs.find( function ( li, index )  {
						return ( li.hasClassName('active') ) ? li : false ;
					});

					if ( activeTab ) {

						activeTabIndex = tabs.pluck( 'id' ).indexOf( activeTab.id );

						if ( ( activeTabIndex + 1 ) < tabs.length ) {
							activeTab.removeClassName( 'active' );
							tabs[ (activeTabIndex+1) ].addClassName( 'active' );

							$$( '#' + div.id + ' div.tabset_content' )[activeTabIndex].hide();
							$$( '#' + div.id + ' div.tabset_content' )[(activeTabIndex+1)].show();
						}

					}

					if ( ( options ) && ( typeof options.afterChange == 'function' ) ) {
						options.afterChange();
					}

				});

			});

		});


	}

});