/* 
 * AFX Ajax Shoutbox
 */

//global vars  
var inputUser = $("#sb_nick");  
var inputMessage = $("#sb_message");  
var loading = $("#sb_loading");  
var messageList = $(".sb_list");
var lastShout = $(".lastshout");



//check if all fields are filled  
function checkForm(){  
    if(inputMessage.attr("value"))  
        return true;  
    else  
        return false;
}

function updateShoutbox(){  
    //just for the fade effect  
    messageList.hide();  
    loading.fadeIn();  
    //send the post to shoutbox2.php  
    $.ajax({  
        type: "POST", url: "shoutbox2.php", data: "action=update",  
        complete: function(data){  
            loading.fadeOut();  
            messageList.html(data.responseText);  
            messageList.fadeIn(2000);  
        }  
    });
    
    $.ajax({  
        type: "POST", url: "shoutbox2.php", data: "action=last",  
        complete: function(data){   
            lastShout.html(data.responseText);   
        }  
    });

    t=setTimeout('updateShoutbox()',600000);
}

//load for the first time the shoutbox data 
updateShoutbox(); 

//on submit event  
$("#sb_form").submit(function(){  
    if(checkForm()){  
        var nick = inputUser.attr("value");  
        var message = inputMessage.attr("value");  
        //we deactivate submit button while sending  
        $("#sb_send").attr({disabled:true, value:"Sending..."});  
        $("#sb_send").blur();  
        //send the post to shoutbox2.php  
        $.ajax({  
            type: "POST", url: "shoutbox2.php", data: "sb_nick=" + nick + "&sb_message=" + message + "&action=insert",  
            complete: function(data){  
                messageList.html(data.responseText);  
                updateShoutbox();  
                //reactivate the send button and clear text field
                $("#sb_send").attr({disabled:false, value:"Shout!"});
                $("#sb_message").val("");
            }  
         });  
    }  
    else alert("Please fill all fields!");  
    //we prevent the refresh of the page after submitting the form  
    return false;  
});


    

