/******************************/
/**   SHOUTBOX for jQuery    **/
/**                          **/
/** Author: Tomasz Nabrzeski **/
/******************************/
var Shoutbox = {
    timer: null,

    init: function() {
        if ($('#shoutbox-list').length) {
            Shoutbox.update();

            $('#shoutbox-list').css('overflow-x:', 'auto');
            $('#shoutbox form').submit(Shoutbox.post);
        }
    },

    update: function() {
        clearTimeout(Shoutbox.timer);
        
        $.get(baseUrl+'shoutbox/view/', function(response) {
            $('#shoutbox-list').html(response);
        });

        Shoutbox.timer = setTimeout('Shoutbox.update()', 10000);
    },

    post: function() {
        $('#shoutbox input[type=submit]').attr('disabled', 'disabled').blur();
        
        $.ajax({
            type: "post",
            url: baseUrl+"shoutbox/post/",
            data: $(this).serialize(),
            cache: false,
            success: function(response){
                if (response=='ok'){
                    $('#shoutbox input[name=message]').val('');
                    $('#shoutbox-list').attr('scrollTop', 0);
                    Shoutbox.update();
                } else {
                    alert(response);
                }
            },
            complete: function() {
                $('#shoutbox input[type=submit]').attr('disabled', '');
            }
        });

        return false;
    }
}

$(document).ready(function() {
    Shoutbox.init();
});
