var W3CDOM = (document.getElementsByTagName && document.createElement);

if (W3CDOM)
	document.write('<link rel="stylesheet" href="onlyone_js.css" />');

window.onload = function () {
	if (!W3CDOM) return;
	// start up W3C DOM scripts
	rollup();
}

window.onload=rollup;

function rollout(objMenuitem, objEvent)
{
    var iKeyCode;

    // Check if from a keyboard - non IE, but
    // irrelevant as tab doesn't trigger the 
    // keypress event in IE
    if (objEvent && objEvent.type == 'keypress')
    {
        if (objEvent.keyCode)
            iKeyCode = objEvent.keyCode;
        else if (objEvent.which)
            iKeyCode = objEvent.which;

        // If it's not the enter key or space key, 
        // pass control back to the browser
        if (iKeyCode != 13 && iKeyCode != 32)
            return true;
    }

    // Work out what we need to do
    if (objMenuitem.nextSibling.style.display == 'block')
        var strDisplay = 'none'
    else
        var strDisplay = 'block';

    // Close any nested lists that are open
    var objMenu = document.getElementById('mainnav');
    var objNested = objMenu.getElementsByTagName('ul');

    for (var i=0; i<objNested.length; i++)
        if (objNested[i].style.display == 'block')
            objNested[i].style.display = 'none';

    objMenuitem.nextSibling.style.display = strDisplay;
    
    // Keep any parent menus for this item open
    while (objMenuitem.parentNode.parentNode.id != 'mainnav')
    {
        objMenuitem = objMenuitem.parentNode.parentNode;
        objMenuitem.style.display = 'block';
    }

    // Stop the browser requesting the link
    return false;
}

function rollup()
{
    var bRollup, objLinks, objNode, objAnchor;

    // Check we're working with a DOM compliant browser
    if (document.getElementById && document.createElement)
    {
        var strLocation = window.location;

        var objMenu = document.getElementById('mainnav');
				var topLevelItemPreviouslySelected = false;
				var previously_selected_link = get_previously_selected_link();
								
				// Do a pre check for the current url as a top level link
        var objTopLevelItems = objMenu.getElementsByTagName('li');
        for (var link_index=0; link_index<objTopLevelItems.length; link_index++) {
					var topLevelItem = objTopLevelItems[link_index];
					var topLevelItemLink = topLevelItem.firstChild;
					
					while ( topLevelItemLink != undefined && 'a' != topLevelItemLink.nodeName.toLowerCase() ) {
						topLevelItemLink = topLevelItemLink.nextSibling;
					}
					
					if (topLevelItemLink == undefined) {
						continue;
					}
					
					// We have a top level link
					if (topLevelItemLink.href == strLocation) {
						keep_node_open(topLevelItemLink,strLocation);
						set_previously_selected_link(strLocation);						
						break;
					} else if (topLevelItemLink.href == previously_selected_link) {
						topLevelItemPreviouslySelected = true;
					}
				}				

        var objNested = objMenu.getElementsByTagName('ul');
        // Hide each of the nested unordered list
        for (var i=0; i<objNested.length; i++)
        {
            objLinks = objNested[i].getElementsByTagName('a');
            
            // Only hide, if the current location is not found in the list
            bRollup = true;

            for (var j=0; j<objLinks.length; j++)
            {
              if (objLinks[j].href == strLocation)
              {                     
                bRollup = false;
                keep_node_open(objLinks[j],strLocation);
								// Set session cookie so we know strLocation was selected
								// when the next link is clicked
								set_previously_selected_link(strLocation);
              } else if (objLinks[j].href == previously_selected_link) {
								objLinks[j].id = 'jsPreviousLinkClicked';
							}
            }
            
            // If the firstChild of the ul is a link and is the currentpage
            // don't roll it up 
            
            // find parent <li> element
            var parent_node = objNested[i].parentNode;
            while (parent_node != undefined && 
                   parent_node.nodeName.toLowerCase() != 'li') {
              parent_node = parent_node.parentNode; 
            }
            
            // See if we can find a link as an immediate child
            var nodeWithLink = null;
            if (parent_node != undefined) {
              nodeWithLink = parent_node.firstChild;
            }
            while ( nodeWithLink != undefined && 'a' != nodeWithLink.nodeName.toLowerCase() ) {
              if (nodeWithLink.nextSibling) {
                nodeWithLink = nodeWithLink.nextSibling;
              }
              else {
                nodeWithLink = null;
              }
            }
            
            bIntroPage = false;
            if (nodeWithLink) {
              bIntroPage = true;
              if (nodeWithLink.href == strLocation)
              {
                bRollup = false;
                keep_node_open(nodeWithLink,strLocation);
								set_previously_selected_link(strLocation);
              } else if (nodeWithLink.href == previously_selected_link) {
								nodeWithLink.id = 'jsPreviousLinkClicked';
							}
            }
            
            if (bRollup == true)
                objNested[i].style.display = 'none';
            else
                objNested[i].style.display = 'block';

            // The top-level is a real page which is a link already
            if (bIntroPage)
              continue;
              
            // Place the top-level text in an anchor tag
            objNode = objNested[i].parentNode;
            strContent = objNode.firstChild.data;
                        
            objAnchor = document.createElement('a');
            objAnchor.href = '#';
            objAnchor.onclick = function(event){return rollout(this, event);}
            objAnchor.onkeypress = function(event){return rollout(this, event);}
            objAnchor.appendChild(document.createTextNode(strContent));

            objNode.replaceChild(objAnchor, objNode.firstChild);
        }

        // Keep any parent menus for the current item
        if (document.getElementById('jsKeepOpen')) {
	        var objKeepOpen = document.getElementById('jsKeepOpen');
					keep_parent_nodes_open(objKeepOpen);
        } else {
					// No menu item selected 
					// Check the menu cookie for a previously selected link
					var objKeepOpen = document.getElementById('jsPreviousLinkClicked');
					if (objKeepOpen) {
						keep_parent_nodes_open(objKeepOpen);
						if (topLevelItemPreviouslySelected) {
							var topLevelUL = objKeepOpen.parentNode.getElementsByTagName('ul');
							if (topLevelUL.length) {
								topLevelUL[0].style.display = 'block';
							}
						} else {
							keep_parent_nodes_open(objKeepOpen);
						}						
					}
				}
    }
}

function keep_parent_nodes_open(objKeepOpen) {
  while (objKeepOpen.parentNode.id != 'mainnav') {
      objKeepOpen = objKeepOpen.parentNode;
      objKeepOpen.style.display = 'block';
  }	
}

// Returns the link that was last clicked on the menu based on a javascript
// cookie
function get_previously_selected_link() {
	return readCookie('only_one_menu');
}

function set_previously_selected_link(link) {
	return createCookie('only_one_menu',link,0);	
}

function keep_node_open(aNode,strLocation) {  
  // Added by John Hunter  
  // remove link for current page

  aNode.id = 'jsKeepOpen';
  // RT 2007-03-30, RJ doesn't need
  // objNode = aNode;
  // strContent = objNode.firstChild.data;
  // var objCurrentPage = document.createElement('normal');
  // objCurrentPage.appendChild(document.createTextNode(strContent));
  // objNode.parentNode.replaceChild(objCurrentPage, objNode);
  
  // // Add an id so we can keep the parents open
  // objCurrentPage.id = 'jsKeepOpen';
}

// http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
