/**
* @package jQuery Chat
*/

if(jQueryNoConflict == undefined){
    jQuery.noConflict();
    var jQueryNoConflict = true;
}

var instanse = false;
var state;
var nickname;
var scrollLock = false;

function Chat(){
    this.update = updateChat;
    this.send = sendChat;
    this.getState = getStateOfChat;
    this.getHistory = getChatHistory;
}

//gets the state of the chat
function getStateOfChat(){
    if (!instanse) {
        instanse = true;
        jQuery.ajax({
            type: "POST",
            url: "/jQchat/process.php",
            data: {
                'function': 'getState',
                'file' : 'blockchat.txt'
            },
            dataType: "json",

            success: function(data){
                state = data.state;
                instanse = false;
            }
        });
    }
}

//populate the block with our chat history
function getChatHistory(){
    jQuery.ajax({
        type: "POST",
        url: "/jQchat/process.php",
        data: {
            'function': 'getHistory',
            'file' : 'blockchat.txt'
        },
        dataType: "json",
        success: function(data){
            if (data.text) {
                for (var i = 0; i < data.text.length; i++) {
                    jQuery('#chat-area').append(jQuery("<p>" + data.text[i] + "</p>"));
                    if (!scrollLock) {
                        if (document.getElementById('chat-area') != null) {
                            document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
                        }
                    }

                }
            }
            instanse = false;
            state = data.state;
        }
    });
    setInterval('chat.update()', 1500);
}

//Updates the chat
function updateChat(){
    if (!instanse) {
        instanse = true;
        jQuery.ajax({
            type: "POST",
            url: "/jQchat/process.php",
            data: {
                'function': 'update',
                'file' : 'blockchat.txt',
                'state': state,
                'nickname': nickname
            },
            dataType: "json",
            success: function(data){
                if (data.text) {
                    for (var i = 0; i < data.text.length; i++) {
                        jQuery('#chat-area').append(jQuery("<p>" + data.text[i] + "</p>"));
                        if (!scrollLock) {
                            if (jQuery('#chat-area') != null) {
                                document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
                            }
                        }
                    }
                }
                instanse = false;
                state = data.state;
            }
        });
    }
    else {
        setTimeout(updateChat, 1500);
    }
}

//send the message
function sendChat(message){
    updateChat();
    jQuery.ajax({
        type: "POST",
        url: "/jQchat/process.php",
        data: {
            'function': 'send',
            'file' : 'blockchat.txt',
            'message': message,
            'nickname': nickname
        },
        dataType: "json",
        success: function(data){
            updateChat();
        }
    });
}


