﻿/*
 * 
 * AJAX Chat Client - Kevin Mesiab
 *
*/

//
// Global variables
//

var isFirefox;
var isIE;
var timeout;
var timeoutScroll;
var refreshRate = 2000; // two seconds
var rnd = Math.random();
var room = "Général";
var userGuid;
var userName;

//
// Get a DOM element
//
function getElement(id)
{
	return document.getElementById(id);
}

//
// Get browser type
//
function sniff() {

	var ff = "Netscape";
	var ie = "Microsoft Internet Explorer";
	
	isFirefox = ( navigator.appName == ff );
	isIE = (navigator.appName == ie ); 
}
//
// Capture the enter key on 
// the input box and initiate a
// GET to the post url
//
function captureReturn( event )
{
	if( isIE )
		event = window.event;

	//
	// Supress event bubbling
	//
	if( event.keyCode == 13 )
	{
		//
		// fetch the feed
		//
		postText();

		if( isIE )
			event.returnValue = false;
		else
			event.preventDefault();
	}
}

//
// Auto scroll the chat window
// if the text exceeds the div 
//
function scrollChatPane()
{
	pane = getElement( "chatpane" );

	if( pane.scrollTop < pane.scrollHeight - pane.offsetHeight )
	{
		pane.scrollTop = pane.scrollTop + 1;
	}

	timeoutScroll = window.setTimeout("scrollChatPane()", 50);
}

//
// Show the little loading animation
// when the page starts
//
function showLoadScreen()
{
	showChat(true);

	var loading = "<div style=\"text-align:center;\"><h4>Loading...</h4><img src=\"spinwait.gif\" /></div>";

	chat = getElement("chatbuffer");
	user = getElement("userlist");

	chat.innerHTML = loading;
	user.innerHTML = loading;

	timeoutScroll = window.setTimeout("scrollChatPane()", 500);
}

//
// Start the fetch timer to update
// the chatpane and userlist
//
function setTimers()
{
	timeout = window.setTimeout("fetch()", refreshRate);
}

//
// Start the async fetch 
// and reset the fetch timer
//
function fetch()
{
	window.clearTimeout(timeout);
	showUpdating(true);
	fetchUserList();
	setTimers();
}

/* #############################
 * 
 * function: fetchUserList()
 * purpose:
 * 
 * This function retrieves the userlist.  
 * The userlist is returned as a set of
 * list-items from the server.  The existing
 * list is replaced and the new list is rendered
 * between two <ul> tags.  This function will 
 * call to update the chat buffer when it completes.
 *		 
 * notes: A random number is generated 
 * when this page loads.  This number is
 * incrimented and appended to the url to
 * prevent caching problems in IE
 *
 * #############################
*/
function fetchUserList()
{
	var usersHtml = Chat.UserList(room, fetchUserListCallBack);
}

function fetchUserListCallBack(result)
{
	//alert(result.Exception);
	obj = getElement("userlist")
	if(result.error == null)
		obj.innerHTML = result.value;
	else
		obj.innerHTML = "DISCONNECTED";

	fetchBufferText();
}

/* #############################
 * 
 * function: fetchBufferText()
 * purpose:
 * 
 * This function retrieves the last 
 * twenty lines of the chat buffer.
 * The chat buffer is returned as a 
 * set of list-items and are rendered
 * in a <ul> tag.  This function calls
 * the scroll function to scroll the 
 * chat pane.
 *
 * notes: A random number is generated 
 * when this page loads.  This number is
 * incrimented and appended to the url to
 * prevent caching problems in IE
 *
 * #############################
*/
function fetchBufferText()
{
	Chat.BufferText(room, fetchBufferTextCallBack);
}

function fetchBufferTextCallBack(result)
{
	obj = getElement( "chatbuffer" );
	if(result.error == null)
		obj.innerHTML = result.value;
	else
		obj.innerHTML = "DISCONNECTED";

	ping();

	scrollChatPane();
}

function ping()
{
	Chat.Ping(room, userGuid, pingCallBack);
}

function pingCallBack()
{
	showUpdating(false);
}

/* #############################
 * 
 * function: postText()
 * purpose:
 * 
 * A users chat is posted to the server in 
 * the querystring of the posttext.aspx url.
 * The format of the querystring is: 
 *
 * ?u=[username]&t=[chat text]
 *
 * Because of the nature of the url encoding
 * certain chat text will fail to post.  
 * The chat text cannot contain any values
 * that are invalid in a url, or are part
 * of the url structure, such as the ampersand (&)
 * forward slash (/), etc.  
 *
 * #############################
*/
function postText()
{
	chatbox = getElement("mytext");
	chat = chatbox.value;
	chatbox.value = "";
	
	Chat.PostText(room, userGuid, chat, postTextCallBack);
}

function postTextCallBack(result)
{
	fetch();
}

function login()
{
	showLoadScreen();
	userName = getElement("txtUsername").value;
	Chat.Login(room, userName, loginCallBack);
}

function loginCallBack(result)
{
	if(result.error == null)
	{
		userGuid = result.value;
		
		removeLogin();

		setTimers();

		mytext = getElement( "mytext" );
		mytext.onkeypress = captureReturn;
	}
	else
		alert(result.error.Message);
}

function showChat(visible)
{
	chat = getElement("chatPanel");
	if(visible)
		chat.style.visibility="visible";
	else
		chat.style.visibility="hidden";
}

function removeLogin()
{
	login = getElement("loginPanel");
	login.style.display = "none";
}

function showUpdating(visible)
{
	o = getElement("updating");
	if(visible)
		o.style.visibility="visible";
	else
		o.style.visibility="hidden";
}