var rsslounge = {
    
    /**
     * initialize all events
     */
    init: function() {
        jQuery(document).ready(function() {
            // initialize carousel
            $("#carousel").simplecarousel({
                next: $('.next'),
                prev: $('.prev'),
                slidespeed: 700,
                auto: 5000,
                width: 480,
                height: 280
            });
            
            // send button
            $('#contact input[type="submit"]').click(rsslounge.send);
            
            $('#badge').mouseenter(function() {
                $('#badgeinfo').fadeIn();
            });
            
            $('#badge').mouseleave(function() {
                $('#badgeinfo').fadeOut();
            });
        });
    },
    
        
    /**
     * send new mail
     */
    send: function() {
        // hide form
        $('#contact ul').hide();
        
        // show loading
        $('#loading').show();
        
        $.ajax({
            type: "POST",
            url: "index/send",
            data: { 
                'name': $('#name').val(),
                'email': $('#email').val(),
                'message': $('#message').val()
            },
            dataType: 'json',
            success: function(response){
                    // error
                    if(typeof response.errors != 'undefined') {
                        // show errors
                        rsslounge.showErrors($('#contact'), response.errors);
                        
                        // show form
                        $('#contact ul').show();
                    }
                    
                    // success: show ok
                    else {
                        $('#success').show();
                    }
                    
                    // hide loading
                    $('#loading').hide();
                }
            });
    },
    
    
    /**
     * insert error messages in form
     * @param form target where input fields in
     * @param errors an array with all error messages
     */
    showErrors: function(form, errors) {
        $('span.error').remove();
        $.each(errors, function(key, val) {
            form.find('#'+key).parent('li').append('<span class="error">'+val+'</span>');
        });
    }
}