(function(){
	jQuery.fn.jqContentSlider = function(configs){
		var autoRun, currentSlide, childs, totChilds, owner;
		
		//Configurações por defeito
		configs = jQuery.extend({
			animSpeed : 800,
			prev : false,
			next : false,
			autoSlide : true,
			autoSlideInterval : 5000,
			autoSlideElement : '',
			autoSlideDirection : 'next',
			slideNavigation : true,
			slideNavigationElement : '.pontos',
			setNavigationElementWidth : false,
			stopAutoSlideWhenClick : false,
			startSlide : 1,
			msiedetection : false
		}, configs);
		
		return this.each(function(){
			
      owner = jQuery(this).parent();
			
			childs = jQuery(this).children().hide();
			
			
			jQuery(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 != ''){
				jQuery('.' + configs.prev).live('click', function(){
					if(configs.autoSlide){
						clearInterval(autoRun);
					}

					prevSlide();
				
					if(configs.autoSlide && !configs.stopAutoSlideWhenClick){
						AutoSlide();
					}
				});
			};
			
			//Configurar o evento click para o elemento que avança o slide
			if(configs.next != ''){
				jQuery('.' + configs.next).live('click', function(){
					if(configs.autoSlide){
						clearInterval(autoRun);
					}
					
					nextSlide();

					if(configs.autoSlide && (!configs.stopAutoSlideWhenClick)){
						AutoSlide();
					}
				});
			};
			
			//Configurar o autoslide
			if(configs.autoSlide){
				AutoSlide();
			};
			
			//Configurar o elemento de autoslide
			if(configs.autoSlideElement != ''){
				var i=-1;
				jQuery('.' + 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 !=''){
			/*
      	jQuery(configs.slideNavigationElement).append(jQuery('<div></div>').attr('class','prev').click(function(){
            if(configs.autoSlide){
    					clearInterval(autoRun);
    				}
    
    				prevSlide();
    			
    				if(configs.autoSlide && !configs.stopAutoSlideWhenClick){
    					AutoSlide();
    				}
        }));
				*/
				//Criar elementos da paginação
				
        
        childs.each(function(i){
					var elemClass;
					
					if(i == (configs.startSlide - 1)){
						elemClass = 'sel';
					}
					else{
						elemClass = 'un';
					}
					
					jQuery(configs.slideNavigationElement).append(jQuery('<li></li>').attr("class", elemClass).html('<a href="javascript:void(0);"></a>').attr({'rel' : i}));
				});
				
				
				
				
				/*
				jQuery(configs.slideNavigationElement).append(jQuery('<div></div>').attr('class','next').click(function(){
            if(configs.autoSlide){
    					clearInterval(autoRun);
    				}
    
    				nextSlide();
    			
    				if(configs.autoSlide && !configs.stopAutoSlideWhenClick){
    					AutoSlide();
    				}
        }));
				*/
				//Adicionar evento aos elementos previamente criados
				jQuery(configs.slideNavigationElement + ' .un').live('click', function(){
					clearInterval(autoRun);
					
					var that = jQuery(this);
					
					if(configs.msiedetection && jQuery.browser.msie){
						jQuery(childs[currentSlide - 1]).hide();
					}
					else{
						jQuery(childs[currentSlide - 1]).fadeOut('fast', 'linear');
					}
					
					jQuery(configs.slideNavigationElement + ' .sel').attr('class', 'un');
					
					currentSlide = parseInt(that.attr('rel'), 10) + 1;
					
					if(configs.msiedetection && jQuery.browser.msie){
						jQuery(childs[currentSlide - 1]).show();
					}
					else{
						jQuery(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear');
					}
					
					jQuery(".ano_selected").html(jQuery(childs[currentSlide - 1]).attr("rel"));
					
					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 && jQuery.browser.msie){
						jQuery(childs[0]).hide();
						jQuery(childs[currentSlide - 1]).show();
					}
					else{
						jQuery(childs[0]).fadeOut('fast', 'linear');
						jQuery(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear');
					}
					
					
					jQuery(".ano_selected").html(jQuery(childs[currentSlide - 1]).attr("rel"));
					
					//Seleccionar elemento de navegação
					if(configs.slideNavigation && configs.slideNavigationElement !=''){
						SlideNavigationCurrent(currentSlide - 1);
					}
				}
				else{
					if(configs.msiedetection && jQuery.browser.msie){
						jQuery(childs[currentSlide - 1]).hide();
						jQuery(childs[currentSlide - 2]).show();
					}
					else{
						jQuery(childs[currentSlide - 1]).fadeOut('fast', 'linear');
						jQuery(childs[currentSlide - 2]).fadeIn(configs.animSpeed, 'linear');
					}
          
          jQuery(".ano_selected").html(jQuery(childs[currentSlide - 2]).attr("rel"));
          
					//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;
					//alert("teste");
					if(configs.msiedetection && jQuery.browser.msie){
						jQuery(childs[totChilds - 1]).hide();
						jQuery(childs[currentSlide - 1]).show();
					}
					else{
						jQuery(childs[totChilds - 1]).fadeOut('fast', 'linear');
						jQuery(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear');
					}
          
          jQuery(".ano_selected").html(jQuery(childs[currentSlide]).attr("rel"));
          
					//Seleccionar elemento de navegação
					if(configs.slideNavigation && configs.slideNavigationElement !=''){
						SlideNavigationCurrent(currentSlide - 1);
					}
				}
				else{
					if(configs.msiedetection && jQuery.browser.msie){
						jQuery(childs[currentSlide - 1]).hide();
						jQuery(childs[currentSlide]).show();
					}
					else{
						jQuery(childs[currentSlide - 1]).fadeOut('fast', 'linear');
						jQuery(childs[currentSlide]).fadeIn(configs.animSpeed, 'linear');
					}
          
          jQuery(".ano_selected").html(jQuery(childs[currentSlide]).attr("rel"));
          
					//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){
				jQuery(configs.slideNavigationElement + ' .sel').attr('class', 'un');
				
				jQuery(configs.slideNavigationElement + ' li').filter(function(i){
					return (parseInt(jQuery(this).attr('rel'), 10) == (toCompare)) ? true : false;
				}).attr('class', 'sel');
			};
		});
	};
})(jQuery);
