


function Div
(
  id
, opts
)
	{
	this.id = id;
	Div[ id ] = this;	// keep track of this div thingy;

	this.init();

	if ( !opts )	return;
	for ( var i in opts )
		if ( this['_'+i] )	this['_'+i]( opts[i] );

	}	// End constructor; Div



Div.prototype.init = function ()
		{
		if ( this.div )	return;
		
		this.div = document.getElementById( this.id );	// link this div with the element within the doc
		if ( !this.div )	return;

		this.div.thisJS = this;	// link the element with this script/obj;
		this.displayStyle = ( this.div.style.display && this.div.style.display != 'none'  ?  this.div.style.display  :  'block' );

//  Make backward compatible (crossing fingers);
//	Migrate <DIV>s using height/overflow to open and close
		var h = 1;
		try { 
				h = parseInt( this.div.style.height );
				}
		catch ( ex )
				{}
		if ( h == 0  &&  this.div.style.overflow == 'hidden' )
			{
			this.div.style.height = 'auto';
			this.div.style.overflow = 'visible';

			this.div.style.display = 'none';
			}

		}	// End (subsequent) constructor; init()


//  Initialize tabbed interface
Div.prototype._tabs = function
(
  args
)
		{
		var anchors = this.div.getElementsByTagName( 'a' );
		if ( anchors.length < 1 )	return;
		this.tabs = new Array();
		this.activeTabClass = args.activeTabClass;

		for ( var a = 0;  a < anchors.length;  a++ )
			{
			var tab = anchors[a];
			var p;
			if ( tab.href  &&  (p = tab.href.indexOf( '##' )) > -1 )	
				{
				this.tabs.push( tab );

				var divName = tab.href.substr( p+2 );
				if ( (typeof Div[divName]) == 'undefined' )		new Div( divName );

				tab.href = 'javascript:Div.' + this.id + '.activate( ' + (this.tabs.length-1) + ' );'
				tab.tabDiv = Div[divName];
				}
			}	// End for; span of anchors
		}	// End init; _tabs



Div.prototype.activate = function
(
	trigger
)
		{
		for ( var t = 0;  t < this.tabs.length;  t++ )
			{
			this.tabs[t].className = ( t == trigger  ?  this.activeTabClass  :  '' );	// active check differs between anchor and tab/div...
			this.tabs[t].tabDiv.peek( this.tabs[t].tabDiv == this.tabs[trigger].tabDiv ); // check object reference in order to support many (anchors) to one (tab/div);
			}
		}	// End method; activate()




Div.calcX = function
(
  obj
)
		{
		return	( obj  ?  obj.offsetLeft + Div.calcX( obj.offsetParent )  :  0  );
		}	// End method; calcX





Div.prototype.createScrollingViewport = function
(
  element
, step
, interval
)
		{
		this.init();
		this.scrolled = 
		  { element: element
		  , step: step
			, timer: window.setInterval( 'Div.' + this.id + '.scrollViewport();', interval )
		  };

		}	// End method; createScrollingViewport




Div.prototype.peek = function
(
  keepOpen
)
		{
		this.init();

		if ( keepOpen || ( (typeof keepOpen) == 'undefined' && this.div.style.display == 'none' ) )
			{
			this.div.style.display = ( this.displayStyle  ?  this.displayStyle  :  'block' );
			keepOpen = true;
			}
		else
			{
			this.div.style.display = 'none';
			keepOpen = false;
			}

		if ( !this.tabs )	return;
		for ( var t in this.tabs )
			this.tabs[t].tabDiv.peek( keepOpen && this.tabs[t].className == this.activeTabClass );
		}	// End method; peek



Div.prototype.posX = function()
		{
		this.init();
		return	( !this.div.posX || this.div.posX == 0  ?  this.div.posX = Div.calcX( this.div )  :  this.div.posX );
		}	// End method; posX



Div.prototype.scrollViewport = function ()
		{
		var mT = parseInt( this.scrolled.element.style.marginTop );
		this.scrolled.element.style.marginTop = ( this.scrolled.element.scrollHeight + mT > 0  ?  (mT - this.scrolled.step) + 'px'  :  this.div.style.height );
		}	// End method; scrollViewport
