"use strict";

var pt = pt || {};
pt.portoeditora = pt.portoeditora || {};
pt.portoeditora.infopedia = pt.portoeditora.infopedia || {};
pt.portoeditora.infopedia.ev = pt.portoeditora.infopedia.ev || {};
pt.portoeditora.infopedia.ev.conjugador = pt.portoeditora.infopedia.ev.conjugador || {};

pt.portoeditora.infopedia.ev.conjugador.filtro = pt.portoeditora.infopedia.ev.conjugador.filtro || {
	modo: "all",
	tempo: "all",
	pessoa: "all"
};

pt.portoeditora.infopedia.ev.conjugador.init = function () {
	pt.portoeditora.infopedia.ev.conjugador.modo = "conjugador";
	pt.portoeditora.infopedia.ev.conjugador.dicioUri = jQuery("body").attr("data-dicio-uri");
	pt.portoeditora.infopedia.ev.conjugador.dicioFiltro = jQuery("body").attr("data-filtro");

	var $conjugarContainer = jQuery("#conjugarContainer");
	var $alertContainer = jQuery("#alertContainer");
	var $filtroContainer = jQuery("#filtro-container");
	var $listContainer = jQuery("#list-container");
	var $modoContainer = jQuery("#modo-container");
	var $tempoContainer = jQuery("#tempo-container");
	var $pessoaContainer = jQuery("#pessoa-container");
	var $aplicarFiltroContainer = jQuery("#aplicar-filtro-container");
	var $aplicarListContainer = jQuery("#aplicar-list-container");
	var $filtroContainerStatus = jQuery("#filtro-container-status");
	
	var $jogarContainer = jQuery("#jogarContainer");
	
	if('verbos-ingleses' === pt.portoeditora.infopedia.ev.conjugador.dicioUri
			|| ('verbos-portugueses-basico' === pt.portoeditora.infopedia.ev.conjugador.dicioUri && !pt.portoeditora.infopedia.ev.conjugador.dicioFiltro)) {
		$modoContainer.remove();
	}
	
    var $barContainer = jQuery(".seletor-conjugador-container .seletor-conjugador-bar");
    if($barContainer.length > 0) {
    	var $conjugar = jQuery(".bt-conjugar", $barContainer);
		
		$conjugar.click(function() {
    		if(!jQuery(this).hasClass("ativo")
    				&& jQuery('#alertContainer').is(":hidden")) {
    			
    			jQuery('#panel-3').hide();
    			jQuery('#panel-2').show();
    			
    			if(pt.portoeditora.infopedia.ev.conjugador.jogar.started) {
    				pt.portoeditora.infopedia.ev.conjugador.jogar.stopProgress();
    				
    				$jogarContainer.hide();
    				$alertContainer.show();
    			} else {
	    			$jogar.removeClass("ativo");
	    			jQuery(this).addClass("ativo");
	    			
	    			pt.portoeditora.infopedia.ev.conjugador.modo = "conjugar";
	    			
	    			$jogarContainer.hide();
	    			$conjugarContainer.show();
    			}
    		}
    	});
		
		jQuery("#ok-bt").click(function() {
			pt.portoeditora.infopedia.ev.conjugador.jogar.resetJogo();
			
			pt.portoeditora.infopedia.ev.conjugador.jogar.started = false;
			
			$jogar.removeClass("ativo");
			$conjugar.addClass("ativo");
			
			pt.portoeditora.infopedia.ev.conjugador.modo = "conjugar";
			
			$alertContainer.hide();
			$conjugarContainer.show();
		});
		
		jQuery("#cancelar-bt").click(function() {
			pt.portoeditora.infopedia.ev.conjugador.jogar.startProgress();
			
			$alertContainer.hide();
			$jogarContainer.show();
		});
		
		jQuery("#alertAutocompleteContainer .alert-block-container img").click(function() {
			jQuery('#alertAutocompleteContainer').hide();
			jQuery('#conjugarContainer').show();
		});
		
		// Lista de entradas
		if($listContainer.length > 0) {
			var $listSeletorRadio = jQuery(".list-block-container [data-entrada]", $listContainer);
			
			$listSeletorRadio.click(function() {
				var $this = jQuery(this);
				
				var $ativo = jQuery(".list-block-container [data-entrada=\""+pt.portoeditora.infopedia.ev.conjugador.entrada+"\"]", $listContainer);
				if($ativo.length > 0) {
					$ativo.removeClass('ativo');
					$ativo.parent().removeClass('ativo');
				}
				
				pt.portoeditora.infopedia.ev.conjugador.entrada = $this.attr("data-entrada");
				
				$this.addClass("ativo");
				$this.parent().addClass("ativo");
	    	});

			jQuery("#list-container-bt").click(function() {
				var $this = jQuery(this);
				
				var $btImg = jQuery("img", $this);
				
				if($btImg.attr("src") == "/images/down.svg") {
					$btImg.attr("src", "/images/up.svg");
					
					$filtroContainer.hide();
					
					$listContainer.css('margin-bottom', '20px');
					
					jQuery('.list-block-container', $listContainer).show();
					
					$aplicarListContainer.show();
				} else {
					$btImg.attr("src", "/images/down.svg");
					
					jQuery('.list-block-container', $listContainer).hide();
					
					$listContainer.css('margin-bottom', '0px');
					
					$aplicarListContainer.hide();
					
					$filtroContainer.show();
				}
	    	});
			
			var $listContainerInput = jQuery(".list-container-left", $listContainer);
			
			var $aplicarListBt = jQuery("#aplicar-list-bt");
			$aplicarListBt.click(function() {
				var $this = jQuery(this);
				
				if(pt.portoeditora.infopedia.ev.conjugador.entrada) {
					$listContainerInput.text(pt.portoeditora.infopedia.ev.conjugador.entrada);
					
					jQuery("#list-container-bt img").attr("src", "/images/down.svg");
					
					jQuery('.list-block-container', $listContainer).hide();
					$aplicarListContainer.hide();
					
					$listContainer.css('margin-bottom', '0px');
					
					var $filtroContainerBt = jQuery("#filtro-container-bt");
					if(jQuery("img", $filtroContainerBt).attr("src") == "/images/up.svg") {
						$filtroContainerBt.trigger("click");
					}
					
					$filtroContainer.show();

					if(pt.portoeditora.infopedia.ev.conjugador.entrada) {
						pt.portoeditora.infopedia.ev.conjugador.conjugar.getContent(
							pt.portoeditora.infopedia.ev.conjugador.dicioUri, pt.portoeditora.infopedia.ev.conjugador.entrada, true
						);
					}
				}
	    	});
		}
		
		jQuery("#filtro-container-bt").click(function() {
			var $this = jQuery(this);
			
			var $btImg = jQuery("img", $this);

			var $switch = $btImg.attr('data-switch');
			$btImg.attr("data-switch", $btImg.attr("src"));
			$btImg.attr("src", $switch);
			
			var $status = $btImg.attr('data-status');
			$btImg.attr("data-status", ("down" == $status ? "up" : "down"));
			
			if("down" === $status) {
				jQuery('#filtro-container').css('margin-bottom', '20px');
				
				if($filtroContainerStatus.children().length == 0) {
					$filtroContainerStatus.parent().hide();
				}
				
				jQuery(".filtro-container-info", $filtroContainer).hide();
				
				$modoContainer.show();
				$tempoContainer.show();
				$pessoaContainer.show();
			} else {
				jQuery('#filtro-container').css('margin-bottom', '0px');
				
				if($filtroContainerStatus.children().length == 0) {
					$filtroContainerStatus.parent().hide();
					
					if(jQuery(".conjugar-content-container").is(':hidden')) {
						jQuery(".filtro-container-info", $filtroContainer).show();
					}
				}
				
				$modoContainer.hide();
				$tempoContainer.hide();
				$pessoaContainer.hide();
				
				var $btInImg = jQuery("img", $modoContainerBt);
				var $status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $modoContainer).hide();
				
				
				$btInImg = jQuery("img", $tempoContainerBt);
				$status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $tempoContainer).hide();
				
				$btInImg = jQuery("img", $pessoaContainerBt);
				$status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $pessoaContainer).hide();
				
				$aplicarFiltroContainer.hide();
			}
    	});
		
		var $modoContainerBt = jQuery("#modo-container-bt");
		$modoContainerBt.click(function() {
			var $this = jQuery(this);
			
			var $btImg = jQuery("img", $this);

			var $switch = $btImg.attr('data-switch');
			$btImg.attr("data-switch", $btImg.attr("src"));
			$btImg.attr("src", $switch);
			
			var $status = $btImg.attr('data-status');
			$btImg.attr("data-status", ("down" == $status ? "up" : "down"));
			
			if("down" === $status) {
				jQuery("ul", $modoContainer).show();
				
				$aplicarFiltroContainer.show();
				
				var $btInImg = jQuery("img", $tempoContainerBt);
				var $status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $tempoContainer).hide();
				
				$btInImg = jQuery("img", $pessoaContainerBt);
				$status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $pessoaContainer).hide();
			} else {
				jQuery("ul", $modoContainer).hide();
				
				$aplicarFiltroContainer.hide();
			}
    	});

		var $tempoContainerBt = jQuery("#tempo-container-bt");
		$tempoContainerBt.click(function() {
			var $this = jQuery(this);
			
			var $btImg = jQuery("img", $this);

			var $switch = $btImg.attr('data-switch');
			$btImg.attr("data-switch", $btImg.attr("src"));
			$btImg.attr("src", $switch);
			
			var $status = $btImg.attr('data-status');
			$btImg.attr("data-status", ("down" == $status ? "up" : "down"));
			
			if("down" === $status) {
				jQuery("ul", $tempoContainer).show();
				
				$aplicarFiltroContainer.show();
				
				var $btInImg = jQuery("img", $modoContainerBt);
				var $status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $modoContainer).hide();
				
				$btInImg = jQuery("img", $pessoaContainerBt);
				$status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $pessoaContainer).hide();
			} else {
				jQuery("ul", $tempoContainer).hide();
				
				$aplicarFiltroContainer.hide();
			}
    	});
		
		var $pessoaContainerBt = jQuery("#pessoa-container-bt");
		$pessoaContainerBt.click(function() {
			var $this = jQuery(this);
			
			var $btImg = jQuery("img", $this);
			
			var $switch = $btImg.attr('data-switch');
			$btImg.attr("data-switch", $btImg.attr("src"));
			$btImg.attr("src", $switch);
			
			var $status = $btImg.attr('data-status');
			$btImg.attr("data-status", ("down" == $status ? "up" : "down"));
			
			
			if("down" === $status) {
				jQuery("ul", $pessoaContainer).show();
				
				$aplicarFiltroContainer.show();
				
				var $btInImg = jQuery("img", $modoContainerBt);
				var $status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $modoContainer).hide();
				
				$btInImg = jQuery("img", $tempoContainerBt);
				$status = $btInImg.attr("data-status");
				if($status != "down") {
					$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
					$switch = $btInImg.attr('data-switch');
					$btInImg.attr("data-switch", $btInImg.attr("src"));
					$btInImg.attr("src", $switch);
				}
				jQuery("ul", $tempoContainer).hide();
			} else {
				jQuery("ul", $pessoaContainer).hide();
				
				$aplicarFiltroContainer.hide();
			}
    	});
		
		var $aplicarFiltroBt = jQuery("#aplicar-filtro-bt");
		$aplicarFiltroBt.click(function() {
			var $this = jQuery(this);
			
			var $btInImg = jQuery("#filtro-container-bt img");
			$status = $btInImg.attr("data-status");
			if($status != "down") {
				$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
				var $switch = $btInImg.attr('data-switch');
				$btInImg.attr("data-switch", $btInImg.attr("src"));
				$btInImg.attr("src", $switch);
			}
			
			$modoContainer.hide();
			$tempoContainer.hide();
			$pessoaContainer.hide();
			
			$btInImg = jQuery("img", $modoContainerBt);
			var $status = $btInImg.attr("data-status");
			if($status != "down") {
				$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
				$switch = $btInImg.attr('data-switch');
				$btInImg.attr("data-switch", $btInImg.attr("src"));
				$btInImg.attr("src", $switch);
			}
			jQuery("ul", $modoContainer).hide();
			
			$btInImg = jQuery("img", $tempoContainerBt);
			$status = $btInImg.attr("data-status");
			if($status != "down") {
				$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
				$switch = $btInImg.attr('data-switch');
				$btInImg.attr("data-switch", $btInImg.attr("src"));
				$btInImg.attr("src", $switch);
			}
			jQuery("ul", $tempoContainer).hide();
			
			$btInImg = jQuery("img", $pessoaContainerBt);
			$status = $btInImg.attr("data-status");
			if($status != "down") {
				$btInImg.attr("data-status", ("down" == $status ? "up" : "down"));
				$switch = $btInImg.attr('data-switch');
				$btInImg.attr("data-switch", $btInImg.attr("src"));
				$btInImg.attr("src", $switch);
			}
			jQuery("ul", $pessoaContainer).hide();
			
			$aplicarFiltroContainer.hide();
			
			$filtroContainerStatus.empty();
			
			if(pt.portoeditora.infopedia.ev.conjugador.filtro.modo != "all"
				|| pt.portoeditora.infopedia.ev.conjugador.filtro.tempo != "all"
				|| pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa != "all") {
				
				if(pt.portoeditora.infopedia.ev.conjugador.filtro.modo != "all") {
					$filtroContainerStatus.append("<li data-filtro='modo'>" + pt.portoeditora.infopedia.ev.conjugador.filtro.modo + " <img src='/images/x.svg'/></li>");
				}
				
				if(pt.portoeditora.infopedia.ev.conjugador.filtro.tempo != "all") {
					var $filtroTempo = pt.portoeditora.infopedia.ev.conjugador.filtro.tempo;
					
					if("verbos-ingleses" === pt.portoeditora.infopedia.ev.conjugador.dicioUri) {
						if("Simple present" === pt.portoeditora.infopedia.ev.conjugador.filtro.tempo) {
							$filtroTempo = "Present simple";
						} else if("Simple past" === pt.portoeditora.infopedia.ev.conjugador.filtro.tempo) {
							$filtroTempo = "Past simple";
						} else if("Future" === pt.portoeditora.infopedia.ev.conjugador.filtro.tempo) {
							$filtroTempo = "future simple";
						}
					}
					
					$filtroContainerStatus.append("<li data-filtro='tempo'>" + $filtroTempo + " <img src='/images/x.svg'/></li>");
				}
				
				if(pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa != "all") {
					$filtroContainerStatus.append("<li data-filtro='pessoa'>" + pt.portoeditora.infopedia.ev.conjugador.filtro.pessoaValue + " <img src='/images/x.svg'/></li>");
				}
				
				jQuery("li", $filtroContainerStatus).click(function() {
					var $this = jQuery(this);
					var filtro = $this.attr("data-filtro");
					
					if(filtro == "modo") {
						var $filtro = jQuery("[data-modo-filtro]", $modoContainer);
						
						$filtro.removeClass("ativo");
						
						pt.portoeditora.infopedia.ev.conjugador.filtro.modo = "all";
						
						$filtro.first().addClass("ativo");
					} else if(filtro == "tempo") {
						var $filtro = jQuery("[data-tempo-filtro]", $tempoContainer);
						
						$filtro.removeClass("ativo");
						
						pt.portoeditora.infopedia.ev.conjugador.filtro.tempo = "all";
						
						$filtro.first().addClass("ativo");
					} else if(filtro == "pessoa") {
						var $filtro = jQuery("[data-pessoa-filtro]", $pessoaContainer);
						
						$filtro.removeClass("ativo");
						
						pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa = "all";
						
						$filtro.first().addClass("ativo");
					}
					
					$this.remove();
					
					if(pt.portoeditora.infopedia.ev.conjugador.filtro.modo == "all"
						&& pt.portoeditora.infopedia.ev.conjugador.filtro.tempo == "all"
						&& pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa == "all") {
						
						$filtroContainerStatus.parent().hide();
						
						if(jQuery('#conjugar-content-container').is(":hidden")
								&& $modoContainer.is(":hidden")
								&& $tempoContainer.is(":hidden")
								&& $pessoaContainer.is(":hidden")) {
							
							jQuery(".filtro-container-info", $filtroContainer).show();
						}
					}
					
					if(pt.portoeditora.infopedia.ev.conjugador.entrada) {
						pt.portoeditora.infopedia.ev.conjugador.conjugar.getContent(
							pt.portoeditora.infopedia.ev.conjugador.dicioUri, pt.portoeditora.infopedia.ev.conjugador.entrada, true
						);
					}
				});
				
				$filtroContainerStatus.parent().show();
			} else {
				$filtroContainerStatus.parent().hide();
				
                jQuery('#filtro-container').css('margin-bottom', '0px');
				
				if(jQuery('#conjugar-content-container').is(":hidden")
						&& $modoContainer.is(":hidden")
						&& $tempoContainer.is(":hidden")
						&& $pessoaContainer.is(":hidden")) {

					jQuery(".filtro-container-info", $filtroContainer).show();
				}
			}
			
			if(pt.portoeditora.infopedia.ev.conjugador.entrada) {
				pt.portoeditora.infopedia.ev.conjugador.conjugar.getContent(
					pt.portoeditora.infopedia.ev.conjugador.dicioUri,
					pt.portoeditora.infopedia.ev.conjugador.entrada,
					true
				);
			}
    	});
		
		// MODO DE JOGO
    	var $jogar = jQuery(".bt-jogar", $barContainer);
    	
    	$jogar.click(function() {
    		if(!jQuery(this).hasClass("ativo")) {
    			$conjugar.removeClass("ativo");
    			jQuery(this).addClass("ativo");
    			
    			pt.portoeditora.infopedia.ev.conjugador.modo = "jogar";
    			
    			jQuery('#alertAutocompleteContainer').hide();
    			$conjugarContainer.hide();
    			jQuery('#panel-3').hide();
    			$jogarContainer.show();
    			jQuery('#panel-2').show();
    			
    			pt.portoeditora.infopedia.ev.conjugador.jogar.resetJogo();
    		}
    	});
    }
    
    setTimeout(function() {
    	jQuery("div.logoText").hide();
    	jQuery("#introContainer").hide();
    	
    	jQuery("div.logo").show();
    	jQuery(".seletor-conjugador-container").show();
    	
    	jQuery('#panel-1').hide();
    	jQuery('#panel-2').show();
    	
    	$conjugarContainer.show();
    }, 5000);
};


