/////////////////////////////////////////////////////////
// This object sets-up a simple hover activated menuing system
// that associates itself with the Element ID as supplied in menuId
function waldenDropdown (menuId)
	{
		
	/////////////////////////////////////////////////////////
	// This function shows the submenu to the user
	this.hover = function ()
		{
		thisObj.menuList.style.display = "block";
		}
		
		
	/////////////////////////////////////////////////////////
	// This function hides the submenu from the user
	this.mouseout = function ()
		{			
		thisObj.menuList.style.display = "none";
		}
		
	/////////////////////////////////////////////////////////
	// INITIALIZE THE  OBJECT:
	
	// declare a static, local instance of THIS for later use
	var thisObj = this;
	
	// grab the element that contains the menu and store it as a property of this object
	this.menuEl = document.getElementById( menuId );
	
	// find the UL that contains the actual drop-down and assign it to a property of this object
	//this.menuList = this.menuEl.childNodes[3];
	for( x2 = 0; x2 < this. menuEl.childNodes.length; x2++ )
		{
		if( this.menuEl.childNodes[x2].tagName && this.menuEl.childNodes[x2].tagName == "UL" )
			{
			this.menuList = this.menuEl.childNodes[x2];
			}
		}
	
	// hide the dropdown menus
	this.mouseout();
	
	// assign the hover event handler
	this.menuEl.onmouseover = this.hover;
	
	// assign the mouseout event handler
	this.menuEl.onmouseout = this.mouseout;
	}
	

/////////////////////////////////////////////////////////
// This function returns an array of all the elements with a classname that matches the one supplied
function getElementsByClassName(classname, node) 
	{
	if( !node ) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for( var i=0,j=els.length; i<j; i++ )
		{
		if( re.test(els[i].className )) 
			{
			a.push(els[i]);
			}
		}
	return a;
	}


/////////////////////////////////////////////////////////
// This function attaches a menu object to every element in the DOM
// that has a class of "has_submenu"
function init ()
	{
	menus = getElementsByClassName( "has_submenu" );
	for( x = 0, array_length = menus.length; x < array_length; x++ )
		{
		menus[x].waldenDropdownMenu = new waldenDropdown( menus[x].id );
		}
	}
	
/////////////////////////////////////////////////////////
// This function allows the menu system to start working as soon as
// the DOM has loaded. Thanks to http://www.thefutureoftheweb.com/blog/adddomloadevent
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();

/////////////////////////////////////////////////////////

addDOMLoadEvent(init);

