﻿//<![CDATA[
window.addEvent('load', function() {
    Element.Events.outerClick = {
        base : 'click',

        condition : function(event){
            event.stopPropagation();
            return false;
        },
        onAdd : function(fn){
            this.getDocument().addEvent('click', fn);
        },
        onRemove : function(fn){
            this.getDocument().removeEvent('click', fn);
        }
    };
    
    window.addEvent('domready',function() { 
        (function($) {
            /* for keeping track of what's "open" */
            var activeClass = 'dropdown-active', showingDropdown, showingMenu, showingParent;
            /* hides the current menu */
            var hideMenu = function() {
	            if(showingDropdown) {
		            showingDropdown.removeClass(activeClass);
		            showingMenu.setStyle('display','none');
	            }
            };
            /* recurse through dropdown menus */
            $$('.dropdown').each(function(dropdown) {
	            /* track elements: menu, parent */
	            var menu = dropdown.getNext('div.dropdown-menu'), parent = dropdown.getParent('div');
	            /* function that shows THIS menu */
	            var showMenu = function() {
		            hideMenu();
		            showingDropdown = dropdown.addClass('dropdown-active');
		            showingMenu = menu.setStyle('display','block');
		            showingParent = parent;
	            };
	            /* function to show menu when clicked */
	            dropdown.addEvent('click',function(e) {
		            if(e) e.stop();
		            showMenu();
	            });
	            /* function to show menu when someone tabs to the box */
	            dropdown.addEvent('focus',function() {
		            showMenu();
	            });
	            dropdown.addEvent('outerClick', function(){
                hideMenu();
            });
            });	        
        })(document.id);
    });
});
 //]]> 
