$(document).ready(function() {
    $('.fade').cycle({
        fx: 'fade',
        timeout: 8000,
        pager: '#pagg',
        speed: 1000
    });

    $("a[rel^='prettyPhoto']").prettyPhoto();
    $("#nome").Watermark("Nome");
    $("#email").Watermark("E-mail");

    $.validator.methods.equal = function(value, element, param) {
        return value != param;
    };

    function response(data) {
        $("#frm").resetForm();
        $("#nome").Watermark("Nome");
        $("#email").Watermark("E-mail");
        $('#error').html(data).fadeIn('slow').animate({ opacity: '+=0' }, 3000).fadeOut('slow');
        window.setTimeout("$('#error').html('')", 4000);
    }

    if (location.href.indexOf("#newsletter") != -1) {
        $('#nome').focus();
    }

    $("#frm").validate({
        errorLabelContainer: $("#error"),
        wrapper: 'p',
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                cache: false,
                url: "../default/exec.asp",
                data: InvariantEncoding("frm"),
                beforeSend: function() { $("#error").html("<img src='../imagens/ajax-loader.gif' alt='' /> Aguarde...").fadeIn("slow") },
                success: response
            });
        },

        rules: {
            nome: {
                required: true,
                equal: 'Nome'
            },
            email: {
                required: true,
                email: true,
                equal: 'E-mail'
            }
        },

        messages: {
            nome: {
                required: 'Preencha o nome.',
                equal: 'Preencha o nome.'
            },
            email: {
                required: 'Preencha o e-mail',
                equal: 'Preencha o e-mail',
                email: 'O endere&ccedil;o de e-mail n&atilde;o &eacute; v&aacute;lido.'
            }
        }

    });


    function InvariantEncoding(idForm) {
        var elements = decodeURIComponent($("#" + idForm).serialize());
        var valores = elements.split("&");
        var newArray = [];
        for (i = 0; i < valores.length; i++) {
            conteudo = valores[i];
            nome = conteudo.split("=")[0]
            valor = conteudo.split("=")[1]
            newArray.push(nome + "=" + escape(valor))
        }
        return newArray.join('&').toString();
    }


});



