/**
* Inicializa as funções assim que os elementos (DOM) são carregados
*/
jQuery(function() {
	MAIN._init();
});

var MAIN = {
	/**
	* Função de chamada das outras funções que inicializam o site
	*/
	_init: function() {
		try {
			MAIN._navigation();
			MAIN._searchReplaceBG();
			MAIN._enterEvent();
		} catch (e) {
			console.log('Error: ' + e.description);
		}
	},		
	/**
	* Adiciona o background cinza translucido nos itens do menu
	* @author Alex
	*/
	_navigation: function() {
		$('#nav li a').each(function(){
		    var parent = $(this).parent();
		    if(!parent.hasClass('selected')){
		        $(this).hover(
		            function () {
                        parent.addClass('selected');
                    }, 
                    function () {
                        parent.removeClass("selected");
                    }
                )
            }
		})
	},		
	/**
	* Ocultar Texto no campo de busca
	* @author Alex
	*/
	_searchReplaceBG: function() {
	    var busca = $('#pesquisar ul li.txt_busca input');
	    var termo = '';
		busca.focus(function(){		    
		    $(this).css('backgroundImage','none');
		})
		busca.blur(function(){
		    termo = $(this).val().length;
		    if(termo == 0){
		        $(this).css('backgroundImage','url(../img/tit/digite.png)')
		    }
		})
	},
	/**
	*
	*
	*/
	_enterEvent: function(){
	    var busca = $('#pesquisar ul li.txt_busca input');
	    var btOk = $('li.bt_ok input');
	    var termo = '';
	    var active = false;
	    
	    function Valida(){
            if(busca.val().length < 1){
                alert('O termo pesquisado deve ser maior que 1 caractere !');
                return false; 
            }else{
                location.href = "../busca/resultados.aspx?k=" + busca.val();
            }
	    }
	    
	    $(busca).focus(function(){
	        $(this).css('backgroundImage','none');
	        active = true;	        
	        $(busca).unbind().keypress(function (e) {
                if (e.which == 13 && active == true) {
                    Valida();
                    return false;       
                }
            })
        })
        
        $(busca).blur(function(){
            active = false;
        })
        
        btOk.click(function(){Valida();})
	}
};