(function($) {
	
	// define function
	$.fn.tabbed_interface = function(options) {
		
		// valores padrao de variaveis
		var defaults = {
				switch_duration:   500,       // duração do efeito
				auto_switch:       true,      // trocar automaticamente?
				auto_switch_delay: 2000,      // tempo para troca automatica
				contents_tag:      'div',     // tag para buscar o conteudo
				specific_list:     '',        // informar lista especifica
				switch_fx:         'fade',    // efeito para a troca (fade, slide, false)
				half_fx_time:      true,      // dividir o tempo de cada parte da animação pela metade? 
				position:          'absolute',// valor da propriedade css position a ser inserida nas tags de conteudo
				carousel:          false      // integração com jCarousel
		};
				
		// substitui os valores padrao, caso informados
		var options = $.extend(defaults, options);
				
		if(options.half_fx_time) {
			options.half_fx_time = 2;
		} else {
			options.half_fx_time = 1;
		}
		
		var tabTimer;
		
		// para cada seletor oferecido, criamos uma interface com abas
		return this.each(function(el) {
			el = this;
			$(el).find('ul'+options.specific_list).find('li').find('a').click(function() {
				$(el).trigger('stopTimer');
				
				var obj = this.hash;
				
				switch(options.switch_fx) {					
					case 'slide':						
						// utilizando slideUp e slideDown.
						//$(el).find(options.contents_tag).slideUp(options.switch_duration / options.half_fx_time).filter(this.hash).slideDown(options.switch_duration / options.half_fx_time);
						$(el).find(options.contents_tag).slideUp(options.switch_duration);
						setTimeout(function() {							
							$(el).find(options.contents_tag).filter(obj).slideDown(options.switch_duration);
						}, options.switch_duration);
					break;
					case 'fade':					
						// utilizando fadeOut e fadeIn.
						$(el).find(options.contents_tag).fadeOut(options.switch_duration / options.half_fx_time).filter(this.hash).fadeIn(options.switch_duration / options.half_fx_time);
					break;
					case 'show':
						// utilizando hide e show.
						//$(el).find(options.contents_tag).hide(options.switch_duration / options.half_fx_time).filter(this.hash).show(options.switch_duration / options.half_fx_time);
						$(el).find(options.contents_tag).hide(options.switch_duration);
						setTimeout(function() {							
							$(el).find(options.contents_tag).filter(obj).show(options.switch_duration);
						}, options.switch_duration);
					break;
					case false:
					default:
						// nao utilizar efeito (hide() show()).
						$(el).find(options.contents_tag).hide().filter(this.hash).show();
					break;
				}
								
				
				// confere a tag ativa uma classe, removendo das outras
				$(el).find('ul'+options.specific_list).find('li').find('a').removeClass('active').filter(this).addClass('active');
				
				// encerra o evento de click
				return false;
			});
			
			// torna os posicionamentos dos conteudos absolutos
			$(el).find(options.contents_tag).css('position',options.position).not(':first').hide();
			
			// se auto_switch for true, iniciamos o timer
			if(options.auto_switch) {
				// Trigger para iniciar o timer
				$(el).bind('startTimer', function() {
					tabTimer = setTimeout(function() {
						var my_id = $(el).find(options.contents_tag).not(':hidden').next().attr('id') || $(el).find(options.contents_tag).not(':hidden').siblings(options.contents_tag).first().attr('id');
						item = $(el).find('ul'+options.specific_list).find('li').find('a[href="#'+my_id+'"]')
						item.click();
						
						// gatilho, pois o timer esta vinculado a um elemento
						$(el).trigger('startTimer');
						
						// Integracao com o jCarousel
						if(options.carousel) {													
							carousel = $(el).find('ul'+options.specific_list).data('jcarousel');
							carousel.scroll(item.parent('li').index() + 1);
						}
					}, options.auto_switch_delay);
				});
				
				// Trigger para parar o timer
				$(el).bind('stopTimer', function() {
					clearTimeout(tabTimer);
				});
				
				// Se o usuario estiver com o mouse sobre a lista, deve parar o timer
				$(el).hover(function() {
					// para o timer
					$(el).trigger('stopTimer');
				},function() {
					// re-inicia o  timer
					$(el).trigger('startTimer');
				});
				
				// nova chamada para um novo timer
				$(el).trigger('startTimer');
			}
		});
	};
})(jQuery);
