/**
* @package jQuery Chat
*/

var instanse = false;
var state;
var nickname;
var exitTimer;
var scrollLock = false;

function Chat(){
    this.update = updateChat;
    this.send = sendChat;
    this.getState = getStateOfChat;
    this.addUser = _addUser;
    this.removeUser = _removeUser;
    this.bumpUser = _bumpUser;
}

//gets the state of the chat
function getStateOfChat(){
    if (!instanse) {
        instanse = true;
        jQuery.ajax({
            type: "POST",
            url: GeeklogConfig.site_url+"/jQchat/process.php?callback=?",
            data: {
                'function': 'getState'
            },
            dataType: "jsonp",

            success: function(data){
                state = data.state;
                if (data.users) {
                    jQuery('#user-area').html(jQuery("<p>" + data.users + "</p>"));
                }
                instanse = false;
            }
        });
    }
}

//Updates the chat
function updateChat(){
    if (!instanse) {
        instanse = true;
        jQuery.ajax({
            type: "POST",
            url: GeeklogConfig.site_url+"/jQchat/process.php?callback=?",
            data: {
                'function': 'update',
                'state': state,
                'nickname': nickname
            },
            dataType: "jsonp",
            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) {
                            document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
                        }
                    }
                }
                if (data.users) {
                    jQuery('#user-area').html(jQuery("<p>" + data.users + "</p>"));
                }
                instanse = false;
                state = data.state;
            }
        });
    }
    else {
        setTimeout("updateChat()", 1500);
    }
}

//send the message
function sendChat(message){
    updateChat();
    jQuery.ajax({
        type: "POST",
        url: GeeklogConfig.site_url+"/jQchat/process.php?callback=?",
        data: {
            'function': 'send',
            'message': message,
            'nickname': nickname
        },
        dataType: "jsonp",
        success: function(data){
            updateChat();
        }
    });
}

//add a user to our chat
function _addUser(){
    jQuery.ajax({
        type: "POST",
        url: GeeklogConfig.site_url+"/jQchat/process.php?callback=?",
        data: {
            'function': 'addUser',
            'state': state,
            'nickname': nickname
        },
        dataType: "jsonp",
        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) {
                        document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
                    }
                }
            }
            if (data.users) {
                jQuery('#user-area').html(jQuery("<p>" + data.users + "</p>"));
            }
            instanse = false;
            state = data.state;
        }
    });
    setInterval("updateChat()", 1500);
    exitTimer = setTimeout("_bumpUser()",600000);
}

//remove a user from our chat
function _removeUser(){
    jQuery.ajax({
        type: "POST",
        url: GeeklogConfig.site_url+"/jQchat/process.php?callback=?",
        data: {
            'function': 'removeUser',
            'nickname': nickname,
        },
        dataType: "jsonp",
        success: function(data){
            return true;
        }
    });
}

function _bumpUser(){
    _removeUser();
    if (confirm('Chat expired, would you like to rejoin?')) {
        //window.reload(true);
        _addUser();
    }else{
        window.close();
    }
}

