menu = {
    menuId:'',
    active:{},
    init:function(menuId){
        this.menuId = menuId;
        this.hideSubs();
        this.checkShouldBeActive();
        this.addOnClicks();
    },
    addOnClicks:function(){
        $('#'+this.menuId + ' a').each(function(){
            if($(this).attr('href') == undefined){
                $(this).click(function(){
                    menu.hideAll($(this).attr('title'));
                    menu.hideShow(this);
                });
            }
        });
    },
    /**
    * This hides all the sub menus.
    */
    hideSubs:function(){
        $('#'+this.menuId + ' ul').css('display','none');
    },
    /**
    * Sets a menu to active for reference in code late.
    */
    setActive:function(title,active){
        if(active){
            this.active[title] = true;
        }else{
            this.active[title] = false;
        }
    },
    /**
    * Checks to see if a given menu item is active.
    */
    isActive:function(title){
        if(this.active[title]==false || this.active[title] == undefined){
            return false;
        }else{
            return true;
        }
    },
    /**
    * Hide/Shows a given sub menu.
    */
    hideShow:function(item,slide){
        if(item != 'object'){
            item = $(item);
        }
        if(slide==undefined){
            slide = true;
        }
        var title = $(item).attr('title');
        if(menu.isActive(title)){
            //If menu item is active
            if(slide){
                item.parent().children('ul').slideUp();
            }else{
                item.parent().children('ul').css('display','none');
            }
            item.removeClass('active');
            menu.setActive(title,false);
        }else{
            //If menu item is not active.
            if(slide){
                item.parent().children('ul').slideDown();
            }else{
                item.parent().children('ul').css('display','');
            }
            item.addClass('active');
            menu.setActive(title,true);
        }

    },
    /**
    * Hides all menus accept the one that is being activared.
    */
    hideAll:function(title){
        $.each(this.active,function(key){
            if(title != key && this==true){
                menu.hideShow($('#'+menu.menuId + ' a[title="'+key+'"]'));
            }
        });
    },
    checkShouldBeActive:function(){
        var location = new String(window.location);
        var locLength = location.length;
        $('#'+this.menuId + ' li ul a').each(function(){
            var href = $(this).attr('href');
            //alert(href);
            var len = href.length;
            //alert(location.substring((locLength-len),locLength)+' '+href);
            if(href!=undefined && location.substring((locLength-len),locLength)==href){
                //alert(typeof($(this).parent().parent().parent().children('a:nth-child(1)')));
                //alert($(this).parent().parent().parent().children('a:nth-child(1)'));
                menu.hideShow($(this).parent().parent().parent().children('a:nth-child(1)'),false);
            }
        });
    }
    
}
