// JavaScript Document

var errorFields = []

$(function(){

	if ($(".pergunta").length >= 1) {
		$(".faq").click(function(){
			if ($(this).find(".resposta").css("display") == "block") {
				$(this).find(".resposta").hide("fast")
			} else {
				$(this).find(".resposta").show("fast")
			}
			return false;
		})
	}
	
	if ($('#link-destaques .cotacao').length == 1) {
		date = new Date();
		$.get('/index.php/remote/cotacao/' + date.getTime(), function(resp){
			$('.cotacao').html(resp)
		})
	}

    /*---------------------
        botoes pagina contato
    -----------------------*/
    if ($('#formContato').length >= 1) {
        $('.botao-enviar').click(function(){
            $('#formContato').submit()
        })

        $('.botao-cadastrar').click(function(){
            $(this).parent().parent().parent().submit()
        })
    }

    /*---------------------------
        form solicite resgate
    ----------------------------*/
    if ($('#formResgate').length >= 1) {

    	//if ($.browser.msie) {
    		$("input:radio").addClass("radio-button")
    		$("label").addClass("resgate")
    	//}

        $('#total').click(function(){
            disabled(true)
        })
        $('#parcial').click(function(){
            disabled(false)
            Mask.financier(document.getElementById('valor'), true)
        })
        $('#cotas').click(function(){
            disabled(false)
        })
        $('#valor').keyup(function(){
            if ($('#parcial').attr('checked'))
			    Mask.financier(document.getElementById('valor'), true)
        })
        
	    $('#motivo').change(function(){
	        if ($(this).val()=='Outros')
	            $('#especificar').show()
            else
                $('#especificar').hide()
        })

        $('#tipo_pf').click(function(){
            exibe('pf')
        })
        
        $('#tipo_pj').click(function(){
            exibe('pj')
        })
        
        $('.botao-enviar').click(function(){
            $('#formResgate').submit()
        })

        $('#solicitacao_r').click(function(){
            solicitacao('resgate')
        })

        $('#solicitacao_t').click(function(){
            solicitacao('transferencia')
        })

        function exibe(t){
            if (t=='pf'){
                $('#li_pf').show()
                $('#li_pj').hide()
            } else {
                $('#li_pj').show()
                $('#li_pf').hide()
            }
        }

        function solicitacao(t){
            if (t=='resgate'){
                $('#ul_fundo').show()
                $('#ul_transferencia').hide()
                $('#ul_banco').show()
                $('#li_motivo').show()
            } else {
                $('#ul_fundo').hide()
                $('#ul_transferencia').show()
                $('#ul_banco').hide()
                $('#li_motivo').hide()
            }
        }

        function disabled(state){
		    $('#valor').attr('disabled', state)
	    }
    
	    /*-- funcoes ao entrar na tela --*/
	    if ($('#tipo_pf').attr('checked'))
	        exibe('pf')
        else
            exibe('pj')

        if ($('#solicitacao_r').attr('checked'))
	        solicitacao('resgate')
        else
            solicitacao('transferencia')

	    if ($('#motivo').val()=='Outros')
	        $('#especificar').show()
        
        if ($('#total').attr('checked'))
            disabled(true)
        else
            disabled(false)
        
    }
    
    /*-------------
        Confirmacao de dados do resgate
    -------------*/
    if ($('#formInfo').length >= 1) {
        $('.botao-enviar').click(function(){
            $('#formInfo').submit()
        })
    }
    
    
    
    /*------------------
        Histórico de Cotas
    ---------------------*/   
    if ($('#formHistoricoCotas').length >= 1) {
    
        $("#loading").ajaxStart(function(){
           $(this).show();
         });  
        $("#loading").ajaxStop(function(){
           $(this).hide();
         });

        $('#formHistoricoCotas').submit(function()
        {  
          $.ajax({
            type: "POST",
            dataType: "html",
            url: "/index.php/sparta/performanceBusca/"+id,
            data: $(this).serialize(),
            success: function(response){
              $('#resultado').html(response);
            }
          });
          
          return false;
        });

        $('#dt_inicial').focus(function(){
            $(this).mask('99/99/9999')
            $(this).css('background-color', '#eee')
        })
        $('#dt_inicial').blur(function(){
            $(this).css('background-color', '#fff')
        })
        $('#dt_final').focus(function(){
            $(this).mask('99/99/9999')
            $(this).css('background-color', '#eee')
        })
        $('#dt_final').blur(function(){
            $(this).css('background-color', '#fff')
        })
    }

    /*-----
     MASCARAS NOS CADASTROS
     ----*/
    if ($('.data input[name^="data_"]').length > 0) $('.data input[name^="data_"]').mask('99/99/9999')
    if ($('.telefone-ddd').length > 0) $('.telefone-ddd').mask('99')
    if ($('input.telefone').length > 0) $('input.telefone').mask('99999999')
    
	if ($('input[name^="cpf"]').length > 0 && $('input[name^="cpf_acionario"]').length == 0) $('input[name^="cpf"]').mask('999.999.999-99')
	if ($('input[name^="cnpj"]').length > 0) $('input[name^="cnpj"]').mask('99.999.999/9999-99')
	
	if ($('input[name^="cep"]').length > 0) $('input[name^="cep"]').mask('99999999')
	
	// habilitar especificar patrimonio
	if ($('#patrimonio_estimado').length > 0) {
	    $('#patrimonio_estimado').change(function(){
	        $('#especificar_patrimonio_span').hide()
	        $(this).find('option').each(function(i, item){
	            if (item.value == $(this).parent().val() && (i == 1 || i == ($(this).parent().find('option').length-1))) {
	                $('#especificar_patrimonio_span').show()
	            }
	        })
	    })
	    
	    $('#patrimonio_estimado').change()
	}

    // habilitar especificar prolabore
	if ($('#salario_prolabore').length > 0) {
	    $('#salario_prolabore').change(function(){
	        $('#especificar_prolabore_span').hide()
	        $(this).find('option').each(function(i, item){
	            if (item.value == $(this).parent().val() && (i == 1 || i == ($(this).parent().find('option').length-1))) {
	                $('#especificar_prolabore_span').show()
	            }
	        })
	    })
	    
	    $('#salario_prolabore').change()
	}

    // busca CEP \\ 
    if ($('input[id^="cep"]').length > 0) {
        $('input[id^="cep"]').blur(function(){
            if ($(this).val()!='') {
                $(this).parent().find('.loading').show()

                this.value = this.value.replace(/-/g, '');
                data = new Date()
                
                suffix = this.id.substr(3)
                
                $.get(cepURL + this.value + '/' + data.getTime(), function(data){
                    $('.loading').hide()
                    resp = eval(data)
                    if (resp[0].resultado == 1) {
                        $('#estado' + suffix).val(resp[0].estado)
                        $('#cidade' + suffix).val(resp[0].cidade)
                        $('#bairro' + suffix).val(resp[0].bairro)
                        if ($('#logradouro' + suffix).length > 0)
                            $('#logradouro' + suffix).val(resp[0].logradouro)
                        else
                            $('#endereco' + suffix).val(resp[0].logradouro)
                        $('#pais' + suffix).val("Brasil")
                    }
                })
            }
        })
    }

    // validacao dos formularios
    if (errorFields.length > 0) {
        for (i=0; i<errorFields.length; i++)
            $('#'+errorFields[i]).addClass('field-error')
        
        $('.field-error').blur(function(){
            if ($.trim($(this).val()) != '')
                $(this).removeClass('field-error')
        })
    }

    

    // borda arredondada
    /*if ($('.box-formulario').length > 0) {
        $('.box-formulario').corner("round 15px")
		$('.borda-formulario').corner("round 15px")
        if ($('.box-consulta').length == 0) {
            $('.box-formulario').css('height', '500px')
            $('.box-formulario').css('overflow-y', 'scroll')
            $('.box-formulario').css('overflow-x', 'hidden')
        }
    }*/

    /*-----------------------
        Altura dos itens das caracteristicas
    -----------------------*/
    if ($('.caract-caixa').length > 0) {
        $('.caract-caixa').each(function(){
            if ($(this).find('.caract-dir').height() < $(this).find('.caract-esq').height())
                $(this).find('.caract-dir').css('height', $(this).find('.caract-esq').height() + 'px' )
        })
    }
})
