//------------------------------------------------------------------------------
// IPS JS: Tabby Cat.
// (c) 2005 Invision Power Services, Inc.
// http://www.invisionpower.com
// Matt Mecham (02/11/05)
//------------------------------------------------------------------------------

var _tab_factory;

/*-------------------------------------------------------------------------*/
// Initiate object
/*-------------------------------------------------------------------------*/

function tab_factory()
{
	/**
	* ID: CSS: Tab off class
	*
	* @var INT
	*/
	this.css_tab_off     = 'pp-taboff';
	
	/**
	* ID: Form
	*
	* @var string
	*/
	this.css_tab_on      = 'pp-tabon';
	
	/**
	* String: Part tab set up
	*
	* @var string
	*/
	this.string_tab_id   = 'tabtab';
	
	/**
	* String: Part tab pane
	*
	* @var string
	*/
	this.string_tab_pane  = 'tabpane';
	
	/**
	* String: Tab array
	*
	* @var array
	*/
	this.stored_tabs  = new Array();
	
	/**
	* String: Panes array
	*
	* @var array
	*/
	this.stored_panes  = new Array();
	
	/** String: Starting pane
	*
	* @var string
	*/
	this.default_tab = '';
}

/*-------------------------------------------------------------------------*/
// Initiate object 
function tab_factory_init_tabs() { }
/*-------------------------------------------------------------------------*/

tab_factory.prototype.init_tabs = function()
{
	//--------------------------------------------
	// Get page divs...
	//--------------------------------------------
	
	var divs     = document.getElementsByTagName('DIV');
	var divcount = 0;
	
	_tab_factory = this;
	
	//--------------------------------------------
	// Sort through and grab links
	//--------------------------------------------
	
	for ( var i = 0 ; i <= divs.length ; i++ )
	{
		try
		{
			if ( ! divs[i].id )
			{
				continue;
			}
		}
		catch(error)
		{
			continue;
		}
		
		var divid   = divs[i].id;
		var divname = divid.replace( /^(.*)-(\S+)$/, "$1" );
		var divnum  = this.get_id_from_text( divs[i].id );
		
		//--------------------------------------------
		// Work with it?
		//--------------------------------------------
		
		if ( divname == this.string_tab_id )
		{
			this.stored_tabs[ divnum ] = divs[i];
			
			divs[i].style.cursor = 'pointer';
			
			divs[i].onclick = this.toggle_tabs;
			
			//--------------------------------------------
			// Starting pane?
			//--------------------------------------------
			
			var _start = 0;
			
			if ( this.default_tab != '' )
			{
				if ( this.default_tab == divnum )
				{
					_start = 1;
				}
			}
			else if ( divcount == 0 )
			{
				_start = 1;
			}
			
			//--------------------------------------------
			// Set correct classes / display
			//--------------------------------------------
			
			this.stored_panes[ divnum ] = document.getElementById( this.string_tab_pane + '-' + divnum );
			
			if ( _start == 1 )
			{
				// First tab...
				divs[i].className     = this.css_tab_on;
				divs[i].style.display = 'block';
				
				this.stored_panes[ divnum ].style.display   = 'block';
			}
			else
			{
				// Second tab
				divs[i].className     = this.css_tab_off;
				divs[i].style.display = 'block';
				
				this.stored_panes[ divnum ].style.display   = 'none';
			}
			
			divcount++;
		}
	}
};

/*-------------------------------------------------------------------------*/
// The form has changed...
/*-------------------------------------------------------------------------*/

tab_factory.prototype.toggle_tabs = function(event)
{
	//-----------------------------------------
	// No? Get one then
	//-----------------------------------------
	
	var tabid = _tab_factory.get_id_from_text( this.id );
	
	//-----------------------------------------
	// Got it?
	//-----------------------------------------
	
	for ( var i in _tab_factory.stored_tabs )
	{
		if ( i == tabid )
		{
			_tab_factory.stored_tabs[ i ].style.display   = 'block';
			_tab_factory.stored_tabs[ i ].className       = _tab_factory.css_tab_on;
			_tab_factory.stored_panes[ i ].style.display  = 'block';
		}
		else
		{
			_tab_factory.stored_tabs[ i ].style.display   = 'block';
			_tab_factory.stored_tabs[ i ].className       = _tab_factory.css_tab_off;
			_tab_factory.stored_panes[ i ].style.display  = 'none';
		}
	}
	
	// Stop scrolling to top of the page...
	ipsclass.cancel_bubble( event );
	
	return false;
};



/*-------------------------------------------------------------------------*/
// Grab ID from string
/*-------------------------------------------------------------------------*/

tab_factory.prototype.get_id_from_text = function( id )
{
	return id.replace( /.*\-(\S+)/, "$1" );
};
