var MessageType = {
MSG: 'msg',
SYS: 'sys',
STATUS: 'status'
};
dwr.engine.setErrorHandler(dwrErrorHandler);
function dwrErrorHandler(message, exception) {
System.logout();
}
function connectHandler() {
BlazeDS.connectHandler();
}
function messageHandler(headers, body) {
BlazeDS.messageHandler(headers, body);
}
function isComponentsLoaded() {
try {
BlazeDS.getMessengerEngine().ping();
return true;
} catch (e) {
return false;
}
}
var BlazeDS = {
pinger: null,
loginId: "",
userId: "",
password: "",
callbacks: [],
calling: false,
getMessengerEngine: function() {
if (navigator.appName.indexOf ("Microsoft") !=-1) {
return window['messenger'];
} else {
return document['messenger'];
}
},
ping: function() {
var contacts = Buddylist.getContacts();
IMService.ping(contacts, function(response) {
if (!response.result) {
System.logout();
} else {
var updatedContacts = response.data;
for (var i=0; i<updatedContacts.length; i++) {
var obj = new Object();
obj.sender = updatedContacts[i].name;
obj.status = updatedContacts[i].status;
BlazeDS.setContactStatus(obj);
}
BlazeDS.setStatus(Status.state, "auto-status");
}
});
},
connectHandler: function() {
BlazeDS.pinger = setInterval(function(){ BlazeDS.ping() }, 1000*60);
setTimeout(function() { BlazeDS.setStatus(StatusType.ONLINE, "auto-online") }, 1000);
setTimeout(function() { BlazeDS.getUnreadMessages() }, 3000);
},
getTimezone: function() {
var localDate = new Date();
var timezone = localDate.getTimezoneOffset()/60*-1;
return timezone;
},
login: function(user, pass) {
var timezone = BlazeDS.getTimezone();
IMService.login(user, pass, timezone, BlazeDS.handleLogin);
},
ssoLogin: function() {
var timezone = BlazeDS.getTimezone();
IMService.ssoLogin(timezone, BlazeDS.handleLogin);
},
handleLogin: function(response) {
if (response.result) {
BlazeDS.postLoginInit(response);
} else {
System.handleInvalidLogin(response.data.error);
}
},
postLoginInit: function(response) {
if (isComponentsLoaded()) {
userId = response.data.userId;
demoMode = response.data.demoMode;
BlazeDS.getMessengerEngine().subscribe(userId, 'chat');
System.handleLogin(userId, response.data.contacts, response.data.blocked, false);
} else {
setTimeout(function() {BlazeDS.postLoginInit(response)}, 1000);
}
},
logout: function() {
if($('confirmCall'))  Windows.close('confirmCall');
try {
CallWindow.hide(BlazeDS.calling);
} catch (e) {}
BlazeDS.getMessengerEngine().disconnect();
clearInterval(BlazeDS.pinger);
},
register: function(user, pass, email) {
IMService.register(user, pass, email, function(response) {
System.handleRegister(response.data);
});
},
setStatus: function(status, msg) {
var body = new Object();
body.command = "setStatus";
body.status = status;
body.message = msg;
this.doSendMessage(MessageType.STATUS, userId, userId, body);
},
setContactStatus: function(response) {
if (response.status == StatusType.OFFLINE) {
for (roomName in Chatroom.windows) {
System.handleLeaveRoom(response.sender, roomName);
}
if (BlazeDS.calling && CallWindow.recipient == response.sender) {
BlazeDS.handleHang(response);
}
}
System.handleBuddyStatus(response.sender, response.status);
},
addContact: function(contact, group) {
var body = new Object();
body.command = "addContact";
body.contact = contact;
body.group = group;
this.doSendMessage(MessageType.SYS, userId, userId, body, function(response) {
Buddylist.handleAddNewBuddy(response.contact, response.group, response.contactStatus, response.contactBlocked, response.result);
BlazeDS.setStatus(Status.state, Status.awayMessage);
});
},
getUnreadMessages: function() {
IMService.getUnreadMessages(function(response) {
var messages = response.data.unreadMessages;
var serverTZ = response.data.serverTZ;
for (var i=0; i<messages.length; i++) {
var sender = messages[i].contact;
var date = messages[i].date;
var tzOffset = date.getTimezoneOffset() * 60000 + serverTZ; //calculate time zone offset
date.setTime(date.getTime()+tzOffset);
var message = messages[i].text;
var isBold = ""+messages[i].bold;
var isItalic = ""+messages[i].italic;
var isUnderline = ""+messages[i].underline;
var fontName = messages[i].fontName;
var fontSize = messages[i].fontSize;
var fontColor = messages[i].fontColor;
System.handleMessage(sender, userId, message, false, isBold, isItalic, isUnderline, fontName, fontSize, fontColor, date);
}
});
},
removeContact: function(contact) {
var body = new Object();
body.command = "removeContact";
body.contact = contact;
this.doSendMessage(MessageType.SYS, userId, userId, body);
},
removeGroup: function(group) {
var body = new Object();
body.command = "removeGroup";
body.group = group;
this.doSendMessage(MessageType.SYS, userId, userId, body);
},
blockContact: function(contact) {
var body = new Object();
body.command = "blockContact";
body.contact = contact;
this.doSendMessage(MessageType.SYS, userId, userId, body);
},
unblockContact: function(contact) {
var body = new Object();
body.command = "unblockContact";
body.contact = contact;
this.doSendMessage(MessageType.SYS, userId, userId, body);
BlazeDS.setStatus(Status.state, Status.awayMessage);
},
getRoomList: function() {
var body = new Object();
body.command = "roomList";
this.doSendMessage(MessageType.SYS, userId, userId, body, function(response) {
var rooms = response.rooms;
ChatroomList.showRoomList(rooms);
});
},
joinRoom: function(room) {
var body = new Object();
body.command = "joinRoom";
body.room = room;
this.doSendMessage(MessageType.SYS, userId, userId, body, function(response) {
if (response.result == 1) {
$('newroom_error_msg').innerHTML = Languages.get('invalidRoom');
return;
}
Windows.close('newRoom');
var users = new Array();
var room = response.room;
for (var i=0; i<response.users.source.length; i++) {
if (response.users[i] != userId) {
users.push(response.users.source[i]);
}
}
BlazeDS.getMessengerEngine().addSubscription("recipient."+room, "sender <> '"+userId+"'");
Chatroom.showChatRoom(response.room, users);
BlazeDS.notifyJoinRoom(room);
});
},
notifyJoinRoom: function(room) {
var body = new Object();
body.command = "contactJoinedRoom";
body.room = room;
this.doSendMessage(MessageType.SYS, userId, room, body);
},
leaveRoom: function(room) {
BlazeDS.getMessengerEngine().removeSubscription("recipient."+room, "sender <> '"+userId+"'");
var body = new Object();
body.command = "leaveRoom";
body.room = room;
this.doSendMessage(MessageType.SYS, userId, room, body);
},
updateRoom: function(response) {
if (response.command == 'contactJoinedRoom') {
System.handleJoinRoom(response.sender, response.room);
} else {
System.handleLeaveRoom(response.sender, response.room);
}
},
initCall: function(recipient) {
if (BlazeDS.calling) {
Dialogs.callInProgress(recipient);
return;
}
BlazeDS.calling = true;
var body = new Object();
body.command = "initCall";
this.doSendMessage(MessageType.SYS, userId, recipient, body)
CallWindow.initCall(recipient, true);
if(audioNotify == true) {
setTimeout(function() { CallWindow.getCallEngine().playSound('calling', true); }, 1000);
}
},
handleCallRequest: function(response) {
if (BlazeDS.calling) {
var body = new Object();
body.command = "busy";
this.doSendMessage(MessageType.SYS, userId, response.sender, body)
return;
}
CallWindow.initCall(response.sender, false);
setTimeout(function() {
Dialogs.confirmCall(response.sender);
if(audioNotify == true) { //TODO: catch not loaded yet swf exception
CallWindow.getCallEngine().playSound('incomingCall', true);
}
}, 1000);
if(titlebarBlinker == true && useBlinker == true) {
clearTimeout(blinkerTimer);
blinkerTimer = setTimeout("titlebarBlink('Incoming call', '"+response.sender+"', 0, false)", blinkSpeed);
}
},
acceptCall: function(sender) {
BlazeDS.calling = true;
CallWindow.getCallEngine().stopSound();
var body = new Object();
body.command = "acceptCall";
this.doSendMessage(MessageType.SYS, userId, sender, body)
},
declineCall: function(sender) {
BlazeDS.calling = false;
CallWindow.getCallEngine().stopSound();
CallWindow.hide(false);
var body = new Object();
body.command = "declineCall";
this.doSendMessage(MessageType.SYS, userId, sender, body)
},
hang: function(recipient) {
BlazeDS.calling = false;
var body = new Object();
body.command = "hang";
this.doSendMessage(MessageType.SYS, userId, recipient, body);
},
handleHang: function(response) {
BlazeDS.calling = false;
if($('confirmCall'))  Windows.close('confirmCall');
CallWindow.hide(false);
},
handleStartCall: function(response) {
if($('confirmCall')) { //call confirmed from another instance
BlazeDS.handleHang(response);
return;
}
CallWindow.getCallEngine().stopSound();
var connectTo = eval("response."+userId+"_contact");
if (!CallWindow.getCallEngine().isMicEnabled()) {
showVideoWindow();
}
setTimeout(function() {
CallWindow.getCallEngine().startCall(connectTo, userId, response.callId, response.red5url);
Windows.getWindow('callWindow').setTitle(connectTo+": talking");
CallWindow.showControls(true);
new Effect.Pulsate(Windows.getWindow('callWindow').getId() + '_top');
}, 1000);
//    alert("startVideoCall, callId="+response.callId+", red5url="+response.red5url+", connect to:"+connectTo);
//System.handleVideoCall(connectTo, userId, response.callId, response.red5url);
},
handleDeclineCall: function(response) {
if (response.sender == userId) { //decline came from us or another instance
BlazeDS.handleHang(response);
return;
}
if (!BlazeDS.calling) {
return;
}
BlazeDS.calling = false;
CallWindow.showControls(false);
CallWindow.getCallEngine().stopSound();
if(audioNotify == true) {
CallWindow.getCallEngine().playSound('callFailed', false);
}
Dialogs.callDeclined(response.sender);
},
handleBusy: function(response) {
BlazeDS.calling = false;
setTimeout(function() {
CallWindow.showControls(false);
CallWindow.getCallEngine().stopSound();
if(audioNotify == true) {
CallWindow.getCallEngine().playSound('busy', false);
}
Dialogs.callBusy(response.sender);
}, 1000);
},
broadcast: function(recipient) {
var body = new Object();
body.command = "broadcast";
this.doSendMessage(MessageType.SYS, userId, recipient, body);
},
handleBroadcast: function(response) {
CallWindow.getCallEngine().playStream();
},
startVideo: function(recipient) {
var body = new Object();
body.command = "startVideo";
this.doSendMessage(MessageType.SYS, userId, recipient, body);
},
handleStartVideo: function(response) {
CallWindow.videoEnabled = true;
showVideoWindow();
},
stopVideo: function(recipient) {
CallWindow.getCallEngine().clearOutVideo();
var body = new Object();
body.command = "stopVideo";
this.doSendMessage(MessageType.SYS, userId, recipient, body);
},
handleStopVideo: function(response) {
CallWindow.getCallEngine().clearInVideo();
CallWindow.videoEnabled = false;
if (!CallWindow.cameraEnabled) {
hideVideoWindow();
}
},
sendMessage: function(recipient, msg, chatroom, isBold, isItalic, isUnderline, fontName, fontSize, fontColor) {
var body = new Object();
body.message = msg;
body.chatroom = chatroom;
body.isBold = isBold;
body.isItalic = isItalic;
body.isUnderline = isUnderline;
body.fontName = fontName;
body.fontSize = fontSize;
body.fontColor = fontColor;
this.doSendMessage(MessageType.MSG, userId, recipient, body);
},
doSendMessage: function(type, sender, recipient, body, callbackFn) {
if (callbackFn != null) {
var callbackId = ""+new Date().getTime();
this.callbacks[callbackId] = callbackFn;
body.callbackId = callbackId;
}
body.recipient = recipient;
if (type == MessageType.STATUS) {
var users = Buddylist.getActiveUsers();
BlazeDS.getMessengerEngine().broadcast(sender, users, type, body);
} else if (type == MessageType.MSG || body.command=='declineCall') {
var users = new Array();
users.push(sender);
users.push(recipient);
BlazeDS.getMessengerEngine().broadcast(sender, users, type, body);
} else {
BlazeDS.getMessengerEngine().sendMessage(sender, recipient, type, body);
}
},
messageHandler: function(headers, body) {
if (typeof body.callbackId != 'undefined') {
var callbackFn = BlazeDS.callbacks[body.callbackId];
delete BlazeDS.callbacks[body.callbackId];
callbackFn(body);
return;
}
if (headers['type'] == MessageType.MSG) {
var sender = body.sender;
var recipient = body.recipient;
if (sender == userId) {
if(body.chatroom == 'true') {
Chatroom.windows[recipient].sendResult(body.message, body.isBold, body.isItalic, body.isUnderline, body.fontName, body.fontSize, body.fontColor, body.error);
} else {
IM.windows[recipient].sendResult(body.message, body.isBold, body.isItalic, body.isUnderline, body.fontName, body.fontSize, body.fontColor, body.error);
}
} else {
System.handleMessage(sender, recipient, body.message, body.chatroom, body.isBold, body.isItalic, body.isUnderline, body.fontName, body.fontSize, body.fontColor);
}
} else if (headers['type'] == MessageType.STATUS) {
BlazeDS.setContactStatus(body);
} else if (headers['type'] == MessageType.SYS) {
if (body.command == "contactJoinedRoom" || body.command == "leaveRoom") {
BlazeDS.updateRoom(body);
} else if (body.command == "initCall") {
BlazeDS.handleCallRequest(body);
} else if (body.command == "startCall") {
BlazeDS.handleStartCall(body);
} else if (body.command == "declineCall") {
BlazeDS.handleDeclineCall(body);
} else if (body.command == "busy") {
BlazeDS.handleBusy(body);
} else if (body.command == "broadcast") {
BlazeDS.handleBroadcast(body);
} else if (body.command == "startVideo") {
BlazeDS.handleStartVideo(body);
} else if (body.command == "stopVideo") {
BlazeDS.handleStopVideo(body);
} else if (body.command == "hang") {
BlazeDS.handleHang(body);
}
}
},
errorHandler: function(event) {
alert('got error...');
}
};
