var TabMenus = Class.create();
TabMenus.prototype =  
{
	initialize: function(element) 
	{
		this.element = $(element);
		this.menu = Array.from(this.element.getElementsByTagName('a'));
		this.show(this.getInitialMenu())
		this.menu.each(this.setupMenu.bind(this));
	},

    getInitialMenu: function()
    {
        var initial = null;
        var anchor = this.menuID(location);

        if (anchor == null)
        {
            initial = this.menu.first();
        }
        else
        {
            initial = this.menu.find(function(v,i) 
            { 
                return (this.menuID(v) == anchor); 
            }.bind(this));
        }
        
        return initial;
    },

	setupMenu: function(element) 
	{
		Event.observe(element, 'click', this.activate.bindAsEventListener(this), false);
	},

	activate: function(evt) 
	{
		var element = Event.findElement(evt, "a");
		Event.stop(evt);
		this.show(element);
		this.menu.without(element).each(this.hide.bind(this));
	},
	
	show: function(element) 
	{
	    Element.addClassName(element, 'active');
	    Element.removeClassName(this.menuID(element), 'hidden');
	},
	
	hide: function(element) 
	{
	    Element.removeClassName(element, 'active');
	    Element.addClassName(this.menuID(element), 'hidden');
	},
	
	menuID: function(from) 
	{
	    var match = from.href.match(/#(\w.+)/);
	    if (match != null)
	    {
	        match = match[1];
	    }
	    
	    return match;
	}
}

Event.observe(window, 'load', function() { new TabMenus('subMenu'); }, false);
