jQuery(function($) {
    $('.em-but').click(function() {
        var form = $(this).closest('form');
        var input = form.serialize();
        
        form.find('input, button').attr('disabled', 'disabled');
        form.find('img').show();
        
        $.post('_em.php', input, function(data) {
            if (data == 'true') {
                form.find('input, button, p').hide();
                form.append('<div><p>Success, thanks!</p></div>');
                
                var msg_delay = window.setTimeout(function() {
                    form.find('input, button, p').removeAttr('disabled').show();
                    form[0].reset();
                    form.find('div').remove();
                    form.find('img').hide();
                    window.clearTimeout(msg_delay);
                }, 1500);
            } else {
                form.find('input, button, p').hide();
                form.append('<div><p>Sorry, invalid!</p></div>');
                
                var msg_delay = window.setTimeout(function() {
                    form.find('input, button, p').removeAttr('disabled').show();
                    form.find('div').remove();
                    form.find('img').hide();
                    window.clearTimeout(msg_delay);
                }, 1500);
            }
        });
    });
    
    var is_over = false;
    $($('ul.rotate li').hide().get(0)).show();
    
    $('ul.rotate li').click(function() {
        var next = $(this).next('li');
        if (next[0] == undefined) next = $(this).parent('ul').children()[0];
        $(next).fadeIn();
        $(this).fadeOut();
    }).mouseover(function() {
        is_over = true;
    }).mouseout(function() {
        is_over = false;
    });
    
    var rotate = setInterval(function() {
        if (!is_over) $('ul.rotate li:visible').click();
    }, 4000);
});
