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.createModalIfDoesntExist = function(username, touid) { var chatModal = $('#chat-modal-'+touid); if(!chatModal.length) { var chatModal = $('#chat-modal').clone(); chatModal.attr('id','chat-modal-'+touid); var uuid = utils.generateUUID(); 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){ chatModal.hide(); taskbar.discard('chat', uuid); }); chatModal.on('click', function(e) { module.bringModalToTop(chatModal); }); addSendHandler(chatModal, touid); getChatMessages(chatModal, touid); } taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username}); return chatModal; } module.load = function(uuid) { var chatModal = $('div[UUID="'+uuid+'"]'); chatModal.show(); module.bringModalToTop(chatModal); } module.minimize = function(uuid) { var chatModal = $('div[UUID="'+uuid+'"]'); chatModal.hide(); taskbar.minimize('chat', uuid); } function getChatMessages(chatModal, touid) { socket.emit('getChatMessages', {touid:touid}, function(messages) { for(var i = 0; i