Aime is not just a home automation solution, but can help you book a cab, track fitness and save energy too – e27

“);
//get message bubbles
//this is to make sure that the new messages are placed properly in the canvas
var msgbubbleids = [];
var appended = 0;
canvas.find(“.msgfromserver”).each(function(){
msgbubbleids.push($(this).attr(“data-messageid”));
});
canvas.append(htmlobj);
htmlobj.find(“.msgfromserver”).each(function(){
var messageid = $(this).attr(“data-messageid”);
//alert(canvas.find(“.msg-bubble-“+messageid).length);
//if 2 or more already in the canvas
if(canvas.find(“.msg-bubble-“+messageid).length >= 2){
htmlobj.find(“.msg-bubble-“+messageid).remove();
}
else if(msgbubbleids.length){
var aftermeid = 0;
for(i=msgbubbleids.length-1; i>=0; i–){
//alert((messageid*1)+”>”+(msgbubbleids[i]*1))
if((messageid*1)>(msgbubbleids[i]*1)){
aftermeid = msgbubbleids[i];
break;
}
}
//only insert the message bubble after the proper id
canvas.find(“.msg-bubble-“+aftermeid).after($(this));
msgbubbleids.push(messageid);
appended++;
}
});
var htmlclean = htmlobj.html()
htmlobj.remove();
if(appended”+name+””);
//set profile image
var circle = chat_popup.find(“.circle”);
var circleparent = circle.parent();
var acircle = $(““);
circleparent.prepend(acircle);
circle.attr(‘src’, profile_img).appendTo(acircle);
GlobalJS.setHrefs(chat_popup.find(“.profile-name”));
}
else{
//popup name
chat_popup.find(“.title h4”).text(name);
//set profile image
chat_popup.find(“.circle”).attr(‘src’, profile_img);
}
var closebutton = chat_popup.find(“.close-chat-popup-box”);
closebutton.attr(“data-chatpopupid”, chat_popup_id)
closebutton.click(function(){
MagnetoChat.closePopup($(this).attr(“data-chatpopupid”));
});

//for file button update the for id
var fileobj = chat_popup.find(“.message-attachment”).find(“input”);
var filelabelobj = chat_popup.find(“.message-attachment”).find(“label”);
var fileobjform = chat_popup.find(“.message-attachment”).find(“form”);
var fileobjiframe = chat_popup.find(“.message-attachment”).find(“fileobjiframe”);
var forid = filelabelobj.attr(“for”) + “-“+id;
filelabelobj.attr(“for”, forid);
fileobj.attr(“data-id”, id);
fileobj.attr(“data-convid”, convid);
fileobj.attr(“data-toids”, toids);
fileobj.attr(“id”, forid);
fileobjform.attr(“id”, “form-“+forid);
fileobjiframe.attr(“id”, “iframe-“+forid);
MagnetoChat.registerFileButton(fileobj, “full”);

//set the textarea data
var txtarea = chat_popup.find(“.txtChatMessage”);
txtarea.attr(“data-id”, id);
txtarea.attr(“data-convid”, convid);
txtarea.attr(“data-toids”, toids);
txtarea.attr(“id”, “txtChatMessage-“+MagnetoChat.randomID());
txtarea.on(“focus”, function(){
$(“.chat-open-user-box”).find(“.chat-open-user-box-header”).removeClass(“new-message-alert-header”);
$(this).parents(“.chat-open-user-box”).find(“.chat-open-user-box-header”).addClass(“new-message-alert-header”);
});
MagnetoChat.registerTxtArea(txtarea, “full”);

