/* ------------------------------------------------------------------------------------------------------------------
	Linkman.js
	Contains specifi functions for Link Manager
	Revision History
	----------------

	10-Nov-04 v07
		Created

	18-Nov-04
		Working in essence - now v02

	01-Dec-04
		Removed <new item> so L.length references changed

	09-Dec-04
		Finally solved open folders problem
		Linkman04.js
		
	18-Apr-05
		Move to v5

	21-May-05
		Fix open folder problems

	23-May-05
		Add search facility

	20-Jul-05
		Hamm special - no WellTech on front page

	07-Sep-05
		Converted to getAtt/setAtt

---------------------------------------------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------------------------------DATA STRUCTURES */

	var of = new Array();		// List of open folders
	var cols = new Array();	// list of columns to contain open folders
	var currColInd = 0;			// currently occupied column index
	var openFolders = '';		// open folder list (refno1,refno2, ..)
	var openFoldersSav = '';// save list for difference detection ...
	var expandedFolders = '';// expanded folders - only used for refresh
	var recentLinks = '';		// and recent links
	var timerUpdateLinks;		// set for 3 secs after link call
	var timerUpdateFolders;	// set for every 10 secs
	var inExpandClick = false; // for expanded folder tracking - set only at initialisation

function newInit() {	// new initialisation - merge eventually
	cols[0] = getEl("col1Content");
	if (UserRef > 0) {
	cols[1] = getEl("col2Content");
	cols[2] = getEl("col3Content");
	cols[3] = getEl("col4Content");
	}
	else {
		getEl("col99Content").className = "colRightContent"
		// getEl("col2Content").style.display = "none";
		// getEl("col3Content").style.display = "none";
	}
	addDebug.defEl = getEl("col99Content");	// set default debug
	if (isLocal==0)
		addDebug.defEl = null;	// comment out for debug
}
var debugNos = ',11,'; // add string in form "1,", ",1,4" to use series 1, 2 etc.

function resetUpdateTimers() {
	clearTimeout(timerUpdateLinks);
	clearInterval(timerUpdateFolders);
}

function updateLinksAndFolders (currAct) {
	// called to update link list and, after 3 secs, update server
	addDebug('---- updateLinksAndFolders',10);
	addDebug('     openFolders: ' + openFolders,10);
	addDebug('     recentLinks: ' + recentLinks,10);
	if ((openFolders  != openFoldersSav ) || (recentLinks  != '')) {
	  if (currAct)
			currentAction = currAct	// called from "update frequency"
		else
			currentAction = 7;	// default - open folders
		setAction();
	}
}

/* Update openFolders with current of list */
function updateOpenFolders() {
	addDebug('---- updateOpenFolders',10);
	addDebug('openFolders bef: ' + openFolders,10);
	resetUpdateTimers();
	openFolders = '';
	for (var i = 0; i < of.length; i++)	// cycle through folders
		if (of[i].refno != 0)
			openFolders += of[i].refno + ','
	addDebug('openFolders aft: ' + openFolders,10);
}

function makeOf(fInd, refno) {
	this.fInd = fInd;
	this.refno = refno;
}
		
function addOf (fInd, refno, init) {
	var ind = of.length;
	for (var i = 0; i < ind; i++)	// check to see not there
		if (of[i] == fInd)
			return i;
	of[ind] = new makeOf (fInd, refno);
	if (!init)
		updateOpenFolders();
	return ind;
} // addFolder 
	
function removeOf (refno) {
	var i = 0;
	var s = 'of list: ';
	for (var j = 0; j < of.length; j++)
		s += of[j].fInd + ','
	addDebug(s, 2);
	while (i < of.length && of[i].refno != refno)
		i++;
	if (i < of.length)
		of.splice(i,1);
	var s = 'of list: ';
	for (var j = 0; j < of.length; j++)
		s += of[j].fInd + ','
	addDebug(s, 2);
	while (i < of.length && of[i].refno != refno)
		i++;
	if (i < of.length)
		of.splice(i,1);
	updateOpenFolders();
}

