var NextCallFunction = null;
function openChatUserList(notshow){
	NextCallFunction = "getChatData()";
	if(notshow == undefined) {
		open_layer("ch_userList");
	}
	loadURL("/inc/user_list", "ch_userList");
}

function startChat(id) {
	open_layer("ch_window");
	alreadyChecked = false;
	get_obj("cumsg_"+id).innerHTML = 0;
	NextCallFunction = "initChatBox()";
	loadURL("/inc/chat/"+id, "ch_window");
	document.title = oldTitle;
	//createElement("DIV", attributesArray, "", "", "ch_window_" + id, css_class)
}

function initChatBox()
{
	try {
		document.getElementById("id_messageBox").focus();
		rid = parseInt(document.getElementById("id_messageBox").getAttribute("rid"));
		if(typeof rid != "number") {
			tm = window.setTimeout(initChatBox(), 1000);
			return;
		}
	} catch(e) {
		tm = window.setTimeout(initChatBox(), 1000);
	}
	showIt("ml_"+rid);
	
	document.getElementById("id_messageBox").onkeyup = function(e){
		rid = this.getAttribute("rid");
		var e=window.event || e;
		kc = e.keyCode;
		if(kc == 27) {
			this.value = "";
			get_obj("id_messageBox").style.height = 20 + "px";
		}
		if(kc == 13) {
			sendMessage(rid);
			get_obj("id_messageBox").style.height = 20 + "px";
			this.value = "";
		}
		//get_obj("mmm").innerHTML = this.scrollHeight + " : " + this.scrollTop + " : " + this.style.bottom + " : " + kc;
		this.style.height = this.scrollHeight + "px";
		//get_obj("id_msgBox").style.bottom = (this.scrollHeight) + "px";//
	}
}

function sendMessage(rid) {
	msg = Trim(document.getElementById("id_messageBox").value);
	document.title = oldTitle;
	if(msg == "") return false;
	reqUrl = "/j/?cmd=chat&m=" + encodeURIComponent(msg) + "&id=" + rid;
	request = new AjaxRequest(reqUrl, "GET", sendMessage_hendler, true);
	request.send();
}

function sendMessage_hendler(resp)
{
	if(resp.responseText == "0") {
		return false;
	}
	
	var c = get_obj("ml_"+rid);
	//alert(resp.responseText);
	c.innerHTML = c.innerHTML + resp.responseText;
	showIt("ml_"+rid);
}

function addMsg(rid, msg){
	var c = get_obj("ml_"+rid);
}

var alreadyChecked = false;
function checkNewMessage(id){
	if(alreadyChecked == true) return false;
	reqUrl = "/j/?cmd=checkMsg&id="+id;
	rid = id;
	alreadyChecked = true;
	request = new AjaxRequest(reqUrl, "GET", checkNewMessage_hendler, true);
	request.send();
}
oldTitle = document.title;
function checkNewMessage_hendler(resp) 
{
	if(resp.responseText == "0") {
		return false;
	}
	if(typeof rid != "undefined") {
		var c = get_obj("ml_"+rid);
		if(c != null && c.style.display != "none") {
			get_obj("newMessageAlert").style.block = "none";
			c.innerHTML = c.innerHTML + resp.responseText;
			get_obj("cumsg_"+rid).innerHTML = 0;
			setMessageRead(rid);
			showIt("ml_"+rid);
			alreadyChecked = false;
			document.title = oldTitle;
		} else {
			if(document.title != "*** NEW CHAT MESSAGE ***") {
				oldTitle = document.title;
				document.title = "*** NEW CHAT MESSAGE ***";
			} else {
				document.title = oldTitle;
			}
		}
	}
}

function showIt(elID)
{
	try {
		var el = document.getElementById(elID);
		var lel = el.childNodes[el.childNodes.length-2];
	
		if(lel.scrollIntoView) {
			lel.scrollIntoView(true);
		}
	} catch(e) {
		return true;
	}
	return true;
}

// return [online=<count>, newmessages=<count>]
var chatStatusStarted = false;
function getChatData()
{
	if(chatStatusStarted) return false;
	reqUrl = "/j/?cmd=chatStatus";
	request = new AjaxRequest(reqUrl, "GET", getChatData_hendler, true);
	chatStatusStarted = true;
	request.send();
}

function getChatData_hendler(resp) {
	try {
		var k = window.setTimeout(getChatData, 5000);
		chatStatusStarted = false;

		try {
			eval(resp.responseText);
		} catch(e) {
			return false;
		}
		get_obj("newMessageAlert").style.display = "none";
		get_obj("st_onlines").innerHTML = t.online;
		count = 0;
		for(var k in t.newmessages) {
			mm = get_obj("cumsg_" + k);
			count += parseInt(t.newmessages[k]);
			if(mm) {
				mm.innerHTML = t.newmessages[k];
				if(parseInt(t.newmessages[k]) > 0) {
					checkNewMessage(k);
					get_obj("newMessageAlert").style.display = "block";
				}
			}
		}
		get_obj("newMsg_count").innerHTML = count;
	} catch(e) {}
}

function setMessageRead(id) {
	reqUrl = "/j/?cmd=chatRead&id="+id;
	request = new AjaxRequest(reqUrl, "GET", setMessageRead_handler, true);
	request.send();
}

function setMessageRead_handler(resp){}

function closeChatWindow(id){
	get_obj("ml_" + id).style.display = "none";
	close_layer("ch_window");
}