$(“#chat-in-full .conversation-message-box”).hide();
//get previous messages
MagnetoChat.getPreviousMessages(id, convid, toids, “”, function(obj){
var id = obj[‘id’];
var listid = “chat-conv-“+id;
var listobj = $(“#”+listid);
var convid = obj[‘convid’];
var toids = obj[‘toids’];
var chat_popup_id = “chat-conv-popup-“+id;
var chat_popup = $(“#”+chat_popup_id);
//websocket
try{
//to tell others that the message was read
var wsmsg = {};
wsmsg[“command”] = “fetch”;
wsmsg[“convid”] = convid;
var wsmsgjson = JSON.stringify(wsmsg);
MagnetoChat.WebSocketServer.send( ‘message’, wsmsgjson );
}
catch(e){
}
//if chat popup is ready
if(MagnetoChat.isset(chat_popup.attr(“data-id”))){
var canvas = chat_popup.find(“.conversation-list”);
var canvasloader = chat_popup.find(“.conversation-list .loading”);
var canvas_id = “chat-conv-canvas-“+id;
canvas.attr(“id”, canvas_id);
var txtarea = chat_popup.find(“.txtChatMessage”);

//add this class
canvas.removeClass(“chat-conv-canvas”).addClass(“chat-conv-canvas”);
//add additional properties to canvas
canvas.attr(“data-id”, id);
canvas.attr(“data-toids”, toids);

//set consistent convid to all
MagnetoChat.setDataValue(listobj, chat_popup, canvas, txtarea, “data-convid”, convid);
//hide unseen count cause popup is alrady open
listobj.find(“.unseencount”).hide();

canvasloader.fadeOut(200).remove();
//set new html value
canvas.hide().html(obj[‘json’][‘html’]).fadeIn(200);

//the seen status in the last bubble
var lastbubble = canvas.find(“.msgfromserver”).last();
if(lastbubble.hasClass(“you”)){
var sent = $(‘

‘);
lastbubble.after(sent);
}

clearTimeout(MagnetoChat.fetcherTimeout);
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 100);

MagnetoChat.setSearchUserElem(chat_popup.find(“.txtMessageReceiver”));
// Put the scrollbar at the bottom of the chatbox window.
MagnetoChat.scrollToBottom(canvas);

canvas.scrollGuard();
//onclick canvas and not a highlight
canvas.mousedown(function(e){
$(this).attr(“data-mousepagex”, e.pageX);
});
canvas.mouseup(function(e){
var pageX = $(this).attr(“data-mousepagex”);
//if same position and left click
if(pageX==e.pageX&&e.which==1){
var chat_popup_id = “chat-conv-popup-“+$(this).attr(“data-id”);
MagnetoChat.focusChatPopup(chat_popup_id);
}
});
//scroll to get more previous messages
canvas.scroll(function(){
if($(this).scrollTop()Loading previous messages…”);
//return 0;
MagnetoChat.getPreviousMessages(id, convid, toids, refmessageid, function(obj){
var convid = obj[‘convid’];
var toids = obj[‘toids’];
var refmessageid = obj[‘refmessageid’];
var chat_popup_id = “chat-conv-popup-“+id;
var chat_popup = $(“#”+chat_popup_id);
//if chat popup is ready
if(MagnetoChat.isset(chat_popup.attr(“data-id”))){
var canvas = chat_popup.find(“.conversation-list”);
canvas.find(“.loadingprevmsg”).remove();
canvas.prepend(obj[‘json’][‘html’]);
var firstmessage = canvas.find(“.msg-bubble-“+refmessageid);
var scrolltop = firstmessage.offset().top – canvas.offset().top – (canvas.parent().height()/2);
//scrolltop =20;
canvas.animate({ scrollTop: scrolltop },1)
//alert(canvas.offset().top+” “+firstmessage.offset().top);
//canvas.animate({ scrollTop: 500 },100)
}
});

}
}
});

}
//this has to be run only after getting teh contents of the canvas
MagnetoChat.heightChatConversationDisplay();
});

/*
if(type == “new”){
$.ajax({
url: $(‘base’).attr(‘href’)+’chat/display_new_chat_popup’,
type: ‘GET’,
data: { chat_id: id },
dataType: ‘json’,
success: function(json){
$(“.chat-open-user-container”).append(json.html);
$(“#”+id).find(“.txtChatMessage”).focus();
MagnetoChat.search_user_receiver($(“#”+id).find(“.txtMessageReceiver”));

if(MagnetoChat.popups.length + 1 > MagnetoChat.total_popups){
$(“#”+MagnetoChat.popups[MagnetoChat.total_popups-1]).remove();
Array.remove(MagnetoChat.popups, MagnetoChat.total_popups-1);
}

MagnetoChat.popups.push(id);
MagnetoChat.calculate_popups();
}
});
}
*/
$(“.chat-open-user-box-header”).removeClass(“new-message-alert-header”);
$(“#”+id+” .chat-open-user-box-header”).addClass(“new-message-alert-header”);
}

//creates markup for a new popup.
MagnetoChat.registerPopup = function(id, name, profile_img, convid, toids, listobj, href, dlink){
if($(window).width()”+name+””);
//set profile image
var circle = chat_popup.find(“.circle”);
var circleparent = circle.parent();
var acircle = $(“
“);
circleparent.prepend(acircle);
circle.attr(‘src’, profile_img).appendTo(acircle);
GlobalJS.setHrefs(chat_popup.find(“.profile-name”));
}
else{
//popup name
chat_popup.find(“.title h4”).text(name);
//set profile image
chat_popup.find(“.circle”).attr(‘src’, profile_img);
}
//popup position
var chat_popups = $(“.chat-conv-popup”);
var rightpos = chat_popups.length * (chat_popups.first().width()+20);
var totalwidth = chat_popup.width()+rightpos;
//if popups are too long remove oldest
if(totalwidth>($(“body”).width()-100)){
chat_popups.first().remove();
}
//rearrange the popups
MagnetoChat.arrangePopups();
MagnetoChat.focusChatPopup(chat_popups.last().attr(“id”));
var closebutton = chat_popup.find(“.close-chat-popup-box”);
closebutton.attr(“data-chatpopupid”, chat_popup_id)
closebutton.click(function(){
MagnetoChat.closePopup($(this).attr(“data-chatpopupid”));
});

//for file button update the for id
var fileobj = chat_popup.find(“.message-attachment”).find(“input”);
var filelabelobj = chat_popup.find(“.message-attachment”).find(“label”);
var fileobjform = chat_popup.find(“.message-attachment”).find(“form”);
var fileobjiframe = chat_popup.find(“.message-attachment”).find(“fileobjiframe”);
var forid = filelabelobj.attr(“for”) + “-“+id;
filelabelobj.attr(“for”, forid);
fileobj.attr(“data-id”, id);
fileobj.attr(“data-convid”, convid);
fileobj.attr(“data-toids”, toids);
fileobj.attr(“id”, forid);
fileobjform.attr(“id”, “form-“+forid);
fileobjiframe.attr(“id”, “iframe-“+forid);
MagnetoChat.registerFileButton(fileobj);

//set the textarea data
var txtarea = chat_popup.find(“.txtChatMessage”);
txtarea.attr(“data-id”, id);
txtarea.attr(“data-convid”, convid);
txtarea.attr(“data-toids”, toids);
txtarea.attr(“id”, “txtChatMessage-“+MagnetoChat.randomID());
txtarea.on(“focus”, function(){
$(“.chat-open-user-box”).find(“.chat-open-user-box-header”).removeClass(“new-message-alert-header”);
$(this).parents(“.chat-open-user-box”).find(“.chat-open-user-box-header”).addClass(“new-message-alert-header”);
});
MagnetoChat.registerTxtArea(txtarea);

//see full chat
var href = chat_popup.find(“.seefullchat”).attr(“href”);
href += “?id=”+chat_popup.attr(“data-id”);
chat_popup.find(“.seefullchat”).attr(“href”, href);

//get previous messages
MagnetoChat.getPreviousMessages(id, convid, toids, “”, function(obj){
var id = obj[‘id’];
var listid = “chat-conv-“+id;
var listobj = $(“#”+listid);
var convid = obj[‘convid’];
var toids = obj[‘toids’];
var chat_popup_id = “chat-conv-popup-“+id;
var chat_popup = $(“#”+chat_popup_id);
//websocket
try{
//to tell others that the message was read
var wsmsg = {};
wsmsg[“command”] = “fetch”;
wsmsg[“convid”] = convid;
var wsmsgjson = JSON.stringify(wsmsg);
MagnetoChat.WebSocketServer.send( ‘message’, wsmsgjson );
}
catch(e){
}
//if chat popup is ready
if(MagnetoChat.isset(chat_popup.attr(“data-id”))){
var canvas = chat_popup.find(“.conversation-list”);
var canvasloader = chat_popup.find(“.conversation-list .loading”);
var canvas_id = “chat-conv-canvas-“+id;
canvas.attr(“id”, canvas_id);
var txtarea = chat_popup.find(“.txtChatMessage”);

//add this class
canvas.removeClass(“chat-conv-canvas”).addClass(“chat-conv-canvas”);
//add additional properties to canvas
canvas.attr(“data-id”, id);
canvas.attr(“data-toids”, toids);

//set consistent convid to all
MagnetoChat.setDataValue(listobj, chat_popup, canvas, txtarea, “data-convid”, convid);
//hide unseen count cause popup is alrady open
listobj.find(“.unseencount”).hide();

canvasloader.fadeOut(200).remove();
//set new html value
canvas.hide().html(obj[‘json’][‘html’]).fadeIn(200);

//last bubble
var lastbubble = canvas.find(“.msgfromserver”).last();
if(lastbubble.hasClass(“you”)){
var sent = $(‘

‘);
lastbubble.after(sent);
}

clearTimeout(MagnetoChat.fetcherTimeout);
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 100);

MagnetoChat.setSearchUserElem(chat_popup.find(“.txtMessageReceiver”));
// Put the scrollbar at the bottom of the chatbox window.
MagnetoChat.scrollToBottom(canvas);

canvas.scrollGuard();
//onclick canvas and not a highlight
canvas.mousedown(function(e){
$(this).attr(“data-mousepagex”, e.pageX);
});
canvas.mouseup(function(e){
var pageX = $(this).attr(“data-mousepagex”);
//if same position and left click
if(pageX==e.pageX&&e.which==1){
var chat_popup_id = “chat-conv-popup-“+$(this).attr(“data-id”);
MagnetoChat.focusChatPopup(chat_popup_id);
}
});
//scroll to get more previous messages
canvas.scroll(function(){
if($(this).scrollTop()Loading previous messages…”);
//return 0;
MagnetoChat.getPreviousMessages(id, convid, toids, refmessageid, function(obj){
var convid = obj[‘convid’];
var toids = obj[‘toids’];
var refmessageid = obj[‘refmessageid’];
var chat_popup_id = “chat-conv-popup-“+id;
var chat_popup = $(“#”+chat_popup_id);
//if chat popup is ready
if(MagnetoChat.isset(chat_popup.attr(“data-id”))){
var canvas = chat_popup.find(“.conversation-list”);
canvas.find(“.loadingprevmsg”).remove();
canvas.prepend(obj[‘json’][‘html’]);
var firstmessage = canvas.find(“.msg-bubble-“+refmessageid);
var scrolltop = firstmessage.offset().top – canvas.offset().top – (canvas.parent().height()/2);
//scrolltop =20;
canvas.animate({ scrollTop: scrolltop },1)
//alert(canvas.offset().top+” “+firstmessage.offset().top);
//canvas.animate({ scrollTop: 500 },100)
}
});

}
}
});
}
GlobalJS.setHrefs(chat_popup);
});
/*
if(type == “new”){
$.ajax({
url: $(‘base’).attr(‘href’)+’chat/display_new_chat_popup’,
type: ‘GET’,
data: { chat_id: id },
dataType: ‘json’,
success: function(json){
$(“.chat-open-user-container”).append(json.html);
$(“#”+id).find(“.txtChatMessage”).focus();
MagnetoChat.search_user_receiver($(“#”+id).find(“.txtMessageReceiver”));

if(MagnetoChat.popups.length + 1 > MagnetoChat.total_popups){
$(“#”+MagnetoChat.popups[MagnetoChat.total_popups-1]).remove();
Array.remove(MagnetoChat.popups, MagnetoChat.total_popups-1);
}

MagnetoChat.popups.push(id);
MagnetoChat.calculate_popups();
}
});
}
*/
$(“.chat-open-user-box-header”).removeClass(“new-message-alert-header”);
$(“#”+id+” .chat-open-user-box-header”).addClass(“new-message-alert-header”);
}

//register the file send button
MagnetoChat.registerFileButton = function(fileobj, type){
fileobj.attr(“data-type”, type);
fileobj.unbind(“change”);
fileobj.change(function (){
var filename = $(this).val();
filename = filename.split(“\\”);
filename = filename[filename.length-1];

//stop fetcher when sent something
MagnetoChat.stopFetcher = true;
clearTimeout(MagnetoChat.fetcherTimeout);

if($.trim(filename)!=””){
var id = $(this).attr(“data-id”);
var convid = $(this).attr(“data-convid”);
var toids = $(this).attr(“data-toids”);
var canvas = $(“#chat-conv-canvas-“+id);
var message = {};
message[‘type’] = “file”;
message[‘fileobjid’] = fileobj.attr(“id”);
message[‘filename’] = filename;

//if canvas is not yet ready
if(!canvas.hasClass(“chat-conv-canvas”)){
return false;
}
MagnetoChat.currentCanvas = canvas;
//send message
MagnetoChat.sendMessage(id, canvas, convid, toids, message, function(obj){
var canvas = obj[‘canvas’];
var json = obj[‘json’];
var listobj = $(“#chat-conv-“+canvas.attr(“data-id”));
var chat_popup = $(“#chat-conv-popup-“+canvas.attr(“data-id”));
var txtarea = chat_popup.find(“.txtChatMessage”);
//websocket
try{
//to tell others that the message sent
var wsmsg = {};
wsmsg[“command”] = “fetch”;
wsmsg[“convid”] = canvas.attr(“data-convid”);
var wsmsgjson = JSON.stringify(wsmsg);
MagnetoChat.WebSocketServer.send( ‘message’, wsmsgjson );
}
catch(e){
}
//sending status
var mymsgstatus = canvas.find(“.mymsgstatus”).find(“small.”+obj[“uniqueclass”]);
if(!MagnetoChat.isset(obj[‘failed’])){
var html = json[‘html’];
mymsgstatus.html(“Sent”);
mymsgstatus.addClass(“mymsgstatus-“+json[‘messageid’]).removeClass(obj[“uniqueclass”]);
}
else{
mymsgstatus.html(“Sending failed!”);
return false;
}

//set the new data-convid of the 4 elems
MagnetoChat.setDataValue(listobj, chat_popup, canvas, txtarea, “data-convid”, json[“convid”]);
//move to top
listobj.prependTo(listobj.parent());
var msgbubble = obj[‘canvas’].find(“.”+obj[‘sendbubbleid’]);
msgbubble.addClass(“msgfromserver”);
msgbubble.addClass(“msg-bubble-“+json[‘messageid’]);
msgbubble.attr(“data-messageid”, json[‘messageid’]);
msgbubble.removeClass(“msgfromlocal”);
msgbubble.removeClass(obj[‘sendbubbleid’]);
if(MagnetoChat.isset(json[‘filepath’])){
var spanhtml = msgbubble.find(“span”).html();
msgbubble.find(“span”).html(“ 
“+spanhtml+”“);
}

//if there are messages before
if(MagnetoChat.isset(html)){
var htmlclean = MagnetoChat.cleanHTML(canvas, html);
//msgbubble.before(htmlclean);
}
/*
setTimeout(function(){
canvas.find(“.mymsgstatus”).fadeOut(200,function(){
canvas.find(“.mymsgstatus”).remove();
});
}, 8000);
*/
//scroll to bottom
MagnetoChat.scrollToBottom(canvas);
MagnetoChat.stopFetcher = false;
clearTimeout(MagnetoChat.fetcherTimeout);
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 5000);
});
$(this).val(”);
var type = $(this).attr(“data-type”);
if(type!=”full”){
MagnetoChat.resizeChatPopupTxtBox($(this));
}

//$(“.chat-conversation-receiver”).remove();
}
MagnetoChat.scrollToBottom(canvas);
return false;
});
}

MagnetoChat.registerTxtArea = function(txtarea, type){
txtarea.unbind(“keyup”);
txtarea.unbind(“keypress”);
txtarea.attr(“data-type”, type);
// Textbox inside a chat popup
txtarea.keyup(function(e){
if($(this).val().length == “0”){
$(this).removeAttr(“style”);
var type = $(this).attr(“data-type”);
if(type!=”full”){
MagnetoChat.resizeChatPopupTxtBox($(this));
}
}
});
// Textbox inside a chat popup
txtarea.keypress(function(e){
//adjusting textarea
$(this).attr(“style”,”height:auto; height: “+$(this)[0].scrollHeight+”px”);
var type = $(this).attr(“data-type”);
if(type!=”full”){
MagnetoChat.resizeChatPopupTxtBox($(this));
}
//send the message
if(e.which == 13 && !e.shiftKey) {
//stop fetcher when sent something
MagnetoChat.stopFetcher = true;
clearTimeout(MagnetoChat.fetcherTimeout);

if($.trim($(this).val())!=””){
/*
//sydney’s codes
$(this).parents(“.chat-open-user-box”).find(“.dropdown”).removeClass(“hide”);
if($(this).parents(“.chat-open-user-box-conversation”).hasClass(“chat-new-conversation”)){
var receiver_value_obj = $(“.txtMessageReceiver”).select2(‘data’);

var receiver_value_array = [];
$.each(receiver_value_obj,function(index,value){
receiver_value_array.push(value.text);
});
var receiver_value_string = receiver_value_array.join(“,”);

//If receiver is only one display the image of the receiver
if(receiver_value_obj.length == “1”){
var html_image = ‘‘;
$(this).parents(“.chat-open-user-box”).find(“.title”).prepend(html_image);
}

$(this).parents(“.chat-open-user-box”).find(“.title h4”).text(receiver_value_string);
$(this).parents(“.chat-open-user-box-conversation”).removeClass(“chat-new-conversation”);
}
*/
var id = $(this).attr(“data-id”);
var convid = $(this).attr(“data-convid”);
var toids = $(this).attr(“data-toids”);
var canvas = $(“#chat-conv-canvas-“+id);
var message = $(this).val();
//if canvas is not yet ready
if(!canvas.hasClass(“chat-conv-canvas”)){
return false;
}
MagnetoChat.currentCanvas = canvas;
canvas.find(“.mymsgstatus”).remove();
//send message
MagnetoChat.sendMessage(id, canvas, convid, toids, message, function(obj){
var canvas = obj[‘canvas’];
var json = obj[‘json’];
var listobj = $(“#chat-conv-“+canvas.attr(“data-id”));
var chat_popup = $(“#chat-conv-popup-“+canvas.attr(“data-id”));
var txtarea = chat_popup.find(“.txtChatMessage”);
//websocket
try{
//to tell others that the message sent
var wsmsg = {};
wsmsg[“command”] = “fetch”;
wsmsg[“convid”] = canvas.attr(“data-convid”);
var wsmsgjson = JSON.stringify(wsmsg);
MagnetoChat.WebSocketServer.send( ‘message’, wsmsgjson );
}
catch(e){
}
//sending status
var mymsgstatus = canvas.find(“.mymsgstatus”).find(“small.”+obj[“uniqueclass”]);
if(!MagnetoChat.isset(obj[‘failed’])){
var html = json[‘html’];
mymsgstatus.html(“Sent”);
mymsgstatus.addClass(“mymsgstatus-“+json[‘messageid’]).removeClass(obj[“uniqueclass”]);
}
else{
mymsgstatus.html(“Sending failed!”);
return false;
}

if(!MagnetoChat.isset(listobj.attr(“data-convid”))){
//refresh chat list
MagnetoChat.fillChatList();
}
//set the new data-convid of the 4 elems
MagnetoChat.setDataValue(listobj, chat_popup, canvas, txtarea, “data-convid”, json[“convid”]);

//move to top list
listobj.prependTo(listobj.parent());

var msgbubble = obj[‘canvas’].find(“.”+obj[‘sendbubbleid’]);
msgbubble.addClass(“msgfromserver”);
msgbubble.addClass(“msg-bubble-“+json[‘messageid’]);
msgbubble.attr(“data-messageid”, json[‘messageid’]);
msgbubble.removeClass(“msgfromlocal”);
msgbubble.removeClass(obj[‘sendbubbleid’]);

/*
setTimeout(function(){
canvas.find(“.mymsgstatus”).fadeOut(200,function(){
canvas.find(“.mymsgstatus”).remove();
});
}, 8000);
*/
//scroll to bottom
MagnetoChat.scrollToBottom(canvas);
MagnetoChat.stopFetcher = false;
clearTimeout(MagnetoChat.fetcherTimeout);
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 5000);
});
$(this).val(”);
if(type!=”full”){
MagnetoChat.resizeChatPopupTxtBox($(this));
}
//$(“.chat-conversation-receiver”).remove();
}
MagnetoChat.scrollToBottom(canvas);
return false;
}
});
}

// For chat popup
MagnetoChat.resizeChatPopupTxtBox = function(txtobj){
// Height of the chat box popup
var box = parseInt($(txtobj).parents(“.chat-open-user-box”).innerHeight());

// If chat popup is a new conversation (new message)
if($(txtobj).parents(“.chat-open-user-box-conversation”).hasClass(“chat-new-conversation”)){
// Sum of chatbox header + chatbox user receiver textbox + chatbox message box textarea
var total = parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.chat-open-user-box-header”).innerHeight()) + parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.chat-conversation-receiver”).outerHeight(true)) + parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.conversation-message-box textarea”).innerHeight());
var conversation = (box – total);
}
else {
// if Chat popup is an existing conversation but the user will not add an additional member on that chat.
if($(txtobj).parents(“.chat-open-user-box”).find(“.chat-conversation-receiver”).hasClass(“hide”)){
// Sum of chatbox header + chatbox user receiver textbox + chatbox message box textarea
var total = parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.chat-open-user-box-header”).innerHeight()) + parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.conversation-message-box textarea”).innerHeight());
var conversation = (box – total);
}
// else if Chat popup is an existing conversation but the user will add an additional member on that chat.
else {
var total = parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.chat-open-user-box-header”).innerHeight()) + parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.chat-conversation-receiver”).outerHeight(true)) + parseInt($(txtobj).parents(“.chat-open-user-box”).find(“.conversation-message-box textarea”).innerHeight());
var conversation = (box – total);
}
}
$(txtobj).parents(“.chat-open-user-box”).find(“.conversation-list”).attr(“style”,”height: “+conversation+ “px !important”);
}

