summaryrefslogtreecommitdiffstats
path: root/pages/chat.php
blob: 11bb9350f37be6f288ced2e382619d9d476a6776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
htmlHeader(
	array('stats'), 'Chat', 
	'BetaChat for Pathery.com', 
	array('scores', 'dateformat')
);
?>
<body>
<?php
topbar($Links);

?>

<style>
.chatMessage {
	width:99%;
	height:40px;
	margin:5px;
	background-color:#222;
	display:block;
	white-space:nowrap;
	overflow-x:hidden;
}
.chatTimestamp {
	font-family:"Times New Roman", Times, serif;


	float:left;
	padding: 5px 3px 3px 3px;
	min-width:70px;
	overflow:hidden;
	text-overflow: ellipsis;
	white-space:nowrap;
}
.chatUsername {
	font-family:Comic Sans MS, Comic Sans MS5, cursive;
	float:left;
	padding: 5px 0px 3px 3px;
	min-width:60px;
	overflow:hidden;
	text-overflow: ellipsis;
	white-space:nowrap;
}
.chatText {
	font-family:Comic Sans MS, Comic Sans MS5, cursive;
	width:99%;
	float:left;
	padding: 5px 0px 3px 3px;
	width:330px;

}

#chatContainer {
	border:1px solid red; 
	width:600px;
	margin:0 auto;
	height:400px;
	overflow:auto;
}
.chatContainer2 {
	border:1px solid yellow; 
	width:610px;
	margin:0 auto;
	height:450px;
	overflow:auto;
}



</style>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script>
var lastID = 1;
var chatTimerDelay = 5000;

var skipNextGetChat = false;

var chatBuffer = new Array();

getChatTimer();
function getChatTimer() {
	setTimeout("getChatTimer()", chatTimerDelay);
	if (skipNextGetChat) {
		skipNextGetChat = false;
		return;
	}
	getChat();
}


getChat();
function getChatDone(data) {
	var items = [];
	var p; //our prep string
	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, user) {
	
		var postDate = new Date();
		postDate.setTime(postDate.getTime() + user.secondsSince * 1000);
		//var timestamp = postDate.format("ddd h:MM TT");
		var timestamp = postDate.format("h:MM:ss");
	
		console.log("INSIDE BUILD START");
		p = '';
		p = p+ " <span class='chatTimestamp'>["+timestamp+"]</span>";
		p = p+ "	<div class='grid_td' style='float:left; width:35px; height:35px; background:"+user.wallColor+" url(images/marks/"+user.wallEmblem+");'>";
		p = p+ "		<div style='background-color:transparent;' class='grid_td_inner grid_td_rocks'>";
		p = p+ "		</div>";
		p = p+ "	</div>";
		
		p = p+ "<span class='chatUsername'><a href='achievements?id="+user.userID+"' style='color:"+user.displayColor+"'>";
		//p = p+ user.dateSent+": ";
		p = p+ user.displayName+"</a>:</span>";
		
		p = p+ "	<span class='chatText'>";
		p = p+ htmlEncode(user.message);
		p = p+ "	</span>";
		
		lastID = user.ID;
		
		items.unshift('<div class="chatMessage" id="' + key + '">' + p + '</div>');
		newChats = true;
	});
	
	console.log("BUILD DONE");
	
	if (newChats) {
		var new_chatDiv = $('<div/>', {
			'class': 'my-new-list',
			'style': 'display: none',
			html: items.join('')
		});
		new_chatDiv.prependTo('#chatContainer').slideDown('fast'); 
	}
	console.log('(END) lastID', lastID);
}

function getChat(message) {
	console.log('LASTID:', lastID);
	var dataString = 'getChatFromID='+lastID;
	if (chatBuffer.length > 0) {
		dataString += '&send=true&messages='+chatBuffer.join('|:|');
		chatBuffer.length = 0;
	}
	//$.getJSON('ajax_chat.php?getChatFromID='+lastID++message, );
	
	$.ajax({
		type: "GET",  
		url: "ajax_chat.php",  
		data: dataString, 
		success: function(data) {getChatDone(data);}
	});
	
}

function sendChat() {
	var message = $("input#message").val().replace("|:|", "||");
	chatBuffer.push(message);
	$("input#message").val('');
	if (skipNextGetChat == false) {
		skipNextGetChat = true;
		getChat();
	}
	return false;  
}

$(document).ready(function() {

    $('#sendChat').live("submit", function() {
        sendChat()
    });
	 
});

function htmlEncode(value){
    if (value) {
        return jQuery('<div />').text(value).html();
    } else {
        return '';
    }
}

</script>

<h3>this page will overflow...</h3>

<div class='chatContainer2'>
	<form id='sendChat' onsubmit="return false">
	<input type="hidden" name="stuff" value="1724">
	<input type="text" name="message" id="message" value="" maxlength="255" autocomplete="off" >
	<input type="button" class="send" id='chatSendBtn' value='Send' onClick="sendChat();">
	</form>
	<div id='chatContainer'>

	</div>

</div>


<?


htmlFooter();
?>