function openFoldersFromList() {
	addDebug("----  openFoldersFromList",10);
	addDebug('openFolders: ' + openFolders,10);
	var fs = openFolders;			// local list
	of.length = 0;
	for (var i = 0; i < F.length; i++) {
		F[i].open = null;
		F[i].display = null;
	}
	dispFolder (0, false, true);		// always init 0
	if (fs > '') {
		addDebug("---- Init open folders",8);
		while (fs > '') {
			var n = parseInt(fs);
			addDebug("val: " + n,8);
			addOf (0, n, true);
			var p = fs.indexOf(',');
			if (p == -1)
				fs = ''
			else {
				fs = fs.slice(p+1);
			}
		}
	}
	inUpdate = false;
	reposFolders(true);
	openFoldersSav = openFolders;
} // openFoldersFromList

function doUnload() {
	return;
}

/* ---------------------------------------------------------------------------------------------- END DATA STRUCTURES */

/* Intialise link manager -------------------------------------------------------------------------- linkManInit */
function linkManInit(){
	setUserName();
	setFolderStruct(0)
//	dispFolder (0, false, true);
}

/* Intialise link manager ------------------------------------------------------------------------ linkManReinit */
function linkManReinit(){
	addDebug("---- linkManReinit", 10);
	setUserName();
	setFolderStruct(0, true)
	for (var i = 0; i < cols.length; i++)
		while (cols[i].lastChild)   			// delete open link displays
			cols[i].removeChild(cols[i].lastChild)
	currColInd = 0;
	// reposFolders(true);		// repos by refno
}

/* Set user name ---------------------------------------------------------------------------------- setUserName  */
function setUserName () {
	getEl("danName").innerHTML = "Web link manager for " + UserFn + " " + UserSn
}

/* Create folder structure - permanent structure for folder display ----------------------------- setFolderStruct */
function setFolderStruct(trgtExpand, doClear) {
	var fs = getEl("fsBody");
	var s;
	addDebug ("setFolderStruct -----------------------", 6);
	fillFolderCounts(0);					// fill in counts
	if (doClear)
		while (fs.lastChild)   			// delete rows in case refresh
			fs.removeChild(fs.lastChild)
	for (var i = 0; i < FolderCnt; i++) {
		var foldIt = createEl("div");
		/* ------------------------------------------------------------------ Put counts here - display turned off* /
		var x = createEl("span");
		setAtt(x, "innerHTML", F[i].cntCurr);
		x.style.width = "17px";
		x.style.width = "0";
		x.style.align = "right";
		x.style.display = "none";
		foldIt.appendChild(x);
		var x = createEl("span");
		setAtt(x, "innerHTML", '(' + F[i].cntAll + ')');
		x.style.width = "40px";
		x.style.width = "0";
		x.style.display = "none";
		foldIt.appendChild(x);
		/* */
		
		/* ----------------------------------------------------------------------------------- put expand GIFs here */
		var sp1 = createEl("span");
		if (F[i].nested && (i > 0)) {
			x = createEl("a");
			setAtt(x, "title", "Click to expand");
			setAtt(x, "href", "javascript: void expandFolder (" + i + ")");
			var expB = createEl("img");
			setAtt(expB, "src", "tr_closed-off.gif");
			setAtt(expB, "border", "0");
			x.appendChild(expB);
			sp1.appendChild(x);
		}

		var indent = (F[i].lev+1)*levIndent;
		var W = indent;
		sp1.style.width = pix(W);
		if (i > 0)
			sp1.style.paddingLeft = pix(indent - 10);
		if (debugLink==1)
			debugS += sp1.outerHTML + '\n';
		foldIt.appendChild(sp1);

		var x = createEl("a");
		setAtt(foldIt, "className", "cssFLp");
		setAtt(foldIt, "id", F[i].elId);
		setAtt(x, "href", "javascript: void folderClick(" + String(i) + ");");
		// x.style.paddingLeft = pix((F[i].lev+1)*levIndent);								// with this if outer lev=-1
		x.style.paddingLeft = pix(1);								// with this if outer lev=-1
		s = F[i].title;
		// s = s + ' (' + i + ')';		// folder no for debugging
		setAtt(x, "innerHTML", s);
		setAtt(x, "title", F[i].cntCurr + ' (' + F[i].cntAll + ') items');
		var sp2 = createEl("span");
		sp2.appendChild(x);
			sp2.style.width = pix(210-W); //el.offsetWidth - 14;
		foldIt.appendChild(sp2);

		var sp3 = createEl("span");
		setAtt(sp3, "innerHTML",'<a title="Left click for menu" ' +
					 'href="javascript: void LinkMenu(' + i + ',1);">o</a>');
		foldIt.appendChild(sp3);

    F[i].folderListEl = fs.appendChild(foldIt);
    F[i].div = foldIt;		// Save div, so we can manipulate
		// addDebug ("F["+i+"].parentRefno: " + F[i].parentRefno + ", refno: " + F[i].refno + ", elId: " + F[i].elId, 6);
		addDebug ("F["+i+"].refno: " + F[i].refno + ", parentInd: " + F[i].parentInd + ", elId: " + F[i].elId, 6);
	} // for i 
	if (!trgtExpand)
		trgtExpand = 0
	else if (trgtExpand = "max")
		trgtExpand = F.maxLev;
	if (F.maxLev > 0) {		// create radio boxes
		foldIt = createEl("div");
		var s = '<table id="fsRadio"><td>Expand:</td><td id="radioList">';
		for (i = 0; i <= F.maxLev; i++) {
			s += '<label> <input type="radio" name="expRg" value="radio" onclick="expandClick(' + i + ')"';
			if (i==trgtExpand)
				s += ' checked="true"';
			if (i==0)
				s += '>none'
			else if (i==F.maxLev)
				s += '>all'
			else
				s += '>' + String(i);
			s += '</label>';
		}
		s += '<div id="divRadioOther"><label> <input id="radioOther" type="radio" name="expRg" value="radio">other</label></div>';
								// add "other" hidden label,as a way of clearing the radio buttons, because we are too lazy to do properly
		s += '</td></table>';
		foldIt.innerHTML = s;
		fs.appendChild(foldIt);
		dispEl("divRadioOther", 0);		// Hide this one AFTER adding to document!
	}
	// expandClick(0);	// finally collapse folder structhre
	expandClick(trgtExpand);	// finally collapse folder structhre
	F.funct = 2;
} // setFolderStruct

