﻿//Atribui o bg de acordo com a página e somente qdo acabar de carregar
window.onload = function(){$('body').attr('id','hp')};
/**
* Inicializa as funções assim que os elementos (DOM) são carregados
*/
jQuery(function() {
	HOME._init();
	var contItens = 0;
});

var HOME = {
	/**
	* Função de chamada das outras funções que inicializam o site
	*/
	_init: function() {
		try {
			HOME._filterType();
			HOME._scrollItens();
			HOME._notes();
			HOME._fullText();
			HOME._initScrollItens();
		} catch (e) {
			console.log('Error: ' + e.description);
		}
	},
	_alerta: function(){
	        alert('Chamada da função alerta');
	},		
	/**
	* Exibe a div com os checkbox dos tipos de relatórios
	* @author Alex
	*/
	_filterType: function() {
		var filters = $('div#type');
		$('a.filtro_tipo').unbind().click(function(){
		    $('div.hiddenbox').hide();
		    if(filters.is(':visible')){
		        filters.hide();
		    }else{
		        filters.show();
		    }
		})
		
		$(filters).find('input.bt_filter_ok').unbind().click(function(){
		    filters.hide();
		})		
	},
	/**
	* Inicializa as variaveis d scrollItens
	* @author Alex
	*/
	_initScrollItens: function() {
	    contItens=0;
	    $('ul.news_itens').css('top','0');
	},		
	/**
	* Faz o scroll dos itens da barra lateral
	* @author Alex
	*/
	_scrollItens: function() {
        var topAtual ='';
		var barHeight = $('ul.news_itens').height();
		var itens = $('ul.news_itens li');
		var maskHeight = 475;
		var dif = barHeight - maskHeight;
		var sUp = $('li.scrl_up a');
		var sDown = $('li.scrl_down a');
		var step = 0;
				
		sDown.unbind().click(function(){		    
		    if(step < dif){
		        
		        topAtual = $('ul.news_itens').css('top');
		        if(topAtual){
		            if(topAtual == 'NaN' || topAtual == 'auto'){
		                topAtual = 0;
		            }
                    topAtual = -1*parseInt(topAtual);
                    step=topAtual;
		        }
	            step+=($(itens[contItens]).height()+10);	
	            $('ul.news_itens').animate({"top":-(step)},"slow");

	            contItens++;
	            barHeight-=step;
		    }
		    return false;
		})
		
		sUp.unbind().click(function(){
		    if(contItens > 0){		        
		        contItens--;
		        barHeight+=step; 		        
	            
	            topAtual = $('ul.news_itens').css('top');
		        if(topAtual){
                    topAtual = -1*parseInt(topAtual);
                    step=topAtual;
		        }
		        step-= ($(itens[contItens]).height()+10);
	            $('ul.news_itens').animate({"top":-(step)},"slow");	            	                  
		    }
		    return false;
		})			
	},		
	/**
	* Adiciona a classe Selected ao item clicado
	* @author Alex
	*/
	_selectIten: function(){
	    $('ul.news_itens li div a').click(function(){
	        $('ul.news_itens li').removeClass('selected');
	        $(this).parents('li').addClass('selected');
	        return;
	    })
	},
	/*
	* Notas Rápidas - ScrollLatreral
	* @author Alex
	*/
	_notes:function(){
	    //Variaveis para o Scroll
	    var noteWidth = $('div.box_notas div.mask ul li').width();
	    var itens = $('div.box_notas div.mask ul li').size();
	    var notesWidth = noteWidth * itens;
		var maskwidth = 277;
		var dif = notesWidth - maskwidth;
		var sEsq = $('li.esq a');
		var sDir = $('li.dir a');
		var cont = 0;
		var step = 0;
		
		//Setando comprimento do container de notas
		$('div.box_notas div.mask ul').width(noteWidth * itens);
		
		//Mostrando os botões para rolagem
		if(itens > 1)$('#notas ul li.dir a').css('visibility','visible');
				
		sDir.unbind().click(function(){
		    if(step < dif){
	            step+= 277;
	            $('div.box_notas div.mask ul').animate({"left":-(step)},"slow");
	            cont++;
	            notesWidth-=step;
	            if(cont > 0 )$('#notas ul li.esq a').css('visibility','visible');	            
	            if(cont == (itens-1))$(this).css('visibility','hidden');	
		    }
		    return false;
		})
		
		sEsq.unbind().click(function(){
		    if(cont > 0){
		        cont--;
		        notesWidth+=step; 		        
	            step-= 277;
	            $('div.box_notas div.mask ul').animate({"left":-(step)},"slow");
	            if(cont == 0 || cont < 0)$('#notas ul li.esq a').css('visibility','hidden');	  
	            if(cont < itens)$('#notas ul li.dir a').css('visibility','visible');	          	                  
		    }
		    return false;
		})	
	},
	/*
	* Rolagem da matéria principal
	* @author Alex
	*/
	_fullText:function(){
	    //Variaveis para o Scroll
	    var textHeight = $('#full_text div.mask div.full_content').height();
		var maskHeight = 289;
		var dif = textHeight - maskHeight;
		var sDown = $('div.scroll a.down');
		var sUp = $('div.scroll a.up');
		var step = 0;
		
		//Mostrando os botões para rolagem
		if(textHeight > maskHeight)$(sDown).css('visibility','visible');
		
		//Subir	
		sUp.unbind().click(function(){
		    if(step > 0){
	            step-=100;
	            $('#full_text div.mask div.full_content').animate({"top":-(step)},"slow");
	            if(step == 0)$(sUp).css('visibility','hidden');	  
	            if(step < maskHeight)$(sDown).css('visibility','visible');	
		    }
		    return false;
		})		
		
		//Descer	
		sDown.unbind().click(function(){
		    if(step < dif){	             
	            step+=100;
	            $('#full_text div.mask div.full_content').animate({"top":-(step)},"slow");
	            if(step > 0)$(sUp).css('visibility','visible');	  
	            if(step >= dif)$(sDown).css('visibility','hidden');	          	                  
		    }
		    return false;
		})	
	}
};