/* Guarda IDs dos elementos */

var abas = '.nav-tabs';
var conteudos = '.abas-conteudo';

//Define the variables
var easing, e, pos;

	
jQuery(document).ready(function(){
	
	// menu
	jQuery(".info-nome span").click(function(){
		if(jQuery(this).parent().hasClass('ativo')){
			jQuery(this).parent().removeClass('ativo');
		} else {
			jQuery(this).parent().addClass('ativo');
		}
		return false;
	});
		
	//toogle
	jQuery(".visualizacoes a").click(function(){
		var painel = jQuery("."+jQuery(this).attr('rel'));
		if(painel.is(":visible")){
			painel.hide();
			jQuery(this).removeClass("ativo");
			painel.parent().children(":visible").width(940);
		}
		else {
			painel.parent().children(":visible").width('');
			painel.show();
			jQuery(this).addClass("ativo");
		}
		return false;
	});
	
	// cortina
	jQuery(".cortina ul.cortina-menu li.btn").click(function(){
		var menuCortina = jQuery(".cortina-menu");
		if(menuCortina.hasClass("escondido")){
			menuCortina.children().each(function(){
				jQuery(this).height("");
			});
			menuCortina.removeClass("escondido");
			menuCortina.parent().children().removeClass("escondido")
			jQuery(this).attr("title", "Clique para esconder");
		}else {
			menuCortina.children().each(function(){
				jQuery(this).height(jQuery(this).height()+1);
			});
			menuCortina.parent().children().addClass("escondido");
			jQuery(this).attr("title", "Clique para expandir");
		}
		return false;
	});


	//abas
	var abaAtual = '1';
	
	/* Oculta todas as abas */
	
//	jQuery(conteudos + ' div.aba').hide();
//	
//	/* Exibe a primeira aba */
//	
//	jQuery(conteudos + ' div.aba:first-child').show();
//	jQuery(abas + ' :first-child').addClass('ativo');
	
	/* Quando uma aba for clicada */
	
	jQuery(abas + ' li').click(function()
	{
	
		/* Remove todas as classes de todas as abas */
	
		jQuery(abas + ' li').removeClass('ativo');
		
		/* Acrescenta uma classe CSS marcando visualmente a aba como selecionada */
		
		jQuery(this).addClass('ativo');
		
		/* Oculta todas as abas abertas */
		
		jQuery(conteudos + ' div.aba').hide();
		
		/* Exibe a aba que foi clicada */
		
		jQuery(conteudos +  ' ' + jQuery(this).find('a').attr('href')).show();
		
		/* Fim :D */
		
		return false;
		
	});
	
	//toogle
	jQuery.each(jQuery("div.titulo_azul"), function(i,obj){
		jQuery(obj).attr("onclick",jQuery(obj).find('a').attr("onclick"));
	});
		
	jQuery.each(jQuery("div.titulo_cinza"), function(i,obj){
		jQuery(obj).attr("onclick",jQuery(obj).find('a').attr("onclick"));
	});
	
	jQuery('input[type="text"]').setMask();
	
	//jQuery('.rf-cal-inp').setMask("date");
	
//	jQuery.each(jQuery(".rf-cal-inp"), function(i,obj){
//		jQuery(obj).attr("onkeypress","return formatarMascara(this,event,'##/##/####');");
//	});
	
	
	
	
	jQuery( "#dialog_sobre" ).dialog({
		width: 400,
    	modal: true,
        autoOpen: false,
        show: "fade",
        hide: "fade"
    });

	jQuery( "#sobre" ).click(function() {
		jQuery( "#dialog_sobre" ).dialog( "open" );
        return false;
    });
    
	montarTabelaOrdenacao();
	
	
	jQuery("ul.modulos li.inativo a.coleta").each(function(i,a) { 
		jQuery(a).attr("onclick","return false;");
    });


	// Topo - Get the click event
	jQuery("a.ir_topo").click(function() {

		// Get the class
		easing = jQuery(this).attr("class");

		// Get the scroll pos
		pos = jQuery(window).scrollTop();

		// Set the body top margin
		jQuery("body").css({
			"margin-top": -pos+"px",
			"overflow-y": "scroll", // This property is posed for fix the blink of the window width change 
		});

		// Make the scroll handle on the position 0
		jQuery(window).scrollTop(0);

		// Get the easing
		e = "cubic-bezier(0.600, 0.040, 0.980, 0.335)"; //efeito 1

		// Add the transition property to the body element
		jQuery("body").css("transition", "all 1s "+e);

		// Apply the scroll effects
		jQuery("body").css("margin-top", "0");

		// Wait until the transition end
		jQuery("body").on("webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd", function(){
			// Remove the transition property
			jQuery("body").css("transition", "none");
		});
	});
	
});

function configurarAbas(form){
	jQuery(form).each(function(i,f){
		jQuery(f).append("<input type='hidden' name='valorAba' id='valorAba' value='' />");
		jQuery("a",f).each(function(i,link) { 
			jQuery(link).attr("onclick","setAba(this,"+jQuery(link).closest('div.aba').attr('id')+");"+jQuery(link).attr("onclick"));
	    });
	});
	
	
}

function setAba(link,aba){
	form = jQuery(link).closest('form');
	hidden = jQuery('#valorAba',form);
	jQuery(hidden).val(jQuery(aba).attr('id'));
}

function exibirAba(aba){
	idAba = "#"+jQuery(aba).attr('id');
	li = jQuery("a[href='"+idAba+"']").closest("li").click();
	
	jQuery(abas + ' li').removeClass('ativo');
	/* Acrescenta uma classe CSS marcando visualmente a aba como selecionada */
	jQuery(li).addClass('ativo');
	/* Oculta todas as abas abertas */
	jQuery(conteudos + ' div.aba').hide();
	/* Exibe a aba que foi clicada */
	jQuery(conteudos +  ' ' + idAba).show();
	
}

function exibirMenu(menu){
	jQuery(menu).toggleClass('on');
}

function montarTabelaOrdenacao(){
	jQuery.each(jQuery("table.tablesorter"), function(j,table){
		headers = {};
		
		jQuery("thead th",table).each(function(j,linha) { 
            if(jQuery(linha).hasClass("noSorter")){
            	headers[j] = {
            		    sorter:false
            	};
            }
        });
		jQuery(table).tablesorter({
			widgets: ['zebra'],
			dateFormat: "pt",
			headers:headers
		});
	});
	
}

function selecionarOrdenacao(link){
	
	coluna = jQuery('.headerSortUp').index();
	orderby = 1;
	
	if(coluna == -1){
		coluna = jQuery('.headerSortDown').index();
		orderby = 0;
	}

	if(coluna != -1){
		var form = jQuery(link).parents('form:first');
		jQuery('<input>').attr({
		    type: 'hidden',
		    id: 'orderby',
		    name: 'orderby',
			value:coluna
		}).appendTo(form);

		jQuery('<input>').attr({
		    type: 'hidden',
		    id: 'colunasort',
		    name: 'colunasort',
			value:orderby
		}).appendTo(form);
	
	}	
	
}



jQuery.noConflict();