window.Tabs = function(tabs, options) {
	this.tabs = [];
	
	if(typeof tabs == 'object') {
		this.tabs = tabs;
	}

	if( this.tabs.length == 0 ) {
		return false;
	}
	
	this.prefixes = { button:'hdr_', container:'tab_' };
	this.events = { 
		on_before_active: false,
		on_after_active : false
	}

	this.classes = {
		button: {
			active: 'active', 
			inactive:'tab_hdr' 
		},
		container: {
			active:'active_tab', 
			inactive:'tab_cont' 
		}
	};

	this.active_tab = {index:0, elem:''};

	if( typeof options == 'object' ) {
		if( options.default_tab ) {
			var index = 0;
			if( typeof options.default_tab == 'number' ) {
				if ( this.tabs.length >= options.default_tab ) {
					index = options.default_tab;
					this.setDefaultTab(index);
				}
			} else {
				index = array_search(options.default_tab, this.tabs);
				if ( index ) {
					this.setDefaultTab(index);
				}
			}
		}
	}

	this.init();
}

Tabs.prototype = {
	init: function() {
		var oThis = this;
		var fn = function(oEvent){ oThis.setActiveTab(this.id);  };

		for ( var i = 0, j = this.tabs.length; i < j; i++  ) {
			if ( $( this.prefixes.button+this.tabs[i] ) && $( this.prefixes.container+this.tabs[i] ) ) {
				$( this.prefixes.button+this.tabs[i] ).observe('click', fn );
				$( this.prefixes.button+this.tabs[i]).addClassName(i!= this.active_tab.index ? this.classes.button.inactive : this.classes.button.active );
				$( this.prefixes.container+this.tabs[i]).addClassName(i!=this.active_tab.index ? this.classes.container.inactive : this.classes.container.active);
			}
		}
	},

	setActiveTab: function(n) {
		var elem = n.replace( this.prefixes.button, '' );
		for ( var i = 0, j = this.tabs.length; i < j; i++  ) {
			if ( elem == this.tabs[i] ) {
				// onbeforeactive
				if(this.events.on_before_active) {
					if( !eval(this.events.on_before_active) ) {
						continue;
					}
				}

				$( this.prefixes.button+this.tabs[i] ).addClassName( this.classes.button.active );
				this.active_tab = {index:i,elem:this.tabs[i]};//???

				// onafteractive
				if(this.events.on_after_active) {
					eval(this.events.on_after_active);
				}

				//onbeforeshowcontent
				$( this.prefixes.container+this.tabs[i] ).addClassName( this.classes.container.active );
				//onaftershowcontent
			} else {
				$( this.prefixes.button+this.tabs[i] ).removeClassName( this.classes.button.active );
				$( this.prefixes.container+this.tabs[i] ).removeClassName( this.classes.container.active );					
			}
		}
	},

	getActiveTab: function() {
		return this.active_tab;
	},

	setPrefixes: function( button_prefix, container_prefix ) {
		this.prefixes.button = button_prefix;
		this.prefixes.container = container_prefix;
	},
	
	setDefaultTab: function(index) {
		this.active_tab.index = index;
		this.active_tab.elem = this.tabs[this.active_tab.index];
	},

	getTabsCount: function() {
		return this.tabs.length;
	},

	addEvent : function(type, evt) {
    	for (k in this.events)
    	{
    		if ( k == type ) {
    			evt = evt + "(this)";
    			eval("this.events."+k+" = '"+evt+"'");
    		}
    	}
	}
};
