(function(){
	jQuery.fn.jqContentSlider = function(configs){
		var autoRun, currentSlide, childs, totChilds;
		
		//Configurações por defeito
		configs = jQuery.extend({
			animSpeed : 600,
			prev : '',
			next : '',
			autoSlide : true,
			autoSlideInterval : 17000,
			autoSlideElement : '',
			autoSlideDirection : 'next',
			slideNavigation : true,
			slideNavigationElement : '.paginacao_slider',
			setNavigationElementWidth : true,
			stopAutoSlideWhenClick : false,
			startSlide : 1,
			msiedetection : false
		}, configs);
		
		return this.each(function(){
			childs = jQuery(this).children().hide();
			jQuery(childs[configs.startSlide - 1]).show();
			jQuery('.left_slide', jQuery(childs[configs.startSlide - 1])).animate({
        width : 220
      }, 100);
			currentSlide = configs.startSlide;
			
			totChilds = childs.length;
			
			//Configurar o evento click para o elemento que retrocede o slide
			if(configs.prev != ''){
			 
				jQuery('.' + 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 != ''){
			  
				jQuery('.' + 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;
				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 !=''){
			   
				//Criar elementos da paginação
				
        childs.each(function(i){
					var elemClass;
					
					if(i == (configs.startSlide - 1)){
						elemClass = 'sel';
					}
					else{
						elemClass = 'unsel';
					}
					
          jQuery(configs.slideNavigationElement).append(jQuery('<li></li>').html('<a class="' + elemClass + '"href="#" rel="' + i + '">'+(i + 1)+'</a>'));

				});
				
				jQuery(configs.slideNavigationElement).append(jQuery('<li></li>').html(jQuery('<a class="pause_unsel" href="#"></a>').stop().click(function(){
				  
				  
				  if(jQuery(this).is('.pause_unsel')){
				    clearInterval(autoRun);
				    jQuery(this).removeClass('pause_unsel').addClass('start_unsel');
          }
          else{
            nextSlide();
				    AutoSlide();
				    jQuery(this).removeClass('start_unsel').addClass('pause_unsel');
          }
				
        })));
				
				//Adicionar evento aos elementos previamente criados
				jQuery(configs.slideNavigationElement + ' li a.unsel').live('click', function(){
				  
				  jQuery(".start_unsel").removeClass('start_unsel').addClass('pause_unsel');
				  
					clearInterval(autoRun);
					
					
					
					var that = jQuery(this);
					
					if(configs.msiedetection && jQuery.browser.msie){
						jQuery(childs[currentSlide - 1]).hide();
					}
					else{
            jQuery('.left_slide', childs[currentSlide - 1]).animate({
					     width : 0
            }, configs.animSpeed, function(){
              
              jQuery(childs[currentSlide - 1]).fadeOut(100, 'linear');
              
               jQuery(configs.slideNavigationElement + ' li a.sel').attr('class', 'unsel');
					     currentSlide = parseInt(that.attr('rel'), 10) + 1;
               
               
               that.attr('class', 'sel');
					
    					//Configurar o autoslide
    					if(configs.autoSlide && !configs.stopAutoSlideWhenClick){
    						AutoSlide();
    					}
      					
              jQuery(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear', function(){
  						  jQuery('.left_slide', childs[currentSlide - 1]).animate({
                  width : 220
                }, configs.animSpeed);

                 //currentSlide++;
              });
            });
          
          
          
          }
					

					
					return false;
				});
				
				//Calcular width do elemento
				if(configs.setNavigationElementWidth){
				   
					var ulChilds = jQuery(configs.slideNavigationElement).children().length;
					var childWidth = jQuery(configs.slideNavigationElement + ' li:last').outerWidth(true);
					
					jQuery(configs.slideNavigationElement).width(parseInt((ulChilds * childWidth), 10));
				}
			};
			
			/*
			//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');
					}
					
					//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');
					}
					
					//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 && jQuery.browser.msie){
						jQuery(childs[totChilds - 1]).hide();
						jQuery(childs[currentSlide - 1]).show();
					}
					else{
            
            jQuery('.left_slide', childs[totChilds - 1]).stop().animate({
					     width : 0
            }, configs.animSpeed, function(){
              
              jQuery(childs[totChilds - 1]).fadeOut(100, 'linear');
              
               //Seleccionar elemento de navegação
      					if(configs.slideNavigation && configs.slideNavigationElement !=''){
      						SlideNavigationCurrent(currentSlide - 1);
      					}
      					
              jQuery(childs[currentSlide - 1]).fadeIn(configs.animSpeed, 'linear', function(){
  						  jQuery('.left_slide', childs[currentSlide - 1]).animate({
                  width : 220
                }, configs.animSpeed);

                 //currentSlide++;
              });
            });
    
					}
					
					
				}
				else{
					if(configs.msiedetection && jQuery.browser.msie){
						jQuery(childs[currentSlide - 1]).hide();
						jQuery(childs[currentSlide]).show();
					}
					else{
					 jQuery('.left_slide', childs[currentSlide - 1]).stop().animate({
					     width : 0
            }, configs.animSpeed, function(){
              
              jQuery(childs[currentSlide - 1]).fadeOut(100, 'linear');
              
                //Seleccionar elemento de navegação
      					if(configs.slideNavigation && configs.slideNavigationElement !=''){
      						SlideNavigationCurrent(currentSlide);
      					}
      					
              jQuery(childs[currentSlide]).fadeIn(configs.animSpeed, 'linear', function(){
  						  jQuery('.left_slide', childs[currentSlide]).animate({
                  width : 220
                }, configs.animSpeed);
      					
                 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 + ' li a.sel').attr('class', 'unsel');
				
				jQuery(configs.slideNavigationElement + ' li a').filter(function(i){
					return (parseInt(jQuery(this).attr('rel'), 10) == (toCompare)) ? true : false;
				}).attr('class', 'sel');
			};
		});
	};
})(jQuery);
