var CustomTab = Class.create({
  initialize: function(tabnames, contents, selectid) {
    this.tabnames  = tabnames;
    this.contents = contents;
    this.selected = tabnames[selectid];
    this.htable = new Hash();
    this.funcs = new Hash();
    
    for(var i=0; i<tabnames.length; i++){
    	var tab = $(tabnames[i]);
    	var content = $(contents[i]);
    	tab.observe('click', this.selectTab.bind(this));
    	this.htable.set(tabnames[i], content);
    	if(i != selectid ){
    		if(content != null) content.hide();
    	}else{
    		tab.addClassName('selected');
    	}
    	if(arguments.length == 4){
    		this.funcs.set(tabnames[i], arguments[3][i]);
    	}else{
    	}
    }
  },

  selectTab: function(event) {
	  var el = Event.element(event);
	  var elid = el.identify();	  
	  if(elid != this.selected){
		 var tabid = this.selected;
		 var content = this.htable.get(tabid);
		 $(tabid).removeClassName('selected');
		 
		 // content.setStyle({display: 'none'});
		 if(content != null) content.hide();
		 el.addClassName('selected');
		 
		 content = this.htable.get(elid);
		 // content.setStyle({display: 'block'});
		 if(content != null) content.show();
		 this.selected = elid;
		 var func = this.funcs.get(elid);
		 if(func != null)
			 func();
	  }
	  
  }
});