var temp = 0;
/* Fill folder counts -------------------------------------------------------------------------- fillFolderCounts */
function fillFolderCounts(startFInd) {
	var startLInd = 0;
	var currLev = F[startFInd].lev;
	var trgtLev = currLev + 1;
	var currN = 0;										// this is our count
	temp++;
	/* count up current folder contents */

	/* -- NEED TO SORT THIS OUT --
	var parentRefno = L[startLInd].parentRefno;
	for (var i = startLInd; i < L.length && L[i].lev > currLev; i++) {
		/* First establish items in current folder * /
		if ((L[i].parentRefno==parentRefno) && (L[i].lev == trgtLev))
			currN++;
	} // for i
	*/
	F[startFInd].cntCurr = currN;
	/* -- NEED TO SORT THIS OUT --
	for (i = startFInd+1; i < F.length && F[i].lev > currLev; i++)
	/* Now traverse next level folders * /
		if (F[i].lev == trgtLev)
			currN += fillFolderCounts(i);
	*/
	F[startFInd].cntAll = currN;
	return currN;
}

/* Clear radio button -------------------------------------------------------------------------------- clearClick */
function clearClick() {
	setAtt(getEl("radioOther"), "checked", "true");
	return;
	rl = getEl("radioList");
	for (var i = 0; i < rl.length; i++) {
		rl[i].checked = false;
	}
}

/* Respond to folder name click --------------------------------------------------------------------- folderClick */
function folderClick(N) {
   if (inInit)
    return;
	addDebug("folderClick: " + N + ", F.funct: " + F.funct, 7);
	switch (F.funct) {
		case 1 :
			expandFolder(N);
			break;
		case 2 :
			dispFolder (N);
			break;
		case 3 : 
			setMoveToFolder (N);
			break;
	}
}

