//--------------------------------------------------------------------------------------------------
// All material contained within this and associated downloaded pages is the property of 4thorder(TM) 
// Copyright © 2005.  All rights reserved.
//
// Author: Michael Falatine || Authors email: scripts@4thorder.us
//
// USAGE: You may use this script for commercial or personal use, however, the copyright is retained-
// by 4thorder (TM).
//
// For other free Scripts visit: http://www.4thorder.us/Scripts/
//---------------------------------------------------------------------------------------------------

// :::::::::::::::::::::::::
// :::: Global Variables :::
// :::::::::::::::::::::::::

var firstLoad=0
var GlobalECState=0 // 0=collapsed, 1=expanded; used for inital state of ALL branches

// :::::::::::::::::::::::::
// :::: Global Functions :::
// :::::::::::::::::::::::::
window.onload=InitializePage
function InitializePage()
{
document.getElementsByTagName("DIV")[0].setAttribute("id", "Container");
Icons();
attachEventhandlers();
}

// Attach event handlers to all images within container
function attachEventhandlers()
{
IMGCollection=document.getElementsByTagName("IMG");
THCollection=document.getElementsByTagName("TH");
if (IMGCollection!=null)
	{	for (l=0; l<IMGCollection.length; l++)
			{IMGCollection[l].onmouseup=onMouseUpHandler;}
	}
if (THCollection!=null)
	{	for (m=0; l<THCollection.length; m++)
			{THCollection[m].onmouseup=onMouseUpHandler;}
	}
}

function Icons()
{
// Collect all tables
TABLECollection=Container.getElementsByTagName("TABLE");
// Loop through and insert icon in all TH elements
if (TABLECollection!=null)
	{for (i=0; i<TABLECollection.length; i++)
		{	
			// Grab second TH element and FIRST TR element   !!! LEFT SIDE VERSION CHANGE !!!
			THCollection=TABLECollection[i].getElementsByTagName("TH");
			THElement=THCollection[0];

			TRCollection=TABLECollection[i].getElementsByTagName("TR");
			TRElement=TRCollection[1];	
					
			if(firstLoad==0)
				{// Yes: FIRST LOAD OF PAGE -- insert image
					if(GlobalECState==0)
						{// Global ECState is COLLAPSED (+)	(0)	
						THElement.innerHTML="<IMG border='0' src='images/plus.gif' width='25' height='25'>"				
						
						IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
						IMGCollection[0].setAttribute("ECState",0);
						
						TRElement.style.display='none';
						}
					else
						{// Global ECState is EXPANDED (-) (1)
						THElement.innerHTML="<IMG border='0' src='images/minus.gif' width='25' height='25'>"
						
						IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
						IMGCollection[0].setAttribute("ECState",1);
						
						TRElement.style.display='inline';				
						}
				}
			else
				{// No: FIRST LOAD OF PAGE - change image
				 //	Grab ECState of image and expand or collapse branch

				IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
				State=IMGCollection[0].getAttribute("ECState");
				if(State==0)
					{// ECState is COLLAPSED (+) (0)
					// Change Image
					IMGCollection[0].setAttribute("src","images/plus.gif");
					// Hide DIV
					TRElement.style.display='none';
					}
				else
					{// ECState is EXPANDED (-) (1)
					// Change Image
					IMGCollection[0].setAttribute("src","images/minus.gif");
					// Show DIV
					TRElement.style.display='inline';
					}
				}
			}
	}
if(firstLoad==0){firstLoad=1;}
}

// ::::::::::::::::::::::::
// :::: Event Handlers ::::
// ::::::::::::::::::::::::

function onMouseUpHandler()
{
if(window.event.button==1)
	{
	// Grab Target Element
	    imgObj=window.event.srcElement;
	    
	// Toggle ECState
	State=imgObj.getAttribute("ECState");
	if(State==0)
		{imgObj.setAttribute("ECState",1);}
	else{imgObj.setAttribute("ECState",0);}
	}
Icons();
}
