var WEBSITE_URL="http://cewt.co/?id=";var TOPIC_PROMPT="-- Type your chat title here --";Ajax.Request.prototype.abort=function(){this.transport.onreadystatechange=Prototype.emptyFunction;this.transport.abort();Ajax.activeRequestCount--};function createCookie(c,d,e){if(e){var b=new Date();b.setTime(b.getTime()+(e*24*60*60*1000));var a="; expires="+b.toGMTString()}else{var a=""}document.cookie=c+"="+d+a+"; path=/"}function getCookie(b){var e=document.cookie.split(";");var d;for(var c=0;c<e.length;c++){var f=e[c].indexOf("=");var a=e[c].substr(0,f);while(a.charAt(0)==" "){a=a.substr(1)}if(b==a&&e[c].length>f){return e[c].substr(f+1)}}return null}var ChatRequest=Class.create({initialize:function(c,b,a,d){this.command="START";this.topic=c;this.persona=b;this.invite=a;this.category=d}});function startChat(){var c=chatWithFriend?"INVITE":$("theirPersona").value;var b=$("myPersona").value;if($("useTwitterPersona").checked){b="tw:"+twitterUsername}var d=$("category").value;var a=new ChatRequest($("newTopicTitle").value,b,c,d);new Ajax.Request("Dispatch",{method:"post",postBody:Object.toJSON(a),onSuccess:function(e){chatStarted(e.responseJSON)}})}var JoinRequest=Class.create({initialize:function(b,a){this.command="JOIN";this.id=b;this.persona=a}});function joinChat(a){var b=a.id.substring("join_".length);joinChatById(b)}function joinChatById(b){var a=new JoinRequest(b,$("myPersona").value);new Ajax.Request("Dispatch",{method:"post",postBody:Object.toJSON(a),onSuccess:function(c){chatStarted(c.responseJSON)}})}function chatStarted(a){$("chatBody").innerHTML="";if(a.status=="OK"){$("publicChat","scrollToggle","startArea").invoke("hide");$("privateChat","chatArea").invoke("show");newChatLoaded(a.topic,a.publicId,a.privateId,a.participant)}else{if(a.status=="LATE"){$("publicChat","scrollToggle","chatArea").invoke("show");$("privateChat","startArea").invoke("hide");newChatLoaded(a.topic,a.publicId)}else{alert(a.status)}}}function leaveChat(){if(currentChatId){new Ajax.Request("Dispatch",{method:"post",postBody:'{"command":"LEAVE","id":"'+currentChatId+'","participant":"'+currentParticipant+'"}'});currentChatId=null;$("newChatDiv").show()}}var publicChatId;var lastLine;var currentChatId;var currentParticipant;var currentTopic;var chatState;function newChatLoaded(c,b,e,f){if(currentChatId){new Ajax.Request("Dispatch",{method:"post",postBody:'{"command":"LEAVE","id":"'+currentChatId+'","participant":"'+currentParticipant+'"}'})}currentChatId=e;currentParticipant=f;currentTopic=c;chatState=null;if(e){$("newChatDiv").hide()}else{$("newChatDiv").show()}publicChatId=b;lastLine=0;liveDelay=0;personas={};var h=$("chatTitle");if(h&&c){h.innerHTML=c}var g=$("tweetBtn");if(g){g.observe("click",showTweetbox)}var a=$("good");if(a){a.observe("click",respond)}a=$("bad");if(a){a.observe("click",respond)}parseWaiting();parseLive();var d=$("chatEdit");if(d){if(f==1){d.disabled=true;d.style.backgroundColor=8421504;d.value="Waiting for other user.."}d.observe("keydown",editKeyDown.bindAsEventListener())}var i=$("linkInput");if(i){i.value=WEBSITE_URL+b}requestPoll()}var personas;function getPersonaStyle(a){if(a&&personas){var b=0;while(personas[b]){if(personas[b++]==a){return"persona"+b}}personas[b++]=a;return"persona"+b}}function editKeyDown(a){if(a.keyCode==13){var b=$("chatEdit");new Ajax.Request("Chat",{method:"post",postBody:currentChatId+"\n"+currentParticipant+"\n"+b.value});b.value="";a.stop()}}var pollRequest;var adjust=0;function respond(a){if(this.id=="good"){this.src="img/thumbs_up.png";adjust=911832355}else{this.src="img/thumbs_down.png";adjust=2238006629}setScoreText("Your vote has been sent");if(chatState!="LIVE"){$("good").style.display="none";$("bad").style.display="none"}}function requestPoll(){if(pollRequest){lastPoll=0;pollRequest.abort()}var c=getCookie("cewt");var a=$("category").value;var b=lastPoll;if(typeof(c)!="undefined"&&typeof(liveDelay)!="undefined"){b=((liveDelay*13)+parseInt(c))&4294967295;b=((b*31)+lastPoll)&4294967295;b^=adjust;adjust=0}pollRequest=new Ajax.Request("Poll",{method:"get",parameters:{since:lastPoll,id:publicChatId,lastLine:lastLine,category:a,run:b},onSuccess:function(d){pollRequest=undefined;parsePoll(d.responseJSON)},onFailure:function(){setTimeout("requestPoll()",5000)}})}var lastPoll;function parsePoll(a){pollRequest=null;if(a.until){lastPoll=a.until}if(a.waiting){waitingPoll=a.waiting;parseWaiting()}if(a.live){livePoll=a.live;parseLive()}if(a.topChats){parseChatList("topChats",a.topChats)}var b=$("chatBody");if(a.id==publicChatId){if(a.lines&&b){parseLines(a.lines,b)}if(a.chat){parseChatStatus(a.chat)}}if(a.status=="OK"){requestPoll()}}var waitingPoll;function parseChatStatus(b){var d=$("chatEdit");if(chatState!=b.status){chatState=b.status;if(d){if(b.status=="LIVE"){d.disabled=false;d.style.backgroundColor=16777215;d.value=""}else{if(b.status=="FINISHED"){d.disabled=true;d.style.backgroundColor=12632256}}}if(b.status=="FINISHED"){$("newChatDiv").show()}}var a=$("chatVisitors");var c=$("chatAudience");if(a){a.innerHTML=b.visits}if(c){c.style.width=b.audience<10?b.audience*10:b.audience<50?60+b.audience*4:b.audience<290?235+b.audience/2:380;c.innerHTML=b.audience}currentRating=b.rating;displayRating()}var currentRating;function displayRating(){var c=$("chatRating");var a=$("chatScore");if(typeof(c)=="undefined"||typeof(a)=="undefined"){return}if(currentRating==0){setScoreText("meh")}else{if(currentRating>0){var b=currentRating<25?currentRating*5:125;a.innerHTML=currentRating;a.style.left=(255+b)+"px";c.style.backgroundColor="#d0ffd0";c.style.border="1px solid #808080";c.style.left="250px";c.style.width=b+"px"}else{var b=currentRating>-25?currentRating*5:-125;a.innerHTML=-currentRating;a.style.left=(245+b-a.clientWidth)+"px";c.style.backgroundColor="#ffd0d0";c.style.border="1px solid #808080";c.style.left=(250+b)+"px";c.style.width=(-b)+"px"}}}function setScoreText(b){var c=$("chatRating");var a=$("chatScore");a.style.left="60px";a.innerHTML=b;a.style.left=(250-a.clientWidth/2)+"px";c.style.border="0px";c.style.left="220px";c.style.width="100px";c.style.backgroundColor="#f0f0ff"}var liveDelay;function parseLines(d,g){for(var e=0;e<d.length;e++){var c=d[e];if(c.persona){var b="";var f=getPersonaStyle(c.persona);if(f){b+='<span class="'+f+'">'}b+=c.persona;if(f){b+="</span>"}b+=" : "+c.text+"<br/>";g.innerHTML+=b}else{g.innerHTML+=c.text+"<br/>"}liveDelay=((liveDelay*31)+c.id)&4294967295}var a=$("disableScroll");if(a==null||!a.checked){g.scrollTop=g.scrollHeight}lastLine+=d.length;document.title="Cewt Chat ("+lastLine+")"}function parseWaiting(){if(!waitingPoll){return}var c="";waitingPoll.sort(function(f,e){return f.time-e.time});var d=0;for(var b=0;b<waitingPoll.length;b++){var a=waitingPoll[b];if(a.id!=publicChatId){d++;c+='<div class="list'+(d%2)+'">';c+='<div class="listInfo">Join ';if(a.who&&a.who!="Stranger"){c+=a.who+" "}if(a.invite&&a.invite!="Stranger"){c+="(as "+a.invite+") "}c+="</div>";c+='<a href="#" id="join_'+a.id+'" onclick="joinChat(this);">'+a.topic+"</a>";c+="</div>"}}$("waitingList").innerHTML=c}var livePoll;function parseLive(){if(!livePoll){return}var b="";livePoll.sort(function(f,e){return f.time-e.time});var d=0;for(var c=0;c<livePoll.length;c++){var a=livePoll[c];if(a.id!=publicChatId){d++;b+='<div class="list'+(d%2)+'">';b+='<a href="#" id="view_'+a.id+'" onclick="joinChat(this);">'+a.topic+"</a></div>"}}$("liveList").innerHTML=b}function parseChatList(f,d){var b="";var e=0;for(var c=0;c<d.length;c++){var a=d[c];if(a.id!=publicChatId){e++;b+='<div class="list'+(e%2)+'">';b+='<a href="#" id="view_'+a.id+'" onclick="joinChat(this);">'+a.topic+"</a><br/>";b+="</div>"}}$(f).innerHTML=b}var twitterUsername;var twitterId;function wireUpTwitter(){if(typeof(twttr)=="undefined"){return}twttr.anywhere(function(c){var a,d,e,b;if(c.isConnected()){a=c.currentUser;e=a.data("profile_image_url");$("twitterConnected").style.display="inline";$("twitterUser").innerHTML=a.data("screen_name")}else{$("twitterConnect").style.display="inline"}c("#twitterConnect").connectButton();c.bind("authComplete",function(m,f){twitterUsername=f.data("screen_name");$("twitterUser").innerHTML=twitterUsername;$("twitterConnected").style.display="inline";$("twitterConnect").style.display="none";$("useTwitterPersona").checked=true;var k=document.cookie.split(";");var j;for(var h=0;h<k.length;h++){var n=k[h].indexOf("=");var g=k[h].substr(0,n);while(g.charAt(0)==" "){g=g.substr(1)}var l=k[h].substr(n+1);if(g=="twitter_anywhere_identity"){createCookie(g,l.replace(/:/,"|"))}}});c.bind("signOut",function(f){$("twitterConnected").style.display="none";$("twitterConnect").style.display="inline";$("useTwitterPersona").checked=false})})}function signOutTwitter(){twttr.anywhere.signOut()}function showTweetbox(){if($("tweetBox").innerHTML!=""){$("tweetBox").innerHTML="";return}twttr.anywhere(function(a){var b="View the chat on #cewtchat ";if(currentParticipant==1){b="Join my chat on #cewtchat "}if(currentParticipant==2){b="View our chat on #cewtchat "}b+=WEBSITE_URL+publicChatId;b+=" - "+currentTopic;a("#tweetBox").tweetBox({height:44,width:400,label:"Tell the world",counter:false,defaultContent:b})})}function topicFocus(){var a=$("newTopicTitle");if(a.value==TOPIC_PROMPT){a.value="";a.style.color="#000000"}}function topicKeydown(b){var a=$("newTopicTitle");if(b.keyCode==13){startChat()}if(b.keyCode!=8&&a.value.length>35){b.stop()}}function topicKeyup(a){$("startChat").disabled=($("newTopicTitle").value.length<4)}var chatWithFriend;function selectWith(){chatWithFriend=(this.id=="withFriend");if(chatWithFriend){$("withFriend").className="withSelected";$("withStranger").className="withTitle"}else{$("withFriend").className="withTitle";$("withStranger").className="withSelected"}}function doOnLoad(){var a=$("startChat");if(a){a.observe("click",startChat)}var d=$("newTopicTitle");if(d){d.style.color="#606060";d.value=TOPIC_PROMPT;d.observe("focus",topicFocus);d.observe("keydown",topicKeydown);d.observe("keydown",topicKeyup)}$("withStranger").observe("click",selectWith);$("withFriend").observe("click",selectWith);$("theirPersona").observe("change",selectWith);var b=$("category");if(b){b.observe("change",requestPoll)}lastPoll=0;requestPoll();if(window.location.href&&window.location.href.indexOf("?id=")>0){var c=window.location.href.indexOf("?id=");var e=window.location.href.substring(c+4,c+16);$("joinChatDiv").show();$("joinChat").observe("click",function(){joinChatById(e)})}wireUpTwitter.defer()};
