var Tabs = Class.create();

Tabs.prototype = {
	selectTab: function (e) {
		var targ;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) targ = targ.parentNode;

		// Activate the pane.
		this.activatePane(targ.className.match(/pane[0-9]+/));

		// Cancel the action.
		e.returnValue = false;
	},
	
	activatePane: function(pane) {
		// Remove the current selected pane
		this.container.className = this.container.className.replace(/selected-pane[0-9]+/, "");

		// Add in a new current selected pane
		this.container.addClassName("selected-" + pane);
	},

	initialize: function(e, initialTab) {
		// Remember the container
		this.container = $(e);
		
		// Replace any default tabs with the one specified
		if (initialTab) {
			this.activatePane(initialTab);
		}

		// Get all links in the container
		var tabs = this.container.getElementsByTagName("a");
		for (i = 0; i < tabs.length; i++) {
			var tab = tabs[i];
			if (tab.className.match(/pane[0-9]+/)) {
				Event.observe(tab, "click", this.selectTab.bindAsEventListener(this));
			}
		}
	}
};
