var Tabber = Class.create();
Tabber.prototype = {
	options: {
		'selectorHead' : '.tabheader',
		'selectorTab'  : '.tab',
		'contentUrls'  : []
	},
	activeTabIndex : 0,
	tabs : [],
	element: null,
	initialize: function(element, options) {
		
		Object.extend(this.options, options);
		this.activeTabIndex = this.options.activeTabIndex||0;
		this.element = $(element);
		
		heads = this.element.select(this.options.selectorHead);
		heads.each(function(head, index){
			
			tab = new Tabber.tab(this.element.down(this.options.selectorTab, index), head, {
				'onHeadClick' : this.activateTab.bind(this, index)
			});
			this.tabs.push(tab);
			
		}.bind(this));
	},
	
	getTab: function(index) {
		return this.tabs[index];
	},
	
	getActiveTab: function() {
		return this.getTab(this.activeTabIndex);
	},
	
	activateTab: function(index) {
		this.tabs.each(function(tab, i){
			if (index == i) {
				tab.activate();
				if (this.options.contentUrls[i] && this.getTab(index).elementContainer().innerHTML.stripTags().strip().empty()) {
					tab.updateTabContentAjax(this.options.contentUrls[i]);
				}
			}else {
				tab.deactivate();
			}
		}.bind(this))
	}
}

Tabber.tab = Class.create();
Tabber.tab.prototype = {
	statusClassNames: {
		'active'   : 'active',
		'inactive' : 'inactive'
	},
	
	container : null,
	head : null,
	onUpdate: null,
		
	initialize: function(elContainer, elHead) {
		if (arguments[2]) {
			this.setOptions(arguments[2]);		
		}
		this.elementContainer(elContainer);
		this.elementHead(elHead).observe('click', this._headClick.bind(this));
	},
	
	_headClick: function() {
		 this.onHeadClick();
	},
	
	setOptions: function(options) {
		if (options.statusClassNames) {
			Object.extend(this.statusClassNames, arguments[2]['statusClassNames']);
		}
		
		this.onUpdate = Object.isFunction(options.onUpdate)?options.onUpdate:Prototype.emptyFunction;
		this.onBeforUpdate = Object.isFunction(options.onBeforUpdate)?options.onBeforUpdate:Prototype.emptyFunction;
		this.onHeadClick = Object.isFunction(options.onHeadClick)?options.onHeadClick:Prototype.emptyFunction;
		this.onActivate = Object.isFunction(options.onActivate)?options.onActivate:Prototype.emptyFunction;
	},
	
	setOption: function(name, value) {
		this[name] = value;
	},
	
	activate: function() {
		this.onActivate();
		[this.container, this.head].each(function(el){
			el.classNames().add(this.statusClassNames.active);
			el.classNames().remove(this.statusClassNames.inactive);	
		}.bind(this));
		
	},
	
	deactivate: function() {
		[this.container, this.head].each(function(el){
			el.classNames().remove(this.statusClassNames.active);
			el.classNames().add(this.statusClassNames.inactive);	
		}.bind(this))
	},
	
	/**
	 * Set or get element container
	 */
	elementContainer: function() {
		if (arguments[0] && Object.isElement(arguments[0])) {
			this.container = $(arguments[0]);
		}else if (arguments[0] && !Object.isElement(arguments[0])) {
			throw new Error('Invalid type of argument 1. Type of element expected')
		}
		
		return this.container;
	},
	
	elementHead: function() {
		if (arguments[0] && Object.isElement(arguments[0])) {
			this.head = $(arguments[0]);
		}else if (arguments[0] && !Object.isElement(arguments[0])) {
			throw new Error('Invalid type of argument 1. Type of element expected')
		}
		
		return this.head;
	},
	
	setTabContent: function(content) {
		this.elementContainer().update(content);
	},
	
	updateTabContentAjax: function(url, options) {
		this.onBeforUpdate();
		options = options||{};
		options['method'] = options['method'] || 'GET';
		onSuccess = options.onSuccess || Prototype.emptyFunction;
		options.onSuccess = function() {
			onSuccess.apply(this, arguments);
			this.onUpdate();
		}.bind(this);
		new Ajax.Updater(this.elementContainer(), url, options);
	}
}
