// FORMULARAIO VOLUNTÁRIO
function openFormulario(){
	if( parseInt($("#voluntario").css("margin-top")) < 0 ){
		$("#voluntario").animate({
			marginTop: "0px"
			}, 1000 , "easeInOutCubic"
		);
	}else{
		$("#voluntario").animate({
			marginTop: "-160px"
			}, 1500
		);
	}
}

$(document).ready(function(){					   
	
	$('button, .bt').button();
	$('#voluntario').css('visibility', 'visible');
    
    // AGUARDE O AJAX //
    function blockThis(){
        $.blockUI({
            message: ('<img style="float:left" src="img/load.gif" title="carregando"><div style="float:left; position:relative; top:1px; left:5px">Carregando!</div>'),
            showOverlay: false, 
            centerY: false, 
            css:{
              border:'none', 
              padding:'15px', 
              top:'10px', 
              left:'10px', 
              right:'0',
              backgroundColor:'#000', 
              'border-radius':'10px', 
              '-webkit-border-radius':'10px', 
              '-moz-border-radius':'10px', 
              opacity:.5, 
              color:'#fff',
              width:'100px'
            } 
        })
    };
    
    $(document)
      .ajaxStart(blockThis)
      .ajaxStop($.unblockUI);
	
	// FORMULARAIO VOLUNTÁRIO //
	$('.openTopForm').click(function(){
		$('html, body').animate({
			scrollTop:0
		}, 
		1500, "easeInOutCubic",
		function(){
			$("#voluntario").animate({
				marginTop: "0px"
				}, 1000 , "easeInOutCubic"
			);
		});
	});  

	
	$('.bt_inscreverSe').click(function(){
        $.ajax({
            url: 'php/cadastro_voluntario.php',
            type: "POST",
            data: $("#form_formulario_voluntario").serialize(),
            dataType: "json",
            success: 
                function(data) {
                    if( data['error'] ){
                        $(".dialog_voluntario_msg p").html("Ops! você esqueceu de preencher alguns campos obrigatórios ou algum campo foi preenchido com valores inválidos, verifique a baixo mais detalhes:<br><br><div style='color:red'>"+data['msg']+"</div>");
                        $(".dialog_voluntario_msg").dialog('open');
                    }else{                    
                        if( data['email'] && data['db'] ){
                            $(".dialog_voluntario_msg p").html("Seu cadastro foi efetuado com sucesso em nossa base de dados, assim que surgir uma oportunidade entraremos em contato.<br><br> Obrigado pelo apoio!");
                            $(".dialog_voluntario_msg").dialog('open');
							openFormulario();
							$('#formulario_voluntario input, #formulario_voluntario textarea').val('');							
                        }else{
                            $(".dialog_voluntario_msg p").html("Ocorreu um erro ao efetuar seu cadastro em nossa base de dados, caso este erro já tenha ocorrido outras vezes, favor entrar em contato com o Instituto Morena Rosa pelo fone: (44) 3351-5000.");
                            $(".dialog_voluntario_msg").dialog('open');
                        }
                    }
                }
        });            
    });
	    
	$("#voluntario_bt").click(function (){
		openFormulario();
	});
	
	telefone = "(##) ####-####";	
	$('#formulario_voluntario input[name=telefone]').mask( {"mask":telefone} );
	$('#formulario_voluntario input[name=celular]').mask( {"mask":telefone} );
	
	$('.ck_entidade').attr('checked', false);
	$('.ck_entidade').click(function(){
		id = $(this).val();
		$(".check_inst_"+id).attr('checked', !$(".check_inst_"+id).attr('checked') );
	});	
	
	$('.dialog').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});

	$('select[name=entidade_bt]').focus(
		function(){
			$('.dialog_entidade').dialog('open');
		}
	);
	
	// TELEVISÃO //
	$("#televisao").agile_carousel({
		first_last_buttons: "no",
		hover_next_prev_buttons: "yes",
		next_prev_buttons: "no",
		pause_button: "no",
		slide_buttons: "yes",
		slide_captions: "Cuidado com o Meio Ambiente|Educação de Qualidade|Unidade Móvel Médico Odontológica", 
		slide_directory: "img/banner",
		slide_links: "programa.php?4,programa.php?2,programa.php?1",
		slide_number_display: "no",
		timer_length: "7000",
		transition_duration: 1000,
		transition_easing: "easeOutExpo",	
		transition_type: "carousel",
		water_mark: "no"
	});
	
	// MENU //
	$("#menuVoluntariado").click(function(){
        openFormulario();
    });	
});

