//////////////////// //Global constants //////////////////// var opentok_apiKey = '<%=SocialStream::Presence.opentok_api_key%>'; //Correspondence between slugs and session ids var slugOfSession = new Array(); //slugOfSession['session_id'] = "slug"; //////////////////// //Videochat button actions //////////////////// function clickVideoChatButton(slug){ var videoBoxVisibility = toggleVideoBoxForSlug(slug); if (videoBoxVisibility) { openVideoChatWindow(slug); } else { closeVideoChatWindow(slug); } } function closeVideoChatWindow(slug){ closeVideoSession(slug); return; } //Single variable for all slugs. var reconnectAttemptsVideo = 1; function openVideoChatWindow(slug){ if (slug in contactsInfo) { var connectionStatus = contactsInfo[slug].videoChatStatus; } else { var connectionStatus = null; } if (TB.checkSystemRequirements() != TB.HAS_REQUIREMENTS) { showNotificationOnVideoBox(slug,I18n.t('chat.videochat.requirements')); if(connectionStatus!=null){ contactsInfo[slug].videoChatStatus = "disconnected"; } return; } if(! isUserConnected()){ showNotificationOnVideoBox(slug, I18n.t("chat.videochat.offline")); if(connectionStatus!=null){ contactsInfo[slug].videoChatStatus = "disconnected"; } return; } if(! isSlugChatConnected(slug)){ showNotificationOnVideoBox(slug, I18n.t("chat.videochat.guestOffline", {name: getNameFromSlug(slug)})); if(connectionStatus!=null){ contactsInfo[slug].videoChatStatus = "disconnected"; } return; } if(connectionStatus==null){ if(reconnectAttemptsVideo > 0){ reconnectAttemptsVideo--; showNotificationOnVideoBox(slug,I18n.t('chat.videochat.connectingWait')); setTimeout(function() { openVideoChatWindow(slug); }, 5000); } else { showNotificationOnVideoBox(slug,I18n.t('chat.videochat.unable')); } return; } else { reconnectAttemptsVideo = 1; } if(connectionStatus!="disconnected"){ return; } //connectionStatus=="disconnected" //Start negotiation negotiateVideoChatSession(slug); } //////////////////// //Session establishment //////////////////// //VideoChat Session: Step 1 function negotiateVideoChatSession(slug){ showNotificationOnVideoBox(slug,I18n.t('chat.videochat.negotiating')); contactsInfo[slug].videoChatStatus="negotiating"; if(contactsInfo[slug].version != null){ connectingVideoChatSession(slug); return; } else { sendIQStanzaForRequestClientInfo(slug); } } //VideoChat Session: Step 2 function connectingVideoChatSession(slug){ if (contactsInfo[slug].version == getJavascriptXMPPClientName()) { contactsInfo[slug].videoChatStatus="connecting"; showNotificationOnVideoBox(slug,I18n.t('chat.videochat.connecting')); requestSessionIDAndTokenForVideoChat(slug); } else { showNotificationOnVideoBox(slug,I18n.t("chat.videochat.clientIssue", {name: getNameFromSlug(slug)})); contactsInfo[slug].videoChatStatus="disconnected"; } } function requestSessionIDAndTokenForVideoChat(slug){ $.post("/requestVideoChat.xml", { }, function(data){ var session = $(data).find("session"); if (session.length == 1) { if (($(session).find("id").length == 1) && (($(session).find("user_token").length == 1)) && (($(session).find("guest_token").length == 1))) { contactsInfo[slug].session_id = $(session).find("id").text(); contactsInfo[slug].user_token = $(session).find("user_token").text(); contactsInfo[slug].guest_token = $(session).find("guest_token").text(); sendVideoChatRequestToUser(slug); return; } } showNotificationOnVideoBox(slug, I18n.t('chat.videochat.serverIssue')); contactsInfo[slug].videoChatStatus="disconnected"; }); } //VideoChat Session: Step 3 function sendVideoChatRequestToUser(slug){ contactsInfo[slug].videoChatStatus="waiting"; showNotificationOnVideoBox(slug,I18n.t('chat.videochat.waiting')); sendIQStanzaToRequestVideochat(slug); } //VideoChat Session: Step 4 function receiveVideoChatResponseFromUser(slug,response){ if(contactsInfo[slug].videoChatStatus!="waiting"){ return; } if(response=="yes"){ showNotificationOnVideoBox(slug,I18n.t('chat.videochat.establishing')); contactsInfo[slug].videoChatStatus="establishing"; initVideoCallWith(slug); } else if (response=="no"){ showNotificationOnVideoBox(slug,I18n.t("chat.videochat.rejected", {name: getNameFromSlug(slug)})); contactsInfo[slug].videoChatStatus="disconnected"; } else { showNotificationOnVideoBox(slug,I18n.t("chat.videochat.rejectedBusy", {name: getNameFromSlug(slug)})); contactsInfo[slug].videoChatStatus="disconnected"; } } //Cancelation received function receiveVideoChatCancelationFromUser(slug,response){ if(slug in contactsInfo){ if(contactsInfo[slug].videoChatStatus=="pending"){ showNotificationOnVideoBox(slug,I18n.t("chat.videochat.cancel", {name: getNameFromSlug(slug)})); contactsInfo[slug].videoChatStatus="disconnected"; } } else { closeVideoSession(slug); } } //VideoChat Session: Step 5 function closeVideoSession(slug){ if(slug in contactsInfo){ var connectionStatus = contactsInfo[slug].videoChatStatus; if(connectionStatus=="connected"){ showNotificationOnVideoBox(slug,I18n.t('chat.videochat.disconnecting')); //Close opentok session... if(contactsInfo[slug].session.connected){ contactsInfo[slug].session.disconnect(); } restoreChatBoxAfterVideoCall(slug); } else if(connectionStatus=="waiting"){ sendIQStanzaToCancelVideochat(slug); } else if(connectionStatus=="pending"){ sendIQStanzaToResponseVideochat(slug,"no"); } contactsInfo[slug].videoChatStatus="disconnected"; } toggleVideoBoxForSlug(slug,false); } ///////////// // Videochat interface functions //////////// function clientInfoReceivedTrigger(slug){ //Check for videochat request if ((slug in contactsInfo)&&(contactsInfo[slug].videoChatStatus="negotiating")){ connectingVideoChatSession(slug); } } function showNotificationOnVideoBox(slug,msg){ setVideoBoxContent(slug,"

