﻿//-------------------------------------------------------------------------
// CodeGear Chat Room Info script
//-------------------------------------------------------------------------
// Global Variables
//
// Use this variables to fill in default parameters that you don't want to override.
var chatRoomServer = "http://chat.codegear.com/";
// var chatRoomServer = "http://ussvs-bdn13.codegear.com/";
// var chatRoomServer = "http://jonathanconroe/chat/";
var chatRoomHasPeopleFormat = "<a href=\"{ROOMURL}\" target=\"_blank\" title=\"Chat with the {PEOPLE} {PEOPLESTR} in the {ROOM} room.\"><img src=\"/images/w3/icons-small-communication.gif\" />Join the chat!</a>";
var chatRoomNoPeopleFormat = "<a href=\"{ROOMURL}\" target=\"_blank\" title=\"Start a chat in the {ROOM} room.\"><img src=\"/images/w3/icons-small-communication.gif\" />Start a chat!</a>";
var chatRoomNoModeratorFormat = "{ROOM} chat room closed.";
var joinMeIfUserInRoomFormat = "<a href=\"{ROOMURL}\" target=\"_blank\">Join me in the {ROOM} chat room.</a>";
var joinMeIfUserNotInRoomFormat = "Not in the {ROOM} chat room right now, come back later.";
var chatWithMeUserAvailableFormat = "Chat with me now!";
var chatWithMeUserNotAvailableFormat = "Not available to chat.";
//-------------------------------------------------------------------------
// Function showRoomInfo
//
// Parameters:
// roomID:					Specifies the room ID of the room to retrieve information for.
//
// roomName:				Specifies the friendly name of the room.
//							Default = null
//
// onlyIfModeratorPresent:	Specifies whether to show a join link if no moderators are present.
//							Default = false
//
// hasPeopleFormat:			Specifies the format of the link/text to display if the room contains people.
//							Default = chatRoomHasPeopleFormat
//
// noModeratorFormat:		Specifies the format of the link/text to display if the room doesn't contain moderators and onlyIfModeratorPresent is true.
//							Default = chatRoomNoModeratorFormat
//
// noPeopleFormat:			Specifies the format of the link/text to display if the room doesn't contain anyone.
//							Default = chatRoomNoPeopleFormat
//
// Available substitutions:
// {PEOPLE}
// {MODERATORS}
// {USERS}
// {ROOM}
// {ROOMDESC}
// {PEOPLESTR}
// {MODERATORSTR}
// {USERSTR}
// {ROOMURL}
//-------------------------------------------------------------------------
function showRoomInfo( roomID, roomName, onlyIfModeratorPresent, hasPeopleFormat, noModeratorFormat, noPeopleFormat )
{
	if( typeof( roomName ) == "undefined" )
		roomName = null;

	if( typeof( onlyIfModeratorPresent ) == "undefined" )
		onlyIfModeratorPresent = false;

	if( typeof( hasPeopleFormat ) == "undefined" )
		hasPeopleFormat = chatRoomHasPeopleFormat;

	if( typeof( noModeratorFormat ) == "undefined" )
		noModeratorFormat = chatRoomNoModeratorFormat;

	if( typeof( noPeopleFormat ) == "undefined" )
		noPeopleFormat = chatRoomNoPeopleFormat;

	chatRoomInfoSections.push( new Array( roomID, roomName, onlyIfModeratorPresent, hasPeopleFormat, noModeratorFormat, noPeopleFormat ) );

	document.write( "<span id=\"chatRoomStatus_" + roomID + "_" + ( chatRoomInfoSections.length - 1 ) + "\" class=\"chatroominfo\">Please wait...</span>" );
}
//-------------------------------------------------------------------------
// Function joinMe
//
// Parameters:
// roomID:					Specifies the room ID of the room to retrieve information for.
//
// userID:					Specifies the user ID of the user to join.
//
// roomName:				Specifies the friendly name of the room.
//							Default = null
//
// userFriendlyName:		Specifies the friendly name of the user.
//							Default = null
//
// showIfNotInRoom:			Specifies whether to show the link if the user isn't in the room.
//							Default = false
//
// ifUserInRoomFormat:		Specifies the format of the link/text to display if the specified user is in the room.
//							Default = joinMeIfUserInRoomFormat
//
// ifUserNotInRoomFormat:	Specifies the format of the link/text to display if the specified user is not in the room.
//							Default = joinMeIfUserNotInRoomFormat
//
// Available substitutions:
// {ROOM}
// {ROOMID}
// {USER}
// {USERID}
// {ROOMURL}
//-------------------------------------------------------------------------
function joinMe( roomID, userID, roomName, userFriendlyName, showIfNotInRoom, ifUserInRoomFormat, ifUserNotInRoomFormat )
{
	if( typeof( roomName ) == "undefined" )
		roomName = null;

	if( typeof( userFriendlyName ) == "undefined" )
		roomName = null;

	if( typeof( showIfNotInRoom ) == "undefined" )
		showIfNotInRoom = false;

	if( typeof( ifUserInRoomFormat ) == "undefined" )
		ifUserInRoomFormat = joinMeIfUserInRoomFormat;

	if( typeof( ifUserNotInRoomFormat ) == "undefined" )
		ifUserNotInRoomFormat = joinMeIfUserNotInRoomFormat;

	joinMeSections.push( new Array( roomID, userID, roomName, userFriendlyName, showIfNotInRoom, ifUserInRoomFormat, ifUserNotInRoomFormat ) );

	document.write( "<span id=\"joinMe_" + ( joinMeSections.length - 1 ) + "\" class=\"joinmeinfo\">Please wait...</span>" );
}
//-------------------------------------------------------------------------
// Function chatWithMe
//
// Parameters:
// userID:					Specifies the user ID of the user to chat with.
//
// userFriendlyName:		Specifies the friendly name of the user.
//							Default = null
//
// userAvailableFormat:		Specifies the format of the link/text to display if the specified user is available and willing to be talked to.
//							Default = chatWithMeUserAvailableFormat
//
// userNotAvailableFormat:	Specifies the format of the link/text to display if the specified user is not available.
//							Default = chatWithMeUserNotAvailableFormat
//
// Available substitutions:
// {USER}
// {USERID}
// {CHATURL}
//-------------------------------------------------------------------------
function chatWithMe( userID, userFriendlyName, userAvailableFormat, userNotAvailableFormat )
{
	if( typeof( userFriendlyName ) == "undefined" )
		roomName = null;

	if( typeof( userAvailableFormat ) == "undefined" )
		userAvailableFormat = chatWithMeUserAvailableFormat;

	if( typeof( userNotAvailableFormat ) == "undefined" )
		userNotAvailableFormat = chatWithMeUserNotAvailableFormat;

	chatWithMeSections.push( new Array( userID, userFriendlyName, userAvailableFormat, userNotAvailableFormat ) );

	document.write( "<span id=\"chatWithMe_" + ( chatWithMeSections.length - 1 ) + "\" class=\"chatwithmeinfo\">Please wait...</span>" );
}
//-------------------------------------------------------------------------
// Start of script.
var chatRoomInfoSections = new Array();
var joinMeSections = new Array();
var chatWithMeSections = new Array();
var currentJoinMeIndex = 0;
var currentChatWithMeIndex = 0;

