// JavaScript Document

/*
##### Dropdowns #####
##### v1.0 8/06 www.louddog.com
Custom styled dhtml <select> replacement. Permits full CSS control of dropdown visual style.
Built for IE6+ Firefox1.5+
Works in NN&+ and Safari2.0+, but currently disabled to address dhtml/flash layer bug (js function swapHtml)
Consists of:
    mouse event -   used with blurDD. Detetcs if cursor is clicked outside of opened dropdown, 
                    and closes it. Loaded as an onclick event handler in the <body> tag.
    showDD -        open dropdown
    hideDD -        close dropdown
    blurDD -        Uses coordinates passed from showDD to determine the space 
                    outside of the dropdown. If mouse event occurs outside of the 
                    open dropdown, it closes.
In the markup, the dropdown is built as an unordered list (<ul>) nested in 
a <div class="dropdown">.

*/
var ddState; //Flags the display status of the drop-down to close when clicked outside
//Information about the dimensions and position of dropdown.
//Enables the DOM to sense if the cursor is clicked outside of the dropdown, to close it.
var ddWidth;
var ddHeight;
var ddX;
var ddY;
var ddMargin;
var leftPos;
var rightPos;
var topPos;
var bottomPos;



//Detect the position of the mouse cursor.
//Used to calculate if the cursor is clicked outside of the dropdown to close it.
if (!document.all)  document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;



function getMouseXY(e) 
{
    if (document.all) 
    {
      if ( typeof( event ) != 'undefined' ) 
      {
				//cursorX = event.clientX + document.body.scrollLeft
				//cursorY = event.clientY + document.body.scrollTop
        cursorX = event.clientX
        cursorY = event.clientY
      } 
    } 
    else 
    {
        cursorX = e.pageX
        cursorY = e.pageY
    }  
}

function findPosX(obj) 
{
    var curleft = 0;
    
    while (obj.offsetParent) 
    {
        curleft += obj.offsetLeft;
    		obj = obj.offsetParent;
	}
    
    return curleft;
}

function findPosY(obj) 
{
    var curtop = 0;
    
    while (obj.offsetParent) 
    {
    	curtop += obj.offsetTop
    	obj = obj.offsetParent;
	}
    return curtop;
}




// Display dropdown when the "closed" state is clicked
function showDD(obj) 
{
		//alert(document.body.clientHeight)
		var buttonWidth = 20 //the width of the open/close graphic button, to include in width calculations

    //Traverse the tree to identify the nodes that will be manipulated
    
    var block = obj.getAttribute("id");
    var findUl = document.getElementById(block).getElementsByTagName("ul");

    //Display the dropdown
    findUl[0].style.display = "block";
		
		//Gets an array of dom objects that have the class dropdown in a div tag
		dropDownArr = document.getElementsByClassName("dropdown");
		
		// If the ddState flag has a value then loop through dropDownArr 
		// until you find the ddState that equals the one in dropDownArr and hide that specific
		if (ddState) {			
			for (j=0;j<=(dropDownArr.length - 1); j++) {
				if (ddState == dropDownArr[j].id && dropDownArr[j].id !== block) {
						var hideUl = document.getElementById(dropDownArr[j].id).getElementsByTagName("ul");
						hideUl[0].style.display = "none";
						ddState = ""
					}
				}
			}		

    
    //Get the nodes' properties
    ddWidth = document.getElementById(block).offsetWidth;
    findUl[0].style.width = ddWidth-buttonWidth+"px";
    ddHeight = findUl[0].offsetHeight;
    ddX = findPosX(obj);
    ddY = findPosY(obj);
    //ddMargin = document.getElementById('container').offsetLeft;

		
		
		
    //Calculate the coordinates of the space outside of the dropdown.
    //Used when the cursor is clicked outside of the dropdown to close it.
    
    topPos = document.getElementById(block).offsetTop;
    leftPos = ddX;
    rightPos = leftPos + findUl[0].offsetWidth+buttonWidth;
    topPos = ddY;
    bottomPos = topPos+findUl[0].offsetHeight;
    
    //Flag the DOM that the dropdown is displayed, and the ID to close when the list is clicked outside
    ddState = block;

}




//Close the dropdown after one of its options is clicked
function hideDD(obj) 
{
    var findUl = obj.parentNode.parentNode;
    findUl.style.display = "none";
}



//Close the dropdown on "blur" -- when the cursor clicks outside of the dropdown while its displayed
//Detects the click's position based on values set in the function showDD
function blurDD(obj) 
{
    if (document.getElementById(obj)) 
    {
	    var hideUL = document.getElementById(obj).getElementsByTagName("ul");
		if (cursorX < leftPos || cursorX > rightPos || cursorY < topPos || cursorY > bottomPos) 
		{
            hideUL[0].style.display = "none";
		}
    	var findA = document.getElementById(obj).getElementsByTagName("a");
    	findA.onclick = function() {ddButton(obj);} 

	}

}

/* Swap the dropdown javascript commands when the button is clicked */
function ddButton(obj) 
{
    var findDiv = obj.parentNode;
    var findA = findDiv.getElementsByTagName("a");
    findA[0].onclick = function() {ddButtonClose(obj); return false;} 
}

function ddButtonClose(obj) 
{
    var findDiv = obj.parentNode;
    var findA = findDiv.getElementsByTagName("a");
    var findUl = findDiv.parentNode.getElementsByTagName("ul");
    findUl[0].style.display = "none";
    findA[0].onclick = function() {ddButton(obj); return false;} 
}

function ddGotopath( URL )
{
  document.location = URL;
}
