/*
xbCollapsibleLists.js 2001-11-13

Contributor(s): Michael Bostock, Netscape Communications, Copyright 1997
                Bob Clary, Netscape Communications, Copyright 2001
                Seth Dillingham, Macrobyte Resources, Copyright 2001

Netscape grants you a royalty free license to use or modify this
software provided that this copyright notice appears on all copies.
This software is provided "AS IS," without a warranty of any kind.

See xbCollapsibleLists.js.changelog.txt for details of changes.

************** 2003-05-05
This version hacked to bits by James Nicholls of GTP iCommerce Pty Ltd 
to remove graphical buttons and make the titles the clickable links. 
*/


var _list_id = 0;
var _item_id = 0;
var _mLists = new Array();
var _parentElement = null;
var gMasterString = '';

document.lists = _mLists;

function List(visible, width, height, bgColor) 
{
	this.lists			= new Array();	// sublists
	this.items			= new Array();	// layers
	this.types			= new Array();	// type
	this.strs			= new Array();	// content
	this.visible		= visible;
	this.id				= _list_id;
	this.width			= width || 350;
	this.height			= height || 22;
	

	if (bgColor) 
		this.bgColor = bgColor;

	_mLists[_list_id++] = this;
}

function _listSetFont(i,j) 
{
	this.fontIntro = i;
	this.fontOutro = j;
}

function _listGetFont() 
{
	return [this.fontIntro, this.fontOutro];
}

function setIndent(indent) 
{ 
	this.i = indent; 
	if (this.i < 0) 
	{ 
		this.i = 0; 
		this.space = false; 
	}
	else
		this.space = true;
}

function getIndent(indent)
{
	return this.i;
}

function _writeItemDOMHTML( obj, s, flList, listObj )
{
	var styleObj;
	var outerDiv, innerLeft, innerRight;
	var str;
	var leftEdge = 0;
	
	styleObj = new xbStyle(obj);
	styleObj.setVisibility('hidden');
	outerDiv = document.createElement( "DIV" );
	outerDiv.id = "DIV_" + obj.id;
	styleObj = new xbStyle( outerDiv );
	styleObj.setWidth( this.width );
	
	leftEdge = 15;	

<!-- ** WRITE TEXT CELL ** -->
	innerRight = document.createElement( "DIV" );
	innerRight.noWrap = true;
	innerRight.style.position = "absolute";
	
	styleObj = new xbStyle( innerRight );
	styleObj.setLeft( leftEdge + ( this.l * this.i ) );
	styleObj.setWidth( this.width - 15 - this.l * this.i );
	styleObj.setBackgroundColor( "transparent" );
	
	if ( listObj )
		s = '<A TARGET="_self" HREF="javascript:expand(' + listObj.id + ');" class="' + this.fontIntro + '">' + s + '</A>';
	
	innerRight.innerHTML = s;
<!-- ** END TEXT CELL ** -->

	outerDiv.appendChild( innerRight );

	obj.appendChild( outerDiv );
	
	return;
}

function _writeItem( obj, s, flList, listObj )
{
	var cellStyle = '';
	var str = '';
	var styleObj = new xbStyle( obj );
	
	styleObj.setVisibility( 'hidden' );
	
	if ( navigator.DOMCSS1 )
		cellStyle = ' style="background-color: transparent;"';
	
	str += '<TABLE WIDTH='+this.width+' NOWRAP BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR>';

	str += '<TD WIDTH="15" NOWRAP' + cellStyle + '>&nbsp;</TD>';

	if (this.l>0 && this.i>0) 
		str += '<TD WIDTH="' + this.l*this.i+ '" NOWRAP' + cellStyle + '>&nbsp;</TD>';

	str += '<TD HEIGHT="' + ( this.height - 3) + '" WIDTH="' + ( this.width - 15 - this.l * this.i ) + '" VALIGN="MIDDLE" ALIGN="LEFT"' + cellStyle + '>';
	
	if ( flList ) 
	{
 		str += '<A TARGET="_self" HREF="javascript:expand(' + listObj.id + ');" class="' + this.fontIntro + '">';

		str += s;		
		
		str += '</A></TD>';
	} 
	else 
	{
	str += s;
	};
	
	str += '</TD></TR></TABLE>';
	
	styleObj.setInnerHTML( str );
	
	return;
}

