var dropdown_rootNode = null; // Reference to root node of the current navigation.
var dropdown_timer = null; // Timer to hide all menu's on a 'onmouseout'.
var dropdown_timerWait = 750; // Time to wait before closing all open menu's.
var dropdown_zIndex = 5; // zIndex of the navigation.

function createDropdown() {
    if (document.getElementById) { 
        try {
			var rootNode = document.getElementById('mainnav');
            // Add javascript to items in navigation and hide submenu's.
            for (var j = 0; j < rootNode.childNodes.length; j++) {
            	var liNode = rootNode.childNodes[j];
				if (liNode.nodeName == 'LI') {
                	// Init subitems
                     __initDropdown(liNode);
					// Add a mouseover-event to each list item.
                    liNode.onmouseover = function() {
                    	try {
                        	clearTimeout(dropdown_timer);
                        }
                        catch(e) { }
                        __showDropdown(this);
                        //this.className = 'hover';
                    }

                    liNode.onmouseout = function() {
                    	try {
                        	clearTimeout(dropdown_timer);
                        }
                        catch(e) { }
                        dropdown_timer = setTimeout('__hideDropdown()', dropdown_timerWait);
                        //this.className = '';
                    }
                 }
			}
		}
        catch(e) {
            // Ignore
        }
    }
}

// Add classes and mouse-events to subitems.
function __initDropdown(rootNode) {
    //dropdown_zIndex += 2;
    var zIndex = dropdown_zIndex;
    // Open subitems
    for (var i = 0; i < rootNode.childNodes.length; i++) {
        var ulNode = rootNode.childNodes[i];
        if(ulNode.nodeName == 'UL') {
            ulNode.style.display = 'none';
            ulNode.style.visibility = 'hidden';
            //ulNode.style.left = findPosX(rootNode) + 'px';
            ulNode.style.zIndex = zIndex;

            for (var j = 0; j < ulNode.childNodes.length; j++) {
                var liNode = ulNode.childNodes[j];
                if(liNode.nodeName == 'LI') {
                    // liNode.style.zIndex = zIndex + 1;
                    // Init subitems
                    __initDropdown(liNode);
                    // Add a mouseover-event to each list item.
                    liNode.onmouseover = function() {
                        try {
                            clearTimeout(dropdown_timer);
                        }
                        catch(e) { }
                        //this.className += ' hover';
                        __showDropdown(this);
                    }

                    liNode.onmouseout = function() {
                        try {
                            clearTimeout(dropdown_timer);
                        }
                        catch(e) { }
                        //this.className = this.className.replace('hover', '');
                        dropdown_timer = setTimeout('__hideDropdown()', dropdown_timerWait);
                    }
                }
            }
        }
    }
}

// Show submenu
function __showDropdown(rootNode) {
    if(dropdown_rootNode == null) {
        dropdown_rootNode = rootNode.parentNode.parentNode;
    }
	
    // Hide siblings' submenu's
    var ulNode = rootNode.parentNode;
    for(var i = 0; i < ulNode.childNodes.length; i++) {
        var liNode = ulNode.childNodes[i];
        if(liNode.nodeName == 'LI') {
            if(liNode != rootNode) {
                __hideDropdown(liNode);
            }
        }
    }

    // Show submenu's (if any)
    for(var i = 0; i < rootNode.childNodes.length; i++) {
        var ulNode = rootNode.childNodes[i];
        if(ulNode.nodeName == 'UL') {
            ulNode.style.display = 'block';
            ulNode.style.visibility = 'visible';
        	if (parseInt(findPosX(ulNode)+ulNode.offsetWidth)>parseInt(findPosX(document.getElementById('mainnav'))+document.getElementById('mainnav').offsetWidth+getMargin(document.getElementById('mainnav')))) {
        		//alert('out of bounds');
        		//ulNode.style.marginLeft = parseInt(-ulNode.offsetWidth) +'px';
        	}
        }
    }
}

// Hide submenu's
function __hideDropdown(rootNode) {
    if(__hideDropdown.arguments.length < 1) {
        var tmpNode = dropdown_rootNode;
        dropdown_rootNode = null;

        if(tmpNode != null) {
            for(var i = 0; i < tmpNode.childNodes.length; i++) {
                var ulNode = tmpNode.childNodes[i];

                if(ulNode.nodeName == 'UL') {
                    for(var j = 0; j < ulNode.childNodes.length; j++) {
                        var liNode = ulNode.childNodes[j];
                        if(liNode.nodeName == 'LI') {
                            __hideDropdown(liNode);
                        }
                    }
                }
            }
        }
    } else {
        for(var i = 0; i < rootNode.childNodes.length; i++) {
            var ulNode = rootNode.childNodes[i];

            if(ulNode.nodeName == 'UL') {
                ulNode.style.display = 'none';
                ulNode.style.visibility = 'hidden';

                for(var j = 0; j < ulNode.childNodes.length; j++) {
                    var liNode = ulNode.childNodes[j];
                    if(liNode.nodeName == 'LI') {
                        __hideDropdown(liNode);
                    }
                }
            }
        }
        //rootNode.className = '';
    }
   
}


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

function findPosY(obj) {
var curtop = 0;

  if (obj.offsetParent)  {
    while (obj.offsetParent) {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}


function getHeight() {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function getMargin(elm) {
	var elmMargin = 0;
	if(document.all) { // IE
		elmMargin=parseInt(elm.currentStyle.marginLeft)+parseInt(elm.currentStyle.marginRight);
	} else { // Mozilla
		elmMargin=parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-left'))+parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-right'));
	}
	return elmMargin;
}

//if (window.attachEvent) window.attachEvent("onload", createDropdown);