/* ##### CONJUGAR ##### */

pt.portoeditora.infopedia.ev.conjugador.conjugar = pt.portoeditora.infopedia.ev.conjugador.conjugar || {};

pt.portoeditora.infopedia.ev.conjugador.conjugar.input = pt.portoeditora.infopedia.ev.conjugador.conjugar.input || {};
pt.portoeditora.infopedia.ev.conjugador.conjugar.input.selected = false;

pt.portoeditora.infopedia.ev.conjugador.conjugar.init = function () {

	jQuery("#conjugar-input-container-bt").click(function() {
		if(!pt.portoeditora.infopedia.ev.conjugador.conjugar.input.selected) {
			var $searchBarAutocomplete = jQuery('.search-bar-autocomplete');
			
			if($searchBarAutocomplete.length > 0) {
				var $searchBarAutocompleteSelected = jQuery('ul li.active', $searchBarAutocomplete);
		    	
		        if($searchBarAutocompleteSelected.length > 0) {
		        	pt.portoeditora.infopedia.ev.conjugador.conjugar.input.selected = true;
		        	
		        	var $title = $searchBarAutocompleteSelected.attr("title");
		            
		        	jQuery('#conjugarContainer input').val($title);

		            jQuery('ul', $searchBarAutocomplete).hide();
	
		            $searchBarAutocompleteSelected.removeClass('active');
		        }
			}
		}

		if(pt.portoeditora.infopedia.ev.conjugador.conjugar.input.selected) {
			var $this = jQuery(this);
			
			pt.portoeditora.infopedia.ev.conjugador.entrada = jQuery('#conjugarContainer input').val();
	
			var $filtroContainerBt = jQuery("#filtro-container-bt");
			if(jQuery("img", $filtroContainerBt).attr("data-status") == "up") {
				$filtroContainerBt.trigger("click");
			}
			
			if(pt.portoeditora.infopedia.ev.conjugador.entrada) {
				pt.portoeditora.infopedia.ev.conjugador.conjugar.getContent(
					pt.portoeditora.infopedia.ev.conjugador.dicioUri, pt.portoeditora.infopedia.ev.conjugador.entrada, false
				);
			}
		}
	});
	
	pt.portoeditora.infopedia.ev.conjugador.conjugar.autocomplete.init();
}

