(function(){
	jQuery.fn.jqContentSlider = function(configs){
		var autoRun, currentSlide, childs, totChilds;
		
		//Configurações por defeito
		configs = jQuery.extend({
			animSpeed : 600,
			prev : '',
			next : '',
			autoSlide : false,
			autoSlideInterval : 1000,
			autoSlideElement : '',
			autoSlideDirection : 'next',
			slideNavigation : false,
			slideNavigationElement : '',
			setNavigationElementWidth : true,
			stopAutoSlideWhenClick : false,
			startSlide : 1,
			msiedetection : true
		}, configs);
		
		return this.each(function(){
			childs = jQuery(this).children().hide();
			//jQuery(childs[configs.startSlide - 1]).show();
			jQuery(childs[configs.startSlide - 1]).fadeIn('slow');
			
			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);
					}

					nextSlide();
				});
			};
			
			//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);
					}

					prevSlide();
				});
			};
			
			//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'){
							nextSlide();
						}
						else if(configs.autoSlideDirection == 'next'){
							prevSlide();
						}
					}
					
					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';
					}
					
					//Linhas específicas para este site
					if(i == totChilds - 1 && videoCampaign == true){
						jQuery(configs.slideNavigationElement).append(jQuery('<li></li>').html('<a class="video" href="#" rel="' + i + '"></a>'));
					}
					else if (i < totChilds - 1){
						jQuery(configs.slideNavigationElement).append(jQuery('<li></li>').html('<a class="' + elemClass + '"href="#" rel="' + i + '"></a>'));
					}
				});
				
				//Adicionar evento aos elementos previamente criados
				jQuery(configs.slideNavigationElement + ' li a').click(function(){
					clearInterval(autoRun);
					//jQuery('.wrappervideo .padvideo').empty();
					
					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 + ' li a.sel').attr('class', 'unsel');
					jQuery(configs.slideNavigationElement + ' li a.videosel').attr('class', 'video');
					
					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');
					}
					
					if(that.attr('class') == 'video'){
						that.attr('class', 'videosel');
					}
					else{
						that.attr('class', 'sel');
					}
					
					var imgLink = jQuery('.slidercampaign > a').filter(function(){
						return (jQuery(this).attr('rel') == that.attr('rel')) ? true : false;
					});
					
					if(imgLink.length > 0){
						LoadCampaignImage(null, that.attr('rel'), imgLink)
					}
					
					var wrapLink = jQuery('.slidercampaign .campaignfoto').filter(function(){
						return (jQuery(this).attr('rel') == that.attr('rel')) ? true : false;
					});
					
					if(wrapLink.length > 0){
						LoadCampaignFitas(wrapLink);
					}
					
					//Configurar o autoslide
					if(configs.autoSlide && !configs.stopAutoSlideWhenClick){
						AutoSlide();
					}
					
					return false;
				});
				
				//Calcular width do elemento
				if(configs.setNavigationElementWidth){
					var ulChilds = jQuery(configs.slideNavigationElement).children().length,
					childWidth = jQuery(configs.slideNavigationElement + ' li:last').outerWidth(true),
					childsMult = ((ulChilds - 1) * childWidth);
					
					if (videoCampaign == false) {
						childsMult = ((ulChilds * childWidth) - 7);
					}					
					jQuery(configs.slideNavigationElement).width(parseInt(childsMult, 10));
				}
				
				//Linha específica para este site
				if (videoCampaign == true) {
					jQuery(configs.slideNavigationElement).width(jQuery(configs.slideNavigationElement).width() + 63);
				}
			};
			
			//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(childs[totChilds - 1]).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]).show();
					}
					else{
						jQuery(childs[currentSlide - 1]).fadeOut('fast', 'linear');
						jQuery(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 (){nextSlide()}, configs.autoSlideInterval);
				}
				else if(configs.autoSlideDirection == 'next'){
					autoRun = setInterval(function (){prevSlide()}, 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);
