// ---------------------------------------------------------------------------
// --- Name:    Easy DHTML Treeview                                         --
// --- Original idea by : D.D. de Kerf                  --
// --- Updated by Jean-Michel Garnier, garnierjm@yahoo.fr                   --
// ---------------------------------------------------------------------------

/*****************************************************************************
Name : toggle
Parameters :  node , DOM element (<div> tag)
Description :     Description, collapse or unfold a branch
Author : Jean-Michel Garnier / D.D. de Kerf. 
Major modification by Chris Woodward.
*****************************************************************************/

function toggle(node) {
	// If the browser doesn't support childnode traversal return true so that any link is followed
	if (!node.childNodes)
		return true ;
		
	// find the child DIV containing the folder's children, which thanks to Mozilla has to be in a separate table cell.
	var tbody = node.parentNode.parentNode.parentNode ;
	if (!tbody) return true ;
	var tr			= findChild(tbody, "TR", 2) ; // Note second row
	if (!tr) return true ;										// If second row isn't there we must have a partial load so just follow the link
	var td			= findChild(tr, "TD", 1) ;
	var nextDIV = findChild(td, "DIV", 1);
	
	// Find the <tr> node containing the folder images
	//    node(div).table     .tbody     .tr
	var table		= findChild(node, "TABLE", 1) ;
	tbody				= findChild(table, "TBODY", 1) ;
	tr					= findChild(tbody, "TR", 1) ; // first row
	
	// Store the img nodes that need changing
	var imgPlus;
	var imgFolder;
	td = findChild(tr, "TD", 1) ;
	var a = findChild(td, "A", 1) ;
	imgPlus = findChild(a, "IMG", 1) ;
	if (!imgPlus) return true ;
	td = findChild(tr, "TD", 2) ;
	if (!td) return true ;
	// folder image is now optional so need to check that an image is in this TD and it is not the title TD
	a = findChild(td, "A", 1) ;
	if (a)
		imgFolder = findChild(a, "IMG", 1) ;		// Note this may be null
	// Unfold the branch if it isn't visible
	if (nextDIV.style.display == 'none') {
		// Change the +/- image (if there is an image)
		imgPlus.src = getImgDirectory(imgPlus.src) + "minus.gif";
		nextDIV.style.display = 'block';
	}
	// Collapse the branch if it IS visible
	else {
		// Change the +/- image (if there is an image)
		imgPlus.src = getImgDirectory(imgPlus.src) + "plus.gif";		
		nextDIV.style.display = 'none';
	}
	
	if (imgFolder)														// Check if there is a folder image
	{
		// Change the folder image (check if there is a lowsrc image as Mozilla decided to remove it)
		if (imgFolder.lowsrc + "" != "undefined" && imgFolder.lowsrc != imgFolder.src)
		{
			var othersrc ;
			othersrc = imgFolder.src ;
			imgFolder.src = imgFolder.lowsrc;
			imgFolder.lowsrc = othersrc;
		}
	}
	
	return false ;
}

/*****************************************************************************
Name : toggle2
Parameters :  node DOM element (<a> tag), folderCode String
Description :    if you use the "code" attribute in a folder element, toggle2 is called
instead of toggle. The consequence is that you MUST implement a selectFolder function in your page.
Author : Jean-Michel Garnier
*****************************************************************************/
function toggle2(node, folderCode) {
    toggle(node);
    return selectFolder(folderCode);
}

/*****************************************************************************
Name : getImgDirectory
Parameters : Image source path
Return : Image source Directory
Author : Jean-Michel Garnier
*****************************************************************************/

function getImgDirectory(source) {
    return source.substring(0, source.lastIndexOf('/') + 1);
}

/*****************************************************************************
Name : findChild
Parameters :  node to search, tag to search for, nth tag to return
Description : Work around of mozilla's finding text elements between tags on separate lines where IE doesn't (which makes firstChild ambiguous).
Use to find the nth instance of a tag type. E.g. findChild(this, "TD", 2) will find the 2nd TD child of this node.
Author : Chris Woodward
*****************************************************************************/
function findChild(node, tag, pos)
{
	var i ;
	var n = 0 ;
	for (i=0; i < node.childNodes.length; i++)
	{
		if(node.childNodes.item(i).nodeName == tag) 
			n++ ;
		if (n == pos)
			return node.childNodes.item(i) ;
	}
	return null ;
}

/************************************
************* IMPORTANT *************
*************************************

The functions below are NOT used by the DHTML treeview. Netherless, have a look bc some be useful if you
need to make XSLT on the client (since IE 5.5 and soon Mozilla !)

/*****************************************************************************
Name : jsTrim
Parameters : value, String
Return : the same String, with space characters removed
Description : equivalent to trim function
Author : Jean-Michel Garnier
*****************************************************************************/

function jsTrim(value) {
    var result = "";
    for (i=0; i < value.length; i++) {
        if (value.charAt(i) != ' ') {
            result += value.charAt(i);
        }
    }
    return result;
}

/*****************************************************************************
Name : findObj
Parameters :
- n String object's name
- d Document document
Return : a reference on the object if it exists
Description : Search an object in a document from its name.
Author : Macromedia
*****************************************************************************/

function findObj(n, d) {
  var p, i, x;
  if (!d)
    d = document;
  if ( (p=n.indexOf("?") )>0 && parent.frames.length ) {
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
  }
  if (!(x=d[n])&& d.all )
	x = d.all[n];
  for (i=0; !x && i < d.forms.length; i++)
	x = d.forms[i][n];
  for (i=0; !x && d.layers && i<d.layers.length; i++)
	x = findObj(n, d.layers[i].document);

  return x;
}