/* Respond to expand radio button ------------------------------------------------------------------- expandClick */
function expandClick(lev) {
   if (inInit)
    return;
	// addDebug ("--------- ec: " + lev + " ---------");
	/*
	for (var i = 0; i < F.length; i++) {
		if (F[i].lev <= lev)
			dispEl (F[i].div, 1);
		else
			dispEl (F[i].div, 0);
	}
	*/
	for (var i = 1; i < F.length; i++) {	// dont do outer
		if (F[i].lev < lev)
			expandFolder (i, true)
		else
			contractFolder (i, true)
	}
} // expandClick

/* Expand folders from list of open folders --------------------------------------------------- expandFoldersFromList */
function expandFoldersFromList () {
/* */
	var fl = ',' + openFolders;	// folder list
	var fla = ',';							// append list (parents)
	var j;											// extra counter
	var pn;											// parent refno

	function getParentRefno(rn) {
					 // get parent refno for folder refno rn
		for (var i = 0; i < F.length; i++) {
			if (F[i].refno == rn)  {
				return F[i].parentRefno
			}
		} // for
		return 0;
	} // getParentRefno
	if ((expandedFolders > '') && (expandedFolders != ',')) {
		fla = expandedFolders;
	}
	else {
		for (var i = 1; i < F.length; i++) {	// dont do outer
			if (fl.indexOf(','+F[i].refno+',') > 0)  {
				pn = F[i].parentRefno;
				while (pn > 0) {
					fla = fla + pn + ',';
					pn = getParentRefno(pn);
				} // while
			} // if
		} // for
	} // else
	// now concatenate the lists
	// fl = fl + fla;
	for (i = 1; i < F.length; i++) {	// dont do outer
		if (fla.indexOf(','+F[i].refno+',') >= 0)  {
			expandFolder (i, true)
		}
	}
 /* */
} // expandFoldersFromList 

/* Expand folder ----------------------------------------------------------------------------------- expandFolder */
function expandFolder (N, auto) {
   if (inInit)
    return;
	if (!N)
		return;
	addDebug ('expandFolder ' + N + ' ----------', 4);
	// addDebug ('expand bef: ' + N);
	if (F[N].expanded || !F[N].nested)
		return;
	// addDebug ('expand aft: ' + N);
	if (!inExpandClick) {
		if (expandedFolders=='')
			expandedFolders = ',';
		expandedFolders += F[N].refno + ',';
	}
	F[N].expanded = true;
	var s = F[N].div.innerHTML;
	// addDebug(s);
	s = s.replace(/Expand/, "Contract");
	s = s.replace(/expand/, "contract");
	s = s.replace(/expandFolder/, "contractFolder");
	s = s.replace(/tr_closed-off/, "tr_open-off");
	F[N].div.innerHTML = s;
	var lev = F[N].lev+1;
	if (!auto)
		clearClick();
	for (var i = ++N; (i < F.length) && (F[i].lev >= lev); i++) {
		if (F[i].lev == lev)
			dispEl (F[i].div, 1);
	}
} // expandFolder 

/* Contract folder --------------------------------------------------------------------------------- contractFolder */
function contractFolder (N, auto) {
	var p;	// for position
	var s;	// .. of string
	addDebug ('contractFolder --------', 4);
	// addDebug ('contract bef: ' + N);
	if (!F[N].expanded || !F[N].nested)
		return;
	// addDebug ('contract aft: ' + N);
	if (!inExpandClick) {
		s = ','+F[N].refno+',';
		p = expandedFolders.indexOf(s);
		if (p >= 0)  {
			expandedFolders = expandedFolders.slice(0,p) + expandedFolders.slice(p+s.length-1);	// leave trailing comma
		}
	}
	F[N].expanded = null;
	var s = F[N].div.innerHTML;
	s = s.replace(/Contract/, "Expand");
	s = s.replace(/contract/, "expand");
	s = s.replace(/contractFolder/, "expandFolder");
	s = s.replace(/tr_open-off/, "tr_closed-off");
	F[N].div.innerHTML = s;
	var lev = F[N].lev+1;
	if (!auto)
		clearClick();
	for (var i = ++N; (i < F.length) && (F[i].lev >= lev); i++) {
		dispEl (F[i].div, 0);
	}
} // contractFolder 

