$(function(){

    $('#respond .submit-form').click(function (e) {
        e.preventDefault();

        var form = $(this).closest('form');

        if(validate(form))
            form.trigger('submit');
    });
	
	$('#indique .submit-form').bind('click', function (e) {
		e.preventDefault();
		
		$(this).closest('form').trigger('submit');
	});

	$("#cadastre-se form, #indique form").submit(function(e){
		e.stopImmediatePropagation();
        e.preventDefault();

        var form = $(this);

		if(!validate(form)){
        	return false;
		}
        else
        {
            var params = form.serialize();
            $.ajax({
                url: $.data(document.body, 'ajaxUrl'),
                type: "POST",
                cache: false,
                dataType: "json",
                data: params,
                beforeSend: function () {
                    if(form.attr('id') == 'form-cadastro')
                        form.find('.errormessage').text('Enviando dados...');
                    else
                        form.find('.errormessage').text('Enviando e-mail...');

                },
                success: function(retorno)
                {


                    if(form.attr('id') == 'form-cadastro')
                    {
                        var message = "";

                        if(retorno.exists == 1)
                            message = "Este e-mail já está cadastrado.";
                        else if(retorno.inserted != 1)
                            message = "E-mail não cadastrado, tente novamente.";
                        else
                        {
                            message = "E-mail cadastrado com sucesso.";
                            formReset(form);
                        }

                        form.find('.errormessage').text(message);
                    }
                    else
                    {
                        form.find('.errormessage').text('E-mail enviado com sucesso.');
                        formReset(form);
                    }
                }
            })
        }
	})



	fixMasksPlace();

    //List Archives


	$("#list-archives ul li").first().find('ul').stop().slideDown('fast');

	$("#list-archives li.list-year span").click(function(){
		$("#list-archives ul li ul").stop().slideUp('fast');
		$(this).next().stop().slideToggle('fast');
	})

	/////////////////////////////////////////////







});
