var ySArray = new Array();
var contactArray = new Array();
var yShout = Class.create();
var MHContact = Class.create();
var pageTitleBak = '';
var pageTitleBakSet = false;
var truncateChat = false;

// Very nifty string format function, courtesy of Nick Chapman:
// http://chapnickman.com/2006/02/10/string-formatting-in-javascript/
String.prototype.format = function() {
  var params = String.prototype.format.arguments;
  var toReturn = this;

  for (var i = 0; i < params.length; i++) {
    var regex = new RegExp('\{[' + i + ']\}', 'g');
    toReturn = toReturn.replace(regex, params[i]);
  }
 return toReturn;
}

MHContact.prototype = {
	initialize: function() { },
	
	doInit: function(initVars) {
		this.initVars(initVars);
	},

	initVars: function(initVars) {
		this.availability = initVars.availability;
		this.userId = initVars.userId;
		this.userPic = initVars.userPic;
		this.displayName = initVars.displayName;
		this.ministryTitle = initVars.ministryTitle;
		this.donateEnabled = initVars.donateEnabled;
		this.profileUrl = initVars.profileUrl;
		this.hasWebsite = initVars.hasWebsite;
		this.phone = initVars.phone; 
		this.websiteUrl = initVars.websiteUrl;
	},

	isChatEnabled: function() {return !this.isOffline();},

	isOffline: function(){return this.availability == 'offline';},
	isOnline: function(){return this.availability == 'nowOnline';},
	isIdle: function(){return this.availability == 'idle';}

};