" + msg +"

"); } function updateInterfaceAfterVideoRequestReceived(slug){ if (contactsInfo[slug].videoChatStatus!="disconnected"){ sendIQStanzaToResponseVideochat(slug,"busy"); return; } else { contactsInfo[slug].videoChatStatus="pending"; } var jid = slug+"@"+contactsInfo[slug].domain //Show chatbox if (createChatBox(slug, getNameFromSlug(slug), jid, user_name, user_jid)) { } else { getChatBoxForSlug(slug).chatbox("option", "boxManager").toggleBox(true); }; var chatBox = getChatBoxForSlug(slug); //Show invitation message showInvitationInVideoBox(slug); //Show videobox toggleVideoBoxForSlug(slug,true); } function showInvitationInVideoBox(slug){ var title = "

" + I18n.t("chat.videochat.call", {name: getNameFromSlug(slug)}) + "

"; var msg = title + "

" + I18n.t("chat.videochat.accept") + " -" + " " + I18n.t("chat.videochat.deny") + "

"; setVideoBoxContent(slug,msg); setVideochatRequestButtonsFunction(); } function setVideochatRequestButtonsFunction(){ $(".videoChatButton").click(function(event) { event.preventDefault(); var result = $(this).attr("value"); var slug = $(this).attr("slug"); sendIQStanzaToResponseVideochat(slug,result); if(result=="yes"){ showNotificationOnVideoBox(slug,I18n.t('chat.videochat.establishing')); contactsInfo[slug].videoChatStatus="establishing"; initVideoCallWith(slug); } else { contactsInfo[slug].videoChatStatus="disconnected"; closeVideoSession(slug); } }); } function togglePublisherDivOnVideoChat(slug){ var publisherDiv = getPublisherVideoBoxForSlug(slug); var chatBox = getChatBoxForSlug(slug); var videoBox = getVideoBoxForSlug(slug); if((chatBox==null)||(videoBox==null)||(publisherDiv==null)){ return; } var videoBoxVisibility = publisherDiv.css("z-index")!="-1"; var desp = OPENTOK_PUBLISHER_HEIGHT + 1; var scrollHeight = $(chatBox)[0].scrollHeight; var outerHeight = chatBox.outerHeight(); var diff = scrollHeight - chatBox.scrollTop() - outerHeight; var scrollMov = 145; if (! videoBoxVisibility){ //Show publisher div publisherDiv.css("z-index","100"); //Resize windows chatBox.css("height",chatBox.height() - desp ); videoBox.css("height",videoBox.height() + desp ); chatBox.scrollTop(chatBox.scrollTop() + scrollMov); } else { //Hide publisher div publisherDiv.css("z-index","-1"); //Resize windows chatBox.css("height",chatBox.height() + desp ); videoBox.css("height",videoBox.height() - desp ); if(diff < desp){ chatBox.scrollTop(chatBox.scrollTop() - diff); } else { chatBox.scrollTop(chatBox.scrollTop() - scrollMov); } } } function restoreChatBoxAfterVideoCall(slug){ //Original dimensions showNotificationOnVideoBox(slug,I18n.t('chat.videochat.disconnected')); $(getChatBoxButtonForSlug(slug,"videoChange")).find("span").hide(); toggleVideoBoxForSlug(slug,false); var chatBox = getChatBoxForSlug(slug); chatBox.height(chatBoxHeight); } function checkVideocallFeature(){ if (opentok_apiKey=="default"){ //Hide videochat buttons //Changing Tooltip CSS class by JQuery var ss = document.styleSheets; for (var i=0; i