var cx;	// for copy node try
/* Generate display header, showing folder path ------------------------------------------------------ dispHeader */
function dispHeader (fInd, el) { // append path to fInd, append to element el

	function addItem (ind, el, isSel, app, first) {
		var div;
		var s;
		div = createEl("div");
		if (isSel) {
			setAtt(div, "className", "fHead fHeadSel")
			setAtt(div, "id", F[fInd].elIdDisp);
			s = F[ind].title;
			s = '';
			s += '<a title="Left click for menu" ' +
					 'href="javascript: void LinkMenu(' + fInd + ',2);">' + F[ind].title + '</a>';
		}
		else {
			setAtt(div, "className", "fHead");
			s = F[ind].title;
		}
		if (first) {
			/* - omit LinkMenu special button - see how it works with menu just on title
			s += '<span class="cssHeadMenu">';
			s += '<a title="Left click for menu" ' +
					 'href="javascript: void LinkMenu(' + fInd + ',2);">o</a>';
			s += '</span>';
			*/
			s += '<span class="cssHeadButtons">';
			/* try doing without this
			s += '<a class="cssHeadButton" title="Click" href="javascript: void closeContent(' + fInd + ')">' + '&bull;' + '</a>';
			*/
			s += '<a class="cssHeadButton" title="Click" href="javascript: void closeDisplay(' + fInd + ')">' + 'X' + '</a>';
			s += '</span>';
		}
		setAtt(div, "innerHTML", s);
		div.style.paddingLeft = pix((F[ind].lev+1)*levIndent);								// with this if outer lev=-1
    el.insertBefore(div, el.firstChild);
    if (app && false)		// if we put the same node in 2 places, the original is MOVED.
    	el.appendChild(div);
	} // addItem 

	var sInd = (fInd==0) ? fInd : fInd-1;		// start index - 0 if outer, else 1
	var o;
	o = createEl("div");
	setAtt(o, "className", "cssFoldersHead");
	while (F[sInd].lev > 0)
		sInd--;
	if (fInd > 0 && F[sInd].lev < 0)		// don't want outer leva unless clicked
		sInd++;
	var eInd = fInd;
	/*  Display "child" folders - not really necessary
	var lev = F[eInd].lev;
	if (fInd > 0)
		while (eInd < F.length-1 && F[eInd+1].lev > lev) {
			lev++;
			eInd++;
		}
	*/
	var lev = F[eInd].lev + 1;
	for (var i = eInd; i >= sInd; i--) {
		if (F[i].lev < lev) {
			//addDebug('i=' + i + ', fInd=' + fInd + ', sInd=' + sInd + ', lev=' + lev);
			addItem(i, o, (i == fInd), 1, lev<=1)
			lev--;
		}
	} 
	el.appendChild(o);
} // dispHeader 

/* Generate display ----------------------------------------------------------------------------------- dispLinks */
function dispLinks (fInd, el) {

	function addItem (ind, el, cn, app) {
		var o = createEl("div");
		var titS = '';

		setAtt(o, "className", cn);
		if (ind == -1) {
			setAtt(o, "innerHTML", ".....")
    	dispEl (o, 0);
		}
		else {
			setAtt(o, "id", L[ind].elId)
			var sp1 = createEl("span");
			var tit = L[ind].title;
			if (L[ind].realParentRefno)
			  tit += '&deg;';
			if (fInd == 0)
				titS = getFolderString(ind) + L[ind].url
			else
				titS = L[ind].url;
			setAtt(sp1, "innerHTML",
						'<a class="lrl" title="' + titS + '" href="' + L[ind].url + ' "target="_blank" onclick="callLink('+ind+')">' + tit + '</a>' );
						// '<a class="lrl" title="' + L[ind].url + '" href="javascript: void callLink(' + ind + ',3);">' + tit + '</a>' );
			sp1.style.width = "220px"; //el.offsetWidth - 14;
			o.appendChild(sp1);
			var sp2 = createEl("span");
			setAtt(sp2, "innerHTML",'<a title="Left click for menu" ' +
					 'href="javascript: void LinkMenu(' + ind + ',3);">o</a>');
			o.appendChild(sp2);
			// o.style.paddingLeft = pix((F[ind].lev+1)*levIndent);								// with this if outer lev=-1
		}
    el.appendChild(o);
	} // addItem 

	var currLev = F[fInd].lev;
	var trgtLev = currLev + 1;
	
	var o;
	o = createEl("div");
	setAtt(o, "className", "cssFolder");

	addItem (-1, o, "csitem cslnk");
	var parentRefno = F[fInd].refno;
	addDebug ("dispLinks -----------------------", 5);
	addDebug ("parentRefno: " + parentRefno + ", trgtLev: " + trgtLev, 5);
	for (var i = 0; (i < L.length); i++) {
		addDebug ("L["+i+"].parentRefno: " + L[i].parentRefno + ", refno: " + L[i].refno + ", folderInd: " + L[i].folderInd, 5);
		if (L[i].parentRefno==parentRefno)
				addItem(i, o, "csitem cslnk")
	} // for i
	F[fInd].displayBody = el.appendChild(o); 
} // dispLinks 

