/**
 * @author darkness51
 * @description Plugin para simular el comportamiento de mootabs
 */

(function($)
{
	$.fn.mootabs = function(options)
	{
		/**
		 * Se encarga de sustituir los valores enviados en los valores por default
		 */
		var opt = $.extend({}, $.fn.mootabs.defaults, options);
		
		return this.each(function()
		{
			element = $(this) 
			element.css({'width': opt.width, 'height': opt.height});
			titles = $('ul.mootabs_title li');
			elid = element.attr('id');
			panelHeight = element.height() - ($(titles[0]).height() + 1);
			panels = $('#' + elid + ' .mootabs_panel');
			// Asignamos css a los paneles
			$(panels).css('height', panelHeight);
			activeTitle = titles[0];
			// Asignamos a cada elemento los eventos
			titles.each(function(index, item)
			{
				$(item).click(function()
				{
					$(item).removeClass(opt.mouseOverClass);
					activate(item);
				});
				
				$(item).mouseover(function()
				{
					if (item != activeTitle)
					{
						$(item).addClass(opt.mouseOverClass);
					}
				});
				
				$(item).mouseout(function()
				{
					if (item != activeTitle)
					{
						$(item).removeClass(opt.mouseOverClass);
					}
				});
			});
			
			if (opt.activateOnLoad != 'none')
			{
				if(opt.activateOnLoad == 'first')
				{
					activate(titles[0], true)
				}
				else
				{
					activate(opt.activeOnLoad, true);
				}
			}
			
			function activate(tab, skipAnim)
			{
				if (typeof skipAnim == 'undefined')
				{
					skipAnim = false;
				}
	
				if (typeof tab == 'string')
				{
					mytab = $('#content_tab ul li');
					tab = mytab;
				}
		
				if (typeof tab == 'object')
				{
					//var panels = $('#content_tab .mootabs_panel');
					var newTab = $(tab).attr('title');
					$(panels).removeClass('active');
					activePanel = $(panels).filter('#'+newTab);
					$(activePanel).addClass('active');
					$(titles).removeClass('active');
	
					$(tab).addClass('active');
					activeTitle = tab;
			
					_getContent();
				}
			}
	
			function _getContent()
			{
				$(activePanel).html(opt.ajaxLoadingText);
				$(activePanel).load(opt.ajaxUrl, {"tab": $(activePanel).attr('id')});
			}
		});
	}
	
	
	$.fn.mootabs.defaults = {
		width: '250px',
		height: '200px',
		duration: 1000,
		mouseOverClass: 'active',
		activateOnLoad: 'first',
		ajaxUrl: '',
		ajaxLoadingText: 'Loading...'
	}
})(jQuery);

