var lastID = 1; //Internet Explorer doesn't support "const" //CONSTANTS: ... var channelTimerDelay = 30000; var chatTimerDelayUpper = 9000; var chatTimerDelayLower = 2000; var chatTimerDelayDefault = 5000; var chatTimerDelayWindowHidden = 90000; var chatTimerDelay = chatTimerDelayDefault; var isChatWindowVisible = true; var skipNextGetChat = false; var chatBuffer = new Array(); getChatTimer(); function getChatTimer() { setTimeout("getChatTimer()", chatTimerDelay); if (isChatWindowVisible) chatTimerDelay = Math.min(chatTimerDelay + 300, chatTimerDelayUpper); if (skipNextGetChat) { skipNextGetChat = false; return; } getChat(); //addChatMessage("Debug", "Chat timer called!"); } var pollChannelListTimer = setInterval(function () {pollChannelList()}, channelTimerDelay); var firstGetChat = true; function getChatDone(data) { var newChats = false; ////console.log('datalength', data.length); if (data.length < 3 || data == 'false') return; //console.log("START BUILD"); //console.log("JSON TO PARSE", data); json = jQuery.parseJSON(data); //console.log("JSON PREPED"); $.each(json, function(key, chat) { var postDate = new Date(); postDate.setTime(postDate.getTime() + chat.secondsSince * 1000); if (!chat.message) return; var isSelf = (chat.userID == userObj.ID); var isServer = false; if (chat.userID == null || chat.userID <= 0) { isServer = true; chat.displayName = 'SERVER' } if (chat.isJoinLeave == 'true') { console.log('IsJoin Leave!') //return true; } //Message is legitimately new or a server message? if (chat.ID > lastID || typeof(chat.ID) == 'undefined') { addChatMessage(chat.displayName, chat.message, postDate, isSelf, isServer, chat.wallColor, chat.wallEmblem, chat.wallOrientation, chat.userID, chat.displayColor, chat.ID); newChats = true; } //console.log("INSIDE BUILD START"); if (chat.ID > 0) lastID = chat.ID; }); //console.log("BUILD DONE"); if (newChats && !firstGetChat) { if (isChatWindowVisible) chatTimerDelay = chatTimerDelayLower; if (chatIsMuted == 'false') { soundManager.setVolume('charm', 20); soundManager.setPan('charm', -60) soundManager.setPosition('charm',150); soundManager.play('charm'); } } firstGetChat = false; } function addChatMessage(displayName, message, postDate, isSelf, isServer, wallColor, wallEmblem, wallOrientation, userID, displayColor, chatID) { if(!postDate) postDate = new Date(); var timestamp = postDate.format("hh:MM:ss"); var timestampDetails = postDate.format("ddd h:MM TT"); var strClass = ''; if (isSelf) strClass += ' self'; if (isServer) strClass += ' server'; var usernameClass = ''; if (message.indexOf("/me ") == 0) { message = message.substring(4); usernameClass = ' me'; } var isSpoiler = false; if (message.indexOf("/spoiler ") == 0) { message = message.substring(9); isSpoiler = true; } if (!isSpoiler) { document.title = displayName + ': '+ message.substring(0, 20) + ' | Pathery Chat'; } else { document.title = displayName + ': ~Spoiler~ | Pathery Chat'; } var p = ''; p = p+ "
"; p = p+ " ["+timestamp+"]"; p = p+ "
"; p = p+ "
"; p = p+ "
"; p = p+ "
"; p = p+ "
"; p = p+ "
"; if (userID == null || userID <= 0) { p = p+ ""; } else { p = p+ ""; } p = p+ displayName+""; if (isSpoiler == true) p = p+ " "; else p = p+ " "; p = p+ chatReplaceAndEncode(message); p = p+ " "; p = p+ "
"; var chatContainer = $("#chatContainer"); var isAtBottom = (chatContainer.length == 1 && chatContainer.scrollTop() >= chatContainer[0].scrollHeight - chatContainer.outerHeight() - 1); chatContainer.append('
' + p + '
'); if (isAtBottom || firstGetChat) { chatContainer.scrollTop(chatContainer[0].scrollHeight); } } function chatReplaceAndEncode(chat) { chat = htmlEncode(chat); chat = chat.replace(/\*\*(\S(.*?\S)?)\*\*/gm, "$1"); chat = chat.replace(/\~\~(\S(.*?\S)?)\~\~/gm, "$1"); chat = chat.replace(/\*(\S(.*?\S)?)\*/gm, "$1"); chat = replaceSmileys(chat); //Surround all URLs with a link var URLexp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; chat = chat.replace(URLexp, "$1"); //Replace # with %23, & with %26, ? with %3F chat = chat.replace(/'); chat = chat.replace(/:\(|=\(/g, ''); chat = chat.replace(/\^\_\^/g, ''); chat = chat.replace(/\:d/gi, ''); chat = chat.replace(/\:o/gi, ''); chat = chat.replace(/\:p/gi, ''); chat = chat.replace(/;p/gi, ''); chat = chat.replace(/\:c/gi, ''); chat = chat.replace(/\:s/gi, ''); chat = chat.replace(/\>_\</g, ''); chat = chat.replace(/\$_\$/gi, ''); chat = chat.replace(/-\.-|-_-/gi, ''); chat = chat.replace(/o\.O|o\_O/g, ''); chat = chat.replace(/O\.o|O\_o/g, ''); chat = chat.replace(/O\.O|O\_O/g, ''); chat = chat.replace(/\:-\//g, ''); chat = chat.replace(/\(y\)/gi, ''); chat = chat.replace(/\(n\)/gi, ''); chat = chat.replace(/\<3/g, ''); return chat; } function prepChat(chat) { chat = chat.join('|:|'); chat = chat.replace(/\&/g,'%26') chat = chat.replace(/\+/g,'%2B') return chat; } var chatIsBusy = false; function getChat(message) { var dataString = 'getChatFromID='+lastID; var backup = new Array(); var fncComplete = ''; if (!chatIsBusy && chatBuffer.length > 0) { chatIsBusy = true; dataString += '&send=true&messages='+prepChat(chatBuffer); backup = chatBuffer.slice(0); chatBuffer.length = 0; fncComplete = function() {chatIsBusy = false;}; } $.ajax({ type: "POST", url: "ajax/chat.ajax.php", data: dataString, error: function() { chatBuffer = backup.concat(chatBuffer); //console.log('concated:', chatBuffer, backup); }, success: function(data) { getChatDone(data); //console.log('b:', backup); }, complete: fncComplete }); } function pollChannelList() { $.ajax({ //type: "POST", url: "ajax/chat.ajax.php?pollChannelList=1", error: function() { console.log('Error: Failed pollChannelList'); }, success: function(data) { console.log("ChannelPoll data recieved", data); pollChannelListDone(data); } }); } function pollChannelListDone(data) { console.log('data recieved:', data); if (data.length < 3 || data == 'false') { $("#channelContainer").html('Channel Empty'); return; } json = jQuery.parseJSON(data); console.log('Loading channel.'); var c = channelListShow(json) console.log(c); var b = $("#channelContainer"); b.html(c); } function channelListShow(JO) { console.log("Formating channelList"); var p = ""; console.log('beginloop'); var previousI = 0; for (var i in JO.users) { console.log('loop') var u = JO.users[i]; var styleClass = ''; if (previousI != i + 1) { if (previousI < i - 1 && previousI != 0) { styleClass = 'border-top: 6px solid #777799;'; } } // u.secFromLastActive could be used in here someplace.. var dateEntered = new Date(); dateEntered.setTime(dateEntered.getTime() + u.secFromEntered * 1000); if (u.wallEmblem == undefined) u.wallEmblem = 'blank.png'; p = p+ ""; p = p+ ""; previousI = i; } p = p+"
"; p = p+ "
"; p = p+ "
"; p = p+ "
"; p = p+ "
"; p = p+ " "+u.display+""; p = p+ "
"; return p; } function sendChat() { var message = $("input#message").val().replace("|:|", "||"); if (message == '') return false; chatBuffer.push(message); $("input#message").val(''); if (skipNextGetChat == false) { skipNextGetChat = true; } getChat(); return false; } function spoil(obj) { $(obj).removeClass("spoiler").hide().fadeIn(600); $(obj).children().removeAttr('onclick'); } $(document).ready(function() { $('#sendChat').live("submit", function() { sendChat() }); //Prevent clicking on links in spoilers $(document).on('click', '.spoiler > a', function() { addChatMessage("Debug", "Child called"); }); }); function htmlEncode(value){ if (value) { return jQuery('
').text(value).html(); } else { return ''; } } function doNothingWhenClickingLinks(self) { return !$(self).closest('.spoiler').length; } //Code for checking if the window is currently visible or not //Adapted from http://stackoverflow.com/a/1060034/238419 $(function() { var hidden = "hidden"; // Standards: if (hidden in document) document.addEventListener("visibilitychange", onchange); else if ((hidden = "mozHidden") in document) document.addEventListener("mozvisibilitychange", onchange); else if ((hidden = "webkitHidden") in document) document.addEventListener("webkitvisibilitychange", onchange); else if ((hidden = "msHidden") in document) document.addEventListener("msvisibilitychange", onchange); // IE 9 and lower: else if ('onfocusin' in document) document.onfocusin = document.onfocusout = onchange; // All others: else window.onpageshow = window.onpagehide = window.onfocus = window.onblur = onchange; function onchange (evt) { var eventMapIsWindowShown = { focus:true, focusin:true, pageshow:true, blur:false, focusout:false, pagehide:false }; evt = evt || window.event; if (evt.type in eventMapIsWindowShown) isChatWindowVisible = eventMapIsWindowShown[evt.type]; else isChatWindowVisible = !this[hidden]; if(isChatWindowVisible) onChatWindowShown(); else onChatWindowHidden(); } }); function onChatWindowShown() { chatTimerDelay = chatTimerDelayDefault; getChat(); } function onChatWindowHidden() { chatTimerDelay = chatTimerDelayWindowHidden; } function setChatMute() { var mutePref = getCookie('pref_chatMute'); $('#chatMute').removeClass("chatMute_"+mutePref); if (mutePref == 'true') { mutePref = 'false'; soundManager.setVolume('pit', 20); soundManager.setPan('pit', -60) soundManager.play('pit'); } else { mutePref = 'true'; } chatIsMuted = mutePref; $('#chatMute').addClass("chatMute_"+mutePref); savePref('chatMute', mutePref); } pollChannelList();