define(['taskbar'], function(taskbar) {
var module = {};
module.bringModalToTop = function(chatModal) {
var topZ = 0;
$('.modal').each(function() {
var thisZ = parseInt($(this).css('zIndex'), 10);
if (thisZ > topZ) {
topZ = thisZ;
}
});
chatModal.css('zIndex', topZ + 1);
}
module.getModal = function(touid) {
return $('#chat-modal-' + touid);
}
module.modalExists = function(touid) {
return $('#chat-modal-' + touid).length !== 0;
}
function checkStatus(chatModal, callback) {
socket.emit('api:user.isOnline', chatModal.touid, function(data) {
if(data.online !== chatModal.online) {
if(data.online) {
module.appendChatMessage(chatModal, chatModal.username + ' is currently online.\n', data.timestamp);
} else {
module.appendChatMessage(chatModal, chatModal.username + ' is currently offline.\n', data.timestamp);
}
chatModal.online = data.online;
}
if(callback)
callback(data.online);
});
}
function checkOnlineStatus(chatModal) {
if(chatModal.intervalId === 0) {
chatModal.intervalId = setInterval(function() {
checkStatus(chatModal);
}, 1000);
}
}
module.createModal = function(username, touid, callback) {
var chatModal = $('#chat-modal').clone(),
uuid = utils.generateUUID();
chatModal.intervalId = 0;
chatModal.touid = touid;
chatModal.username = username;
chatModal.attr('id', 'chat-modal-' + touid);
chatModal.attr('UUID', uuid);
chatModal.appendTo($('body'));
chatModal.draggable({
start:function() {
module.bringModalToTop(chatModal);
}
});
chatModal.find('#chat-with-name').html(username);
chatModal.find('.close').on('click', function(e) {
clearInterval(chatModal.intervalId);
chatModal.intervalId = 0;
chatModal.remove();
chatModal.data('modal', null);
taskbar.discard('chat', uuid);
});
chatModal.on('click', function(e) {
module.bringModalToTop(chatModal);
});
addSendHandler(chatModal);
getChatMessages(chatModal, function() {
checkOnlineStatus(chatModal);
});
taskbar.push('chat', chatModal.attr('UUID'), {
title:' ' + username,
state: ''
});
return chatModal;
}
module.center = function(chatModal) {
chatModal.css("position", "fixed");
chatModal.css("top", "100px");
chatModal.css("left", Math.max(0, (($(window).width() - $(chatModal).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
return chatModal;
}
module.load = function(uuid) {
var chatModal = $('div[UUID="'+uuid+'"]');
chatModal.show();
module.bringModalToTop(chatModal);
checkOnlineStatus(chatModal);
}
module.minimize = function(uuid) {
var chatModal = $('div[UUID="'+uuid+'"]');
chatModal.hide();
taskbar.minimize('chat', uuid);
clearInterval(chatModal.intervalId);
chatModal.intervalId = 0;
}
function getChatMessages(chatModal, callback) {
socket.emit('getChatMessages', {touid:chatModal.touid}, function(messages) {
for(var i = 0; i