//-------------------------------------------------------------------
//	IBM Lotus Team WorkPlace
//	Copyright (c)2004 International Business Machines Corporation
//-------------------------------------------------------------------

//-------------------------------------------------------------
// global vars used to control dynamic toc behaviour

var VISIBLE	= true;
						 
// twist states
var COLLAPSED =  0;
var EXPANDED  =  1;
var NONE      =  2;

var twistImgTag = new Array( "<img src='/qphtml/html/common/folder_twist_open.gif'	width=14 border=0 border=0 align=middle alt='expand' />",
									  "<img src='/qphtml/html/common/folder_twist_close.gif'	width=14 border=0 border=0 align=middle alt='collapse' />", 
									  "<img src='/qphtml/html/common/transparent.gif'			width=14 height=1 border=0 align=middle alt='' />");
								  
var folderImgTag = new Array( "<img src='/qphtml/html/common/folder_close.gif' border=0 align=middle valign=middle alt='' />",
										"<img src='/qphtml/html/common/folder_open.gif'  border=0 align=middle valign=middle alt='' />", 
										"<img src='/qphtml/html/common/transparent.gif'	 width=14 height=1 border=0 align=middle alt='' />");



//-------------------------------------------------------------------
// addTocItemObj( ):
//    called from the toc component, this is the main entry point to
//    the dynamic table of contents infrastructure.  This function
//    will append a TOC ITEM OBJECT to the array of objects tocItem.
//
// Arguments:
//		name   : the plain text name of the toc item
//		url    : the url the pointed to by the toc item
//		icon   : the reprensenting icon img tag of the toc item
//    level  : the hierarchy level of the toc item
//    state  : the current twist (collapsed/expanded) state of the toc item
//    visible: the visibility of the toc item
//		type   : the type of toc item, as follows:
//						D_QPTypePage					"0"
//						D_QPTypeFolder					"1"
//						D_QPTypeRoomSettings			"2"
//						D_QPTypeRoom					"3"
//						D_QPTypeError					"4"
//						D_QPTypeAreaType				"5"
//		selected: true|false whether the toc item is selected or not.
//
var tocItem = new Array( );

function addTocItemObj( name, url, icon, level, state, visible, type, selected)
{
	if ( typeof(selected) == "undefined") selected = false;

	var i = tocItem.length;
	
	tocItem[i] = new Object( );
	tocItem[i].name	= unescape(name);
	tocItem[i].url		= url;
	tocItem[i].icon	= unescape(icon);
	tocItem[i].level	= level;
	tocItem[i].state	= state;
	tocItem[i].visible= visible;
	tocItem[i].type	= type;
	tocItem[i].selected = selected;


	// post processing
	if ( tocItem[i].name == "Go Up") tocItem[i].icon = '<img src="/qphtml/html/common/goup.gif" align="absbottom" width=12 height=16 alt="" border="0" />'

	
	outputHTML( getTocItemHTML( i ), (i == 0) );	
	return tocItem.length;
}


//----------------------------------------------------
// displays all the visible toc items
function displayToc( )
{
	var buf = "";
	for( var i=0; i<tocItem.length; i++)
		buf += getTocItemHTML( i );
		
	outputHTML( buf, true);
}

//----------------------------------------------------
// displays one toc item, if visible
function getTocItemHTML( i )
{
	if ( !tocItem[i].visible) return("");

	// default twisty and icon images
	var twisty = twistImgTag[NONE];
	var icon = tocItem[i].icon;

	// set twisty anchor and image to  + or - for toc items with state of collapse or expand
	if ( tocItem[i].state == EXPANDED || tocItem[i].state == COLLAPSED )
	{
		twisty = "<a href=" + "'javascript:twist(this," + i + ");'" + ">" + twistImgTag[tocItem[i].state] + "</a>";
		icon = folderImgTag[ tocItem[i].state ];
	}

	// for selected folders which have a closed folder image, set the folder icon to open
	if ( tocItem[i].selected && tocItem[i].type == "1" && icon.indexOf("folder_") > -1)
	{
		icon = folderImgTag[EXPANDED];
	}

	// indentation for expandable items
	var indentPixels = tocItem[i].level * 10;
	var space = '<img src="/qphtml/html/common/transparent.gif"	width=' + indentPixels + ' height=1 border=0 alt="">';

	// link elements for anchor
	var itemHref  = ' href="' + tocItem[i].url + '" ' 
	var itemClass = tocItem[i].selected ? '" class="tocSelected-text" ' : '';

	var title = '<a' + itemHref + itemClass + '>' + icon + tocItem[i].name	+ '</a>' + '<br>';

	// in between item delimeters	
	var delimiter = '<img src="/qphtml/html/common/transparent.gif" width=1 height=1 border=0 alt="" class=tocDelimiter>'+'<br>';
	
	var htmlBuffer = space + twisty + title + delimiter;
	
	return( htmlBuffer);
}


//----------------------------------------------------
//
function twist( obj, i)
{
	tocItem[i].state = (( tocItem[i].state == EXPANDED) ? COLLAPSED : EXPANDED);
	toggleLevelVisibility( obj, i);
	displayToc( );
}

//----------------------------------------------------
//
function toggleLevelVisibility( obj, i)
{
	// toggle visibility of all the next level toc items until the next toc item at this parent level
	for (var j=i+1; j<tocItem.length && tocItem[i].level != tocItem[j].level; j++)
	{
		if ( tocItem[i].state == EXPANDED) // expand to show next level only
		{
			tocItem[j].visible = (( tocItem[j].level == tocItem[i].level+1) ? VISIBLE : tocItem[j].visible);
		}
		
		else if ( tocItem[i].state == COLLAPSED) // collapse all levels underneath
		{
			if ( tocItem[j].level >= tocItem[i].level+1)
			{
				tocItem[j].visible = !VISIBLE;
				tocItem[j].state = ((tocItem[j].state == EXPANDED) ? COLLAPSED : tocItem[j].state);
			}
		}
	}
}


//----------------------------------------------------
//
function outputHTML( buffer, bRedraw)
{
	if ( navigator.appName == "Netscape")
	{
		if ( bRedraw)
			document.getElementById("toc").innerHTML = buffer;
		else
			document.getElementById("toc").innerHTML += buffer;
	}
	else
	{
		if ( bRedraw)
			toc.innerHTML = buffer;
		else
			toc.innerHTML += buffer;
	}
}

