1/**
2 * DokuWiki Ajax-Chat plugin
3 * AJAX functions
4 *
5 * @license  GPL2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
7 */
8
9AJAXCHAT_SHORT_INTERVAL = 5;
10AJAXCHAT_MIDDLE_INTERVAL = 10;
11AJAXCHAT_LONG_INTERVAL = 30;
12AJAXCHAT_MAXIMUM_INTERVAL = 5*60;
13AJAXCHAT_CHECKTIMEOUT = 3*1000;
14AJAXCHAT_SENDTIMEOUT = 30*1000;
15AJAXCHAT_NOMESSAGES = "<em>&ndash; no messages &ndash;</em>";
16AJAXCHAT_MESSAGESCLEARED = "<em>&ndash; messages cleared &ndash;</em>";
17
18var ajax_chat = null;
19
20function Mouse_monitor_class(window) {
21    this.lastmousex = 0;
22    this.lastmousey = 0;
23    this.mousemovehooked = false;
24    this.mousemoved = false;
25    this.window = window;
26    this.document = window.document;
27
28    var self = this;
29
30    self.onMouseMove = function() {
31    };
32
33    self.mouseEvent = function(e) {
34		if (false === self.mousemovehooked) {
35			return;
36		}
37
38		var posx = 0;
39		var posy = 0;
40		if (!e) {
41			var e = self.window.event;
42		}
43		if (!e) {
44			var e = self.document.event;
45		}
46		if (!e) {
47			return;
48		}
49		if (e.pageX || e.pageY) {
50			posx = e.pageX;
51			posy = e.pageY;
52		}
53		else {
54			if (e.clientX || e.clientY) {
55				posx = e.clientX + self.document.body.scrollLeft;
56				posy = e.clientY + self.document.body.scrollTop;
57	    	}
58		}
59
60		if (self.lastmousex != 0 && self.lastmousey != 0) {
61	    	if ((posx == 0 && posy == 0) || posx != self.lastmousex || posy != self.lastmousey) {
62				self.mousemoved = true;
63				if (self.onMouseMove) {
64					try {
65						self.onMouseMove();
66					} catch (e) {};
67				}
68				self.onMouseMove = null;
69				self.unhookMouseEvent();
70//				setTimeout(function(){self.unhookMouseEvent();}, 100);
71    	    }
72		}
73
74		self.lastmousex = posx;
75		self.lastmousey = posy;
76    };
77
78    self.hookMouseEvent = function() {
79		if (true === self.mousemovehooked) {
80			return;
81		}
82	// Init to current mouse position
83		self.mousemoved = false;
84//		self.refreshMousePos();
85		self.lastmousex = 0;
86		self.lastmousey = 0;
87		addEvent(self.document, 'mousemove', self.mouseEvent);
88
89		self.mousemovehooked = true;
90    };
91
92    self.unhookMouseEvent = function() {
93		if (false === self.mousemovehooked) {
94			return;
95		}
96
97		removeEvent(self.document, 'mousemove', self.mouseEvent);
98
99		self.mousemovehooked = false;
100    };
101
102    self.sleep = function(delay) {
103	var start = (new Date()).getTime();
104	while ((new Date()).getTime() - start < delay) {
105	};
106    };
107}
108
109//prepare class
110function Ajax_chat_class(objectName, outId, pageId, lastRefresh, window) {
111    this.objectName = objectName;
112
113    this.check_sack = new sack(DOKU_BASE + 'lib/plugins/chat/ajax.php');
114    this.check_sack.AjaxFailedAlert = '';
115    this.check_sack.encodeURIString = false;
116
117    this.send_sack = new sack(DOKU_BASE + 'lib/plugins/chat/ajax.php');
118    this.send_sack.AjaxFailedAlert = '';
119    this.send_sack.encodeURIString = false;
120
121    this.timer = null;
122    this.lastChatAreaUpdate = new Date().getTime();
123    this.chat_timer = null;
124
125    this.outObj = document.getElementById(outId);
126    this.pageId = pageId;
127    this.lastRefresh = lastRefresh;
128    this.lastSend = lastRefresh;
129    this.refresh = AJAXCHAT_SHORT_INTERVAL;
130
131    this.clearing = false;
132    this.sending = false;
133
134    this.sendQueue = new Array();
135
136    var self = this;
137
138    this.mouseMonitor = new Mouse_monitor_class(window);
139
140    this.onMouseMove = function(func) {
141        self.mouseMonitor.onMouseMove = function() {
142			func();
143//			self.updateInfo('requestTimerInfo', 'mouseMoved'+(new Date()).getTime());
144//			self.mouseMonitor.unhookMouseEvent();
145		};
146		self.mouseMonitor.hookMouseEvent();
147    };
148
149
150    this.check_sack.onLoading = function() {
151		self.updateInfo('requestTimerInfo', 'Timer initialized');
152		self.check_sack.requestTimer = setTimeout(
153	    function() {
154//			alert('timeout');
155			self.check_sack.requestTimer = null;
156			try {
157			    if (self.check_sack.xmlhttp.abort) {
158					self.check_sack.xmlhttp.abort();
159			    }
160			} catch (e) {
161			}
162			self.updateInfo('requestTimerInfo', 'Timeout, aborted connection');
163			self.check_sack.onError();
164		},
165	    AJAXCHAT_CHECKTIMEOUT);
166    };
167
168    this.check_sack.onError = function() {
169		if (self.check_sack.requestTimer !== null) {
170			clearTimeout(self.check_sack.requestTimer);
171			self.check_sack.requestTimer = null;
172			self.updateInfo('requestTimerInfo', 'Error, timer aborted');
173		}
174		else {
175			self.updateInfo('requestTimerInfo', 'Error, no timer');
176		}
177        self.updateInfo('ajaxActive','error, waiting');
178		self.check_sack.onCompletion();
179    };
180
181    this.check_sack.onLoaded = function() {
182		if (self.check_sack.requestTimer !== null) {
183		    clearTimeout(self.check_sack.requestTimer);
184		    self.check_sack.requestTimer = null;
185		    self.updateInfo('requestTimerInfo', 'OK, timer cleared');
186		}
187    };
188
189    this.send_sack.onLoading = function() {
190		self.updateInfo('requestTimerInfo', 'Timer initialized');
191		self.send_sack.requestTimer = setTimeout(
192		    function() {
193				self.send_sack.requestTimer = null;
194				try {
195					if (self.send_sack.xmlhttp.abort) {
196						self.send_sack.xmlhttp.abort();
197					}
198				} catch (e) {
199				}
200				self.updateInfo('requestTimerInfo', 'Timeout, aborted connection');
201				self.send_sack.onError();
202			},
203	    	AJAXCHAT_SENDTIMEOUT
204	    );
205    };
206
207    this.send_sack.onError = function() {
208		if (self.send_sack.requestTimer !== null) {
209		    clearTimeout(self.send_sack.requestTimer);
210	    	self.send_sack.requestTimer = null;
211			self.updateInfo('requestTimerInfo', 'Error, timer aborted');
212		}
213		else {
214			self.updateInfo('requestTimerInfo', 'Error, no timer');
215		}
216		self.updateInfo('ajaxActive','error, waiting');
217		self.send_sack.onCompletion();
218    };
219
220    this.send_sack.onLoaded = function() {
221	if (self.send_sack.requestTimer !== null) {
222	    clearTimeout(self.send_sack.requestTimer);
223	    self.send_sack.requestTimer = null;
224	    self.updateInfo('requestTimerInfo', 'OK, timer cleared');
225	}
226//	if (200 != self.send_sack.responseStatus[0]*1) {
227//	    self.send_sack.onCompletion();
228//	}
229    };
230
231    this.getUpdateTimeFromResponse = function(resp) {
232	var time = resp.substring(0, resp.indexOf('\n')).replace(/(.*<!--AJAXCHAT_MTIME:)([0-9]+)(-->.*)/, '$2');
233//	alert(time*1);
234	if (time == null || time == '') {
235	    time = 0;
236	}
237	return time*1;
238    };
239
240    self.updateInfo('ajaxRefreshInterval', this.refresh+'sec');
241}
242
243Ajax_chat_class.prototype.updateInfo = function(infoTag, infoValue) {
244    var infoDiv = document.getElementById('chat-info-pad');
245    if (infoDiv) {
246		if (infoDiv.elements[infoTag]) {
247			infoDiv.elements[infoTag].value = infoValue;
248		}
249    }
250};
251
252Ajax_chat_class.prototype.chatClear = function(user) {
253    var self = this;
254    self.clearing = true;
255
256//    self.chatCheckAbort();
257
258    self.send_sack.oldOnError = self.send_sack.onError;
259    self.send_sack.onError = function() {
260	self.send_sack.oldOnError();
261        self.clearing = false;
262	self.send_sack.onError = self.send_sack.oldOnError;
263//	alert('onerror');
264    };
265
266    this.send_sack.onCompletion = function() {
267	var data = self.send_sack.response;
268	if (data !== '') {
269    	    self.updateInfo('ajaxLastStatus','Messages cleared');
270
271	    self.outObj.innerHTML = AJAXCHAT_MESSAGESCLEARED;
272	    self.outObj.style.display = 'block';
273	    updated = self.getUpdateTimeFromResponse(data);
274	    self.lastRefresh = updated;//((new Date().getTime())/1000);
275	    self.clearing = false;
276	    self.send_sack.onError = self.send_sack.oldOnError;
277	}
278    };
279
280    self.outObj.innerHTML = AJAXCHAT_MESSAGESCLEARED;
281
282    if (this.send_sack.encVar) {
283	this.send_sack.encVar('call', 'clear');
284	this.send_sack.encVar('pageId', self.pageId);
285	this.send_sack.encVar('time', self.lastRefresh);
286	this.send_sack.encVar('user', user);
287	this.send_sack.runAJAX();
288    }
289    else {
290	this.send_sack.runAJAX(
291		'call=clear'+
292		'&pageId='+encodeURIComponent(this.pageId)+
293		'&time='+this.lastRefresh+
294		'&user='+encodeURIComponent(user));
295    }
296};
297
298Ajax_chat_class.prototype.chatSend = function(user,message) {
299    var self = this;
300
301    if (true == self.clearing) {
302	return;
303    }
304
305    self.sending = true;
306
307//    self.chatCheckAbort();
308
309//    sleep(1000);
310
311    self.send_sack.oldOnError = self.send_sack.onError;
312    self.send_sack.onError = function() {
313	if (self.send_sack.onError !== self.send_sack.oldOnError) {
314	    self.send_sack.oldOnError();
315	}
316	self.sending = false;
317	self.send_sack.onError = self.send_sack.oldOnError;
318//		alert('onerror');
319    };
320
321    this.send_sack.onCompletion = function() {
322		var data = self.send_sack.response;
323		if (200 == self.send_sack.responseStatus[0]*1) {
324//			alert(data);
325		    var sentDiv = document.getElementById('chatListSentMessage');
326		    if (sentDiv) {
327			sentDiv.innerHTML = '';
328			sentDiv.style.display = 'none';
329		    }
330		    if (data !== '') {
331			var updated = self.getUpdateTimeFromResponse(data);
332			var actualData = data.replace(/[^\n]+\n[ ]*/,'');
333			if (actualData.length == 0) {
334			//    alert(data);
335			}
336			self.outObj.innerHTML =
337				(actualData.length > 0 ? actualData+(self.lastRefresh > 0 ? self.outObj.innerHTML : '') : AJAXCHAT_NOMESSAGES);
338			self.outObj.style.display = 'block';
339			self.lastRefresh = updated;//((new Date().getTime())/1000);
340			self.lastSend = updated;
341
342			// remove first message from queue
343			self.sendQueue.shift();
344		    }
345
346
347		}
348		else {
349//	    	alert(data);
350//		    alert(self.send_sack.responseStatus[0]*1);
351		}
352/*
353		if (self.sendQueue !== null && self.sendQueue.length > 0) {
354		    var msgrec = self.sendQueue[0];
355
356		    self.send_sack.runAJAX(
357		        'call=send'+
358		        '&pageId='+encodeURIComponent(self.pageId)+
359		        '&time='+self.lastRefresh+
360		        '&user='+encodeURIComponent(msgrec[0])+
361		        '&msg='+encodeURIComponent(msgrec[1]));
362		}
363
364		else
365*/
366		{
367		    self.sending = false;
368		    self.send_sack.onError = self.send_sack.oldOnError;
369		}
370    };
371
372    this.requestTimeout = AJAXCHAT_SENDTIMEOUT;
373
374    // add message to send queue
375    var msgrec = new Array();
376    msgrec[0] = user;
377    msgrec[1] = message;
378//    self.sendQueue.push(msgrec);
379
380    if (this.send_sack.encVar) {
381	this.send_sack.encVar('call', 'send');
382	this.send_sack.encVar('pageId', self.pageId);
383	this.send_sack.encVar('time', self.lastRefresh);
384	this.send_sack.encVar('user', user);
385	this.send_sack.encVar('msg', message);
386	this.send_sack.runAJAX();
387    }
388    else {
389	this.send_sack.runAJAX(
390	    'call=send'+
391	    '&pageId='+encodeURIComponent(self.pageId)+
392	    '&time='+self.lastRefresh+
393	    '&user='+encodeURIComponent(user)+
394	    '&msg='+encodeURIComponent(message));
395    }
396};
397
398Ajax_chat_class.prototype.chatCheck = function() {
399    try {
400//    alert(this);
401    var self = this;
402
403    if (true == self.sending) {
404	return;
405    }
406
407    if (self.chat_timer !== null) {
408	clearTimeout(self.chat_timer);
409	self.chat_timer = null;
410    }
411
412    this.check_sack.onCompletion = function() {
413		if (true == self.clearing || true == self.sending) {
414		    return;
415		}
416
417		var data = self.check_sack.response;
418
419		if (200 != self.check_sack.responseStatus[0]*1) {
420			self.updateInfo('ajaxActive','waiting');
421			self.updateInfo('ajaxLastStatus','Error '+self.check_sack.responseStatus[0]+', waiting');
422		}
423		else {
424			self.updateInfo('ajaxActive','waiting');
425
426			if(data === '') {
427				self.updateInfo('ajaxLastStatus','No change');
428		    }
429		    else {
430				var updated = self.getUpdateTimeFromResponse(data);
431				if ((updated == 0 || updated > self.lastRefresh) && false == self.clearing && false == self.sending) {
432					var actualData = data.replace(/[^\n]+\n[ ]*/,'');
433					if (actualData !== self.lastMessage) {
434					    self.outObj.innerHTML =
435						(actualData.length > 0 ? actualData+(self.lastRefresh > 0 ? self.outObj.innerHTML : '') : AJAXCHAT_NOMESSAGES);
436					    self.outObj.style.display = 'block';
437//				    alert(updated);
438//				    alert(((new Date().getTime())/1000));
439	    				    self.updateInfo('ajaxLastStatus','New messages fetched');
440					    if (actualData.length > 0) {
441						chatNotifySound();
442					    }
443					    else {
444					    }
445					    self.lastMessage = actualData;
446					}
447					self.lastRefresh = updated;//((new Date().getTime())/1000);
448				}
449			}
450		}
451    };
452
453    this.updateInfo('ajaxActive','checking...');
454
455//    alert(self.lastRefresh);
456    if (self.check_sack.encVar) {
457	self.check_sack.encVar('call', 'checkAndGet');
458	self.check_sack.encVar('pageId', self.pageId);
459	self.check_sack.encVar('time', self.lastRefresh);
460	self.check_sack.runAJAX();
461    }
462    else {
463	self.check_sack.runAJAX(
464		'call=checkAndGet&pageId='+encodeURIComponent(self.pageId)+'&time='+self.lastRefresh);
465    }
466
467    var now = new Date().getTime();
468    if ((now - self.lastChatAreaUpdate)/1000 >= 5*60) {
469		self.refresh = AJAXCHAT_MAXIMUM_INTERVAL;
470		self.updateInfo('ajaxRefreshInterval',self.refresh+'sec');
471    }
472    else {
473		if ((now - self.lastChatAreaUpdate)/1000 >= 60) {
474			self.refresh = AJAXCHAT_LONG_INTERVAL;
475			self.updateInfo('ajaxRefreshInterval',self.refresh+'sec');
476
477			self.onMouseMove(function(){
478				if (self.refresh > AJAXCHAT_MIDDLE_INTERVAL) {
479//	    			alert(self.refresh);
480					self.refresh = AJAXCHAT_MIDDLE_INTERVAL;
481					self.lastChatAreaUpdate = new Date().getTime()-(30*1000);
482					self.updateInfo('ajaxRefreshInterval',self.refresh+'sec');
483//					self.chatCheck();
484					self.chat_timer = setTimeout(self.objectName+".chatCheck()", 100);
485				}
486			});
487		}
488		else {
489			if ((now - self.lastChatAreaUpdate)/1000 >= 30) {
490				self.refresh = AJAXCHAT_MIDDLE_INTERVAL;
491				self.updateInfo('ajaxRefreshInterval',self.refresh+'sec');
492    	    }
493		}
494    }
495
496    // Add fuzziness to timer to avoid high loads on server at the same time from multiple clients
497    var timeoutFuzzy =
498		(Math.round(Math.random()) > 0 ? 1 : -1) *
499		Math.round(Math.random()*((self.refresh*1000)/10));
500	self.updateInfo('ajaxNextCheck', 'Next check: ' + self.refresh + 's  ' +
501		(timeoutFuzzy > 0 ? "+"+timeoutFuzzy+"ms" :
502		timeoutFuzzy < 0 ? timeoutFuzzy+"ms" : ""));
503
504    } catch (e) {
505    }
506
507    // Set new timer
508    self.chat_timer = setTimeout(self.objectName+".chatCheck()", (self.refresh*1000) + timeoutFuzzy);
509};
510
511Ajax_chat_class.prototype.chatCheckAbort = function() {
512    var self = this;
513    if (null != self.check_sack.requestTimer) {
514	clearTimeout(self.check_sack.requestTimer);
515	self.check_sack.requestTimer = null;
516        this.updateInfo('ajaxActive','aborted check...');
517    }
518
519    if (self.check_sack.xmlhttp && self.check_sack.xmlhttp.abort) {
520	try {
521//	    self.check_sack.resetData();
522	    self.check_sack.xmlhttp.abort();
523//	    alert('aborted');
524	} catch (e) {
525	}
526    }
527};
528
529function chatSetCookie(name, value, expire) {
530    try {
531	// New code
532	DokuCookie.setValue(name, value, expire);
533    } catch (e) {
534	// Old code
535        var expiref = new Date();
536	fixDate(expiref);
537        expiref.setTime(expiref.getTime() + expire);
538	setCookie(name, value, expiref);
539    }
540}
541
542function chatGetCookie(name) {
543    var value;
544    try {
545	// New code
546	value = DokuCookie.getValue(name);
547    } catch (e) {
548	// Old code
549	value = getCookie(name);
550    }
551    return value;
552}
553
554function new_ajax_chat(outId, pageId, lastRefresh) {
555    ajax_chat = new Ajax_chat_class("ajax_chat", outId, pageId, lastRefresh, window);
556}
557
558function chatRefresh(event) {
559    if (ajax_chat === null) {
560		return;
561    }
562
563    var list = document.getElementById('chat-last-message');
564    if (list) {
565		if (list.className.indexOf(' chat-line-last') != -1) {
566		    list.className = list.className.replace(/ chat-line-last/gi, '');
567		}
568    }
569
570    ajax_chat.chatCheck();
571}
572
573function chatClick(event) {
574    if (ajax_chat === null) {
575	return;
576    }
577
578    if (ajax_chat.refresh > AJAXCHAT_SHORT_INTERVAL) {
579	ajax_chat.refresh = AJAXCHAT_SHORT_INTERVAL;
580	ajax_chat.lastChatAreaUpdate = (new Date()).getTime();
581	ajax_chat.updateInfo('ajaxRefreshInterval',ajax_chat.refresh+'sec');
582    }
583
584    chatRefresh();
585}
586
587function chatAreaUpdated(event) {
588    if (ajax_chat === null) {
589	return;
590    }
591
592    var now = new Date().getTime();
593
594    if ((now - ajax_chat.lastChatAreaUpdate)/1000 < 30) {
595		if (ajax_chat.refresh > AJAXCHAT_SHORT_INTERVAL) {
596			ajax_chat.refresh = AJAXCHAT_SHORT_INTERVAL;
597			ajax_chat.updateInfo('ajaxRefreshInterval',ajax_chat.refresh+'sec');
598			chatRefresh();
599		}
600		else {
601		}
602    }
603    else {
604        chatRefresh();
605    }
606
607    ajax_chat.lastChatAreaUpdate = now;
608}
609
610function chatSendMessage(user,msg) {
611    // do not allow to send more than 1 message per second
612    if (ajax_chat.lastSend > 0) {
613	var now = (new Date()).getTime();
614	if (now - (ajax_chat.lastSend*1000) < 1000) {
615	    return false;
616//	    ajax_chat.sleep(now - (ajax_chat.lastSend*1000));
617	}
618    }
619
620    // format date-time
621    var time = new Date();
622    var timeF;
623    if ((''+time.getHours()).length > 1) {
624	timeF = time.getHours()+":";
625    }
626    else {
627	timeF = "0"+time.getHours()+":";
628    }
629    if ((''+time.getMinutes()).length > 1) {
630	timeF += time.getMinutes()+":";
631    }
632    else {
633	timeF += "0"+time.getMinutes()+":";
634    }
635    if ((''+time.getSeconds()).length > 1) {
636	timeF += time.getSeconds()+"";
637    }
638    else {
639	timeF += "0"+time.getSeconds()+"";
640    }
641
642    var list = document.getElementById('chatListSentMessage');
643
644    // format message
645    list.innerHTML = "<strong>" +
646	user + "</strong> (" +
647	timeF+
648	"): <br /><blockquote>" + msg + "</blockquote>\n";
649
650    // show sent-message div
651    list.style.display = 'block';
652    list.style.height = '';
653
654    // do real sending
655    ajax_chat.chatSend(user,msg);
656
657    return true;
658}
659
660function focusChatPrompt() {
661    var form = document.getElementById('chatForm');
662    if (form) {
663	if (form.chatMessage) {
664	    form.chatMessage.focus();
665	}
666    }
667}
668
669function chatClearMessages(user) {
670    var list = document.getElementById('chatList');
671    list.innerHTML = "&nbsp;";
672    ajax_chat.chatClear(user);
673    focusChatPrompt();
674}
675
676function chatEventKeyUp() {
677    var input = document.getElementById('chatForm').chatMessage;
678}
679
680function chatEventClear() {
681    var form = document.getElementById('chatForm');
682
683    chatClearMessages(form.user.value);
684
685    return false;
686}
687
688function chatEventSubmit() {
689    try {
690	focusChatPrompt();
691
692	var form = document.getElementById('chatForm');
693        if (form.chatMessage.value === '') {
694	    return false;
695	}
696
697	if (chatSendMessage(form.user.value, form.chatMessage.value)) {
698	    form.chatMessage.value = '';
699	}
700    } catch (e) {
701    };
702    return false;
703}
704/*
705function chatEventMouseMove(e) {
706    if (ajax_chat.refresh > AJAXCHAT_MIDDLE_INTERVAL) {
707	ajax_chat.refresh = AJAXCHAT_MIDDLE_INTERVAL;
708	ajax_chat.updateInfo('ajaxRefreshInterval',ajax_chat.refresh+'sec');
709        ajax_chat.lastChatAreaUpdate = new Date().getTime()-(30*1000);
710	ajax_chat.chatCheck();
711    }
712}
713*/
714function saveChatUser() {
715    var input = document.getElementById('chatForm').user;
716    if (!input) {
717	return;
718    }
719
720    if (null !== input.timer) {
721	clearTimeout(input.timer);
722	input.timer = null;
723    }
724
725    input.lastKey = null;
726
727    chatSetCookie('chatName', input.value, 365*24*60*60*1000);
728}
729
730function chatEventUserChanged(e) {
731    var input = document.getElementById('chatForm').user;
732    if (!input) {
733	return;
734    }
735
736    if (null !== input.timer) {
737	clearTimeout(input.timer);
738        input.timer = null;
739    }
740
741    var now = new Date();
742    if (!input.lastKey) {
743	input.lastKey = now.getTime();
744
745        input.timer = setTimeout('saveChatUser()', 1000);
746    }
747    else {
748	input.lastKey = now.getTime();
749        input.timer = setTimeout('saveChatUser()', 1000);
750    }
751}
752
753function chatNotifySound() {
754    var soundPlayer = document.getElementById('newmsg_sound');
755    if (soundPlayer) {
756	try {
757	    soundPlayer.play();
758	} catch (e) {};
759    }
760}
761
762function chatSetVolume(vol,silent,cookie) {
763    var soundPlayer = document.getElementById('newmsg_sound');
764    if (soundPlayer) {
765	try {
766	    soundPlayer.setVolume(vol);
767	    if (!silent && (vol > 0)) {
768		soundPlayer.play();
769	    }
770	    var img = document.getElementById('chatVolumeIcon');
771	    if (img) {
772		img.src = DOKU_BASE+'lib/plugins/chat/volume'+vol+'.gif';
773		img.title = 'Set notify volume ('+(vol > 0 ? vol : 'muted')+')';
774		img.style.width = '';
775		img.style.paddingRight = '10px';
776		img.style.paddingTop = '2px';
777	    }
778	} catch (e) {};
779        if (cookie) {
780    	    chatSetCookie('chatVolume', vol+'', 365*24*60*60*1000);
781	}
782    }
783}
784
785function chatToggleVolume() {
786    var soundPlayer = document.getElementById('newmsg_sound');
787    if (soundPlayer) {
788	try {
789	    var volume = soundPlayer.getVolume()+1;
790	    if (volume > 4) {
791	    	volume = 0;
792	    }
793	    chatSetVolume(volume,false,true);
794	} catch (e) {};
795    }
796}
797
798function chatInitialize() {
799    var form = document.getElementById('chatForm');
800    var list = document.getElementById('chatList');
801
802    if (form) {
803	if (form.user && 'text' == form.user.type) {
804	    var cname = chatGetCookie('chatName');
805	    if (null !== cname) {
806		form.user.value = cname;
807	    }
808	    else {
809		saveChatUser();
810	    }
811	}
812
813	if (form.chatMessage) {
814	    addEvent(form, 'submit', chatEventSubmit);
815	    addEvent(form.user, 'keyup', chatEventUserChanged);
816	    addEvent(form.chatMessage, 'keyup', chatAreaUpdated);
817	    setTimeout('focusChatPrompt()', 1000);
818	}
819	if (form.clearButton) {
820	    addEvent(form.clearButton, 'click', chatEventClear);
821	}
822
823	new_ajax_chat('chatList', form.pageId.value, 0);
824	ajax_chat.chatCheck();
825    }
826
827    if (list) {
828	addEvent(list, 'mouseup', chatClick);
829    }
830
831}
832
833(function(){
834
835//preload images
836(new Image()).src = DOKU_BASE+'lib/plugins/chat/volume0.gif';
837(new Image()).src = DOKU_BASE+'lib/plugins/chat/volume1.gif';
838(new Image()).src = DOKU_BASE+'lib/plugins/chat/volume2.gif';
839(new Image()).src = DOKU_BASE+'lib/plugins/chat/volume3.gif';
840(new Image()).src = DOKU_BASE+'lib/plugins/chat/volume4.gif';
841
842addEvent(window, 'load', chatInitialize);
843
844})();
845