pt.portoeditora.infopedia.ev.conjugador.conjugar.getContent = function (uri, entrada, applyFilter) {
	var $entrada = pt.portoeditora.infopedia.ev.conjugador.entrada;
    
    if($entrada) {
        var $href = '';
        
        if(pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa !== "all") {
        	$href = '/' + pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa;
        }
        
        if(pt.portoeditora.infopedia.ev.conjugador.filtro.tempo !== "all"
        	|| (pt.portoeditora.infopedia.ev.conjugador.filtro.tempo == "all" && pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa !== "all")
        ) {
        	
        	$href = '/' + pt.portoeditora.infopedia.ev.conjugador.filtro.tempo + $href;
        }
        
        if(pt.portoeditora.infopedia.ev.conjugador.filtro.modo !== "all"
        	|| (pt.portoeditora.infopedia.ev.conjugador.filtro.modo == "all" && (pt.portoeditora.infopedia.ev.conjugador.filtro.tempo !== "all" || pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa !== "all"))
        ) {
        	
        	$href = '/' + pt.portoeditora.infopedia.ev.conjugador.filtro.modo + $href;
        }
        
        $href = "/conjugador/" + uri + "/" + $entrada + $href + (pt.portoeditora.infopedia.ev.conjugador.dicioFiltro ? '?filtro=' + pt.portoeditora.infopedia.ev.conjugador.dicioFiltro : '');
        
        jQuery.ajax({
            url: $href,
            type: "GET",
            dataType: "json"
        }).done(function (data) {
        	if(jQuery("#modo-container").is(":hidden") && jQuery("#tempo-container").is(":hidden") && jQuery("#pessoa-container").is(":hidden")) {
				jQuery('#filtro-container').css('margin-bottom', '0px');
			} else {
				jQuery('#filtro-container').css('margin-bottom', '0px');
			}
			
			jQuery("#filtro-container .filtro-container-info").hide();
			
			jQuery('#conjugar-content-container').html(data.html);
			jQuery('#conjugar-content-container').show();
			
			pt.portoeditora.infopedia.ev.conjugador.conjugar.populateModoFilter(pt.portoeditora.infopedia.ev.conjugador.dicioUri, $entrada);
			pt.portoeditora.infopedia.ev.conjugador.conjugar.populateTempoFilter(pt.portoeditora.infopedia.ev.conjugador.dicioUri, $entrada);
			pt.portoeditora.infopedia.ev.conjugador.conjugar.populatePessoaFilter(pt.portoeditora.infopedia.ev.conjugador.dicioUri, $entrada);
        }).fail(function (error) {
        	jQuery('#filtro-container').css('margin-bottom', '20px');
        	
        	jQuery("#filtro-container .filtro-container-info").show();
        });
    }

    return false;
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.autocomplete = pt.portoeditora.infopedia.ev.conjugador.conjugar.autocomplete || {};

pt.portoeditora.infopedia.ev.conjugador.conjugar.autocomplete.init = function() {
	var $searchBarAutocomplete = jQuery('.search-bar-autocomplete');
	
	if($searchBarAutocomplete.length == 0) {
		return false;
	}
	
	jQuery("#filtro-container").css('padding-top', '70px');
	
    var $searchBarWord = jQuery('.conjugar-input-container-left input', $searchBarAutocomplete);
    
    var $searchBarAutocompleteTimer = null;
    var $searchBarAutocompleteSelected = null;
    var $autocompleteIndexSelected = 0;
    
    $searchBarWord.keydown(function(event) {
        var $text = jQuery.trim(jQuery(this).val());
        if($text.length === 1) {
           $text = $text.replace(/\.|@|±|§|!|"|@||#|€|£|$|%|¶|&|•|∞|¢|ﬂ|ﬁ|¡|÷|\/|\|{|}|\[|\]|\(|\)|≠|=|≈|\?|'|¿|\*|◊|\+|`|´|˝|~|^|ˆ|-|_|–|,|”|;|<|>|≥|≤|˛«|©|√|¬|µ|ß|˙|¯|„|‘|\¸|’|œ|∑|®|™|¥|†|ı|ø|π|∏|Ø|˚|‡|¥|Æ|Œ/g,'');
        }

        if (event.keyCode === 13 && $text) {
        	$searchBarAutocompleteSelected = jQuery('ul li.active', $searchBarAutocomplete);
        	
            if($searchBarAutocompleteSelected.length > 0) {
            	pt.portoeditora.infopedia.ev.conjugador.conjugar.input.selected = true;
            	
            	$searchBarAutocompleteSelected.trigger('click');

                jQuery('ul', $searchBarAutocomplete).hide();

                $searchBarAutocompleteSelected.removeClass('active');

                $autocompleteIndexSelected = 0;
            }
        }
    }).keyup(function(event) {
        if(event.keyCode === 40 || event.keyCode === 38) {//return when click down and up arrows
            return;
        }

        clearTimeout($searchBarAutocompleteTimer);

        if(event.keyCode !== 13) {
        	pt.portoeditora.infopedia.ev.conjugador.conjugar.input.selected = false;
        	
            jQuery('ul li', $searchBarAutocomplete).filter("[title]").removeClass('active');

            $autocompleteIndexSelected = 0;

            var $text = jQuery.trim(jQuery(this).val());

            if($text.length > 0) {
                $searchBarAutocompleteTimer = setTimeout(function () {
                    var $palavra = $text;

                    var $promise = $autocomplete($palavra);

                    $promise.done(function (data) {
                    	if(data && data.html) {
                        	jQuery('ul', $searchBarAutocomplete).html(data.html);
                        	
                        	jQuery('ul li:first-child', $searchBarAutocomplete).filter("[title]").addClass('active');
                        	
                            jQuery('ul', $searchBarAutocomplete).show();
                        } else {
                        	jQuery('ul', $searchBarAutocomplete).empty();
                        	jQuery('ul', $searchBarAutocomplete).hide();
                        	
                        	jQuery('#conjugarContainer').hide();
                        	jQuery('#alertAutocompleteContainer').show();
                        }
                    }).fail(function (error) {
                    	jQuery('ul', $searchBarAutocomplete).empty();
                    	jQuery('ul', $searchBarAutocomplete).hide();
                    	
                    	jQuery('#conjugarContainer').hide();
                    	jQuery('#alertAutocompleteContainer').show();
                    });
                }, 300);
            } else {
            	jQuery('ul', $searchBarAutocomplete).empty();
                jQuery('ul', $searchBarAutocomplete).hide();
            }
        }
    }).blur(function () {
        clearTimeout($searchBarAutocompleteTimer);

        $searchBarAutocompleteTimer = setTimeout(function () {
            jQuery('ul', $searchBarAutocomplete).hide();
            
            jQuery('ul li', $searchBarAutocomplete).filter("[title]").removeClass('active');

            $autocompleteIndexSelected = 0;
        }, 300);
    });

    // Autocomplete
    var $autocomplete = function(token) {
        var $deferred = jQuery.Deferred();
        var $text = jQuery.trim(token);
        if ($text) {
            var $href = pt.portoeditora.infopedia.ev.conjugador.dicioUri + "/sugestao-pesquisa/" + $text + (pt.portoeditora.infopedia.ev.conjugador.dicioFiltro ? '?filtro=' + pt.portoeditora.infopedia.ev.conjugador.dicioFiltro : '');
            
            jQuery.ajax({
                url: $href,
                type: "GET",
                dataType: "json"
            }).done(function (data) {
                $deferred.resolve(data);
            }).fail(function (error) {
                $deferred.reject(error);
            });
        } else {
            $deferred.reject("ERROR: No token!");
        }

        return $deferred.promise();
    };

    jQuery('ul', $searchBarAutocomplete).on("mouseover", "li", function(event) {
        $searchBarAutocompleteSelected = jQuery(event.target);
    }).on("click", "li", function(event) {
        if(typeof(jQuery(event.target).attr("title")) === 'undefined') {
            $searchBarAutocompleteSelected = jQuery(event.target).parent();
        } else {
            $searchBarAutocompleteSelected = jQuery(event.target);
        }

        var $title = $searchBarAutocompleteSelected.attr("title");
        
        $searchBarWord.val($title);
        
        pt.portoeditora.infopedia.ev.conjugador.conjugar.input.selected = true;
        
        jQuery("#conjugar-input-container-bt").trigger("click");

        return false;
    });
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.populateModoFilter = function (uri, entrada) {
	var $entrada = pt.portoeditora.infopedia.ev.conjugador.entrada;
    
    if("verbos-ingleses" != uri && $entrada) {
		pt.portoeditora.infopedia.ev.conjugador.conjugar.clearModoFilter();

		var $href = "/conjugador/" + uri + "/" + $entrada + "/modos?tempo=" + pt.portoeditora.infopedia.ev.conjugador.filtro.tempo + "&pessoa=" + pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa;
		
		$href = $href + (pt.portoeditora.infopedia.ev.conjugador.dicioFiltro ? '&filtro=' + pt.portoeditora.infopedia.ev.conjugador.dicioFiltro : '');
		
		jQuery.ajax({
	        url: $href,
	        type: "GET",
	        dataType: "json"
	    }).done(function (data) {
	    	if(data) {
	    		jQuery.each(data, function(key, item) {
	    			jQuery.each(item, function(key, value) {
	    				jQuery("#modo-container ul").append("<li>"+ value +" <div data-modo-filtro=\""+ key +"\" class=\"seletor-radio\"><div class=\"ball\"></div></div></li>");
	    			});
    			});
	    		
	    		var $modoSeletorRadio = jQuery("#modo-container [data-modo-filtro]");
	    		
	    		var $ativo = jQuery("#modo-container [data-modo-filtro=\""+pt.portoeditora.infopedia.ev.conjugador.filtro.modo+"\"]");
	    		if($ativo.length > 0) {
	    			$ativo.addClass('ativo');
	    		} else {
	    			jQuery("#pessoa-container [data-modo-filtro='all']").addClass('ativo');
	    		}
	    		
	    		$modoSeletorRadio.off('click');
	    		
	    		$modoSeletorRadio.click(function() {
	    			var $this = jQuery(this);
	    			
	    			pt.portoeditora.infopedia.ev.conjugador.filtro.modo = $this.attr("data-modo-filtro");
	    			
	    			$modoSeletorRadio.removeClass("ativo");
	    			$this.addClass("ativo");
	    			
	    			pt.portoeditora.infopedia.ev.conjugador.conjugar.populateTempoFilter(uri, entrada);
	    			pt.portoeditora.infopedia.ev.conjugador.conjugar.populatePessoaFilter(uri, entrada);
	        	});
	    	}
	    }).fail(function (error) {
	    	console.log(error);
	    });
    }
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.populateTempoFilter = function (uri, entrada) {
	var $entrada = pt.portoeditora.infopedia.ev.conjugador.entrada;
    
    if($entrada) {
		pt.portoeditora.infopedia.ev.conjugador.conjugar.clearTempoFilter();

		var $href = "/conjugador/" + uri + "/" + $entrada + "/tempos?modo=" + pt.portoeditora.infopedia.ev.conjugador.filtro.modo + "&pessoa=" + pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa;
	
		$href = $href + (pt.portoeditora.infopedia.ev.conjugador.dicioFiltro ? '&filtro=' + pt.portoeditora.infopedia.ev.conjugador.dicioFiltro : '');
		
		jQuery.ajax({
	        url: $href,
	        type: "GET",
	        dataType: "json"
	    }).done(function (data) {
	    	if(data) {
	    		jQuery.each(data, function(key, item) {
	    			jQuery.each(item, function(key, value) {
	    				jQuery("#tempo-container ul").append("<li>"+ value +" <div data-tempo-filtro=\""+ key +"\" class=\"seletor-radio\"><div class=\"ball\"></div></div></li>");
	    			});
    			});
	    		
	    		var $tempoSeletorRadio = jQuery("#tempo-container [data-tempo-filtro]");
	    		
	    		var $ativo = jQuery("#tempo-container [data-tempo-filtro=\""+pt.portoeditora.infopedia.ev.conjugador.filtro.tempo+"\"]");
	    		if($ativo.length > 0) {
	    			$ativo.addClass('ativo');
	    		} else {
	    			jQuery("#pessoa-container [data-tempo-filtro='all']").addClass('ativo');
	    		}
	    		
	    		$tempoSeletorRadio.off('click');
	    		
	    		$tempoSeletorRadio.click(function() {
	    			var $this = jQuery(this);
	    			
	    			pt.portoeditora.infopedia.ev.conjugador.filtro.tempo = $this.attr("data-tempo-filtro");
	    			
	    			$tempoSeletorRadio.removeClass("ativo");
	    			$this.addClass("ativo");
	    			
	    			pt.portoeditora.infopedia.ev.conjugador.conjugar.populateModoFilter(uri, entrada);
	    			pt.portoeditora.infopedia.ev.conjugador.conjugar.populatePessoaFilter(uri, entrada);
	        	});
	    	}
	    }).fail(function (error) {
	    	console.log(error);
	    });
    }
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.populatePessoaFilter = function (uri, entrada) {
	var $entrada = pt.portoeditora.infopedia.ev.conjugador.entrada;
    
    if($entrada) {
		pt.portoeditora.infopedia.ev.conjugador.conjugar.clearPessoaFilter();
		//pt.portoeditora.infopedia.ev.conjugador.conjugar.clearFilterStatus();
		
		//if(pt.portoeditora.infopedia.ev.conjugador.filtro.tempo != "all") {
			var $href = "/conjugador/" + uri + "/" + $entrada + "/pessoas?modo=" + pt.portoeditora.infopedia.ev.conjugador.filtro.modo + "&tempo=" + pt.portoeditora.infopedia.ev.conjugador.filtro.tempo;
		
			$href = $href + (pt.portoeditora.infopedia.ev.conjugador.dicioFiltro ? '&filtro=' + pt.portoeditora.infopedia.ev.conjugador.dicioFiltro : '');
			
			jQuery.ajax({
		        url: $href,
		        type: "GET",
		        dataType: "json"
		    }).done(function (data) {
		    	if(data) {
		    		jQuery.each(data, function(key, item) {
		    			jQuery.each(item, function(key, value) {
		    				jQuery("#pessoa-container ul").append("<li>"+ value +" <div data-pessoa-filtro=\""+ key +"\" data-pessoa-value=\""+ value +"\" class=\"seletor-radio\"><div class=\"ball\"></div></div></li>");
		    			});
	    			});
		    		
		    		var $pessoaSeletorRadio = jQuery("#pessoa-container [data-pessoa-filtro]");
		    		
		    		var $ativo = jQuery("#pessoa-container [data-pessoa-filtro=\""+pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa+"\"]");
		    		if($ativo.length > 0) {
		    			$ativo.addClass('ativo');
		    		} else {
		    			jQuery("#pessoa-container [data-pessoa-filtro='all']").addClass('ativo');
		    		}
		    		
		    		$pessoaSeletorRadio.off('click');
		    		
		    		$pessoaSeletorRadio.click(function() {
		    			var $this = jQuery(this);
		    			
		    			pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa = $this.attr("data-pessoa-filtro");
		    			pt.portoeditora.infopedia.ev.conjugador.filtro.pessoaValue = $this.attr("data-pessoa-value");
		    			
		    			$pessoaSeletorRadio.removeClass("ativo");
		    			$this.addClass("ativo");
		    			
		    			pt.portoeditora.infopedia.ev.conjugador.conjugar.populateModoFilter(uri, entrada);
		    			pt.portoeditora.infopedia.ev.conjugador.conjugar.populateTempoFilter(uri, entrada);
		        	});
		    	}
		    }).fail(function (error) {
		    	console.log(error);
		    });
		//}
    }
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.clearModoFilter = function () {
	//pt.portoeditora.infopedia.ev.conjugador.filtro.modo = "all";
	jQuery("#modo-container [data-modo-filtro]").off('click');
	
	jQuery("#modo-container ul li:not(:first-child)").remove();
	jQuery("#modo-container ul li").addClass("ativo");
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.clearTempoFilter = function () {
	//pt.portoeditora.infopedia.ev.conjugador.filtro.tempo = "all";
	jQuery("#modo-container [data-tempo-filtro]").off('click');
	
	jQuery("#tempo-container ul li:not(:first-child)").remove();
	jQuery("#tempo-container ul li").addClass("ativo");
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.clearPessoaFilter = function () {
	//pt.portoeditora.infopedia.ev.conjugador.filtro.pessoa = "all";
	jQuery("#modo-container [data-pessoa-filtro]").off('click');
	
	jQuery("#pessoa-container ul li:not(:first-child)").remove();
	jQuery("#pessoa-container ul li").addClass("ativo");
};

pt.portoeditora.infopedia.ev.conjugador.conjugar.clearFilterStatus = function () {
	jQuery("#filtro-container-status ul li").off('click');
	jQuery("#filtro-container-status ul").remove();
};

jQuery(document).ready(function () {
	pt.portoeditora.infopedia.ev.conjugador.init();
	pt.portoeditora.infopedia.ev.conjugador.conjugar.init();
});