var refresh = 10000;
var busy = 0;
var messages = new Array();
var chat_id;
var hanging_invigation;

function ebi(id) {
    return document.getElementById(id);
}

function update(xml) {
	xml = xml.documentElement;
	resp = xml.getElementsByTagName("response");

	for (i = 0; i < resp.length; i++) {
		var current = resp[i];
		resp_type = current.getElementsByTagName("type").item(0).childNodes[0].nodeValue;
		
		switch(resp_type)
		{
			case "incoming_invitation": add_incoming_invitation(current); break;
			case "outgoing_invitation": add_outgoing_invitation(current); break;
			case "refuse_invitation": remove_invitation(current); break;
			case "cancel_invitation": remove_invitation(current); break;
			case "chat_activation": activate_chat(current); break;
			case "close_chat": close_chat(current); break;
			case "info": add_info(current); break;
			case "message": add_message(current); break;
		}
	}
}

function chat_connect() {
	//log('chat_connect');
	advAJAX.post({
        url : "?tsk=chat_interface",
        onSuccess : function(obj) {
            update(obj.responseXML);
			//log(obj.responseText);
        },
		onError : function(obj) {
			//log(obj.status);
		},
		"do" : "",
		"busy" : busy,
		"chat_id" : chat_id,
		"sent_messages" : messages.join(",")
    });
	setTimeout("chat_connect()", refresh);
}

function refuse_chat() {
	advAJAX.post({
		url : "?tsk=chat_interface",
		onSuccess : function(obj) {
			update(obj.responseXML);
		},
		onError : function(obj) {
			log(obj.status);
		},
		"do" : "refuse_chat",
		"chat_id" : chat_id
    });
}

function cancel_chat() {
	advAJAX.post({
		url : "?tsk=chat_interface",
		onSuccess : function(obj) {
			update(obj.responseXML);
		},
		onError : function(obj) {
			log(obj.status);
		},
		"do" : "cancel_chat",
		"chat_id" : chat_id
    });
}

function accept_chat() {
	advAJAX.post({
		url : "?tsk=chat_interface",
		onSuccess : function(obj) {
			update(obj.responseXML);
		},
		onError : function(obj) {
			log(obj.status);
		},
		"do" : "accept_chat",
		"chat_id" : chat_id
    });
}

function abort_chat() {
	advAJAX.post({
		url : "?tsk=chat_interface",
		onSuccess : function(obj) {
			update(obj.responseXML);
		},
		onError : function(obj) {
			log(obj.status);
		},
		"do" : "close_chat",
		"chat_id" : chat_id
    });
}

function close_chat(response) /* ######### TODO */
{
	text = response.getElementsByTagName("reason").item(0).childNodes[0].nodeValue;

	add_line(0, "<b>Chat beendet.</b><br>" + text + " <a href=\"javascript:remove_chat()\">Chat schliessen</a>");

	ebi("chat_input").style.disabled = true;
	ebi("chat_input").value = "Chat inaktiv.";
	busy = 0;
}

function remove_chat(local_chat_id)
{
	ebi("chat").style.display = 'none';	
}

function add_incoming_invitation(response)
{
	busy = 1;
	
	chat_id = response.getElementsByTagName("chatid").item(0).childNodes[0].nodeValue;
	user_id = response.getElementsByTagName("userid").item(0).childNodes[0].nodeValue;
	user_name = response.getElementsByTagName("username").item(0).childNodes[0].nodeValue;
	time = response.getElementsByTagName("time").item(0).childNodes[0].nodeValue;

	html = ebi("invitation_i").innerHTML;
	html = html.replace('username', user_name);	
	html = html.replace('00:00', time);	
	html = html.replace('user_id', user_id);
	
	ebi("invitation").innerHTML = html; 
	ebi("invitation").style.display = 'block';
	ebi("help").style.display = 'none';
	
	hanging_invigation = true;
	blink();
	refresh = 5000;
}


