/************************************************************

Written by J. David Eisenberg 
http://www.alistapart.com/articles/domtricks2/

Revised by Bill Hanff April 6, 2004
* Accepts Dynamic Variables
* Multiple-Calls within the same script
* showMultiple Preference


METHODS:
dropdown(); -> Called when page is loaded into the browser

VARIABLES:

head -> head element for the dropdown menu
sub -> sub element for the dropdown menu
parentnode -> Used for event bubbling to the type of parent object clicked
numMenus -> number of menus  with sub-headers hidden. Needed to set listeners 

* Optional *
multiple -> preset set to 1, set multiple to 0 if you only want one menu available at a time.
			-> buggy, especially in WinIE, not  recomended for use
img -> if there is a dropdown image (arraw, plus/minus sign, folder icon, ect.)
    -> Note image must be a .gif and end names with '_closed' and '_open' 
    -> Example: arrow_closed.gif, arrow_open.gif
imgPath -> path to image



EXAMPLE:
window.onload=function(){
	dropdown('head', 'sub', 'LI', 3, 'arrow', '/images/', 1);
	}


NOTES:
* Requires utilities.js be linked 

************************************************************/


var showMultiplePreset=1;

//var showMultiple=showMultiplePreset;
var imgDirectory="";

var drpdnMenus=new Array();


function findOwner( evt )
{
    var node;
    if (isNav6)
    {
        node = evt.target;
        while (node)
        {
            if ( node.nodeType == Node.ELEMENT_NODE &&
                 node.nodeName == drpdnparent)
            {
                return node;
            }
            node = node.parentNode;
        }
    }
    else if (isIE4)
    {
        node = window.event.srcElement;
        while (node)
        {
            if (node.tagName == drpdnparent)
            {
                return node;
            }
            node = node.parentElement;
        }
    }
    return null;
}

function highlight( evt )
{
    var divObj = findOwner( evt );
    divObj.style.cursor = "help";
    divObj.style.color = "#466681";
}

function dim( evt )
{
    var divObj = findOwner( evt );
    divObj.style.cursor = "default";
    divObj.style.color = "#666";
}

function getObject( nameStr )
{
    if (isNav6)
    {
        return document.getElementById( nameStr );
    }
    else if (isIE4)
    {
        return document.all[nameStr];
    }
}


function setDrpDwnVars( obj )
{
	 obj=obj.slice(0,-1);
	 
	 vars = drpdnMenus;
        for(i=0; i<vars.length; i++)
        {
            if (vars[i][0] == obj)
           {
               /*
               drpdnMenus[arrayNum][0]=head;
					drpdnMenus[arrayNum][1]=sub;
					drpdnMenus[arrayNum][2]=parentnode;
					drpdnMenus[arrayNum][3]=numMenus;
					drpdnMenus[arrayNum][4]=drpdnimg;
					drpdnMenus[arrayNum][5]=drpdnimgpath;
					drpdnMenus[arrayNum][6]=showMultiple;
               */
               
               drpdnhead=vars[i][0];
					drpdnsub=vars[i][1];
						
					ddHeadLength=(drpdnhead.length);
						
					drpdnparent=vars[i][2];
					ddNumLists=vars[i][3];
					 
               drpdnimg = (vars[i][4]!=null) ? vars[i][4] : null; 
					drpdnimgpath = (vars[i][5]!=null) ? vars[i][5] : null; 
					showMultiple = (vars[i][6]!=null) ? vars[i][6] : showMultiplePreset; 
					return;
            }
       }
     return null;
}


function showMenu( evt )
{
    
    var owner = findOwner( evt );
    var divNum;
	 
	 if (isNav6)
    {
        divNum = owner.attributes.getNamedItem("id").nodeValue;
    }
    else if (isIE4)
    {
        divNum = owner.id;
    }
	 
	 setDrpDwnVars(divNum);
	 
	 owner.style.color = "#666";
	 divNum = parseInt( divNum.substr(ddHeadLength));
    
    
	 if (getIdProperty( drpdnsub + divNum, "display") != "block" )
    {
    	 	if(!showMultiple){
	    		for (i=0; i < ddNumLists; i++)
    			{	
   	 		setIdProperty(drpdnsub + i, "display", "none");
				
					if(drpdnimg){
					document.images[drpdnimg + i].src = drpdnimgpath+drpdnimg+"_closed.gif";
   	 			}
   	 		}
    	 	}
        setIdProperty(drpdnsub + divNum, "display", "block");
      
        			if(drpdnimg){
					document.images[drpdnimg + divNum].src = drpdnimgpath+drpdnimg+"_open.gif";
    				}
    }
    else
    {
        //document[drpdnsub + divNum].style.display="none"
        setIdProperty(drpdnsub + divNum, "display", "none");
        			if(drpdnimg){
					document.images[drpdnimg + divNum].src = drpdnimgpath+drpdnimg+"_closed.gif";
    				}
    }
}

function setupDropdwnAction( node )
{
    if (isNav6)
    {
        node.addEventListener( "click", showMenu, false);
        node.addEventListener( "mouseover", highlight, false );
        node.addEventListener( "mouseout", dim, false );
    }
    else if (isIE4)
    {
        node.onclick = showMenu;
        node.onmouseover = highlight;
        node.onmouseout = dim;
    }
}

function setupEvents()
{
    var i;
    var theNode;

    for (i=0; i<vocabList.length; i++)
    {
        theNode = document.getElementById( drpdnhead + i );
        setupDropdwnAction( theNode );
    }
}

function dropdown(head, sub, parentnode, numMenus, img, imgPath, multiple)
{	
	drpdnhead=head;
	drpdnsub=sub;
		
	ddHeadLength=(drpdnhead.length);
		
	drpdnparent=parentnode;
	
	showMultiple = (multiple!=null) ? multiple : showMultiplePreset; 
	drpdnimg = (img!=null) ? img : null; 
	drpdnimgpath = (imgPath!=null) ? imgPath : null; 
	
	//alert(showMultiple);
	
	var arrayNum=drpdnMenus.length;
	
	drpdnMenus[arrayNum]=new Array(7);
	
	drpdnMenus[arrayNum][0]=head;
	drpdnMenus[arrayNum][1]=sub;
	drpdnMenus[arrayNum][2]=parentnode;
	drpdnMenus[arrayNum][3]=numMenus;
	drpdnMenus[arrayNum][4]=drpdnimg;
	drpdnMenus[arrayNum][5]=drpdnimgpath;
	drpdnMenus[arrayNum][6]=showMultiple;
	
	 var i;
    var obj;

    for (i=0; i < numMenus; i++)
    {
        obj = getObject( drpdnhead + i );
        setupDropdwnAction( obj );
    }
}

setBrowser();

