var mootabs = new Class({
	Implements: [Events, Options],
	initialize: function(element, options) {
		this.options =  new Hash({
			width:				'300px',
			height:				'200px',
			changeTransition:	Fx.Transitions.Bounce.easeOut,
			duration:			1000,
			mouseOverClass:		'active',
			activateOnLoad:		'first',
			useAjax: 			false,
			ajaxUrl: 			'',
			ajaxMethod: 		'post',
			ajaxLoadingText: 	'Loading...',
			/* History Manager Plugin */
			useHistoryManager: false,
			customHistoryKey: false
		});
		this.options.extend(options);
		this.currentIter = -1;
		
		this.el = $(element);
		this.elid = element;
		
		this.el.setStyles({
			height: this.options.height,
			width: this.options.width
		});
		
		/*this.titles = $$('#' + this.elid + ' ul.mootabs_title li');*/
		this.titles = $$('ul.mootabs_title li');
		this.eltit = [];
		/*this.panelHeight = this.el.getSize().size.y - (this.titles[0].getSize().size.y + 4);*/
		this.panelHeight = this.el.getSize().y - (this.titles[0].getSize().y + 1);
		this.panels = $$('#' + this.elid + ' .mootabs_panel');

		if (this.options.useHistoryManager)
		{
			this.initHistory ();	
		}
		
		this.panels.setStyle('height', this.panelHeight);
		
		this.titles.each(function(item, index) {
			item.addEvent('click', function(){
					item.removeClass(this.options.mouseOverClass);
					if (this.options.useHistoryManager) 
					{
						this.history.setValue(0, index + 1);
						this.currentIter = index;
						//this.history.defaults=[index+1];
						this.eltit[index] = item.getProperty('title');
					}
					this.activate(item);
				}.bind(this)
			);
			
			item.addEvent('mouseover', function() {
				if(item != this.activeTitle)
				{
					item.addClass(this.options.mouseOverClass);
				}
			}.bind(this));
			
			item.addEvent('mouseout', function() {
				if(item != this.activeTitle)
				{
					item.removeClass(this.options.mouseOverClass);
				}
			}.bind(this));
		}.bind(this));
		
		
		if(this.options.activateOnLoad != 'none')
		{
			if(this.options.activateOnLoad == 'first')
			{
				this.activate(this.titles[0], true);
			}
			else
			{
				this.activate(this.options.activateOnLoad, true);	
			}
		}
	},
	
	activate: function(tab, skipAnim){
		if(! $defined(skipAnim))
		{
			skipAnim = false;
		}
		if($type(tab) == 'string') 
		{
			myTab = $$('#' + this.elid + ' ul li').filter('['+'title'+ ('=' || '')+(tab || '')+']')[0];
			tab = myTab;
		}
		
		if($type(tab) == 'element')
		{
			var newTab = tab.getProperty('title');
			this.panels.removeClass('active');
			
			/*this.activePanel = this.panels.filterById(newTab)[0];*/
			this.activePanel = this.panels.filter('#'+newTab);
			
			this.activePanel.addClass('active');
			
			
			
			if(this.options.changeTransition != 'none' && skipAnim==false)
			{
				this.panels.filter('#'+newTab).setStyle('height', 0);
				var changeEffect = new Fx.Elements(this.panels.filter('#'+newTab), {duration: this.options.duration, transition: this.options.changeTransition});
				changeEffect.start({
					'0': {
						'height': [0, this.panelHeight]
					}
				});
			}
			
			this.titles.removeClass('active');
			
			tab.addClass('active');
			
			this.activeTitle = tab;
			
			if(this.options.useAjax)
			{
				this._getContent();
			}
		}
	},
	
	_getContent: function(){
		this.activePanel.set('html', this.options.ajaxLoadingText);
		var updateId = this.activePanel.getProperty('id')[0];
		//this.options.ajaxOptions.extend(newOptions);
		var tabRequest = new Request.HTML({
			url: this.options.ajaxUrl,
			update: updateId
			});
		tabRequest.send('tab=' + this.activeTitle.getProperty('title'));
	},
	
	goTo: function (num)
	{
		console.log ('Item en el historial: '+this.eltit[num]);
	},
	
	initHistory: function ()
	{
		this.fireEvent('onHistoryInit');
		this.historyKey = 'tab-plan';
		if (this.options.custumHistoyKey)
		{
			this.historyKey = this.options.custumHistoyKey;
		}
		
		this.history = new History.Route ({
			defaults: [1],
			pattern: this.historyKey + '\\((\\d+)\\)',
			generate: function(values) {
				return [this.historyKey, '(', values[0], ')'].join('')
			}.bind(this),
			onMatch: function(values, defaults) {
				/*if (parseInt(values[0])-1 < -1)*/
					/*this.goTo(parseInt(values[0])-1);*/
				console.log ('Entro Aqui!!!');
			}.bind(this)
		});
	},
	
	addTab: function(title, label, content){
		//the new title
		var newTitle = new Element('li', {
			'title': title
		});
		newTitle.appendText(label);
		this.titles.include(newTitle);
		$$('#' + this.elid + ' ul').adopt(newTitle);
		newTitle.addEvent('click', function() {
			this.activate(newTitle);
		}.bind(this));
		
		newTitle.addEvent('mouseover', function() {
			if(newTitle != this.activeTitle)
			{
				newTitle.addClass(this.options.mouseOverClass);
			}
		}.bind(this));
		newTitle.addEvent('mouseout', function() {
			if(newTitle != this.activeTitle)
			{
				newTitle.removeClass(this.options.mouseOverClass);
			}
		}.bind(this));
		//the new panel
		var newPanel = new Element('div', {
			'style': {'height': this.options.panelHeight},
			'id': title,
			'class': 'mootabs_panel'
		});
		if(!this.options.useAjax)
		{
			newPanel.setHTML(content);
		}
		this.panels.include(newPanel);
		this.el.adopt(newPanel);
	},
	
	removeTab: function(title){
		if(this.activeTitle.title == title)
		{
			this.activate(this.titles[0]);
		}
		$$('#' + this.elid + ' ul li').filter('['+'title'+ ('=' || '')+(title || '')+']')[0].remove();
		
		$$('#' + this.elid + ' .mootabs_panel').filter('#'+title)[0].remove();
	},
	
	next: function(){
		var nextTab = this.activeTitle.getNext();
		if(!nextTab) {
			nextTab = this.titles[0];
		}
		this.activate(nextTab);
	},
	
	previous: function(){
		var previousTab = this.activeTitle.getPrevious();
		if(!previousTab) {
			previousTab = this.titles[this.titles.length - 1];
		}
		this.activate(previousTab);
	}
});