/**
 * cmfTabs! Extended tabs using Prototype
 * Idea: Andrew Tetlaw http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 *
 * Code: Sergey Kondratenko
 * Copyright (c) 2008, Semantica-IT Ltd.
 * version 1.2
 */
var cmfTabs	=	Class.create();
var cmfTabs_inputTags = ['INPUT', 'SELECT', 'TEXTAREA'];

cmfTabs.prototype = {
	currentTab : null,
	initialize : function(element) {
		this.element = $(element);
		this.options = Object.extend({}, arguments[1] || {});
		this.options.activeClassName	=	this.options.activeClassName || 'selected';
		this.options.activeTabClassName	=	this.options.activeTabClassName || 'selected_tab'
		this.options.method	=	this.options.method || 'display';
		if (this.options.tabs)
		{
			this.tabs = this.options.tabs;
		}
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getDefaultTab(), 1);
		this.menu.each(this.setup.bind(this));
		this.idInfo = [];
	},
	activate : function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		this.menu.without(elm).each(this.hide.bind(this));
		this.show(elm);
	},
	hide : function(elm) {
		$(elm).removeClassName(this.options.activeClassName);
		if (Prototype.Browser.WebKit)
		{
			var	temp = this.tabs(elm);
			for (var i = 0; i < temp.length; i++)
			{
				if (this.options.method == 'display')
					Element.hide(temp[i]);
				else
					Element.removeClassName(temp[i], this.options.activeTabClassName);
			}
		}
		else
		{
			if (this.options.method == 'display')
			{
				$A(this.tabs(elm)).invoke('hide');
			}
			else
			{
				$A(this.tabs(elm)).invoke('removeClassName', this.options.activeTabClassName);
			}
		}
	},
	show : function(elm) {
		if (this.options.onshow)
		{
			this.options.onshow(elm.id);
		}
		$(elm).addClassName(this.options.activeClassName);
		this.currentTab = elm;
		if (Prototype.Browser.WebKit)
		{
			var	temp = this.tabs(elm);
			for (var i = 0; i < temp.length; i++)
			{
				if (this.options.method == 'display')
					Element.show(temp[i]);
				else
					Element.addClassName(temp[i], this.options.activeTabClassName)
			}
		}
		else
		{
			if (this.options.method == 'display')
			{
				$A(this.tabs(elm)).invoke('show');
			}
			else
			{
				$A(this.tabs(elm)).invoke('addClassName', this.options.activeTabClassName);
			}
		}
		if (elm.rel && elm.rel == 'noclick' && arguments[1])
		{
		}
		else
		{
			if (elm.onclick)
			{
				if (Prototype.Browser.IE && elm._onclick)
					elm._onclick();
				else
					elm.onclick();
			}
		}
	},
	setup : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
		if (elm.onclick && Prototype.Browser.IE)
			elm._onclick = function(){eval(elm.onclick)};
	},
	tabs  : function(elm) {
		return $A([$(elm.id + '_tab')]);
	},
	getDefaultTab : function() {
		return (this.options.defaultTab && $(this.options.defaultTab)) || this.menu.first();
	},
	toggle: function(elm)
	{
		if (elm.visible())
		{
			this.menu.without(elm).each(this.hide.bind(this));
			this.show(elm);
		}
	},
	disable: function(elm)
	{
		if (this.currentTab == elm)
			this.currentTab	=	null;
		var _self = this;
		this.tabs(elm).each(
			function(el){
				if (cmfTabs_inputTags.indexOf(el.tagName) != -1)
				{
					el.removeClassName(_self.options.activeTabClassName);
					_self.idInfo[el.id] = el.className;
					el.removeClassName('required');
					el.disabled = true;
				}
			});
		this.hide(elm);
		elm.hide();
		for (var m = 0; m<this.menu.length; m++)
		{
			if (this.menu[m].visible())
			{
				this.toggle(this.menu[m]);
				break;
			}
		}
	},
	enable: function(elm)
	{
		var _self = this;
		this.tabs(elm).each(
			function(el){
				if (cmfTabs_inputTags.indexOf(el.tagName) != -1)
				{
					if (_self.idInfo[el.id])
						el.className = _self.idInfo[el.id];
					el.disabled = false;
				}
			});
		if (this.currentTab == null)
		{
			this.show(elm);
		}
		elm.show();
	}
}