function _writeList()
{
	var item;
	var i;
	var flList;
	
	for ( i = 0; i < this.types.length; i++ )
	{
		item = this.items[ i ];
		flList = ( this.types[ i ] == 'list' );
		
		this._writeItem( item, this.strs[ i ], flList, this.lists[ i ] );
		
		if ( flList && this.lists[ i ].visible )
			this.lists[ i ]._writeList();
	}
	
	this.built = true;
	this.needsRewrite = false;
	self.status = '';
}

function _showList() 
{
	var item;
	var styleObj;
	var i;

	for (i = 0; i < this.types.length; i++) 
	{ 
		item = this.items[i];
		styleObj = new xbStyle(item);
		styleObj.setClipLeft(0);
		styleObj.setClipRight(this.width);
		styleObj.setClipTop(0);
		if (item.height)
		{
			styleObj.setClipBottom(item.height);
			styleObj.setHeight(item.height);
		}
		else
		{
			styleObj.setClipBottom(this.height);
			styleObj.setHeight(this.height);
		}
		
		if ( this.visible )
			styleObj.setVisibility( 'visible' );

		var bg = item.oBgColor || this.bgColor;
		if ((bg == null) || (bg == 'null')) 
			bg = '';

		styleObj.setBackgroundColor(bg);

		if (this.types[i] == 'list' && this.lists[i].visible)
			this.lists[i]._showList();
	}
	this.shown = true;
	this.needsUpdate = false;
}

function setImage(list, item, file)
{
	var id = '_img' + list.id;
	var img = null;
	
	// for DOMHTML or IE4 use cross browser getElementById from xbStyle
	// can't use it for NN4 since it only works for layers in NN4
	if (navigator.DOMHTML || navigator.family == 'ie4')
		img = document.getElementById(id);
	else if (navigator.family == 'nn4') 
		img = item.document.images[0];
		
	if (img)
		img.src = file;
}

function getHeight() 
{
	var totalHeight = 0;
	var i;
	
	if (!this.visible)
		return 0;
	
	for (i = 0; i < this.types.length; i++) 
	{
		if (this.items[i].height)
			totalHeight += this.items[i].height;
		else
			totalHeight += this.height;
		
		if ((this.types[i] == 'list') && this.lists[i].visible)
		{
			totalHeight += this.lists[i].getHeight();
		}
	}
	
	return totalHeight;
}

function _updateList(pVis, x, y) 
{
	var currTop = y; 
	var item;
	var styleObj;
	var i;

	for (i = 0; i < this.types.length; i++) 
	{ 
		item = this.items[i];
		styleObj = new xbStyle(item);

		if (this.visible && pVis) 
		{
			styleObj.setLeft(x);
			styleObj.setTop(currTop);
			if (item.height)  // allow custom heights for each item
				currTop += item.height;
			else
				currTop += this.height;
			
			styleObj.setVisibility('visible');
		} 
		else 
		{
			styleObj.setVisibility('hidden');
		}

		if (this.types[i] == 'list') 
		{
			if (this.lists[i].visible) 
			{
				if (!this.lists[i].built || this.lists[i].needsRewrite) 
					this.lists[i]._writeList();

				if (!this.lists[i].shown || this.lists[i].needsUpdate) 
					this.lists[i]._showList();

				setImage(this.lists[i], item, this.expandedImageURL );
			} 
			else 
				setImage(this.lists[i], item, this.collapsedImageURL );

			if (this.lists[i].built)
				currTop = this.lists[i]._updateList(this.visible && pVis, x, currTop);
		}
	}
	return currTop;
}

function _updateParent( pid, l ) 
{
	var i;

	if ( !l ) 
		l = 0;

	this.pid = pid;
	this.l = l;

	for ( i = 0; i < this.types.length; i++ )
	{
		if ( this.types[ i ] == 'list' )
		{
			this.lists[ i ]._updateParent( pid, l + 1 );
		}
	}
}

function collapse(i) 
{
	_mLists[i].visible = false;

	if (_mLists[i].onexpand != null) 
		_mLists[i].onexpand(_mLists[i].id);

	_mLists[_mLists[i].pid].rebuild();

	if (_mLists[i].postexpand != null) 
		_mLists[i].postexpand(_mLists[i].id);
}

function expand(i) 
{
	_mLists[i].visible = !_mLists[i].visible;

	if (_mLists[i].onexpand != null) 
		_mLists[i].onexpand(_mLists[i].id);

	_mLists[_mLists[i].pid].rebuild();

	if (_mLists[i].postexpand != null) 
		_mLists[i].postexpand(_mLists[i].id);
}