/* --------------------------------------------------------------------------------------------------- callLink */
function callLink(ind) {
	// called to update link list and, after 3 secs, update server
	if (inInit)
		return;
	if (UserRef==1) // Hammie
		if ((L[ind].parentRefno==134) || // WTest and all who sail in her
				(L[ind].parentRefno==582) || 
				(L[ind].parentRefno==407) || 
				(L[ind].parentRefno==429) || 
				(L[ind].parentRefno==444) || 
				(L[ind].parentRefno==473) || 
				(L[ind].parentRefno==480))
			return
	recentLinks += L[ind].refno + ',';
	resetUpdateTimers();
	currentAction = 6;	// update frequency
	currentLinkRefno = ind;
	resetUpdateTimers();
	timerUpdateLinks = setTimeout("updateLinksAndFolders(6)", 2000);
} // callLink

/* --------------------------------------------------------------------------------------------------- dispFolder */
function dispFolder (fInd, repos, init) {
	var o;
	if (F[fInd].open)
		return;
	addDebug("dispFolder ---------------------", 4);
	if (F[fInd].display != null) {
		o = F[fInd].display;
	}
	else {
		o = createEl("div");
		setAtt(o, "className", "cssFoldersDisplay");
		dispHeader (fInd, o);
		dispLinks (fInd, o);
	}
	el = cols[currColInd];

	var x = el.appendChild(o);

	var p = (document.body.clientHeight - (el.offsetTop + x.offsetHeight + x.offsetTop));
	addDebug("space left " + p, 1);
	if (p < 0 && currColInd < cols.length-1) {
		var x = el.removeChild(o);
		currColInd++
		el = cols[currColInd];
	}
	F[fInd].display = el.appendChild(o);
	F[fInd].displayParent = el;
	F[fInd].open = true;
  setAtt(F[fInd].folderListEl, "className", "cssFLp cssFLpSel");
  if (!repos) {
		F[fInd].fullOpen = true;
  	addDebug ("Adding to Of list", 1);
  	addOf (fInd, F[fInd].refno, init);			// Add to open folder list
  }
	if (!repos && !init) {
		resetUpdateTimers();
		timerUpdateFolders= setTimeout("updateLinksAndFolders()", 10000);
	}
	addDebug("Added selection for node " + fInd, 1);
	addDebug ('Height/Top - Col1: ' + F[fInd].displayParent.offsetHeight + '/' + F[fInd].displayParent.offsetTop +
						', display: ' + F[fInd].display.offsetHeight + '/' + F[fInd].display.offsetTop +
						', window: ' + document.body.clientHeight + ', screen: ' + window.screen.height, 1);
} // dispFolder 
// getObjectHeight

