YAHOO.namespace("csu900.chat");
	
var msgbox;
var inbox;

function init() {
	msgbox = document.getElementById("chat_panel");
	inbox = document.getElementById("chat_input");

	// Initialize the temporary Panel to display while waiting for 
	// external content to load
	YAHOO.csu900.chat.wait = new YAHOO.widget.Panel("wait",  
			{ width:"240px", 
				fixedcenter:true, 
				close:false, 
				draggable:false, 
				modal:true,
				visible:false,
				effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
			} );

	YAHOO.csu900.chat.wait.setHeader("Starting Chat, please wait...");
	YAHOO.csu900.chat.wait.setBody('<img src="http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif" />');
	YAHOO.csu900.chat.wait.render(document.body);

	var content = document.getElementById("content");

	var callback = {
		success : function(o) {
			var message =	eval('(' + o.responseText + ')');	
			if (message.count > 0) {
				append_message_list(message);
			}
			YAHOO.csu900.chat.wait.hide();
			scheduleUpdate();
		},
		failure : function(o) {
			/*content.innerHTML = o.responseText;
			content.style.visibility = "visible";
			content.innerHTML = "CONNECTION FAILED!";
			YAHOO.csu900.chat.wait.hide();
			*/
			scheduleUpdate();
		}
	}

	// Show the Panel
	YAHOO.csu900.chat.wait.show();

	// Connect to our data source and load the data
	var conn = YAHOO.util.Connect.asyncRequest("GET", 
		"Communicate?mode=recv&from="+FROM, callback);

	document.onkeypress = onKeyPress;
}

YAHOO.util.Event.addListener(window, "load", init);

function append_message_list (msg) {
		for (var i = 0; i < msg.count; i++) {
			append_message(msg.list[i].from, msg.list[i].time, msg.list[i].mesg);
		}
}

function append_message (from, time, mesg, target) {
		var elem = document.createElement('div');

		var infodiv = document.createElement('div');
		var fromdiv = document.createElement('div');
		var timediv = document.createElement('div');
		var mesgdiv = document.createElement('div');
		var spacer  = document.createElement('div');
		spacer.style.clear = "both";

		YAHOO.util.Dom.addClass(infodiv, "info_line");
		YAHOO.util.Dom.addClass(fromdiv, "from");
		YAHOO.util.Dom.addClass(timediv, "time");
		YAHOO.util.Dom.addClass(mesgdiv, "mesg");

		fromdiv.appendChild(document.createTextNode(from));
		timediv.appendChild(document.createTextNode(time));
		mesgdiv.appendChild(document.createTextNode(mesg));

		infodiv.appendChild(fromdiv);
		infodiv.appendChild(timediv);
		infodiv.appendChild(spacer);
		
		elem.appendChild(infodiv);
		elem.appendChild(mesgdiv);
		if (target == true) {
			YAHOO.util.Dom.addClass(elem, "msg_me");
			YAHOO.util.Dom.addClass(infodiv, "me");
		} else {
			YAHOO.util.Dom.addClass(elem, "msg_other");
			YAHOO.util.Dom.addClass(infodiv, "other");
		}

		msgbox.appendChild(elem);
		msgbox.scrollTop = msgbox.scrollHeight - msgbox.clientHeight - 25;
}

function sendMessage () {
	var callback = {
		success : function(o) {
			dateobj = new Date();
			
			monthstr = dateobj.getMonth() + 1;
			daystr = dateobj.getDate();
			hourstr = dateobj.getHours();
			minstr = dateobj.getMinutes();
			secstr = dateobj.getSeconds();

			monthstr =(monthstr > 8) ? monthstr : "0" + monthstr;
			daystr  = (daystr   > 9) ? daystr   : "0" + daystr;
			hourstr = (hourstr  > 9) ? hourstr  : "0" + hourstr;
			minstr =  (minstr   > 9) ? minstr   : "0" + minstr;
			secstr =  (secstr   > 9) ? secstr   : "0" + secstr;
			secstr += ".0"
			
			datestr = dateobj.getFullYear() + "-" + monthstr + "-" + daystr + " "
								+ hourstr + ":" + minstr + ":" + secstr;
			append_message("Me", datestr, inbox.value, true);
			inbox.value = "";
		},
		failure : function(o) {
		}
	}

	// Escape new lines
	newstr = inbox.value.replace(/\n/g, "\\n");

	// Connect to our data source and load the data
	var conn = YAHOO.util.Connect.asyncRequest("GET", 
								"Communicate?mode=send&to=" + FROM + "&mesg="+newstr,
								callback);
}

function scheduleUpdate() {
	setTimeout("doUpdate();", 1000, "JavaScript");
}

function doUpdate() {
	var callback = {
		success : function(o) {
			var message =	eval('(' + o.responseText + ')');	
			if (message.count > 0) {
				append_message_list(message);
			}
			scheduleUpdate();
		},
		failure : function(o) {
			scheduleUpdate();
		}
	}

	// Connect to our data source and load the data
	var conn = YAHOO.util.Connect.asyncRequest("GET", 
						"Communicate?mode=recv&from="+FROM, callback);
}

function onKeyPress (keyEvent) {
	var evt = window.event ? event : keyEvent;
	if (!evt.shiftKey && evt.keyCode == 13) {
		sendMessage();
		return false;
	} else if (evt.keyCode == 13) {
		inbox.value = inbox.value + "\n";
		return false;
	}
	return true;
}