function add_outgoing_invitation(response)
{
	busy = 1;
	
	chat_id = response.getElementsByTagName("chatid").item(0).childNodes[0].nodeValue;
	user_id = response.getElementsByTagName("userid").item(0).childNodes[0].nodeValue;
	user_name = response.getElementsByTagName("username").item(0).childNodes[0].nodeValue;
	time = response.getElementsByTagName("time").item(0).childNodes[0].nodeValue;
	
	html = ebi("invitation_o").innerHTML;
	html = html.replace('username', user_name);	
	html = html.replace('00:00', time);	
	
	ebi("invitation").innerHTML = html; 
	ebi("invitation").style.display = 'block';
	ebi("help").style.display = 'none';
	refresh = 5000;
}


function remove_invitation(response)
{
	busy = 0;
	ebi("invitation").innerHTML= "";
	ebi("invitation").style.display = 'none';
	
	hanging_invigation = false;
}

function activate_chat(response)
{
	chat_id = response.getElementsByTagName("chatid").item(0).childNodes[0].nodeValue;
	user_id = response.getElementsByTagName("userid").item(0).childNodes[0].nodeValue;
	user_name = response.getElementsByTagName("username").item(0).childNodes[0].nodeValue;

	add_tab(user_name);
	add_chat();
	
	remove_invitation(response);
	ebi("help").style.display = 'none';
	
	refresh = 5000;
	busy = 2;
}

function add_message(response)
{
	message_id = response.getElementsByTagName("messageid").item(0).childNodes[0].nodeValue;
	chat_id = response.getElementsByTagName("chatid").item(0).childNodes[0].nodeValue;
	user_id = response.getElementsByTagName("userid").item(0).childNodes[0].nodeValue;
	user_name = response.getElementsByTagName("username").item(0).childNodes[0].nodeValue;
	time = response.getElementsByTagName("time").item(0).childNodes[0].nodeValue;
	message_text = response.getElementsByTagName("message").item(0).childNodes[0].nodeValue;
	
	messages.push(message_id);

	line_text = "<b>" + time + " " + user_name + ":</b> " + message_text;
	add_line(message_id, line_text);

	
	while(messages.length > 10)
	{
		ebi("chat").removeChild(ebi("message_" + messages[0]));
		messages.shift();
	}
	
}

function add_tab(user_name)
{
	ebi("chat_title").innerHTML = "Direktchat mit " + user_name;
}

function add_chat()
{
	if(messages.length > 0)
	{
		for(a = 0; a < messages.length; a++)
		{
			ebi("chat").removeChild(ebi("message_" + messages[a]));	
		}
		ebi("chat").removeChild(ebi("message_0"));
		ebi("chat_input").value = '';
		
		messages = new Array();
	}
	
	ebi("chat").style.display = 'block';
}

function add_line(message_id, message_text)
{
	new_message = ebi("message_x").cloneNode(true);
	new_message.innerHTML = message_text;
	new_message.setAttribute("id", "message_" + message_id);
	ebi("chat").insertBefore(new_message, ebi("chat_form"));
}

function send_message()
{
	advAJAX.post({
		url : "?tsk=chat_interface",
		onInitialization : function() {
			ebi("chat_input").style.disabled = 'true';
			ebi("chat_input").value = "Nachricht wird gesendet...";
		},
		onSuccess : function(obj) {
			ebi("chat_input").value = "";
			ebi("chat_input").style.disabled = 'false';
			ebi("chat_input").focus();
			//log(obj.responseText);
		},
		onError : function(obj) {
			log(obj.status);
		},
		"do" : "send_message",
		"message" : ebi("chat_input").value,
		"chat_id" : chat_id
    });
	return false;
}

function log(text)
{
 ebi('page_content').innerHTML = text + "<br>" + ebi('page_content').innerHTML;	
}

function blink()
{
	new Effect.Highlight('invitation', {startcolor:'#FFAD39', endcolor:'#FFF5E6'});
	
	if(hanging_invigation)
	{
		setTimeout("blink()", 3000);	
	}
}