/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 * 
 * Subtabulous! Complex tabs using Prototype
 * 
 * Jonathan Katz
 * version 0.2 2007-06-14
 * http://creativecommons.org/licenses/by-sa/2.5/  
 */

var subtabs = Class.create();

subtabs.prototype = {
	initialize : function(element) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		
		this.menu = $A(this.element.getElementsByTagName('a'));
		//this.menuTop = $A(this.element.getElementsByTagName('div'));
        this.activarMenu(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
		//this.menuTop.each(this.menuTab.bind(this));
	},
	setupTab : function(elm) {
    	Event.observe(elm,'mouseover',this.show.bindAsEventListener(this),false)
        Event.observe(elm,'mouseout',this.hide.bindAsEventListener(this),false)
        Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activarMenuFijo :  function(varf) {
    var elm = this.menu.find(function(value) { 
      return value.href.match(/#(\w.+)/)[1] == varf; 
    });
    this.activarMenu(elm);
  },
	activate :  function(ev) {
        var elm = Event.element(ev);
		Event.stop(ev);
		this.activarMenu(elm);
	},
	activarMenu : function(elm) {

    	
        this.menu.without(elm).each(this.hidePanel.bind(this));
        this.showPanel(elm);    	
        $A(this.element.getElementsByTagName('li')).each(function(elm){
          $(elm).removeClassName('activo');
        });
		$(elm).up().addClassName('activo');
        /*var lis = 2;
        while ($(elm).up(lis).nodeName == 'LI') {
          $(elm).up(lis).addClassName('activo');
          lis +=2;
        }*/
    },
	menuTab : function(elm) {
		$(elm).up().observe('mouseover', function(){
      $(elm).up().addClassName('activojs');
    });
    $(elm).up().observe('mouseout', function(){
      $(elm).up().removeClassName('activojs');
    });
    },
    hidePanel : function(elm) {
		$(this.tabID(elm)).removeClassName('visible');
		//$(elm).up().removeClassName('visible');
	},
	showPanel : function(elm) {
    $(this.tabID(elm)).addClassName('visible');
	},
	hide : function(ev) {
		Event.element(ev).up().removeClassName('activojs');
	},
	show : function(ev) {
        Event.element(ev).up().addClassName('activojs');
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}
