var Tabgroup = {

	stack : {},

	init : function(tabgroupId, callback) {
		this.getLi(tabgroupId).invoke('observe', 'click', this.clickElm.bindAsEventListener(this, tabgroupId));
		this.stack[tabgroupId] = callback;
	},

	cleanAll : function() {
		this.stack = {};
	},

	selectTab : function(tabgroupId, index) {
		this.selectOne(tabgroupId, this.getLi(tabgroupId)[index], index);
	},

	/**
	 * @private
	 * @param {} tabgroupId
	 * @param {} srcElm
	 */
	selectOne : function(tabgroupId, srcElm, index) {
		this.getLi(tabgroupId).each(function(o) {
			o[(o==srcElm ? 'add' : 'remove')+'ClassName']('selected');
		});
		this.stack[tabgroupId](srcElm, index);
	},

	/**
	 * @private
	 * @param {} tabgroupId
	 * @return {}
	 */
	getLi : function(tabgroupId) {
		return $$('#' + tabgroupId + '.tabgroup li');
	},

	/**
	 * @private
	 * @param {} tabgroupId
	 * @return {}
	 */
	clickElm : function(ev, tabgroupId) {
		var srcElm = Event.element(ev);
		var index = -1;
		this.getLi(tabgroupId).each(function(o) {
			index++;
			if (o==srcElm) {
				throw $break;
			}
		});
		this.selectOne(tabgroupId, srcElm, index);
	}
}