var UserName = ""; document.domain = "buddypic.com"; var total_tabs = 0; var max_tab_size = "100px"; var tab_array = new Array(); var cur_chat_tab = null; //var bar = $('taskbar'); //var tab_s = $('tab_shell'); //loadTabs(); function loadTabs() { tabObj = readCookie("tabs"); if (tabObj == null) return; var z = JSON.parse(tabObj); if (is_object(z)) { for (var i=0; i< z["tabs"].length;i++) { addTab(z["tabs"][i]); } if (z["tabs"].indexOf(z["cur"]) > -1) { toggleTab(z["cur"]); } } } function toggleTaskBar() { } function reidElement(el, title) { if (el.id && el.id.substr(0,1) == "*") el.id = el.id.substr(1,el.id.length) + "_" + title; if (!el.childNodes) return false; var el = el.firstChild; while (el) { if (el.id && el.id.substr(0,1) == "*") { base = el.id.substr(1,el.id.length); el.id = base + "_" + title; if (base == "chat_min") { el.onclick = toggleDisplay; }else if (base == "chat_close") { el.onclick = removeTab; }else if (base == "chat_msg") { el.onkeydown = chat_msg_keydown; }else if (base == "chat_title") { el.innerHTML = title; } } if (el.childNodes) reidElement(el, title); el = el.nextSibling; } } function xhr_send(to,msg) { if (to == "" || msg == "") { return; } send_msg(to,msg); } function chat_msg_keydown(e) { if (!e) {var e = window.event;} if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; if (!e.currentTarget) { e.currentTarget = this;} if (code == 13) { msg = e.currentTarget.value; to = id2u(e.currentTarget.id); e.preventDefault? e.preventDefault() : e.returnValue = false; e.currentTarget.value = ""; xhr_send(to, msg); } } function preventDefault(e) { e.preventDefault? e.preventDefault() : e.returnValue = false; } function id2u(eid) { return eid.substr(eid.lastIndexOf("_")+1,eid.length); } function addTab(title) { tab_array.push(title); new_tab = document.createElement("td"); new_tab.id = "tab_" + title; new_tab.className = "tab"; new_tab.innerHTML = title; new_tab.onclick = toggleDisplay; tab_s.appendChild(new_tab); chat_win_table = document.createElement("table"); chat_win_table.id = "chat_" + title; chat_win_table.className = "chatwin"; chat_win_tr = document.createElement("tr"); chat_win_td = document.createElement("td"); chat_win_td.innerHTML = "hey!"; chat_win_table.appendChild(chat_win_tr); chat_win_tr.appendChild(chat_win_td); addClass(new_tab, "tab_unclicked"); new_chat_win = $("*chat").cloneNode(true); reidElement(new_chat_win,title); document.body.appendChild(new_chat_win); if (tab_array.length > 0) { $("tb_wrapper").style.display = "block"; } //toggleTab(title); } function removeTab(e) { if (!e) {var e = window.event;} if (!e.currentTarget) { e.currentTarget = this;} var eid = e.currentTarget.id; var title = eid.substr(eid.lastIndexOf("_")+1,eid.length); tab_array.splice(tab_array.indexOf(title),1); rtab = $("tab_" + title); node = tab_s.removeChild(rtab); document.body.removeChild($("chat_" + title)); if (tab_array.length == 0) { $("tb_wrapper").style.display = "none"; } updateTabCookie(); } function updateTabCookie() { var tabObj = Object(); tabObj["tabs"] = tab_array; tabObj["cur"] = cur_chat_tab; createCookie("tabs",JSON.stringify(tabObj),0); } /*function removeTab(title) { tab_array.splice(tab_array.indexOf("tab_" + title),1); rtab = $("tab_" + title); node = tab_s.removeChild(rtab); document.body.removeChild($("chat_" + title)); }*/ function toggleDisplay(e) { if (!e) {var e = window.event;} if (!e.currentTarget) { e.currentTarget = this;} var eid = e.currentTarget.id; var tab = eid.substr(eid.lastIndexOf("_")+1,eid.length); toggleTab(e.currentTarget.id); removeClass(e.currentTarget, "tab_unread"); if (hasClass(e.currentTarget,"tab_clicked")) { cur_chat_tab = tab; }else { cur_chat_tab = null; } updateTabCookie(); } function toggleTab(what) { what = what.split("_"); if (what.length > 1) { what = what[1]; }else {what=what[0];} what_tab = $("tab_" + what); cur_chat = $("chat_" + what); for(i=0;i document.body.clientWidth) { //$("chat_" + tab_array[i]).style.right = findLeft(what_tab) + "px"; $("chat_" + tab_array[i]).style.left = findLeft(what_tab) - $("chat_" + tab_array[i]).offsetWidth + what_tab.offsetWidth + "px"; }else{ $("chat_" + tab_array[i]).style.left = findLeft(what_tab) + "px"; } var chatbox = $('chat_convo_' + what); chatbox.scrollTop = chatbox.scrollHeight; $("chat_msg_" + what).focus(); } } } window.onresize = win_resize; function win_resize(evt) { if (cur_chat_tab == null) {return; } cur_chat.style.bottom = findBottom($('taskbar')) + "px"; cur_chat.style.left = findLeft(what_tab) + "px"; leftval = findLeft(what_tab); cur_chat.style.display = "block"; if (leftval + cur_chat.offsetWidth > document.body.clientWidth) { cur_chat.style.left = findLeft(what_tab) - cur_chat.offsetWidth + what_tab.offsetWidth + "px"; }else{ cur_chat.style.left = findLeft(what_tab) + "px"; } } function $(what) { return document.getElementById(what); } if(!Array.indexOf){ Array.prototype.indexOf = function(obj){ for(var i=0; i=0; i--){ if(this[i]==obj){ return i; } } return -1; } } function findLeft(obj) { if( !obj ) return 0; return obj.offsetLeft + findLeft( obj.offsetParent ); } function findTop(obj) { if( !obj ) return 0; return obj.offsetTop + findTop( obj.offestParent ); } function findBottom(obj) { if( !obj ) return 0; return obj.offsetHeight + findBottom( obj.offestParent ); } function hasClass(ele,cls) { return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); } function addClass(ele,cls) { if (!this.hasClass(ele,cls)) ele.className += " "+cls; } function removeClass(ele,cls) { if (hasClass(ele,cls)) { var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); ele.className=ele.className.replace(reg,' '); } } function swapClass(ele, cls, cls2) { if (hasClass(ele,cls)) { var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); ele.className=ele.className.replace(reg,' ' + cls2); } } function maxLength(e,maxChars) { var code; if (!e) var e = window.event; if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; if (!e.currentTarget) { e.currentTarget = this;} var valid_keys = Array(37,38,38,39,40,8,46,36,35,45); if (valid_keys.indexOf(code) == -1) { if (e.currentTarget.value.length >= maxChars) { e.preventDefault? e.preventDefault() : e.returnValue = false; //e.preventDefault(); //e.returnValue=false; } } } function xhr_send2() { d = document; f = d.frames ? d.frames['xhr'] : d.getElementById('xhr'); p = f.document || f.contentWindow.document; //from = gup("nick"); towho = document.getElementById("to").value; what = document.getElementById("msg").value; if (towho != "" && what != "") { send_msg(from,towho,what); $("msg").value= ""; } } function addIM_Msg(window, from, msg) { if (tab_array.indexOf(window) == -1) { addTab(window); } if (from != UserName) { playIMSound(); } var chatbox = $('chat_convo_' + window); senderclass = (from == UserName) ? "im_sender_me" : "im_sender_other"; chatbox.innerHTML = chatbox.innerHTML + "
" + from + ": " + msg + "
"; chatbox.scrollTop = chatbox.scrollHeight; var tab = $('tab_' + window); if (hasClass(tab, "tab_unclicked")) { addClass(tab, "tab_unread"); } } function addIM_Notice(window, msg) { var chatbox = $('chat_convo_' + window); chatbox.innerHTML = chatbox.innerHTML + "
" + msg + "
"; chatbox.scrollTop = chatbox.scrollHeight; } function send_msg(to,what) { d = document; f = d.frames ? d.frames['xhr'] : d.getElementById('xhr'); p = f.document || f.contentWindow.document; req = document.getElementById('xhr').contentWindow.req; url= "http://chat.buddypic.com:8001/send?to=" + to + "&msg="+ escape(what); req.open("GET", url += (url.match(/\?/) == null ? "?" : "&") + (new Date()).getTime(), true); req.onreadystatechange = function (aEvt) { if (req.readyState == 4) { if(req.status == 200) { //chatDiv = document.getElementById("chat"); //chatDiv.innerHTML = chatDiv.innerHTML + req.responseText; //dump(req.responseText); addIM_Msg(to, UserName, what); res = JSON.parse(req.responseText); if (is_object(res)) { for (key in res) { switch(key) { case "msgok": break; case "msgbad_offline": addIM_Notice(to, to + " is offline. Your msg: \"" + res[key][1] + "\" was not received.") break; default: break; } } } }else { alert("error sending! :P"); } } } req.send(null); } var req_buffer = ""; function poll_simple() { req_poll = new_XHR(); url= "http://chat.buddypic.com:8001/poll"; req_poll.open("GET", url += (url.match(/\?/) == null ? "?" : "&") + (new Date()).getTime(), true); req_poll.onreadystatechange = function (aEvt) { if (req_poll.readyState == 4) { if(req_poll.status == 200) { dump("4: " + req_poll.responseText); }else { dump("error!"); } poll(); } } req_poll.send(null); } function poll() { var req_poll = new_XHR(); url= "http://chat.buddypic.com:8001/poll"; req_poll.open("GET", url += (url.match(/\?/) == null ? "?" : "&") + (new Date()).getTime(), true); req_poll.onreadystatechange = function (aEvt) { /*if (req_poll.readyState == 3) { if(req_poll.status == 200) { //dump("3: " + req_poll.responseText.substring(req_buffer.length, req_poll.responseText.length)); //req_buffer = req_poll.responseText; } }*/ if (req_poll.readyState == 4) { if(req_poll.status == 200) { response = req_poll.responseText; dump("4: " + response); res = JSON.parse(response); if (is_object(res)) { for (key in res) { switch(key) { case "im": //loop through object of {from:[msg1,msg2]} for (from in res[key]) { for (var i =res[key][from].length-1; i>= 0; i--) { addIM_Msg(from, from, res[key][from][i]); } } break; case "msgbad_offline": addIM_Notice(to, to + " is offline. Your msg: \"" + res[key][1] + "\" was not received.") break; case "notice": for (from in res[key]) { for (var i =res[key][from].length-1; i>= 0; i--) { addIM_Notice(from, from + " is offline. Your msg: \"" + res[key][from][i] + "\" was not received.") } } break; default: break; } } }else { alert('no obj'); } if (response != "bad") { poll();} }else { dump("error!"); } req_buffer = ""; //if (response != "bad") { poll();} } } req_poll.send(null); } window.onload = function() { //poll(); } function dump(what) { chatDiv = document.getElementById("dump"); chatDiv.innerHTML = chatDiv.innerHTML + what + "
"; chatDiv.scrollTop = chatDiv.scrollHeight; } function is_object( mixed_var ){ // http://kevin.vanzonneveld.net // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Legaev Andrey // + improved by: Michael White (http://getsprink.com) // * example 1: is_object('23'); // * returns 1: false // * example 2: is_object({foo: 'bar'}); // * returns 2: true // * example 3: is_object(null); // * returns 3: false if(mixed_var instanceof Array) { return false; } else { return (mixed_var !== null) && (typeof( mixed_var ) == 'object'); } } function new_XHR() { return document.getElementById('xhr').contentWindow.createXMLHttpRequest(); } function xhr_Ready() { poll(); } function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { createCookie(name,"",-1); } function playIMSound() { sound=document.getElementById("im_sound"); //if(sound) { sound.Play();} }