if( window.addEventListener )
{
	window.addEventListener( "load", refreshChatRoomStatus, false );
	window.addEventListener( "load", refreshJoinMeStatus, false );
	window.addEventListener( "load", refreshChatWithMeStatus, false );
}
else if( window.attachEvent )
{
	window.attachEvent( "onload", refreshChatRoomStatus );
	window.attachEvent( "onload", refreshJoinMeStatus );
	window.attachEvent( "onload", refreshChatWithMeStatus );
}

function refreshChatRoomStatus()
{
	if( chatRoomInfoSections.length > 0 )
		chatAjax( chatRoomServer + "roominfo", "onGotChatRoomInfo" );
}

function onGotChatRoomInfo( data )
{
	/*var lines = data.split( "\r\n" );
	var regex = /ChatRoomInfo\[ \"([A-Za-z0-9\-_]+)\" \] = new Array\( (\d+), (\d+), \"([A-Za-z0-9 \-_\+\:]+)\", \"([A-Za-z0-9 \-_\+]+)\", \"(.*?)\" \)\;/;

	for( var i = 0; i < lines.length; ++i )
	{
		var matches = lines[ i ].match( regex );
		if( matches )
			ChatRoomInfo[ matches[ 1 ] ] = new Array( parseInt( matches[ 2 ] ), parseInt( matches[ 3 ] ), matches[ 4 ], matches[ 5 ], matches[ 6 ] );
	}
	*/

	updateChatRooms();
}

function updateChatRooms()
{
	for( var i = 0; i < chatRoomInfoSections.length; ++i )
		updateChatRoom( i );

	setTimeout( "refreshChatRoomStatus()", 15000 );
}

