
//Create a JSON (JavaScript Object Notation) Object named 'updater'.
var updater = {
			//Updater has the following properties:
			pollingActive : 0,
			align : 'bottom',
			updatingDisabled: 0,
			writingActive : 0,
			pollOn : 0,
			lastMessageID : 1,
			pollInterval : 3000,
			page: 0,
			messagesPerPage: 3,
			aBID : -1,
			form : null,
			abKey : 0,
			showErrors : 0,
			failureCount : 0,
			useAbMonitor : 1,
			
			init : function(){
				updater.pollIt();
				activityHandler.active();
			},
			
			pollIt : function() {
				if(updater.pollInterval < 1000){
					updater.pollInterval = 1000;
				}
				if(updater.pollInterval > 30000)
					activityHandler.idle();
				updater.pollingActive = 1;
				setTimeout("updater.pollUpdate(updater.lastMessageID)", updater.pollInterval);
				updater.pollInterval = updater.pollInterval * 1.25;
			},
			
			nextPage: function(){
				updater.page += 1;
				if(updater.page > 0){
					updater.updatingDisabled = 1;
				}
				updater.checkUpdates(updater.lastMessageID, 'update');
			},
			
			prevPage: function(){
				updater.page -= 1;
				if(updater.page == 0){
					updater.updatingDisabled = 0;
				}
					
				updater.checkUpdates(updater.lastMessageID, 'update');
			},
			
			firstPage: function(){
				updater.page = 0;
				updater.updatingDisabled = 0;
				
				updater.checkUpdates(updater.lastMessageID, 'update');
			},		
				
			pollUpdate : function(messageID){
				if(updater.updatingDisabled == 1)
					updater.pollIt();
				else{
					updater.pollingActive = 0;
					updater.checkUpdates(messageID, 'poll');
				}
			},
			
			writeMessage : function(){
				updater.writingActive = 1;
				var message = updater.form.message.value;
				//plus signs and spaces get encoded into the same thing in this scheme, then back into spaces.
				//to retain all "+", replace with the HTML identity.
				message = message.replace(/\+/g,"&#43;");   
				
				message = encodeURI(message);
				message = escape(message);
				var callback =
				{
				  argument: action,
				  success: updater.postSuccessCallback,
				  failure: updater.postFailureCallback				
				};
				var action = 'write';
				var postData = "abKey="+updater.abKey;
				postData += "&messageID="+updater.lastMessageID;
				postData += "&aBID="+updater.aBID;
				postData += "&page="+updater.page;
				postData += "&messagesPerPage="+updater.messagesPerPage;
				postData += "&action="+action;
				postData += "&align="+updater.align;
				postData += "&message="+message +"\n";
      
  				var cObj = YAHOO.util.Connect.asyncRequest('POST', '/chat_update.spark', callback,postData);
			
			},
			
			eraseMessage : function(messageID){
				if(!messageID)
					return;
				var callback =
				{
				  argument: action,
				  success: updater.updateSuccessCallback,
				  failure: updater.updateFailureCallback				
				};
				var action = 'erase';
				var postData = "abKey="+updater.abKey;
				postData += "&messageID="+messageID;
				postData += "&aBID="+updater.aBID;
				postData += "&page="+updater.page;
				postData += "&messagesPerPage="+updater.messagesPerPage;
				postData += "&action="+action;
				postData += "&align="+updater.align;
				var cObj = YAHOO.util.Connect.asyncRequest('POST', '/chat_update.spark', callback, postData);
			
			},
			
			banUser : function(messageID, aBUserID){
				if(!aBUserID)
					return;
				var callback =
				{
				  argument: action,
				  success: updater.updateSuccessCallback,
				  failure: updater.updateFailureCallback				
				};
				var action = 'banUser';
				var postData = "abKey="+updater.abKey;
				postData += "&messageID="+messageID;
				postData += "&aBID="+updater.aBID;
				postData += "&page="+updater.page;
				postData += "&messagesPerPage="+updater.messagesPerPage;
				postData += "&action="+action;
				postData += "&align="+updater.align;
				postData += "&banID="+aBUserID;
				var cObj = YAHOO.util.Connect.asyncRequest('POST', '/chat_update.spark', callback, postData);
			},
					
			getOnce: function(){
				var callback =
				{
				  argument: action,
				  success: updater.updateSuccessCallback,
				  failure: updater.updateFailureCallback				
				};
				var postData = "abKey="+updater.abKey+"&messageID="+updater.lastMessageID+"&aBID="+updater.aBID+"&page="+updater.page+"&messagesPerPage="+updater.messagesPerPage+"&align="+updater.align+"&action=getonce&rnd="+(Math.random()*10000000);
				var cObj = YAHOO.util.Connect.asyncRequest('POST', '/chat_update.spark', callback, postData);
			},
			
			checkUpdates : function(messageID, action){
			
				if(messageID > this.lastMessageID){
					this.lastMessageID = messageID;
				}
				
				var callback =
				{
				  argument: action,
				  success: updater.updateSuccessCallback,
				  failure: updater.updateFailureCallback				
				};
				
				var postData = "abKey="+updater.abKey+"&messageID="+updater.lastMessageID+"&aBID="+updater.aBID+"&page="+updater.page+"&messagesPerPage="+updater.messagesPerPage+"&align="+updater.align+"&action="+action+"&rnd="+(Math.random()*10000000);
				if(action == 'poll' && updater.useAbMonitor)
					var cObj = YAHOO.util.Connect.asyncRequest('GET', '/ab_monitor?'+postData, callback);
				else
					var cObj = YAHOO.util.Connect.asyncRequest('POST', '/chat_update.spark', callback, postData); 
			},

			postSuccessCallback: function(o){
				updater.form.message.value = '';
				updater.writingActive = 0;
				updater.updateSuccessCallback(o);
			},
			
			updateSuccessCallback : function(o){
				updater.failureCount = 0;
				if(updater.showErrors == 1){
					errorElem = document.getElementById('messenger_error_div')
					if(!errorElem){
						errorElem = document.createElement('div');
						errorElem.id="messenger_error_div";
						document.getElementsByTagName('body')[0].appendChild(errorElem);
					}
						
					responseText = "<pre>"+o.responseText+"</pre><br /><br /><br />";
					errorElem.innerHTML += responseText;
				}

				
				if(o.responseText){
					var response = eval("("+o.responseText+")");
					if(!YAHOO.lang.isUndefined(response.error)){
						updater.handleError(response.error);
					}else{
						if(o.argument != 'poll'){
							chatTroll.updateNavWindow(response);		
							chatTroll.updateChatWindow(response);
						}
						if(response.lastMessageID != updater.lastMessageID){
							if(o.argument == 'poll'){
								chatTroll.updateNavWindow(response);		
								chatTroll.updateChatWindow(response);
							}
							updater.pollInterval = 1000;
							updater.lastMessageID = response.lastMessageID;
						}
					}
				}
				
				if(updater.pollingActive == 0)
					updater.pollIt();
			},
		
			updateFailureCallback : function(o){
				updater.failureCount++;
				if(updater.showErrors)
					alert("Chat update failed.  Trying again...");
				if(updater.pollingActive == 0 && updater.failureCount < 3)
					updater.pollIt();
			},
			
			postFailureCallback : function(o){
				updater.failureCount++;
				updater.writingActive = 0;
				if(updater.showErrors)
					alert("Posting failed.  Trying again...");
				if(updater.failureCount < 0) //disable for now
					setTimeout("updater.writeMessage()", updater.pollInterval);
			},			
			
			handleError :  function(error){
				switch(error){
					case 'pageNotFound':
						updater.page;
						var updateObj = {
							page : --updater.page,
							messagesPerPage: updater.messagesPerPage,
							lastPage : 1
						}
						chatTroll.updateNavWindow(updateObj);
						alert('Last page reached');
						break;
					default:
						alert(error);
				}
			
			}
		};

function chatOn(){ updater.init(); }

