$().ready(function() {
    // Formulário para contato (fale conosco)
    var contact = $('#contact'),
        contactForm = contact.find('form'),
        contactAddress = contact.find('div > div'),
        contactLink = $('#contact-link'),
        contactInputs = contactForm.find(':input'),
        contactFocus = false,
        header = $('header');
    contactInputs.focus(function(){contactFocus=true}).blur(function(){contactFocus=false});
    contactLink.toggle(
        function() {
            contactForm.show();
            contact.animate({ height: '400px' }, 300);
            header.animate({ paddingTop: '400px' }, 300);
            return false;
        },
        function(){
            contact.animate({ height: '30px' }, 300, function(){contactForm.hide()});
            header.animate({ paddingTop: '30px' }, 300);
            return false;
        }
    );
    contactForm.find('#cancel').click(function() {
        contactLink.click();
        contactInputs.removeClass('error');
    });
    contactForm.find('#submit').click(function() {
        var incomplete = false,
            name = contactForm.find('#name'),
            email = contactForm.find('#email'),
            message = contactForm.find('#message');
        contactInputs.removeClass('error');

        if (!name.val()) {
            name.addClass('error');
            incomplete = true;
        }
        if (!email.val()) {
            email.addClass('error');
            incomplete = true;
        }
        if (!message.val()) {
            message.addClass('error');
            incomplete = true;
        }

        if (!incomplete) {
            var notification = $('<div id="notification"></div>').appendTo(contact.find('.container'));
            var notify = function(feedback) {
                notification.html(feedback)
                            .fadeOut(7000, function() {
                                contactLink.show();
                                notification.remove();
                            });
            };
            var error = 'Houve algum problema e não foi possível enviar a mensagem. Por favor, mande o texto para o e-mail contato@numera.com.br.',
                success = 'Messagem enviada para contato@numera.com.br. Obrigado pela sua participação.';
            $.ajax({
                type: 'POST',
                url: contactForm.attr('action'),
                dataType: 'json',
                data: {
                    name: name.val(),
                    email: email.val(),
                    message: message.val()
                },
                beforeSend: function() {
                    contactForm.find('#cancel').click();
                    contactLink.hide();
                    notification.html('<span>Enviando...</span>');
                },
                error: function() { notify(error) },
                success: function(data) {
                    if (data && data.response) { notify(success) }
                    else { notify(error) }
                }
            });
        }

        return false;
    });

    // Menu principal
    var menu = new Array();
    $("nav a").each(function(i){
        menu[i] = $(this);
        i++;
        $(this).prepend('<span>' + i + '</span>');
    }).hover(
        function () {
            $(this).find('span').animate({bottom: 0, right: 0}, 200);
        },
        function () {
            $(this).find('span').animate({bottom: '-64', right: '-64'}, 200);
        }
    ).focus(function() {
        $(this).find('span').animate({bottom: 0, right: 0}, 200);
    }).blur(function() {
        $(this).find('span').animate({bottom: '-64', right: '-64'}, 200);
    });


    // Navegação pelo teclado
    $(document).keypress(function(e) {
        if (!contactFocus) {
            if (e.which > 48 && e.which < 58 && menu.length > e.which - 49) {
                window.location.href = menu[(e.which - 49)].attr('href');
            } else if (menu.length == e.which - 49) {
                $('#contact-link').click();
            }
        }
    });

    // Localização
    $('#address-link').toggle(function(){
        $('footer address').show('slow');
        return false;
    }, function() {
        $('footer address').hide('fast');
        return false;
    });
    $('<span><a href="#"></a></span>').appendTo('footer address').click(function(){
        $('#address-link').click();
        return false;
    });
});
