(function($){
	$.fn.jqContentSlider = function(configs){
		
		var autoRun, currentSlide, childs, totChilds;
		
		//Configurações por defeito
		configs = $.extend({
			animSpeed : 600,
			prev : '',
			next : '',
			autoSlide : false,
			autoSlideInterval : 1000,
			autoSlideElement : '',
			autoSlideDirection : 'next',
			slideNavigation : false,
			slideNavigationElement : '',
			buildNavigation : true,
			setNavigationElementWidth : true,
			stopAutoSlideWhenClick : false,
			startSlide : 1,
			msiedetection : true
		}, configs);
		
		return this.each(function(){
			
			childs = $(this).children().hide();
			$(childs[configs.startSlide - 1]).show();
			
			currentSlide = configs.startSlide;
			
			totChilds = childs.length;
			
			//Configurar o evento click para o elemento que retrocede o slide
			if(configs.prev != ''){
				$(configs.prev).click(function(){
					if(configs.autoSlide){
						clearInterval(autoRun);
					}

					prevSlide();
				});
			};
			
			//Configurar o evento click para o elemento que avança o slide
			if(configs.next != ''){
				$(configs.next).click(function(){
					if(configs.autoSlide){
						clearInterval(autoRun);
					}

					nextSlide();
				});
			};
			
			//Configurar o autoslide
			if(configs.autoSlide){
				AutoSlide();
			};
			
			//Configurar o elemento de autoslide
			if(configs.autoSlideElement != ''){
				var i=-1;
				$(configs.autoSlideElement).hover(function(){
					++i;
					//Efectuar logo uma mudança
					if(i == 0){
						if(configs.autoSlideDirection == 'prev'){
							prevSlide();
						}
						else if(configs.autoSlideDirection == 'next'){
							nextSlide();
						}
					}
					
					AutoSlide();
				}, function(){
					i=-1;
					clearInterval(autoRun);
				});
			};
			
			if(configs.slideNavigation && configs.slideNavigationElement !=''){
				//Criar elementos da paginação
				if(configs.buildNavigation){
					
					childs.each(function(i){
						var elemClass = 'unsel';
						
						if(i == (configs.startSlide - 1)){
							elemClass = 'sel';
						}
						
						$(configs.slideNavigationElement).append($('<li></li>').html('<a class="' + elemClass + '"href="#" rel="' + i + '"></a>'));
					});
					
					//Calcular width do elemento
					if(configs.setNavigationElementWidth){
						var ulChilds = $(configs.slideNavigationElement).children().length;
						var childWidth = $(configs.slideNavigationElement + ' li:last').outerWidth(true);
						
						$(configs.slideNavigationElement).width(parseInt((ulChilds * childWidth), 10));
					}
				}else{
					$(configs.slideNavigationElement + ' li:eq(' + (configs.startSlide - 1) + ') a').attr('class', 'sel');
				}

				//Adicionar evento aos elementos previamente criados
				$(configs.slideNavigationElement + ' li a').click(function(){
					clearInterval(autoRun);
					
					var that = $(this);
					
					if(configs.msiedetection && $.browser.msie){
						$(childs[currentSlide - 1]).hide();
					}
					else{
						$(childs[currentSlide - 1]).fadeOut('fast', 'linear');
					}
					
					$(configs.slideNavigationElement + ' li a.sel').attr('class', 'unsel');
					
					currentSlide = parseInt(that.attr('rel'), 10) + 1;
					
					if(configs.msiedetection && $.browser.msie){
						$(childs[currentSlide - 1]).show();
					}
					else{
						$(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear');
					}
					
					that.attr('class', 'sel');
					
					//Configurar o autoslide
					if(configs.autoSlide && !configs.stopAutoSlideWhenClick){
						AutoSlide();
					}
					
					return false;
				});
			};
			
			//Função para retroceder
			function prevSlide(){
				if(currentSlide == 1){
					currentSlide = totChilds;
					
					if(configs.msiedetection && $.browser.msie){
						$(childs[0]).hide();
						$(childs[currentSlide - 1]).show();
					}
					else{
						$(childs[0]).fadeOut('fast', 'linear');
						$(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear');
					}
					
					//Seleccionar elemento de navegação
					if(configs.slideNavigation && configs.slideNavigationElement !=''){
						SlideNavigationCurrent(currentSlide - 1);
					}
				}
				else{
					if(configs.msiedetection && $.browser.msie){
						$(childs[currentSlide - 1]).hide();
						$(childs[currentSlide - 2]).show();
					}
					else{
						$(childs[currentSlide - 1]).fadeOut('fast', 'linear');
						$(childs[currentSlide - 2]).fadeIn(configs.animSpeed, 'linear');
					}
					
					//Seleccionar elemento de navegação
					if(configs.slideNavigation && configs.slideNavigationElement !=''){
						SlideNavigationCurrent(currentSlide - 2);
					}

					currentSlide--;
				}
			};
			
			//Função para avançar
			function nextSlide(){
				if(currentSlide == totChilds){
					currentSlide = 1;
					
					if(configs.msiedetection && $.browser.msie){
						$(childs[totChilds - 1]).hide();
						$(childs[currentSlide - 1]).show();
					}
					else{
						$(childs[totChilds - 1]).fadeOut('fast', 'linear');
						$(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear');
					}
					
					//Seleccionar elemento de navegação
					if(configs.slideNavigation && configs.slideNavigationElement !=''){
						SlideNavigationCurrent(currentSlide - 1);
					}
				}
				else{
					if(configs.msiedetection && $.browser.msie){
						$(childs[currentSlide - 1]).hide();
						$(childs[currentSlide]).show();
					}
					else{
						$(childs[currentSlide - 1]).fadeOut('fast', 'linear');
						$(childs[currentSlide]).fadeIn(configs.animSpeed, 'linear');
					}
					
					//Seleccionar elemento de navegação
					if(configs.slideNavigation && configs.slideNavigationElement !=''){
						SlideNavigationCurrent(currentSlide);
					}
					
					currentSlide++;
				}
			};
			
			//Função de autoslide
			function AutoSlide(){
				if(configs.autoSlideDirection == 'prev'){
					autoRun = setInterval(function (){prevSlide()}, configs.autoSlideInterval);
				}
				else if(configs.autoSlideDirection == 'next'){
					autoRun = setInterval(function (){nextSlide()}, configs.autoSlideInterval);
				}
			};
			
			function SlideNavigationCurrent(toCompare){
				$(configs.slideNavigationElement + ' li a.sel').attr('class', 'unsel');
				
				$(configs.slideNavigationElement + ' li a').filter(function(i){
					return (parseInt($(this).attr('rel'), 10) == (toCompare)) ? true : false;
				}).attr('class', 'sel');
			};
		});
	};
})(jQuery);