//this is used to close a popup
MagnetoChat.closePopup = function(id){
var chat_popup = $(“#”+id);
chat_popup.attr(“id”, “”);
chat_popup.fadeOut(200, function(){
chat_popup.remove();
MagnetoChat.arrangePopups();
});
//$(“#”+id).remove();
}

//focus on chat popup
MagnetoChat.focusChatPopup = function(id){
$(“.chat-open-user-box-header”).each(function(){
$(this).removeClass(“new-message-alert-header”);
});
setTimeout(function(){
$(“#”+id).find(“.chat-open-user-box-header”).addClass(“new-message-alert-header”);
$(“#”+id).find(“.txtChatMessage”).focus();
}, 100);
}

//minimize popup
MagnetoChat.minimizePopup = function(obj) {
var listobj = $(“#chat-conv-“+$(obj).attr(“data-id”));
var canvas = $(“#chat-conv-canvas-“+$(obj).attr(“data-id”));
var chat_popup = $(“#chat-conv-popup-“+$(obj).attr(“data-id”));
//max
if($(obj).parents(“.chat-open-user-box”).hasClass(“chat-minimise”)){
$(obj).parents(‘.chat-open-user-box’).removeClass(“chat-minimise”,100, function(){
if(listobj.find(“.unseencount”).is(“:visible”)){
clearTimeout(MagnetoChat.fetcherTimeout);
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 100);
}
MagnetoChat.scrollToBottom(canvas);
});
//alert(chat_popup);
chat_popup.find(“.min-button .fa”).addClass(“hide”);
chat_popup.find(“.min-button .min”).removeClass(“hide”);
//check if has unseen messages
}
//min
else {
$(obj).parents(‘.chat-open-user-box’).addClass(“chat-minimise”,100);
chat_popup.find(“.min-button .fa”).addClass(“hide”);
chat_popup.find(“.min-button .max”).removeClass(“hide”);
}
}

//arrange popups
MagnetoChat.arrangePopups = function(){
var chat_popups = $(“.chat-conv-popup”);
var increment = chat_popups.first().width()+20;
var rightpos = increment;
chat_popups.each(function(){
$(this).css(“right”, rightpos);
rightpos +=increment;
});
var chat_popup = $(“.new-message-alert-header”).parent();
chat_popup.find(“.txtChatMessage”).focus();
}

//move list to top
MagnetoChat.moveListToTop = function(listid){
var list = $(“.”+listid);
list.prependTo(list.parent());
}

//scroll the canvas to bottom
MagnetoChat.scrollToBottom = function(canvas){
canvas.animate({ scrollTop: canvas.prop(‘scrollHeight’) },400);
}

//set page title with count
MagnetoChat.setPageTitleCount = function(thecount){
var title = $(document).attr(“title”);
//remove current count
if(title.indexOf(“(“)==0){
title = title.split(“)”);
if(MagnetoChat.isset(title[1])){
//add count
title = title[1];
}
else{
title = “”;
}
}
if(thecount>0){
title = “(“+thecount+”) “+title;
$(“.msg-count”).html(thecount);
}
else{
$(“.msg-count”).html(“”);
}
$(document).attr(“title”, title);

}

// Function in autocomplete searching of user in chat popup
MagnetoChat.setSearchUserElem = function(obj){
$(obj).select2({
minimumInputLength: 1,
tokenSeparators: [“,”],
multiple: true,
dropdownCssClass: “chat-user-autocomplete”,
formatResult: user_format,
allowClear: true,
ajax: {
url: $(‘base’).attr(‘href’) + “_html/user.json”,
dataType: “json”,
data: function(term, page) {
return {
q: term
};
},
results: function(data, page) {
return {
results: data
};
}
},
initSelection: function(element, callback) {

},
escapeMarkup: function(m) {
return m;
}
})
.on(“change”,function(e){
resize_conversation_box($(obj));
});

$(“.txtMessageReceiver”).on(“select2-focus”, function(e) { global_nomatches_text = “No matches found.”; });
$(“.txtMessageReceiver”).on(“select2-focus”, function(e) { global_search_text = “Search user”; });
}

//set the data- of 4 vital elements
MagnetoChat.setDataValue = function(listobj, chat_popup, canvas, txtarea, key, convid){
//of teh list object
listobj.attr(key, convid);
//of the popup
chat_popup.attr(key, convid);
//of the canvas
canvas.attr(key, convid);
//of the textarea
txtarea.attr(key, convid);
}

//set on/offline status
MagnetoChat.setOnlineStatus = function(listobj, thestatus){
if(thestatus==’on’){
//remove ofline mark
listobj.removeClass(“off”);
listobj.find(“.chat-user-status”).find(“span”).removeClass(“offline”).removeClass(“online”).addClass(“online”);
if(!MagnetoChat.isset(listobj.attr(“data-moved”))){
listobj.prependTo(listobj.parent());
listobj.attr(“data-moved”, true);
}
}
else{
listobj.find(“.chat-user-status”).find(“span”).removeClass(“offline”).removeClass(“online”).addClass(“offline”);
}
}

MagnetoChat.initSearchBox = function(){
// Textbox inside the chat list
$(“.txtSearchUserChatList”).unbind(“keyup”);
$(“.txtSearchUserChatList”).keyup(function(){
var search = $(this).val().toLowerCase();
var search_orig = $(this).val();
setTimeout(function(){
var result = $.grep(MagnetoChat.chat_index, function(el) {
return el.keyword.toLowerCase().indexOf(search) > -1;
});
if(!MagnetoChat.isset(search_orig)){
$(“.searchfor”).addClass(“hide”);
}
else{
$(“.searchfor”).removeClass(“hide”);
$(“.searchfor”).find(“.searchfor_key”).html(search_orig);
var href = $(“.searchfor”).find(“a”).attr(“data-href”);
$(“.searchfor”).find(“a”).attr(“href”, href+escape(search));
}
$(“.chat-user-list”).addClass(“hide”);
$(“.chat-user-list-widget”).addClass(“hide”);
for(key in result){
$(“.chat_index_”+result[key].index).removeClass(“hide”);
}
},200);

});
}
//fill the chat list class: .chat_list
MagnetoChat.fillChatList = function(id){
// if(!MagnetoChat.isset(GlobalJS.isMobile)){
// MagnetoChat.fillChatListWidget(function(){
// MagnetoChat.fillChatListFull(“”, “”, function(){
// clearTimeout(MagnetoChat.fetcherTimeout);
// MagnetoChat.fetcherTimeout = setTimeout(function(){
// MagnetoChat.fetcher();
// }, 5000);
// });
// });
// }
// else{
// MagnetoChat.fillChatListFull(“”, true, function(){
// clearTimeout(MagnetoChat.fetcherTimeout);
// MagnetoChat.fetcherTimeout = setTimeout(function(){
// MagnetoChat.fetcher();
// }, 5000);
// });
// }
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 2000);
}
MagnetoChat.fullChatIntervalFunc = function(){
if($(window).width()>=768){
$(“#chat-in-full .chat-user-list-sidebar”).removeClass(“hide”);
$(“#chat-in-full .chat-user-messages”).removeClass(“hide”);
}
else{

if($(“.chat-user-list”).length0){
$(“.chat-user-list-sidebar”).removeClass(“hide”);
if(!MagnetoChat.isset(MagnetoChat.fullID)){
if($(window).width()>=768){
$(“.chat-user-list”).first().trigger(“click”);
}
}
else{
if($(“#chat-conv-“+MagnetoChat.fullID).attr(“data-id”)){
$(“#chat-conv-“+MagnetoChat.fullID).trigger(“click”);
MagnetoChat.fullID = “”;
}
else{
if($(window).width()>=768){
$(“.chat-user-list”).first().trigger(“click”);
}
}
}
}
else{
$(“.chat-user-messages”).find(“.loading”).hide();
$(“.chat-user-messages”).find(“.searchhere”).removeClass(“hide”);
}
}
for(x in MagnetoChat.afterLoadFunctions){
MagnetoChat.afterLoadFunctions[x]();
MagnetoChat.afterLoadFunctions[x] = function(){};
}
MagnetoChat.initSearchBox();
GlobalJS.setHrefs($(“.chat_list”));
if(MagnetoChat.isset(callback)){
callback();
}
}
});
}
else{
if(MagnetoChat.isset(callback)){
callback();
}
}
}
//fill the chat list of widget. class: .chat_list_widget
MagnetoChat.fillChatListWidget = function(callback){
$.ajax({
// url: MagnetoChat.baseURI+’chat_list_widget’,
url: MagnetoChat.baseURI+’chat_list_widget?site_user_id=’ + ”,
xhrFields: {
withCredentials: false
},
success: function(html){
MagnetoChat.chat_index = [];
$(“.chat_list_widget”).html(html);
$(“.chat-user-list-widget”).unbind(“click”);
$(“.chat-user-list-widget”).click(function(){
var id = $(this).attr(“data-id”);
var name = $(this).attr(“data-name”);
var img = $(this).attr(“data-img”);
var convid = $(this).attr(“data-convid”);
var toids = $(this).attr(“data-toids”);
var listobj = $(this);
MagnetoChat.registerPopup(id, name, img, convid, toids, listobj);
});
MagnetoChat.initSearchBox();
GlobalJS.setHrefs($(“.chat_list_widget”));
if(MagnetoChat.isset(callback)){
callback();
}
}
});
}
MagnetoChat.heightChatConversationDisplay = function(){

var height_of_chatbox = $(“#chat-message-full-view”).outerHeight(true);
if($(“#chat-message-full-view .chat-user-messages”).find(“#chat-message-full-view .chat-write-message-header”).length > 0){
var height_of_header = $(“#chat-message-full-view .chat-write-message-header”).outerHeight(true);
}
else {
var height_of_header = $(“#chat-message-full-view .chat-user-messages-header”).outerHeight(true);
}
var height_of_message_box = $(“#chat-message-full-view .reply-box”).parent(“div”).outerHeight(true);
var height_of_conversation = parseInt(height_of_chatbox) – (parseInt(height_of_header) + parseInt(height_of_message_box));

$(“#chat-message-full-view .conversation-list”).css(“height”,height_of_conversation+”px”);
$(“#chat-in-full .conversation-message-box”).show();

}
MagnetoChat.setupHeightForChatFullViewContainers = function(){
// Returns height of browser viewport
var height_of_browser = $(window).height();
if( $(‘.page-header’).length ){
var height_of_navbar = $(“.page-header”).innerHeight();
}
else {
var height_of_navbar = $(“.jg-page-header”).innerHeight();
}

var height_of_footer = $(“.page-prefooter”).innerHeight();
var height_of_chatbox = parseInt(height_of_browser) – parseInt(height_of_navbar);

$(“#chat-message-full-view”).height(height_of_chatbox);
$(“#chat-message-full-view .chat-user-list-sidebar”).height(height_of_chatbox);
$(“#chat-message-full-view .chat-user-messages”).height(height_of_chatbox);
}

MagnetoChat.resizeWindowChatFullView = function() {
MagnetoChat.setupHeightForChatFullViewContainers();
setTimeout(function(){
MagnetoChat.heightChatConversationDisplay();
},500);
}

MagnetoChat.hideShowWidget = function(){
//not in chat page
if(!MagnetoChat.isset($(“.chat_list”)[0])){
$(“#chat-container”).show();
}
else{
//hide the widget
$(“#chat-container”).hide();
}
setTimeout(function(){
MagnetoChat.hideShowWidget();
}, 100);
}
//INIT FUNCTIONS
MagnetoChat.widget = function(){
if(parseInt(GlobalJS.UserId)>0){
var widget_id = “chat-widget-“+MagnetoChat.randomID();
/*
$(window).unload(function() {
alert(“1″);
});
*/
$.ajax({
// url: MagnetoChat.baseURI+’widget’,
url: MagnetoChat.baseURI+’widget?site_user_id=’ + ”,
xhrFields: {
withCredentials: false
},
success: function(res){
var chatopen = false;
if($(“#chat-container”).attr(“data-open”)==”1″){
chatopen = true;
}
$(“.magneto-chat-widget-container”).remove(); //remove any existing
MagnetoChat.widget_id = widget_id;
$(“body”).prepend(“”);
$(“#”+widget_id).html(res);
$(“#”+widget_id).fadeIn(100);
if(chatopen){
$(“#chat-container”).addClass(“openToggle”);
$(“#chat-container”).attr(“data-open”, “1”);
}
GlobalJS.setHrefs($(“#chat-container”));
MagnetoChat.initSearchBox();
clearTimeout(MagnetoChat.fetcherTimeout);
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 1000);
}
});
}
else{
$.ajax({
// url: MagnetoChat.baseURI+’widget’,
url: MagnetoChat.baseURI+’widget?site_user_id=’ + ”,
xhrFields: {
withCredentials: false
},
success: function(res){
var chatopen = false;
if($(“#chat-container”).attr(“data-open”)==”1″){
chatopen = true;
}
$(“.magneto-chat-widget-container”).remove(); //remove any existing
MagnetoChat.widget_id = widget_id;
$(“body”).prepend(“”);
$(“#”+widget_id).html(res);
$(“#”+widget_id).fadeIn(100);
if(chatopen){
$(“#chat-container”).addClass(“openToggle”);
$(“#chat-container”).attr(“data-open”, “1”);
}
GlobalJS.setHrefs($(“#chat-container”));
}
});
}
}

MagnetoChat.init = function(){
clearTimeout(MagnetoChat.fetcherTimeout);
MagnetoChat.fetcherTimeout = setTimeout(function(){
MagnetoChat.fetcher();
}, 1000);
}

//recalculate when window is loaded and also when window is resized.
window.addEventListener(“resize”, MagnetoChat.calculate_popups);
window.addEventListener(“resize”, MagnetoChat.resizeWindowChatFullView);
window.addEventListener(“load”, MagnetoChat.calculate_popups);

“;
$(“.ui-autocomplete”).append(“

  • “+footer+”
  • “);
    }
    else {
    var no_result_found = “”;
    response([{ label: no_result_found, val: -1}]);
    }
    }
    });
    },
    search: function(){
    $(this).addClass(“header-search-autocomplete-loading”);
    $(“.ui-autocomplete”).remove(“li.autocomplete-response-text”);
    },
    minLength: 1,
    select: function( event, ui ) {
    self.location = ui.item.link
    },
    open: function(event, ui){
    $(‘.ui-autocomplete’).css(‘width’, “310px”);
    $(‘.ui-autocomplete’).css(‘height’, ‘300px’);
    $(‘.ui-autocomplete’).css(‘overflow-y’, ‘scroll’);
    $(‘.ui-autocomplete’).css(‘z-index’, ‘10000’);
    $(this).removeClass(“header-search-autocomplete-loading”);
    }
    })
    .data(“ui-autocomplete”)._renderItem = function (ul, item) {
    var sub = “”;
    var html = “”;
    if (item.category) {

    var li;
    if ( item.category != currentCategory ) {
    ul.append( “

  • ” );
    currentCategory = item.category;
    }

    }

    if(item.icon) {
    var html = ‘‘;
    html += ‘Generic placeholder image‘;
    html += ‘
    ‘;
    }

    html += ‘

    ‘;
    html += ‘

    ‘+item.label+’

    ‘;
    if(item.location){
    html += ‘

    ‘+item.location+’

    ‘;
    }
    html += ‘

    ‘;
    return $(“

  • “)
    .data(“ui-autocomplete-item”, item)
    .append(html)
    .appendTo($(ul).addClass(“navbar-search-list”));
    };

    $(“.new-mobile-header-search”).autocomplete({
    source: function(request, response) {
    $.ajax({
    url: “https://e27.co/index/main_search_new”,
    //url: “http://e27beta.co/index/main_search_new”,
    //url: “http://e27beta.co/_html/header_search.json”,
    dataType: “json”,
    data: {
    q : request.term
    },
    success: function(data) {
    //console.log(“search”);
    $(“#statssaver”).attr(“src”, “https://e27.co/stats/log”);
    try{
    ga(‘send’, ‘pageview’, “/index/main_search_new?q=”+searchq);
    }
    catch(e){
    }
    if(data.length > 0){
    response(data);
    var footer = “”;
    $(“.ui-autocomplete”).append(“

  • “+footer+”
  • “);
    }
    else {
    var no_result_found = “”;
    response([{ label: no_result_found, val: -1}]);
    }
    }
    });
    },
    search: function(){
    $(this).addClass(“header-search-autocomplete-loading”);
    $(“.ui-autocomplete”).remove(“li.autocomplete-response-text”);
    },
    minLength: 1,
    select: function( event, ui ) {
    self.location = ui.item.link
    },
    open: function(event, ui){
    $(‘.ui-autocomplete’).css(‘width’, “100%”);
    $(‘.ui-autocomplete’).css(‘height’, ‘300px’);
    $(‘.ui-autocomplete’).css(‘overflow-y’, ‘scroll’);
    $(‘.ui-autocomplete’).css(‘z-index’, ‘10000’);
    $(this).removeClass(“header-search-autocomplete-loading”);
    }
    })
    .data(“ui-autocomplete”)._renderItem = function (ul, item) {
    var sub = “”;
    var html = “”;
    if (item.category) {

    var li;
    if ( item.category != currentCategory ) {
    ul.append( “

  • ” );
    currentCategory = item.category;
    }

    }

    if(item.icon) {
    var html = ‘‘;
    html += ‘Generic placeholder image‘;
    html += ‘
    ‘;
    }

    html += ‘

    ‘;
    html += ‘

    ‘+item.label+’

    ‘;
    if(item.location){
    html += ‘

    ‘+item.location+’

    ‘;
    }
    html += ‘

    ‘;
    return $(“

  • “)
    .data(“ui-autocomplete-item”, item)
    .append(html)
    .appendTo($(ul).addClass(“navbar-search-list”));
    };
    });
    GlobalJS.onLoad(function(){
    $(‘.navbar-mobile > .dropdown > .dropdown-menu’).click(function(e){
    e.stopPropagation();
    });
    $(‘.navbar-click a’).click(function(e){
    var ulElem = $(this).parent().find(‘ul’);
    if (ulElem.hasClass(‘navbar-open’)){
    ulElem.removeClass(‘navbar-open’).addClass(‘navbar-close’);
    }
    else{
    ulElem.removeClass(‘navbar-close’).addClass(‘navbar-open’);
    }
    });
    });

    function viewAnnoucement(){
    }

    <!–

    –>

  • Comments

    Write a Reply or Comment:

    Your email address will not be published.*