function updateChatRoom( index )
{
	var item = chatRoomInfoSections[ index ];
	var Result = "";

	roomID = item[ 0 ];
	roomName = item[ 1 ];
	onlyIfModeratorPresent = item[ 2 ];
	hasPeopleFormat = item[ 3 ];
	noModeratorFormat = item[ 4 ];
	noPeopleFormat = item[ 5 ];

	if( typeof( ChatRoomInfo[ roomID ] ) == "object" )
	{
		if( !roomName )
			roomName = ChatRoomInfo[ roomID ][ 2 ];

		var roomDesc = ChatRoomInfo[ roomID ][ 3 ];
		var people = ChatRoomInfo[ roomID ][ 0 ];
		var moderators = ChatRoomInfo[ roomID ][ 1 ];
		var users = people - moderators;
		
		if( onlyIfModeratorPresent && moderators == 0 )
		{
			Result = noModeratorFormat;
		}
		else
		{
			if( people == 0 )
				Result = noPeopleFormat;
			else
				Result = hasPeopleFormat;

			// Result = makeRoomLink( roomID, Result, people );
		}

		Result = Result.replace( /\{PEOPLE\}/g, people );
		Result = Result.replace( /\{MODERATORS\}/g, moderators );
		Result = Result.replace( /\{USERS\}/g, users );
		Result = Result.replace( /\{ROOM\}/g, roomName );
		Result = Result.replace( /\{ROOMDESC\}/g, roomDesc );
		Result = Result.replace( /\{PEOPLESTR\}/g, ( ( people != 1 ) ? "people" : "person" ) );
		Result = Result.replace( /\{MODERATORSTR\}/g, ( ( moderators != 1 ) ? "moderators" : "moderator" ) );
		Result = Result.replace( /\{USERSTR\}/g, ( ( users != 1 ) ? "users" : "user" ) );
		Result = Result.replace( /\{ROOMURL\}/g, getRoomURL( roomID ) );
	}
	else
	{
		Result = "<!-- Room doesn't exist '" + roomID + "' -->";
		document.getElementById( "chatRoomStatus_" + roomID + "_" + index ).style.display = "none";
	}

	document.getElementById( "chatRoomStatus_" + roomID + "_" + index ).innerHTML = Result;
}

function refreshJoinMeStatus()
{
	currentJoinMeIndex = 0;

	refreshCurrentJoinMe();
}

function refreshCurrentJoinMe()
{
	if( currentJoinMeIndex >= joinMeSections.length )
	{
		setTimeout( "refreshJoinMeStatus()", 15000 );
		return;
	}

	var room = joinMeSections[ currentJoinMeIndex ][ 0 ];
	var user = joinMeSections[ currentJoinMeIndex ][ 1 ];

	chatAjax( chatRoomServer + "occupantinfo?room=" + escape( room ) + "&uid=" + escape( user ), "onGotOccupantInfo" );
}

function onGotOccupantInfo( data )
{
	var dataArray = parseData( data );
	var result = parseInt( dataArray[ "isInRoom" ] );

	if( !isNaN( result ) )
		updateJoinMe( currentJoinMeIndex, result, dataArray[ "friendlyRoomName" ] );

	++currentJoinMeIndex;
	refreshCurrentJoinMe();
}

function updateJoinMe( index, state, friendlyRoomName )
{
	var item = joinMeSections[ index ];

	roomID = item[ 0 ];
	userID = item[ 1 ];
	roomName = item[ 2 ];
	userFriendlyName = item[ 3 ];
	showIfNotInRoom = item[ 4 ];
	ifUserInRoomFormat = item[ 5 ];
	ifUserNotInRoomFormat = item[ 6 ];

	if( !roomName )
	{
		if( friendlyRoomName )
			roomName = friendlyRoomName;
		else
			roomName = roomID;
	}

	if( !userFriendlyName )
		userFriendlyName = userID;

	var Result;

	if( state == 1 )
		// Result = makeRoomLink( roomID, ifUserInRoomFormat );
		Result = ifUserInRoomFormat;
	else if( showIfNotInRoom )
		Result = ifUserNotInRoomFormat;
	else
		Result = "";

	Result = Result.replace( /\{ROOMID\}/g, roomID );
	Result = Result.replace( /\{ROOM\}/g, roomName );
	Result = Result.replace( /\{USERID\}/g, userID );
	Result = Result.replace( /\{USER\}/g, userFriendlyName );
	Result = Result.replace( /\{ROOMURL\}/g, getRoomURL( roomID ) );

	document.getElementById( "joinMe_" + index ).innerHTML = Result;
}