function build(x, y) 
{
	this._updateParent(this.id);
	this._writeList();
	this._showList();
	this._updateList(true, x, y);
	this.x = x; 
	this.y = y;
}

function rebuild() 
{ 
	this._updateList(true, this.x, this.y); 
}

function getNewItem(item)
{
	var newItem = null;

	_parentElement = item.parentElement;

	if (navigator.DOMHTML)
		newItem = document.getElementById('lItem' + _item_id);
	else if (navigator.family == 'ie4')
		newItem = document.all['lItem'+_item_id];
	else if (navigator.family == 'nn4')
	{
		if (_parentElement)
			newItem = eval('_parentElement.document.layers.lItem'+_item_id);
		else
			newItem = eval('document.layers.lItem'+_item_id);
	}
	
	if (!newItem) 
	{
		if (navigator.DOMHTML)
		{
			newItem					= document.createElement('div');
			newItem.id				= 'lItem' + _item_id;
			newItem.style.position	= 'absolute';

			if (_parentElement)
				_parentElement.appendChild(newItem);
			else 
				document.body.appendChild(newItem);
		}
		else if (navigator.family == 'ie4')
		{
			if (!_parentElement)
				_parentElement = document.body;
				
			_parentElement.insertAdjacentHTML('beforeEnd', '<div id="lItem' + _item_id + '" style="position:absolute;"></div>');
			newItem = document.all['lItem' + _item_id];
		}
		else if (navigator.family == 'nn4') 
		{
			if (_parentElement)
				newItem = new Layer(item.width, _parentElement);
			else
				newItem = new Layer(item.width);
		}
	}

	return newItem;
}

function addItem(str, bgColor, item) 
{

	if (!item) 
		item = getNewItem(this);
	
	if (!item)
		return;

	if (bgColor) 
		item.oBgColor = bgColor;

	this.items[this.items.length] = item;
	this.types[this.types.length] = 'item';
	this.strs[this.strs.length] = str;
	++_item_id;
	
	if ( this.built )
	{
		this._writeItem( item, str, false );
		_mLists[this.pid].rebuild();
		if ( this.visible )
			this._showList();
		else
			this.needsUpdate = true;
	}
	
	return item;
}

function addList(list, str, bgColor, item) 
{
	if (!item) 
		item = getNewItem(this);

	if (!item)
		return;

	if (bgColor) 
		item.oBgColor = bgColor;

	this.lists[this.items.length] = list;
	this.items[this.items.length] = item;
	this.types[this.types.length] = 'list';
	this.strs[this.strs.length] = str;
	++_item_id;
	
	list.parentList = this;
	
	list.pid = this.pid;
	list.l = this.l + 1;
	
	if ( this.built )
	{
		this._writeItem( item, str, true, list );
		_mLists[ this.pid ].rebuild();
		if ( this.visible )
			this._showList();
		else
			this.needsUpdate = true;
	}
	
	return item;
}

List.prototype.setIndent		= setIndent;
List.prototype.getIndent		= getIndent;
List.prototype.addItem			= addItem;
List.prototype.addList			= addList;
List.prototype.build			= build;
List.prototype.rebuild			= rebuild;
List.prototype.setFont			= _listSetFont;
List.prototype.getFont			= _listGetFont;
List.prototype.getHeight		= getHeight;

List.prototype._writeList		= _writeList;
if ( navigator.DOMHTML )
	List.prototype._writeItem	= _writeItemDOMHTML;
else
	List.prototype._writeItem	= _writeItem;

List.prototype._showList		= _showList;
List.prototype._updateList		= _updateList;
List.prototype._updateParent	= _updateParent;

List.prototype.onexpand			= null;
List.prototype.postexpand		= null;
List.prototype.lists			= null;	// sublists
List.prototype.items			= null;	// layers
List.prototype.types			= null;	// type
List.prototype.strs				= null;	// content
List.prototype.x				= 0;
List.prototype.y				= 0;
List.prototype.visible			= false;
List.prototype.id				= -1;
List.prototype.i				= 18;
List.prototype.space			= true;
List.prototype.pid				= 0;
List.prototype.fontIntro		= false;
List.prototype.fontOutro		= false;
List.prototype.width			= 350;
List.prototype.height			= 22;
List.prototype.built			= false;
List.prototype.shown			= false;
List.prototype.needsUpdate		= false;
List.prototype.needsRewrite		= false;
List.prototype.l				= 0;
List.prototype.bgColor			= null;
List.prototype.parentList		= null;
List.prototype.parentElement	= null;