/* ------------------------------------------------------------------------------------------------- reposFolders */
// reposition folders - if byRefno, then search through F{} for new indexes (after update)
function reposFolders(byRefno) {
	addDebug("---- reposFolders, length: " + of.length, 10);
	for (var i = 0; i < of.length; i++)
		closeDisplay(of[i].fInd, true);
	if (byRefno) {
		var i = 0;
		while (i < of.length) {
			if ((of[i].fInd >= F.length) || (F[of[i].fInd].refno != of[i].refno)) { // moved - find new index
				for (var j = 0; j < F.length; j++) {
					if (of[i].refno == F[j].refno) { 
						of[i].fInd = j;
						addDebug("FOUND",8);
						break;
					} // if
				} // for j
				addDebug("i " + i + ", j " + j + ", len " + F.length + ", oflen " + of.length,8);
				if (j == F.length) {		// remove open folder if disappeared
					of.splice(i,1);
					i--;									// removed element, so we don't want i incremented
				}
				addDebug("new oflen " + of.length,8);
			} // if
			i++;
		} // for i
	} // if (byRefno)
	currColInd = 0;
	for (var i = 0; i < of.length; i++)
		dispFolder (of[i].fInd, true, false);
}

/* ------------------------------------------------------------------------------------------------- closeContent */
function closeContent (fInd) {
   if (inInit)
    return;
	var o = F[fInd].display;
	var ch = F[fInd].displayBody.childNodes;
	var disp;
	if (F[fInd].fullOpen) {
		F[fInd].fullOpen = false;
		disp = 0;
	}
	else {
		F[fInd].fullOpen = true;
		disp = 1;
	}
	dispEl (ch[0], 1-disp);
	for (var i = 1; i < ch.length; i++)
		dispEl (ch[i], disp);
	reposFolders();
}

/* ------------------------------------------------------------------------------------------------- closeDisplay */
// Close display for folder fInd; if NOT repos, then remove from open list
function closeDisplay (fInd, repos) {
   if (inInit)
    return;
	if (fInd >= F.length)				// happens if deleting folder!
		return;
	addDebug("Closing display: " + fInd, 9);
	if (!F[fInd].open || !F[fInd].display)
		return;
	addDebug("closeDisplay -------------------", 4);
	addDebug("Removing selection for node " + fInd, 4);
	try {
		if (fInd > 0)
			if (F[fInd].displayParent && F[fInd].display)				// ** IS IT DANGEROUS TO OMIT THE FOLLOWING?
				F[fInd].display = F[fInd].displayParent.removeChild(F[fInd].display);
	}
	catch (e) {
		// Don't know what is going on here, but it works OK! NOT!
	}
	finally {
	}
	F[fInd].open = null;
  setAtt(F[fInd].folderListEl, "className", "cssFLp");
  if (!repos) {
		F[fInd].fullOpen = null;
  	addDebug("Removing from Of list", 4);
  	removeOf (F[fInd].refno);
  	reposFolders();

		resetUpdateTimers();
		timerUpdateFolders= setTimeout("updateLinksAndFolders()", 10000);
  }
}

/* ---------------------------------------------------------------------------------------------------- newResize */
function newResize() {
	reposFolders();
}
/* ---------------------------------------------------------------------------------------------------- folderDetails */
function folderDetails() {
	for (var i = 0; i < F.length; i++) {
		var el = F[i].div;
		if (el)
			addDebug ("F["+i+"] id: " + el.getAttribute("id") + ", class: " + el.getAttribute("className"));
	}
}

/* -------------------------------------------------------------------------------------------------- getFolderString */
function getFolderString (ind) {
					// get list of folder names to L[ind], '/' separated
	var currRefno = 0;
	var currInd = 0;
	function getParentRefno(rn) {
					 // get parent refno for folder refno rn
		for (var i = 0; i < F.length; i++) {
			if (F[i].refno == rn)  {
				currRefno = F[i].refno
				currInd = i;
				return;
			}
		} // for
		currRefno = 0;
		currInd = 0;
	} // getParentRefno

	var s = ''; // string
	/*		// this causes an error.  Used only for page0 reference.  leave for now.
	if (L[ind].realParentRefno)
		currRefno = L[ind].realParentRefno;
	else
	*/
		currRefno = L[ind].parentRefno;
	currInd = 0;
	while ((F[currInd].refno != currRefno) && (currInd < F.length))
		currInd++;
	if (currInd < (F.length)) {
		while (currRefno > 0) {
			s = F[currInd].title + ' / ' + s;
			getParentRefno(F[currInd].parentRefno);
		}
	}
	return s;
} // getFolderString 
	
