/**
 * @author dao
 */

var sdTabs = new Class({
	options: {
		tabs: $$(".tab"),
		panels: $$(".panel"),
		startIndex: 0
	},
	Implements: [Options, Events],
	initialize: function() {
		this.setOptions(this.options);
		this.tabs = $$('.tab');
		this.panels = $$('.panel');
		this.addTabs(this.tabs);
		if (this.tabs.length) this.showPanel(this.options.startIndex);
	},
	addTabs: function(tabs) {
		tabs.each(function(tab, i) {
			tab.store('index', i);
			this.addTab(tab);
		}.bind(this));
	},
	addTab: function(tab) {
		var index = tab.retrieve('index');
		tab.addEvents({
			click: function(e) { new Event(e).stop(); this.showPanel(index); }.bind(this)
		});
	},
	showPanel: function(index) {
		this.hidePanels();
		this.panels[index].disinter();
	},
	hidePanels: function() {
		this.panels.each(function(panel) { panel.bury();});
	}
});

window.addEvent('domready', function() {
	$$('.panel').bury();
});
window.addEvent('load', function() {
	new sdTabs();
});
