var Tabber = Class.create({
	initialize: function(surface, options)
	{
		this.options = {
		};
		
		Object.extend(this.options, options);
		
		this.surface = $(surface);
		this.surface.addClassName('tabber');
		this.tabs = new Array();
		this.tabsHash = new Object();
		
		this.links = Builder.node('ul', {className: 'tabberlinks'});
		this.surface.insert(this.links);
		
		document.observe('tabber:showtabwithname', this.showTabWithNameListener.bind(this));
	},
	
	addTab: function(element, name)
	{
		var link = Builder.node("li", {}, name);
		link.observe('click', this.showTabWithName.curry(name).bind(this));
		this.links.insert(link);		
		
		this.tabs.push(element);
		this.tabsHash[name] = this.tabs.length-1;
		this.surface.insert(element);
	},
	
	getTabAtIndex: function(index)
	{
		if(Object.isUndefined(index))
			return false;
			
		return this.tabs[index];
	},
	
	showTabWithNameListener: function(event)
	{
		var name = event.memo;
		this.showTabWithName(name);
	},
	
	showTabWithName: function(name)
	{
		this.tabs.each(function(tab){
			tab.hide();
		});
		
		this.links.childElements().each(function(elem){
			if(elem.innerHTML == name)
				elem.addClassName('selected');
			else
				elem.removeClassName('selected');
		});
		
		var elem = this.getTabWithName(name);
		
		if(elem)
			elem.show();
		else
			console.log("showTabeWithName cant find element with name: "+name);
	},
	
	getTabWithName: function(name)
	{
		var index = this.tabsHash[name];	
		return this.getTabAtIndex(index);
	}
});