/* --------------------------------------------------------------------------------------------------------- doSearch */
function doSearch (Aword, AinTitle, AinUrl, AinPath) {
				 // search for Aword, in AinTitle and/or AinUrl - create new pseudofolder
	function dispHeader (el) { // append path to fInd, append to element el

		function addItem (el, first) {
			var div;
			var s;
			div = createEl("div");
				setAtt(div, "className", "fHead fHeadSel")
				s = '<a>' + 'Search: ' + Aword + '</a>';
				//s = 'Search results';
				s += '<span class="cssHeadButtons">';
				s += '<a class="cssHeadButton" title="Click" href="javascript: void closeDisplay(' + '0' + ')">' + 'X' + '</a>';
				s += '</span>';
			setAtt(div, "innerHTML", s);
			div.style.paddingLeft = 0;								// with this if outer lev=-1
    	el.insertBefore(div, el.firstChild);
		} // addItem 

		// var sInd = (fInd==0) ? fInd : fInd-1;		// start index - 0 if outer, else 1
		var o;
		o = createEl("div");
		setAtt(o, "className", "cssFoldersHead");
		/*
		while (F[sInd].lev > 0)
			sInd--;
		if (fInd > 0 && F[sInd].lev < 0)		// don't want outer leva unless clicked
			sInd++;
		var eInd = fInd;
		var lev = F[eInd].lev + 1;
		for (var i = eInd; i >= sInd; i--) {
			if (F[i].lev < lev) {
				//addDebug('i=' + i + ', fInd=' + fInd + ', sInd=' + sInd + ', lev=' + lev);
				addItem(i, o, (i == fInd), 1, lev<=1)
				lev--;
			}
		} 
		*/
		addItem(o)
		el.appendChild(o);
	} // dispHeader 

	function dispLinks (el) {

		function addItem (ind, el, cn, app) {
			var o = createEl("div");
			var addIt; 

			setAtt(o, "className", cn);
			if (ind == -1) {
				setAtt(o, "innerHTML", ".....")
    		dispEl (o, 0);
			}
			else {
				setAtt(o, "id", L[ind].elId)
				var sp1 = createEl("span");
				var tit = L[ind].title;
				if (L[ind].realParentRefno)
			  	tit += '&deg;';
				setAtt(sp1, "innerHTML",
								'<a class="lrl" title="' + getFolderString(ind) + L[ind].title + '" href="' + L[ind].url + ' "target="_blank" onclick="callLink('+ind+')">' + tit + '</a>' );
				sp1.style.width = "220px"; //el.offsetWidth - 14;
				o.appendChild(sp1);
				var sp2 = createEl("span");
				setAtt(sp2, "innerHTML",'<a title="Left click for menu" ' +
					 	'href="javascript: void LinkMenu(' + ind + ',3);">o</a>');
				o.appendChild(sp2);
			}
    	el.appendChild(o);
		} // addItem 

		var currLev = 0
	
		var o;
		o = createEl("div");
		setAtt(o, "className", "cssFolder");

		addItem (-1, o, "csitem cslnk");
		for (var i = 0; (i < L.length); i++) {
			if (!L[i].realParentRefno) {	// don't duplicated page 0 items
				addIt = false;
				if (AinTitle && (containsText (L[i].title, Aword)))
					addIt = true;
				if (!addIt && AinUrl && (containsText (L[i].url, Aword)))
					addIt = true;
				if (!addIt && AinPath && (containsText (getFolderString(i), Aword)))
					addIt = true;
				if (addIt)
					addItem(i, o, "csitem cslnk")
			}
		} // for i
		el.appendChild(o); 
	} // dispLinks 

	o = createEl("div");
	setAtt(o, "className", "cssFoldersDisplay");
	dispHeader (o);
	dispLinks (o);

	el = cols[currColInd];

	var x = el.appendChild(o);

	var p = (document.body.clientHeight - (el.offsetTop + x.offsetHeight + x.offsetTop));
	addDebug("space left " + p, 1);
	if (p < 0 && currColInd < cols.length-1) {
		var x = el.removeChild(o);
		currColInd++
		el = cols[currColInd];
	}
  	addDebug ("Adding to Of list", 1);
  	// addOf (fInd, F[fInd].refno, init);			// Add to open folder list
}

