var $last_comment_id=0;
var $chatroom_update_timer;



function prepareChatroom() {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById('chatroom_window')) return false;
	if (!document.getElementById('chatroom_comment')) return false;
	
	// Prepare Chatroom Comment Button
	var $chatroom_comment = document.getElementById('chatroom_comment');
	var $chatroom_comment_button = $chatroom_comment.getElementsByTagName('button')[0];
	$chatroom_comment_button.onclick = function() {
		postComment();
		return false;
	}
	
	// Prepare Chatroom Styling Buttons
	var $italic = document.getElementById('formatting-italic');
	$italic.onchange = function() {
		if (this.checked == false) return false;
		var $bold = document.getElementById('formatting-bold');
		$bold.checked = false;
	}
	var $bold = document.getElementById('formatting-bold');
	$bold.onchange = function() {
		if (this.checked == false) return false;
		var $italic = document.getElementById('formatting-italic');
		$italic.checked = false;
	}
	
	// Prepare Invite Form
	var $invite_button = document.getElementById('invite').getElementsByTagName('button')[0];
	$invite_button.onclick = function() {
		sendInvite();
		return false;
	}
	var $recipient_username = document.getElementById('recipient_username');
	$recipient_username.onfocus = function() {
		this.value = '';
		return false;
	}
	
	updateChatroomWindow();
	updateChatroomResidents();
}
addLoadEvent(prepareChatroom);


function checkForPost($event) {
	if (!$event) var $event = window.event;

	if ($event.keyCode) var $key_code = $event.keyCode;
	else if ($event.which) var $key_code = $event.which;
	
	if ($key_code == 13) {
		postComment();
		return false;
	}
	else return true;
	//var $key_code = String.fromCharCode($key_code);
}


function postComment() {
	$postComment_xmlHttp = createXMLHttpRequest();
	
	var $chatroom_comment = document.getElementById('chatroom_comment');
	var $textarea = $chatroom_comment.getElementsByTagName('textarea')[0];
	var $comment_body = $textarea.value;
	var $colour = document.getElementById('formatting-colour').value;
	var $italic = document.getElementById('formatting-italic').checked;
	if ($italic == true) $italic = 'y'; else $italic = 'n';
	var $bold = document.getElementById('formatting-bold').checked;
	if ($bold == true) $bold = 'y'; else $bold = 'n';
	$comment_body = $comment_body.replace(/\n/g,'');
	$comment_body = escape($comment_body);
	$textarea.value = '';
	var $chatroom_id = document.getElementById('chatroom_id').value;
	
	var $url = 'scripts/chatroom/functions.php';
	var $query_string = 'a=post_comment&body='+$comment_body+'&chatroom_id='+$chatroom_id+'&colour='+$colour+'&italic='+$italic+'&bold='+$bold;
	$postComment_xmlHttp.open("POST",$url,true);
	$postComment_xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	$postComment_xmlHttp.send($query_string);
}


function updateChatroomWindow() {
	$updateChatroomWindow_xmlHttp = createXMLHttpRequest();
	
	var $chatroom_id = document.getElementById('chatroom_id').value;
	
	$updateChatroomWindow_xmlHttp.onreadystatechange = updateChatroomWindow_handleStateChange;
	var $url = 'scripts/chatroom/functions.php';
	//var $date = new Date();
	var $query_string = 'a=update_chatroom_window&chatroom_id='+$chatroom_id+'&last_comment_id='+$last_comment_id;
	$updateChatroomWindow_xmlHttp.open("POST",$url,true);
	$updateChatroomWindow_xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	$updateChatroomWindow_xmlHttp.send($query_string);
	
	$chatroom_update_timer = setTimeout("updateChatroomWindow()",1000);
	return false;
}


function updateChatroomWindow_handleStateChange() {
	if ($updateChatroomWindow_xmlHttp.readyState == 4) {
		if ($updateChatroomWindow_xmlHttp.status == 200) {
			var $chatroom_window = document.getElementById('chatroom_window');
			$chatroom_comment = $updateChatroomWindow_xmlHttp.responseText.split('___chatroom_comment_id___');
			if ($last_comment_id != $chatroom_comment[$chatroom_comment.length-1]) {
				$last_comment_id = $chatroom_comment[$chatroom_comment.length-1];
				$chatroom_comment = $chatroom_comment[0];
				innerXHTML($chatroom_window,$chatroom_comment,true);
				if ($chatroom_comment) $chatroom_window.scrollTop = $chatroom_window.scrollHeight;
			}
		}
	}
}



function updateChatroomResidents() {
	$updateChatroomResidents_xmlHttp = createXMLHttpRequest();
	
	var $chatroom_id = document.getElementById('chatroom_id').value;
	
	$updateChatroomResidents_xmlHttp.onreadystatechange = updateChatroomResidents_handleStateChange;
	var $url = 'scripts/chatroom/functions.php';
	//var $date = new Date();
	var $query_string = 'a=update_chatroom_residents&chatroom_id='+$chatroom_id;
	$updateChatroomResidents_xmlHttp.open("POST",$url,true);
	$updateChatroomResidents_xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	$updateChatroomResidents_xmlHttp.send($query_string);
	
	$residents_update_timer = setTimeout("updateChatroomResidents()",1000);
	return false;
}


function updateChatroomResidents_handleStateChange() {
	if ($updateChatroomResidents_xmlHttp.readyState == 4) {
		if ($updateChatroomResidents_xmlHttp.status == 200) {
			var $chatroom_residents = document.getElementById('chatroom_residents_list');
			$chatroom_residents_list = $updateChatroomResidents_xmlHttp.responseText;
			innerXHTML($chatroom_residents,$chatroom_residents_list);
		}
	}
}


function checkForInvite($event) {
	if (!$event) var $event = window.event;

	if ($event.keyCode) var $key_code = $event.keyCode;
	else if ($event.which) var $key_code = $event.which;
	
	if ($key_code == 13) {
		sendInvite();
		return false;
	}
	else return true;
	//var $key_code = String.fromCharCode($key_code);
}


function sendInvite() {
	
	var $author_username = document.getElementById('author_username').value;
	var $chatroom_id = document.getElementById('chatroom_id').value;
	var $recipient_username = document.getElementById('recipient_username').value;
	
	var $invite_response = document.getElementById('invite_response');
	$invite_response.style.display = 'block';
	
	if ($recipient_username == 'Enter Username' || !$recipient_username) {
		innerXHTML($invite_response,'Please enter a valid username.');
		return false;
	}
	if ($author_username == $recipient_username) {
		innerXHTML($invite_response,"You can't invite yourself now, can you?");
		return false;
	}
	
	$sendInvite_xmlHttp = createXMLHttpRequest();
	
	$sendInvite_xmlHttp.onreadystatechange = sendInvite_handleStateChange;
	var $url = 'scripts/chatroom/functions.php';
	var $query_string = 'a=send_invite&chatroom_id='+$chatroom_id+'&username='+$recipient_username;
	$sendInvite_xmlHttp.open("POST",$url,true);
	$sendInvite_xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	$sendInvite_xmlHttp.send($query_string);
	
	innerXHTML($invite_response,'Please wait...');
	return false;
}


function sendInvite_handleStateChange() {
	if ($sendInvite_xmlHttp.readyState == 4) {
		if ($sendInvite_xmlHttp.status == 200) {
			var $invite_response = document.getElementById('invite_response');
			innerXHTML($invite_response,$sendInvite_xmlHttp.responseText);
			var $recipient_username = document.getElementById('recipient_username');
			$recipient_username.value = '';
			return false;
		}
	}
}



