function armazenarHash(hash){
	localStorage.setItem('location', document.location.toString().split('#')[0]);
	localStorage.setItem('hash', hash);
}

function getHash(){
	currentLocation = document.location.toString().split('#')[0];
	previousLocation = localStorage.getItem('location');
	
	if(currentLocation.match(previousLocation)){
		return localStorage.getItem('hash');
	}
}

function removerHash(){
	localStorage.removeItem('location');
	localStorage.removeItem('hash');
}

function bindEnterClick(event, objectId, triggerEvent) {
	if (event.keyCode == 13) {
		if(triggerEvent) {
			$(":input[id='" + objectId + "']").trigger(triggerEvent);
		}
		return false;
	}	
}

window.onload=function(){
	$('.tabs a').click(function (e) {
		e.preventDefault();
		$(this).tab('show');
	});
	
	// store the currently selected tab in the hash value
	$("a[data-toggle='tab']").on("shown.bs.tab", function (e) {
		var id = $(e.target).attr("href").substr(1);
		window.location.hash = id;
		$('body').scrollTop(0);
	});
	
	// on load of the page: switch to the currently selected tab
	var hash = getHash();
	if(!hash){
		hash = window.location.hash;
	} 
	$('a[data-toggle="tab"][href="' + hash + '"]').tab('show', function(e){
		//$('body').scrollTop(0);
	});
	removerHash();
	
    setTimeout(function(){
        scrollTo(0,-1);
    },0);
}

jQuery(function($) {
	
	$(function () {
	  $('[data-toggle="popover"]').popover();
	});
		
	//#main-slider
	$(function(){
		$('#main-slider.carousel').carousel({
			interval: 2000
		});
	});

	$( '.centered' ).each(function( e ) {
		$(this).css('margin-top',  ($('#main-slider').height() - $(this).height())/2);
	});

	$(window).resize(function(){
		$( '.centered' ).each(function( e ) {
			$(this).css('margin-top',  ($('#main-slider').height() - $(this).height())/2);
		});
	});

	
	//goto top
	$('.gototop').click(function(event) {
		event.preventDefault();
		$('html, body').animate({
			scrollTop: $("body").offset().top
		}, 500);
	});	

	//Pretty Photo
	$("a[rel^='prettyPhoto']").prettyPhoto({
		social_tools: false
	});	
});

$(document).ready(function () {
	
	atualizarAba();
    $(".navbar-collapse ul.navbar-nav a").unbind("click").bind("click", onMenuSelectionado);
    setInstituicaoTab();
	
    var navListItems = $('div.setup-panel div a'),
    	allWells = $('.setup-content'),
    	allNextBtn = $('.nextBtn');

     allWells.hide();

     navListItems.click(function (e) {
         e.preventDefault();
         var $target = $($(this).attr('href')),
                 $item = $(this);

         if (!$item.hasClass('disabled')) {
             navListItems.removeClass('btn-primary').addClass('btn-default');
             $item.addClass('btn-primary');
             allWells.hide();
             $target.show();
             $target.find('input:eq(0)').focus();
         }
     });

     allNextBtn.click(function(){
         var curStep = $(this).closest(".setup-content"),
             curStepBtn = curStep.attr("id"),
             nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
             curInputs = curStep.find("input[type='text'],input[type='url']"),
             isValid = true;

         $(".form-group").removeClass("has-error");
         for(var i=0; i<curInputs.length; i++){
             if (!curInputs[i].validity.valid){
                 isValid = false;
                 $(curInputs[i]).closest(".form-group").addClass("has-error");
             }
         }

         if (isValid)
             nextStepWizard.removeAttr('disabled').trigger('click');
     });

     $('div.setup-panel div a.btn-primary').trigger('click');


	$("#btnCadastroEstudante").on('click', function() {
        location.href = 'dadoscadastrais.html';
    });

    $("#btnCadastroProfessor").on('click', function() {
        location.href = 'dadoscadastrais.html';
    });
    
});

function onMenuSelectionado(event) {
	var target = $(event.target);
	target.tab("show");
	var url = target.attr("url");
	if(url) {
		var tabId = target.attr("href");
		location.href = url + tabId;
	}
}


function loadHTML(event) {
	var target = $(event.target);
	var url = target.attr("url");
	if(url) {
		var tabId = target.attr("href");
		armazenarHash(tabId);
		var update = "#" + target.attr("update");
		var param = null;
		
		$(update).empty();
		$.get(url, param, function(response) {
			var titulo = $(response).find("#divInstituicaoTitulo").html();
			var content = $(response).find(update).html();
			$(update).empty().append(content);
			$("#divInstituicaoTitulo").html(titulo);
			target.tab("show");
		}, "html");
	}
}

function setInstituicaoTab() {
	var target = null;
	var hash = getHash();
	
	$("#instituicao li.active").removeClass("active");
	if (hash) {
		armazenarHash(hash);
		target = $("#instituicao a[href='" + hash + "']");
	} else if (window.location.hash) {
		hash = window.location.hash;
		armazenarHash(hash);
		target = $("#instituicao a[href='" + hash + "']");
	} else {
		target = $("#instituicao a.tab-default");
	}
	target.tab("show");
	
	// Marca o menu principal quando um submenu selecionado.
	var rel = target.attr("rel");
	if(rel) {
		$("#instituicao a[for='" + rel + "']").tab("show");
	}
}

function atualizarAba() {
	var local = location.href;
	var refActiveMenu = $("#ulHomeTopMenu.nav.navbar-nav > li.active").not(".inscricao");
	if(local.search("/index.xhtml") != -1) {
	  $("#index").addClass("active");
	} else if(local.search("/legislacao.xhtml") != -1) {
		$("#legislacao").addClass("active");
	} else if(local.search("/cronograma.xhtml") != -1) {
	  $("#cronograma").addClass("active");
	} else if(local.search("/faq.xhtml") != -1) {
	  $("#faq").addClass("active");
	} else if(local.search("/contato.xhtml") != -1) {
	  $("#contato").addClass("active");
	} else if(local.search("/instituicao/") != -1) {
	  $("#instituicao").addClass("active");
	}	
	refActiveMenu.removeClass("active");
}

function scrollToTopAfterAjaxError(){
	if($('#messages > div:visible').length > 0){
		$('body').scrollTop(0);
	}
}