yShout.prototype = {
	initialize: function() { },
	
	doInit: function(initVars) {
		//TODO: put this logic back in
		//if ($('yshout') == null) return;
		this.initVars(initVars);
		var pars =
			'reqType=init';
			
		getChat(this.chatKey).Ajax (pars, this.initialLoadDone);
	},

	initVars: function(initVars) {

		// Set to true to enable debug messages
		this.doDebug = false;

		this.chatPos = initVars.chatPos;
		this.chatKey = initVars.chatKey;
		this.inFocus = false;
		this.numShouts = 0;
		this.fcActive = false;
		this.yShoutPHP = '/chat.php';
		this.messageID = 0;

	},
	
	sendShout: function() {
		//alert(this.fcActive+":"+this.formValidate());
		if (this.fcActive) return;
		if (!this.formValidate()) return;
		//alert('2');

		var pars =  'reqType=shout' +
								'&shout=' + escape($F(getChatInputElId(this.chatKey))) +
								'&name=' + escape($F('yshout-shout-nickname-'+this.chatKey));
		this.Ajax(pars, this.parseShouts);
		//alert('3');

		getChatInputEl(this.chatKey).value = '';
		if (this.floodTimeout) this.floodControl();
		//alert('4');
	},

	createForm: function() {
		$('yshout-'+this.chatKey).innerHTML = '<div id="yshout-shouts-'+this.chatKey+'" class="yshout-shouts"></div>' +
			'<form id="yshout-form-'+this.chatKey+'" class="yshout-form"><fieldset>' +
			'<input id="yshout-shout-nickname-'+this.chatKey+'" class="yshout-shout-nickname" value="Nickname" type="text" maxlength="25" class="yshout-before-focus" />' +
			'<textarea id="yshout-shout-text-'+this.chatKey+'" class="yshout-shout-text" type="text" maxlength="175" cols="15" rows="1" onClick="removeNewMessageAlert(\''+this.chatKey+'\');" onFocus="chatInputOnFocus(\''+this.chatKey+'\')" onBlur="chatInputOnBlur(\''+this.chatKey+'\')"></textarea>' +
			'<input id="yshout-shout-button-'+this.chatKey+'" class="yshout-shout-button" value="Shout!" type="button" maxlength="175" />' +
			'</fieldset></form>';
	},

	createElement: function(el, eID, eValue, eType) {
		var objElement = document.createElement(el);
		if (eID) objElement.setAttribute('id', eID);
		if (eType) objElement.setAttribute('type', eType);
		if (eValue) objElement.setAttribute('value', eValue);
		return objElement;
	},

	initialLoadDone: function(request) {
		var reqText = request.responseText;

		var jData = parseJSON(reqText);
		var chatEl = getChat(jData.chatKey);

		chatEl.a('Initial load: ' + reqText);
		chatEl.a('After eval: ');

		// Set the variables received from the server
		chatEl.shoutMaxLines = jData.options.shoutMaxLines;
		chatEl.floodTimeout = jData.options.floodTimeout;
		chatEl.showTimestamps = jData.options.showTimestamps;
		

		setUserStatus(chatEl.chatKey, jData.options.status);
		chatEl.createForm();
		chatEl.initEvtHandlers();
		chatEl.loadNickname(jData.options.nickname);
		chatEl.parseShouts(request);
	},

	Ajax: function(pars, func) {
		pars = pars + '&chatKey=' + this.chatKey;
		
		new Ajax.Request (this.yShoutPHP, {
			method: 'post',
			parameters: pars,
			onComplete: func
		});
	},

	parseShouts: function(request) {
		//alert('here');
		//alert(request.responseText);
		var reqText = request.responseText;
		if (reqText == null) return;
		
		//alert(reqText);
		var jData = parseJSON(reqText);
		
		var targetChatKey = jData.chatKey;
		chatElement = getChat(targetChatKey);
		//alert("chatelement: "+chatElement);
		//chatElement.a('parseShouts: ' + reqText);
		//TODO: the order of the following is nit-picky
		jData.shouts.each (
			function(shout) {
				getChat(targetChatKey).parseMessage(shout);
			}
		);
		if (truncateChat)
			chatElement.truncate();
		chatElement.scrollToBottom();
	},

	scrollToBottom: function() {
		$('yshout-shouts-'+this.chatKey).scrollTop = 424242;
	},

	scrollToTop: function() {
		$('yshout-shouts-'+this.chatKey).scrollTop = 0;
	},

	setShoutText: function(newText) {
		var shoutTextBox = getChatInputEl(this.chatKey);
		shoutTextBox.focus();
		shoutTextBox.value = newText;
	},

	toggleInfo: function(messID) {
		var elInfo = $('yshout-messageinfo-'+this.chatKey+'-' + messID);
		var elMessage = $('yshout-message-'+this.chatKey+'-' + messID);
		var elShout = $('yshout-shout-'+this.chatKey+'-' + messID);

		if (elInfo.style.display == 'none') {
			// Show info
			elMessage.style.display = 'none';
			Element.addClassName(elShout, 'yshout-shout-infovisible');
			elInfo.style.display = '';
		} else {
			// Show message
			elMessage.style.display = '';
			Element.removeClassName(elShout, 'yshout-shout-infovisible');
			elInfo.style.display = 'none';
		}
	},
	
	parseMessage: function(shout) {
		if(shout.mymessage){
			removeNewMessageAlert(this.chatKey, shout.nickname);
			shout.nickname = 'me';
		}
		else{
			newMessageAlert(this.chatKey, shout.nickname);
		}
		this.messageID++;
		this.numShouts++;
		
		var container = $('yshout-shouts-'+this.chatKey);
		var nicknameJS = '', userinfo = '', messageTimestamp = '';

		userinfo = '<span class="yshout-messageinfo" id="yshout-messageinfo-'+this.chatKey+'-{0}">{1}</span>'.format(this.messageID, userinfo);
		var nickname = '<span {0} class="yshout-nickname">{1}:</span> '.format(nicknameJS, shout.nickname);

		if (this.showTimestamps && shout.time) {
				messageTimestamp = shout.time;
				messageTimestamp = messageTimestamp.replace(' am', '');
				messageTimestamp = messageTimestamp.replace(' pm', '');
				messageTimestamp = '<span class="yshout-message-timestamp">[' + messageTimestamp + ']</span> ';
		}
		
		var message = '<span class="yshout-message" id="yshout-message-'+this.chatKey+'-{0}">{1}</span>'.format(this.messageID,  shout.message);
	
		var shoutHTML = 
			messageTimestamp 
			+ nickname
			+ message
			+ userinfo;
			
		var objShoutDiv = document.createElement('div');


		Element.addClassName(objShoutDiv, 'yshout-shout');
		switch(shout.shouttype) {
			case 'admin':
				Element.addClassName(objShoutDiv, 'yshout-admin-shout');
				break;
			case 'system':
				Element.addClassName(objShoutDiv, 'yshout-system-shout');
				break;
			case 'user':
				break;
		}
		
		objShoutDiv.setAttribute('id', 'yshout-shout-'+this.chatKey+'-' + this.messageID);
		objShoutDiv.innerHTML = shoutHTML;
		
		container.appendChild(objShoutDiv);
		
		$('yshout-messageinfo-'+this.chatKey+'-' + this.messageID).style.display = 'none';
		
	},

	removeShouts: function(howMany) {
		var objContainer = $('yshout-shouts-'+this.chatKey);
		for (var i = 0; i < howMany; i++) {
			if (this.numShouts == 0) break;
			objContainer.removeChild(objContainer.firstChild);
			this.numShouts--;
		}
	},

	truncate: function() {
		if (this.numShouts > this.shoutMaxLines)
			this.removeShouts(this.numShouts - this.shoutMaxLines);
	},

	formValidate: function() {
		var nameValid =  this.validateInput('yshout-shout-nickname-'+this.chatKey, 'Nickname');
		var shoutValid =  this.validateInput(getChatInputElId(this.chatKey), '');
		return nameValid && shoutValid;
	},

	validateInput: function(el, defaultText) {
		elText = $F(el);
		var idValid = 'yshout-valid', idInvalid = 'yshout-invalid';
		if (elText == defaultText || elText == '') {
			Element.removeClassName(el, idValid);
			Element.addClassName(el, idInvalid);
			$(el).focus();
			return false;
		} else {
			Element.removeClassName(el, idInvalid);
			Element.addClassName(el, idValid);
			return true;
		}
	},

	loadNickname: function(nickName) {
		var loadName = nickName;
		if (loadName) {
			var el = $('yshout-shout-nickname-'+this.chatKey);
			this.resetInput(el);
			el.value = loadName;
		}
	},

	floodControl: function() {
		//this.fcActive = true;
		//$('yshout-shout-button-'+this.chatKey).disabled = true;
		//setTimeout(this.fcDone, this.floodTimeout);
	},

	fcDone: function() {
		this.fcActive = false;
		//TODO: remove
		//$('yshout-shout-button-'+this.chatKey).disabled = false;
	},

	onKP: function(e) {
		removeNewMessageAlert(this.chatKey);
		var key = window.event ? e.keyCode : e.which;
		//alert(key);
		if (key == 13 || key == 3) {
			this.sendShout();
			return false;
		}	
	},

	onF: function(e) {
		var el = Event.element(e);
		this.resetInput(el);
	},

	resetInput: function(el) {
		Event.stopObserving(el, 'focus', this.onFObserver);
		el.value = '';
	},
	
	initEvtHandlers: function() {
		$('yshout-form-'+this.chatKey).onsubmit = function(){ return false; };
		this.onFObserver = this.onF.bindAsEventListener(this);

		Event.observe(getChatInputElId(this.chatKey), 'keypress', this.onKP.bindAsEventListener(this));
		//Event.observe('yshout-shout-nickname-'+this.chatKey, 'keypress', this.onKP.bindAsEventListener(this));
		Event.observe(getChatInputElId(this.chatKey), 'focus', this.onFObserver);
		//Event.observe('yshout-shout-nickname-'+this.chatKey, 'focus', this.onFObserver);
		//Event.observe('yshout-shout-button-'+this.chatKey, 'click', this.sendShout.bindAsEventListener(this));
	},

	a: function(toSay) {
		if (!this.doDebug) return;
		
		var objDebug = $('debug-'+this.chatKey);
		$('debug-'+this.chatKey).style.display = "block";
		var objDebugP = document.createElement('p');
		var pText = document.createTextNode(toSay);
		objDebugP.appendChild(pText);
		if (objDebug.firstChild) objDebug.insertBefore(objDebugP, objDebug.firstChild);
		else objDebug.appendChild(objDebugP);
	},

	setCookie: function(name, value, expires, path, domain, secure) {
	  var curCookie = name + '=' + escape(value) +
	      ((expires) ? '; expires=' + expires.toGMTString() : '') +
	      ((path) ? '; path=' + path : '') +
	      ((domain) ? '; domain=' + domain : '') +
	      ((secure) ? '; secure' : '');
	  document.cookie = curCookie;
	},

	getCookie: function(name) {
	  var dc = document.cookie;
	  var prefix = name + '=';
	  var begin = dc.indexOf('; ' + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  } else
	    begin += 2;
	  var end = document.cookie.indexOf(';', begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}
};

function loadYShout(initVars){
	var existingChat = getChat(initVars.chatKey);
	if  (existingChat == null || existingChat == undefined){
		ySArray.push(new yShout());
		ySArray.last().doInit(initVars);
	}
}

function getChat(chatKey){
	return ySArray.find(function(chat){return (chat.chatKey == chatKey);});		
}

function getContact(userId){
	return contactArray.find(function(contact){return (contact.userId == userId);});		
}

function inputEmphasis(chatKey) {
	removeNewMessageAlert(chatKey);
	Element.addClassName(getChatInputEl(chatKey), 'chat-input-focus');	
}

function inputBlur (chatKey) {
	Element.removeClassName(getChatInputEl(chatKey), 'chat-input-focus');	
}

function getChatForm(chatKey){return $('yshout-'+chatKey);}
function getChatMinBtn(chatKey){return $('chatMin-'+chatKey);}
function getChatMaxBtn(chatKey){return $('chatMax-'+chatKey);}
function getChatTopArea(chatKey){return $('chatTopArea-'+chatKey);}
function getChatTop(chatKey){return $('chatTop-'+chatKey);}

function removeChat(chatKey){
	var chat = getChat(chatKey);
	ySArray = ySArray.without(chat);
}

function newMessageAlert(chatKey, fromNickname){
	if(!getChat(chatKey).inFocus){
		Element.addClassName(getChatTopArea(chatKey), 'chatTopAreaAlert');
		new Effect.Pulsate('chatTopArea-'+chatKey, { duration:2, from:0.8, to:1.0});
		if (!pageTitleBakSet){
			pageTitleBak = document.title;
			pageTitleBakSet = true;
		}
		document.title = fromNickname+' says...';
	}
}

function removeNewMessageAlert(chatKey){
	if (pageTitleBakSet)
		document.title = pageTitleBak;
	Element.removeClassName(getChatTopArea(chatKey), 'chatTopAreaAlert');
}

function parseJSON(jsonData){
	return eval('(' + jsonData + ')');
}

function chatRefresh(){
	//ySArray.last().Ajax('reqType=refresh', ySArray.last().parseShouts);
	ySArray.each (
			function(yS) {
				yS.Ajax('reqType=refresh', yS.parseShouts);
			}
		);
}
	
function toggleChat(chatKey){
	removeNewMessageAlert(chatKey);
	Element.toggle(getChatForm(chatKey), getChatMaxBtn(chatKey), getChatMinBtn(chatKey));
}

function expandTextArea(textarealabel, e){
	if ((textarealabel.textLength % 45 == 0) && (textarealabel.textLength > 1 ))
		if (e.which == 8)
			textarealabel.rows = textarealabel.rows-1;
		else
			textarealabel.rows = textarealabel.rows+1;
}
	
function getChatKeys(){
	var chatKeys = ',';
	ySArray.each (function(yS) {chatKeys += yS.chatKey + ",";});
	return chatKeys;
}
function checkNewChats(){
	var excluded = getChatKeys();
	var url = 'http://www.ministryhome.org/chat.php';
	var pars = 'action=findNewChats&excluded='+excluded;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:updateChatArea});
}
function checkUserStatuses(){
	var targetUserIds = getChatKeys();
	var url = 'http://www.ministryhome.org/chat.php';
	var pars = 'action=checkUserStatuses&targetUserIds='+targetUserIds;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:updateUserStatuses});
}
function checkContactStatuses(){
	var url = 'http://www.ministryhome.org/chat.php';
	var pars = 'action=checkContactStatuses';
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:updateContactStatuses});
}
function updateContactStatuses(req){
	var reqText = req.responseText;
	if (reqText == null) return;
	var jData = parseJSON(reqText);

	jData.statuses.each (
		function(status) {
			if (contactExists(status.userId))
				setContactStatus(status.userId, status.availability);
		}
	);
}
function updateUserStatuses(req){
	var reqText = req.responseText;
	if (reqText == null) return;
	var jData = parseJSON(reqText);

	jData.statuses.each (
		function(status) {
			if (chatExists(status.chatKey))
				setUserStatus(status.chatKey, status.status);
		}
	);
}
function updateChatArea(req){
	var reqText = req.responseText;
	if (reqText == null) return;
	var jData = parseJSON(reqText);

	jData.chats.each (
		function(chat) {
			if (!chatExists(chat.chatKey)){
				var nextChatPos = getNextPosition();
				Element.update(getChatArea(nextChatPos), chat.chatHTML);
				loadYShout({chatKey:chat.chatKey,chatPos:nextChatPos});
			}
		}
	);
}
function newChat(chatKey, signedIn, chatWithSelf){
	if (!signedIn){
		alert("Please sign in before chatting");
	} else if (chatWithSelf){
		alert("You can't chat with yourself!");
	} else if (!chatExists(chatKey)){
		var url = 'http://www.ministryhome.org/chat.php';
		var pars = "action=newChat&chatKey="+chatKey;
		var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:updateChatArea});
	}
}
function closeChat(chatKey){
	var targetChatArea = getChatArea(getChat(chatKey).chatPos);
	Element.update(targetChatArea, '');
	removeChat(chatKey);
	var url = 'http://www.ministryhome.org/chat.php';
	var pars = "action=closeChat&chatKey="+chatKey;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars});
}
function initChatContacts(){
	var url = 'http://www.ministryhome.org/chat.php';
	var pars = "action=getChatContacts";
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete:updateChatContacts});
}
function getContactElId(userId){
	return 'chatContact-'+userId;
}
function updateChatContacts(req){
	var reqText = req.responseText;
	if (reqText == null) return;

	Element.update('chatContacts', '');
	contactArray.clear();

	var jData = parseJSON(reqText);

	if (jData.contactsMinimized) hideChatNetwork();
	else showChatNetwork();
	
	if (jData.contacts.length == 0) {
		Element.show('noContactsMessage');
		Element.hide('chatContacts');
	}
	else {
		Element.hide('noContactsMessage');
		Element.show('chatContacts');
	}

	jData.contacts.each (
		function(contact) {
			contactArray.push(new MHContact());
			contactArray.last().doInit(contact);
		}
	);

	//alert(jData.chatContactsHtml);
	Element.update('chatContacts', jData.chatContactsHtml);

}
function contactNameOnClick(){
	if ($('miniProfileChatButton').disabled) 
		$('miniProfileMailButton').click();
	else
		$('miniProfileChatButton').click();
}
function contactNameOnMouseOver(userId){
	var contactNameElId = 'contactName-'+userId;
	var yPos = Position.cumulativeOffset($(contactNameElId)); 
	showMiniProfile(userId, yPos[1]-110); 
	Element.addClassName($(contactNameElId), 'emphasis');
}
function contactNameOnMouseOut(userId){
	var contactNameElId = 'contactName-'+userId;
	Element.removeClassName($(contactNameElId), 'emphasis');
}
function initChatting(){
	checkNewChats();
	new PeriodicalExecuter(checkNewChats, 6);
	new PeriodicalExecuter(pingServer, 30);
	new PeriodicalExecuter(chatRefresh, 1);
	new PeriodicalExecuter(checkUserStatuses, 30);
	new PeriodicalExecuter(checkContactStatuses, 60);
}
function pingServer(){
	var url = 'http://www.ministryhome.org/ajax.php';
	var pars = 'action=pingServer';
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars});
}
function chatExists(chatKey){
	var chat = getChat(chatKey);
	if (chat == null || chat == undefined)
		return false;
	return true;
}
function contactExists(contactKey){
	var contact = getContact(contactKey);
	if (contact == null || contact == undefined)
		return false;
	return true;
}
function getChatArea(position){
	return $('chatArea'+position);
}
function getNextPosition(){
	for (var i=0; i <= 3 ; i++){
		if (!posTaken(i))
			return i;
	}
	alert('You can currently only have 4 chats open at a time. If you would like to have more than 4 chats open, please email \'Jon Harper\'');
	return null;
}
function posTaken(pos){
	for (var i=0; i < ySArray.length; i++){
		if (ySArray[i].chatPos == pos)
			return true;
	}
	return false;
}
function setUserStatus(chatKey, status){
	setStatus(getChatTop(chatKey), status);
}
function setContactStatus(contactId, status){
	var contact = getContact(contactId);
	var previousStatus = contact.availability;
	contact.availability = status;
	//if (contact.isOnline() && (previousStatus != contact.availability))
	setStatus($(getContactElId(contactId)), status);
}
function setStatus(el, status){
	Element.removeClassName(el, 'nowOnline');
	Element.removeClassName(el, 'idle');
	Element.removeClassName(el, 'offline');
	if (status != null && status != undefined)
		Element.addClassName(el, status);
}
function setTruncateChat(){
	truncateChat = true;
}
function toggleChatNetwork(){
	var pars;
	
	if (Element.visible('chatNetworkBody')){
		pars = 'action=minimizeContacts';
		hideChatNetwork();
	}
	else{
		pars = 'action=maximizeContacts';
		showChatNetwork();
	}
	new Ajax.Request('http://www.ministryhome.org/ajax.php', {method: 'post', parameters: pars});
}	
function hideChatNetwork(){
	Element.hide('chatNetworkBody', 'chatNetworkMin', 'chatNetworkArrowDown');
	Element.show('chatNetworkMax', 'chatNetworkArrow');
}
function showChatNetwork(){
	Element.show('chatNetworkBody', 'chatNetworkMin', 'chatNetworkArrowDown');
	Element.hide('chatNetworkMax', 'chatNetworkArrow');
}
function showMiniProfile(userId, topPosition){
	$('miniProfileArea').style.top = ''+topPosition+'px';
	var targetContact = getContact(userId);

	$('miniProfileImg').src = targetContact.userPic;

	$('miniProfileMailButton').onclick = function(){showMailForm(targetContact.displayName, userId);};

	$('miniProfileChatButton').onclick = function(){newChat(userId, true);};
	$('miniProfileChatButton').disabled = targetContact.isChatEnabled() ? '' : 'disabled'; 

	$('miniProfileDonateButton').onclick = function(){showDonateForm(userId,  targetContact.ministryTitle, targetContact.displayName);};
	$('miniProfileDonateButton').disabled = targetContact.donateEnabled ? '' : 'disabled';

	$('miniProfileProfileLink').href = targetContact.profileUrl;
	if (targetContact.hasWebsite){ 
		Element.show('miniProfileSiteLinkArea');
		$('miniProfileSiteLink').href = targetContact.websiteUrl;
	} else {
		Element.hide('miniProfileSiteLinkArea');
	}
	Element.update('miniProfileDisplayName', targetContact.displayName);
	if (isBlank(targetContact.phone))
		Element.update('miniProfilePhone', '');
	else
		Element.update('miniProfilePhone', targetContact.phone);
	setStatus($('miniProfile'), targetContact.availability); 

	Element.show('miniProfile');
}
function hideMiniProfile(){
	Element.hide('miniProfile');
}
function hideMiniProfileIfNeccessary(e){
	//contains hack for IE's absolute positioned element
	Position.prepare();
	var mouseX = Event.pointerX(e);
	var mouseY = Event.pointerY(e); 
	var mouseYOffset = mouseY - Position.deltaY; 
	if (!Position.withinIncludingScrolloffsets($('miniProfileArea'), mouseX, mouseY) && !Position.withinIncludingScrolloffsets($('chatContacts'), mouseX, mouseY))
		if (!Position.withinIncludingScrolloffsets($('miniProfileArea'), mouseX, mouseYOffset) && !Position.withinIncludingScrolloffsets($('chatContacts'), mouseX, mouseYOffset))
			hideMiniProfile();
}
function chatContactsOnMouseOut(e){
	if (Element.visible('miniProfile')) 
		hideMiniProfileIfNeccessary(e);
}
function chatInputOnFocus(chatKey){
	getChat(chatKey).inFocus=true;
	Element.addClassName(getChatInputEl(chatKey), 'chat-input-focus');
}
function chatInputOnBlur(chatKey){
	getChat(chatKey).inFocus=false;
	Element.removeClassName(getChatInputEl(chatKey), 'chat-input-focus');
}
function getChatInputElId(chatKey){
	return 'yshout-shout-text-'+chatKey;
}
function getChatInputEl(chatKey){
	return $(getChatInputElId(chatKey));
}