function refreshChatWithMeStatus()
{
	currentChatWithMeIndex = 0;

	refreshCurrentChatWithMe();
}

function refreshCurrentChatWithMe()
{
	if( currentChatWithMeIndex >= chatWithMeSections.length )
	{
		setTimeout( "refreshChatWithMeStatus()", 15000 );
		return;
	}

	var user = chatWithMeSections[ currentChatWithMeIndex ][ 0 ];

	chatAjax( chatRoomServer + "occupantinfo?uid=" + escape( user ), "onGotChatWithMeOccupantInfo" );
}

function onGotChatWithMeOccupantInfo( data )
{
	var dataArray = parseData( data );
	var result = parseInt( dataArray[ "isOpenChat" ] );

	if( !isNaN( result ) )
		updateChatWithMe( currentChatWithMeIndex, result );

	++currentChatWithMeIndex;
	refreshCurrentChatWithMe();
}

function updateChatWithMe( index, state )
{
	var item = chatWithMeSections[ index ];

	userID = item[ 0 ];
	userFriendlyName = item[ 1 ];
	userAvailableFormat = item[ 2 ];
	userNotAvailableFormat = item[ 3 ];

	if( !userFriendlyName )
		userFriendlyName = userID;

	var Result;

	if( state == 1 )
		Result = makePrivateChatLink( userID, userAvailableFormat );
	else
		Result = userNotAvailableFormat;

	Result = Result.replace( /\{USERID\}/g, userID );
	Result = Result.replace( /\{USER\}/g, userFriendlyName );
	Result = Result.replace( /\{CHATURL\}/g, getPrivateChatURL( userID ) );

	document.getElementById( "chatWithMe_" + index ).innerHTML = Result;
}

function getRoomURL( roomID )
{
	return chatRoomServer + roomID;
}

/*
function makeRoomLink( roomID, linkContents )
{
	return "<a href=\"" + getRoomURL( roomID ) + "\" target=\"_blank\">" + linkContents + "</a>";
}
*/

function getPrivateChatURL( userID )
{
	return chatRoomServer + "/index.html?chatwith=" + escape( userID );
}

function makePrivateChatLink( userID, linkContents )
{
	return "<a href=\"" + getPrivateChatURL( userID ) + "\" target=\"_blank\">" + linkContents + "</a>";
}

function chatAjax( url, callbackFunction, params )
{
	/*
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject( "Microsoft.XMLHTTP" );
	var ifModifiedSince = new Date( 0 );

	request.open( "GET", url, true );
	request.setRequestHeader( "If-Modified-Since", ifModifiedSince );

	request.onreadystatechange = function()
	{
		if( request.readyState == 4 && request.status == 200 )
		{
			if( request.responseText )
			{
				callbackFunction( request.responseText, params );
			}
		}
	}

	request.send( null );
	*/
	if( typeof( window.chatAjaxID ) == "undefined" )
		window.chatAjaxID = 1;
	else
		++window.chatAjaxID;

	if( url.indexOf( "?" ) == -1 )
		url += "?";
	else
		url += "&";
	url += "cb=" + callbackFunction + "&id=" + window.chatAjaxID;

	scriptObj = document.createElement( "script" );
	scriptObj.setAttribute( "type", "text/javascript" );
	scriptObj.setAttribute( "src", url );
	scriptObj.setAttribute( "id", "cgAutoUpdateScriptSection" + window.chatAjaxID );
	document.getElementsByTagName( "head" ).item( 0 ).appendChild( scriptObj );
}

function chatAjaxCallback( callbackFunction, id )
{
	var scriptObject = document.getElementById( "cgAutoUpdateScriptSection" + id );
	if( scriptObject )
	{
		var parentNode = scriptObject.parentNode;
		parentNode.removeChild( scriptObject );
	}

	eval( callbackFunction + "()" );
}

function parseData( data )
{
	var lines = data.split( "\r\n" );
	var Result = new Array();

	for( var i = 0; i < lines.length; ++i )
	{
		var segments = lines[ i ].split( "=", 2 );
		if( segments.length == 2 )
		{
			Result[ segments[ 0 ] ] = segments[ 1 ];
		}
	}

	return Result;
}

if( typeof( ChatRoomInfo ) == "undefined" )
	ChatRoomInfo = new Array();