/***************************************************************
*  Copyright notice
*
*  (c) 2009 Stefan Aebischer <typo3@pixtron.ch>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * TxPowermailTabs! Simple tabs using Mootools
 * version 1.0 2009-02-19 
 * 
 * Credits to Andrew Teltaw for intial idea
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 *
 */
 
/*
//dwe: renamed to "TxMTTabs"
*/
var TxMTTabs = new Class({
	initialize: function (element) {
		this.element = $(element);
		if (this.element) {
			this.menu = $$('#' + element + ' a');
			this.show(this.getInitialTab());
			
			this.menu.each(function (element) {
				this.setupTab(element);
			}.bind(this));
		}
	},
	setupTab: function (element) {
		var idx = this.menu.indexOf(element);
		element.addEvent('click', this.activate.bind(this, idx));
			
	},
	activate :  function (idx) {
		this.menu.each(function (element, id) {
			this.hide(id);
		}.bind(this));
	
		this.show(idx);
	},
	hide : function (id) {
		this.menu[id].removeClass('active-tab');
		$(this.tabID(this.menu[id])).removeClass('active-tab-body');
	},
	show : function (id) {
		this.menu[id].addClass('active-tab');
		$(this.tabID(this.menu[id])).addClass('active-tab-body');

	},
	tabID : function (elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function () {
		if (document.location.href.match(/#(\w.+)/)) {
			var returnId = 0;
			var loc = RegExp.$1;
			this.menu.each(function (element, id) {
				if (element.href.match(/#(\w.+)/)[1] === loc) {
					returnId = id;
				}
			});
			
			return returnId || 0;
		} else {
			return 0;
		}
	}
});