/* ****************** */
/* *** javascript *** */
/* ****************** */
/* author: TriGen     */ 

// var fm = document.getElementById('flash_obj_id');
// fm.SetVariable("/:varname", 'varvalue');

/* *** global variables *** */

var hardcoded_url     = "http://trig.ath.cx/"; //necessary for flash to enable xmlsockets
var world             = "default";
var force_local       = false;
var timer_eb          = null; // enable button timer
var IE                = document.all?true:false;

if (!IE)
	document.captureEvents(Event.MOUSEMOVE)

var mouse_x           = 0;
var mouse_y           = 0;
var active            = false;
var player            = new Object();
var our_id            = -1; // temp!
var walk_speed        = 100; // millisecs
var baloon_speed      = 6000; // ,,
var walk_stepwidth    = 5; // pixels

var nick_offset_x     = -38;
var nick_offset_y     = +35;

var baloon_offset_x   = -38;
var baloon_offset_y   = -43;

var prev_js_exec      = 0;
var prev_js_exec_sent = 0;

var flash_width       = 315;
var flash_height      = 34;

var dont_make_button  = true;
var shutdown          = false;
var fm = null;
var rpgchat_isbuild   = false;
var rpgchat_noflash   = false;

function uo_rpgchat_build()
{
	if (IE || true)
	{
	/* *** setup *** */
	try
	{
		// Detect Flash first
		if (!FlashInstalled())
		{
			rpgchat_isbuild = false;
			rpgchat_noflash = true;
			return;
		}
	
		// draw button to enter the chatworld
		// (will disappear after 4 seconds)
		if (!dont_make_button)
			uo_make_enable_button();
		// optional:
		uo_make_fadebg_div();

		document.onkeydown   = doKeyDown;
		document.onmousedown = doMouseDown;
		// TEMPORARY WHILE BUILDING: 
		//uo_enable();
		//--
		rpgchat_isbuild = true;
	}
	catch (e)
	{
		alert('error: ' + e);
	}
	} else { window.status = 'rpgchat only supported with IE'; }
}

/*--*/
//function test()
//{
//alert('test');
//	var fm = document.getElementById('uo_flash_obj_id');
//	fm.SetVariable("fakeEvent.text", 'varvalue');
//	alert('done');
//}
/* *** player object *** */
function playerObj(uid, nickname, character, x, y, target_x, target_y)
{
	this.uid = uid;
	this.nickname = nickname;
	this.character = character;
	this.images = new Object();
	this.x = x;
	this.y = y;
	this.target_x = target_x;
	this.target_y = target_y;

	this.getAngle = player_angle;
	this.getDistance = player_distance;
	this.move = player_move;

	this.spawnimg  = player_spawnimg;
	this.nickfield = null;
	this.baloon    = null;

	this.walk = player_walk;
		this.walkfunc = player_dontwalk;
		this.walkvar = 0;
		this.walkvardir = 1;
		this.walktimer = null;

	for (i=1; i<=12; i++)
	{
		this.images[i] = new Image();
		// alert("rpgchat/chars/" + this.character + i + ".gif");
		this.images[i].src = (force_local ? "" : hardcoded_url) + "rpgchat/chars/" + this.character + i + ".gif";
	}

	{	
		this.spawnimg();
	}

	this.baloontimer = null;
	this.showbaloon  = showbaloon; 
	this.hidebaloon  = hidebaloon;

}
function showbaloon() { if (active == true) { this.baloon = fetch("baloon_"  + this.uid); this.baloon.style.display = 'block'; } }
function hidebaloon() { this.baloon = fetch("baloon_"  + this.uid); this.baloon.style.display = 'none'; }

function player_spawnimg()
{
	this.imgObj    = fetch("player_"  + this.uid);
	this.nickfield = fetch("nickname_"+ this.uid);
	this.baloon    = fetch("baloon_"  + this.uid);

	if (this.imgObj == null)
	{
		fetch("player_field").innerHTML += '<img border="0" \n\
										 src="'+ this.images[5].src +'" \n\
										 style="position: absolute; \n\
												left: '+this.x+'px; display: none; \n\
												top: '+this.y+'px; z-index: '+this.uid+';" \n\
										 id="player_'+this.uid+'" />';

		fetch("player_field").innerHTML += '<span style="position:absolute;left:'+(this.x + nick_offset_x)+'px;top:'+(this.y + nick_offset_y)+
			'px; width: 100px; font: 11px courier new; display: none; z-index: '+this.uid+'; text-align: center;" id="nickname_'+this.uid+'">'+this.nickname+'</span>';

		fetch("player_field").innerHTML += '<span style="display: none; position:absolute;left:'+(this.x + nick_offset_x)+'px;top:'+(this.y + nick_offset_y)+
			'px; width: 100px; height: 40px; overflow: hidden; z-index: '+this.uid+'; font: 9px arial; border: solid 1px #000000; padding: 1px; background-color: #ffffff; text-align: left;" id="baloon_'+this.uid+'">joh!</span>';
										  
		this.imgObj    = fetch("player_"   + this.uid);
		this.nickfield = fetch("nickname_" + this.uid);
		this.baloon    = fetch("baloon_"   + this.uid);
		if (active) { this.imgObj.style.display = 'block'; }
	} else {
		this.imgObj.style.left = this.x;
		this.imgObj.style.top  = this.y;
		if (active) { this.imgObj.style.display = 'block'; }
	}
	
	if (this.nickfield != null)
	{
		this.nickfield.style.left = (this.x + nick_offset_x) + 'px';
		this.nickfield.style.top  = (this.y + nick_offset_y) + 'px';
		if (active) { this.nickfield.style.display = 'block'; }
		this.nickfield.innerHTML = this.nickname;
	}
	
	if (this.baloon != null)
	{
		this.baloon.style.left = (this.x + baloon_offset_x) + 'px';
		this.baloon.style.top  = (this.y + baloon_offset_y) + 'px';
		this.baloon.innerHTML = "";
		this.baloon.style.display = 'none';
	}
}
function player_angle()
{
	var dx = this.x - this.target_x;
	var dy = this.y - this.target_y;
	
	var slope = dy / dx;
	var angle = Math.atan(slope);

	if (dx < 0)
		angle += Math.PI;

	angle = 180 * angle          / Math.PI;
	//    = 180 * AngleInRadians / Math.PI

	while (angle < 0)
		angle += 360;

	return angle;
}
function player_distance()
{
	return Math.sqrt( (this.x-this.target_x)*(this.x-this.target_x) + (this.y-this.target_y)*(this.y-this.target_y) ); 
}
function general_distance(x, y, target_x, target_y) {
	return Math.sqrt( (x-target_x)*(x-target_x) + (y-target_y)*(y-target_y) ); 
}
function player_dontwalk() {	
	return void(0); 
} 
function player_walk(walk_to)
{
//uo_console("going to walk: " + walk_to);
	switch (walk_to)
	{
		case 'right':
			this.walkfunc = player_walk_right;
			break;
		case 'left':
			this.walkfunc = player_walk_left;
			break;
		case 'up':
			this.walkfunc = player_walk_up;
			break;
		case 'down':
			this.walkfunc = player_walk_down;
			break;
		default:
			break;
	}
}

function player_move()
{
// math part
	var rads = this.getAngle() * Math.PI / 180.00;
	var radius = walk_stepwidth;
	var new_x = this.x - Math.cos(rads) * radius;
	var new_y = this.y - Math.sin(rads) * radius;
	
	if (!new_x) new_x = this.x;
	if (!new_y) new_y = this.y;

	if (this.getDistance() <= walk_stepwidth)
		new_x = this.target_x;
	if (this.getDistance() <= walk_stepwidth)
		new_y = this.target_y;
	
	if (new_x == this.target_x && new_y == this.target_y )
	{
		if (this.walkfunc == player_walk_right) { this.walkvar =  5; }
		if (this.walkfunc == player_walk_left) {  this.walkvar = 11; }
		if (this.walkfunc == player_walk_down) {  this.walkvar =  8; }
		if (this.walkfunc == player_walk_up) {    this.walkvar =  2; }
		
		fetch(this.imgObj.id).src = this.images[this.walkvar].src;

		this.walkfunc = player_dontwalk;
	} else {
		//alert(this.getAngle());
	}

	this.x = new_x;
	this.y = new_y;

	// update flashvars
	//if (this.uid == our_id)
	//	fetch('flashvars').value = "x="+this.x+"&y="+this.y+
	//							   "&target_x="+this.target_x+"&target_y="+this.target_y;
	//
	// update on the image itself
	
//	uo_console("updating img("+this.imgObj.id+") for " + this.uid + " with x: " + Math.round(this.x, 0));
	
	fetch(this.imgObj.id).style.left = '' + Math.round(this.x, 0) + 'px';
	fetch(this.imgObj.id).style.top  = '' + Math.round(this.y, 0) + 'px';
	
	try {
	
	fetch(this.nickfield.id).style.left = '' + Math.round((parseInt(this.x) + parseInt(nick_offset_x)), 0) + 'px';
	fetch(this.nickfield.id).style.top  = '' + Math.round((parseInt(this.y) + parseInt(nick_offset_y)), 0) + 'px';
	fetch(this.baloon.id).style.left = '' + Math.round((parseInt(this.x) + parseInt(baloon_offset_x)), 0) + 'px';
	fetch(this.baloon.id).style.top  = '' + Math.round((parseInt(this.y) + parseInt(baloon_offset_y)), 0) + 'px';
	
	} catch (e) { alert('' + Math.round((this.x + nick_offset_x), 0) + 'px');  }

	uo_update_flash();
}

function player_walk_right()
{
	if (this.walkvar < 4 || this.walkvar > 6)
		this.walkvar = 6;

	fetch(this.imgObj.id).src = this.images[this.walkvar].src;
	
	if (this.walkvar == 5) 
	{
		if (this.walkvardir == 1)
		{
			this.walkvar = 6;
			this.walkvardir = 0;
		} else {
			this.walkvar = 4;
			this.walkvardir = 1;	
		}
	} else {
		this.walkvar = 5;
	}
	
	if (this.walktimer != null)
		clearTimeout(this.walktimer);
	this.walktimer = setTimeout('player['+this.uid+'].walkfunc();', walk_speed);
	
	// now actually move!
	this.move();
	//alert("walking to right");
}
function player_walk_left()
{
	if (this.walkvar < 10 || this.walkvar > 12)
		this.walkvar = 11;

	fetch(this.imgObj.id).src = this.images[this.walkvar].src;
	
	if (this.walkvar == 11) {
		if (this.walkvardir == 1)
		{
			this.walkvar = 10;
			this.walkvardir = 0;
		} else {
			this.walkvar = 12;
			this.walkvardir = 1;	
		}
	} else {
		this.walkvar = 11;
	}
	
	if (this.walktimer != null)
		clearTimeout(this.walktimer);
	this.walktimer = setTimeout('player['+this.uid+'].walkfunc();', walk_speed);
	
	// now actually move!
	this.move();
	//alert("walking to right");
}
function player_walk_down()
{
	if (this.walkvar < 7 || this.walkvar > 9)
		this.walkvar = 8;

	fetch(this.imgObj.id).src = this.images[this.walkvar].src;
	
	if (this.walkvar == 8) {
		if (this.walkvardir == 1)
		{
			this.walkvar = 7;
			this.walkvardir = 0;
		} else {
			this.walkvar = 9;
			this.walkvardir = 1;	
		}
	} else {
		this.walkvar = 8;
	}
	
	if (this.walktimer != null)
		clearTimeout(this.walktimer);
	this.walktimer = setTimeout('player['+this.uid+'].walkfunc();', walk_speed);
	
	// now actually move!
	this.move();
	//alert("walking to right");
}
function player_walk_up()
{
	if (this.walkvar < 1 || this.walkvar > 3)
		this.walkvar = 2;

	fetch(this.imgObj.id).src = this.images[this.walkvar].src;
	
	if (this.walkvar == 2) {
		if (this.walkvardir == 1)
		{
			this.walkvar = 1;
			this.walkvardir = 0;
		} else {
			this.walkvar = 3;
			this.walkvardir = 1;	
		}
	} else {
		this.walkvar = 2;
	}
	
	if (this.walktimer != null)
		clearTimeout(this.walktimer);
	this.walktimer = setTimeout('player['+this.uid+'].walkfunc();', walk_speed);
	
	// now actually move!
	this.move();
	//alert("walking to right");
}

function uo_hide_players()
{
	for (p in player)
	{
		var o = player[p];
		fetch(o.imgObj.id).style.display = 'none';
		fetch(o.nickfield.id).style.display = 'none';
		fetch(o.baloon.id).style.display = 'none';
	}
}
function uo_show_players()
{
	for (p in player)
	{
		var o = player[p];
		fetch(o.imgObj.id).style.display = 'block';
		fetch(o.nickfield.id).style.display = 'block';

		if (o.baloontimer != null)
			fetch(o.baloon.id).style.display = 'block';
	}
}
/*--*/

function uo_test()
{
	alert("Test succeeded");
}

function uo_make_enable_button()
{
	document.write(' \
	\n\
		<div style="position:       absolute; \n\
					left:           2px; \n\
					top:            2px; \n\
					cursor:         hand; \n\
					border-right:   solid 1px #c0c0c0; \n\
					border-bottom:  solid 1px #c0c0c0;" \n\
			 onClick="javascript:uo_enable();" \n\
			 id="uo_enable_button"> \n\
				<div style="background-color:  #8E98BB; \n\
							border:            solid 1px #485377;; \n\
							padding:           3px; \n\
							font:              11px, trebuchet MS; \n\
							color: white;"> \n\
				\n\
					<b>RPG CHAT!</b> <br/> click here..\n\
				\n\
				</div> \n\
		</div> \n\
	\n\
	');
}
function uo_destroy_enable_button()
{
//	if (!dont_make_button)
//		fetch("uo_enable_button").style.display = 'none';
}
function uo_make_fadebg_div()
{
	document.write(' \
	\n\
		<div style="position:          absolute; \n\
					left:     		   0px; \n\
					top:      		   0px; \n\
					background-color:  white; \n\
					display:           none; \n\
					height:            100%; \n\
					width:             100%; \n\
					filter:            alpha(opacity=75); -moz-opacity: 0.75; \n\
					border:   		   solid 0px blue;" \n\
			 id="uo_fadebg"> \n\
		</div> \n\
		\n\
		<div style="position:          absolute; \n\
					left:     		   0px; \n\
					top:      		   0px; \n\
					display:           none; \n\
					height:            100%; \n\
					width:             100%; \n\
					background-color:  white; \n\
					filter:            alpha(opacity=0); -moz-opacity: 0.0; \n\
					border:   		   solid 0px blue;" \n\
			 id="uo_frame"> \n\
								\n\
				<!--url\'s used in the movie-->\n\
				<!--text used in the movie-->\n\
				&nbsp; <!-- &nbsp; required for IExplorer, otherwise it will not show the object within this <div> -->\n\
				<object style="border: solid 0px red; " id="uo_flash_obj_id" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'+flash_width+'" height="'+flash_height+'" id="test">\n\
					<param name="movie" value="'+(force_local ? "" : hardcoded_url)+'rpgchat/flash/flashcom.swf" />\n\
					<param name="quality" value="high" />\n\
					<param name="bgcolor" value="#ffffff" />\n\
					<param name="FlashVars" id="flashvars" value="" />\n\
					<param name="allowScriptAccess" value="always" />\n\
					<embed style="border: solid 2px blue;" id="uo_flash_obj_id_ns" src="'+(force_local ? "" : hardcoded_url)+'rpgchat/flash/flashcom.swf" quality="high" bgcolor="#ffffff" width="315" height="150" name="test" align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always" swLiveConnect="true" />\n\
				</object>\n\
		</div>\n\
			\n\
		<div style="border: solid 0px red;" id="player_field"></div> \n\
		<a style="display: none; background-color: #80FF80; position: absolute; left: 5px; top: 5px;  \n\
			 " href="javascript:uo_maximize_flash();" id="uo_flash_max_href">show flash console</a>\n\
');
}
//snip1: <param name="allowScriptAccess" value="trig.ath.cx" />

function uo_minimize_flash()
{
if (IE)
	var s = fetch("uo_flash_obj_id");
else
	var s = fetch("uo_flash_obj_id_ns");

	s.style.width  = 1;
	s.style.height = 1;

	fetch("uo_flash_max_href").style.display = "block";
}

function uo_maximize_flash()
{
if (IE)
	var s = fetch("uo_flash_obj_id");
else
	var s = fetch("uo_flash_obj_id_ns");

	s.style.width  = flash_width;
	s.style.height = flash_height;

	fetch("uo_flash_max_href").style.display = "none";
}

function uo_update_flash()
{
	// force string
	player[our_id].x += '';
	player[our_id].y += '';
	player[our_id].target_x += '';
	player[our_id].target_y += '';
	
	// so we can replace the , ;)
	fm.SetVariable("/:current_x", player[our_id].x.replace(',','.'));
	fm.SetVariable("/:current_y", player[our_id].y.replace(',','.'));
	fm.SetVariable("/:target_x",  player[our_id].target_x.replace(',','.'));
	fm.SetVariable("/:target_y",  player[our_id].target_y.replace(',','.'));
}

function uo_setme(me) {
	our_id = me;
	//window.status = "we are: " + me + "ready to spawn";
}

function uo_enable()
{
if (!rpgchat_isbuild)
{
	if (rpgchat_noflash)
		alert("RPG Chat requires the Flash 7 plugin, please make sure it is installed.");
	else
		alert("RPG Chat was not build, uo_rpgchat_build() should be called on your site");
	return;
}
if (shutdown)
{
	alert("You cannot start RPG Chat, perhaps due to an outdated Flash version (Flash 7 is required), Or due to a fatal error.");
	return;
}

// could be visible player objects
	uo_hide_players();
	uo_hide2_flash();

	//if (active == true)
	{
		uo_show_players();
	}
active = true;
	uo_destroy_enable_button();
	
	// optional half blur layer:
	//uo_show_fadebg();
	// display the frame (the `world`)
	uo_show_frame();

	// temporary:
	//

	//fetch("uo_fadebg").style.display = 'none';
	//fetch("uo_frame").style.display = 'none';
	//--

	document.body.style.cursor = 'hand';

	// show flash:
//	var s = (fetch("uo_flash_obj_id")).style;
//	s.width 	= (flash_width)+'px';
//	s.height    = (flash_height)+'px';

uo_show_flash();

if (IE)
{
	try { fm = fetch('uo_flash_obj_id'); } catch (e) { 
		try { fm = fetch('uo_flash_obj_id_ns'); } catch (e) { alert('failed fetching uo_flash_obj_ns'); }
	}
}
else
	fm = fetch('uo_flash_obj_id_ns');

	//alert('setting world: ' + world);
	try { fm.SetVariable("/:world", world); } catch (e) { // fucking mozilla takes more time to load the damn flash object, 
	// we request uo_giveflash_world() in the flash itself, so mozilla will get the world anyways
	}
}

function uo_giveflash_world()
{
//alert('setting world: ' + world);
try {
	fm.SetVariable("/:world", world);
} catch(e) { alert('#' + e); }
}

function uo_del_player(id)
{
	if (player[id].walktimer != null)
		clearTimeout(player[id].walktimer);
	
	fetch(player[id].imgObj.id).style.display = 'none';
	fetch(player[id].nickfield.id).style.display = 'none';
	fetch(player[id].baloon.id).style.display = 'none';

	player[id] = null;
}
/*function uo_update_target(id, target_x, target_y)
{
	player[id].target_x = target_x;
	player[id].target_y = target_y;

}*/
var debug = "";
function uo_add_player(id, nick, chara, x, y, target_x, target_y)
{
//try {
	player[id] = new playerObj(id, nick, chara, x, y, target_x, target_y);	
	uo_set_target(id, target_x, target_y); // target_x & target_y are useless, but for the sake of consistency....

	if (id == our_id)
	{
		uo_update_flash();
		//uo_spawn(); //temp
	}
	//alert("added player: " + id);
	debug = debug + "\n" + id;
//} catch (e) { alert(e); }
}
function deepbug() { alert(debug); }
function uo_spawn()
{
	uo_hide_fadebg();
}


function uo_disable()
{
active = false;
//our_id = -1;
	//uo_destroy_frame();
	//uo_destroy_fadebg();

	uo_hide_frame();

//from now on, disable will not destroy our stuff, just hide it, so all can be rejoined anytime..

	uo_show_enable_button();
	
	document.body.style.cursor = '';

	uo_hide_players();
	uo_hide2_flash();

	window.status = '';
}

function uo_shutdown()
{
active = false;
shutdown = true;
our_id = -1;
	uo_hide_frame();

	uo_show_enable_button();
	
	document.body.style.cursor = '';

	uo_hide_players();

	uo_hide2_flash();

	window.status = '';
}

function uo_show_fadebg() { (fetch("uo_fadebg")).style.display = 'block'; }
function uo_show_frame() { (fetch("uo_frame")).style.display = 'block'; (fetch("uo_frame")).style.visibility = 'visible'; }
function uo_show_enable_button() { if (!dont_make_button) (fetch("uo_enable_button")).style.display = 'block'; } //LIVE: timer_eb = setTimeout('uo_destroy_enable_button();', (2 * 1000)); }

function uo_destroy_fadebg() { (fetch("uo_fadebg")).style.display = 'none'; }
function uo_destroy_frame() { (fetch("uo_frame")).style.display = 'none'; }

function uo_hide_fadebg() { (fetch("uo_fadebg")).style.visibility = 'hidden'; }
function uo_hide_frame() { (fetch("uo_frame")).style.visibility = 'hidden'; }

// more 'set flash' 
function uo_show_flash() { 

	if (IE)
	{
		try { var s = (fetch("uo_flash_obj_id")).style; } 
		catch (e) { 
			var s = (fetch("uo_flash_obj_id_ns")).style;
		} 
	}
	else
		var s = (fetch("uo_flash_obj_id_ns")).style;

	s.width 	= flash_width + 'px';
	s.height 	= flash_height + 'px';
	s.position 	= 'absolute';
	s.top 		= '0px';
	s.left 		= '0px';
}

function uo_hide2_flash()
{ 
	if (IE)
	{
		try { var s = (fetch("uo_flash_obj_id")).style; } catch (e) { return; } 
	}
	else
		var s = (fetch("uo_flash_obj_id_ns")).style;

	s.width 	= '0px';
	s.height 	= '0px';
}

/* *** general functions *** */
function fetch(id) 
{
	if (document.getElementById)
		return document.getElementById(id);
	else if (document.all)
		return document.all[id];
	else 
		return null;
}

/* *** mouse functions (X,Y coordinates) *** */
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;
function getMouseXY(e)
{
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  
	if (tempX < 0) { tempX = 0; }
	if (tempY < 0) { tempY = 0; }  
	
	//document.Show.MouseX.value = tempX;
	//document.Show.MouseY.value = tempY;

	mouse_x = tempX;
	mouse_y = tempY;

	return true;
}

//compatability workaround ;))
function uo_set_targetE(id, x, y, target_x, target_y)
{
	if (id != our_id)
	{
		if (general_distance(player[id].x, x, player[id].y, y) >= 50 ) // smoother walking ;)
		{
			player[id].x = x;
			player[id].y = y;
		}

	} else {
		player[id].x = x;
		player[id].y = y;
	}
	uo_set_target(id, target_x, target_y);
}
function uo_set_target(id, x, y)
{
try {
	player[id].target_x = (x );
	player[id].target_y = (y );

	//uo_console("new coords: " + player[id].target_x + ", " + player[id].target_y);
 
	if (our_id == id)
		uo_update_flash();
							   
	//alert('angle: ' + player[id].getAngle() + ' distance: ' + player[id].getDistance());
	
	var temp = player[id].getAngle();
	//uo_console("temp for us: " + temp);
	if (temp >= 135 && temp < 225)
	{
		player[id].walk('right');
	} else if (temp >= 225 && temp < 315)
	{
		player[id].walk('down');
	} else if (temp >= 45 && temp < 135)
	{
		player[id].walk('up');
	} else {
		player[id].walk('left');
	}
	
	player[id].walkfunc();
} catch (e) {
	uo_shutdown();
}
}

function doMouseDown()
{
	if (!active || our_id == -1)
		return true;

	uo_set_target(our_id, mouse_x - 15, mouse_y - 40);
	
//	window.status = "dbg: " + mouse_x + "," +mouse_y;
try{
	fm.SetVariable("/:newtarget", '1');

	//ERROR: not allowed from fighterz.net e.g.
} catch(e) { alert('doMouseDown: ' + e); }
}

/* *** keyboard functions *** */
var browserIsOK = 0;

function findKeyMap (keycode, shift, ctrl, alt) {
    for (var i = 0; i < fKeyMap.length; i++)
        if (fKeyMap[i].keyCode  == keycode
            && fKeyMap[i].shift == shift
            && fKeyMap[i].ctrl  == ctrl
            && fKeyMap[i].alt   == alt)
               if (fKeyMap[i].isMapped)
                  return ("mapped as " + fKeyMap[i].vKey);
               else
                  return ("unmapped as " + fKeyMap[i].vKey);

    return "not mapped to anything in the gwebkbd.js table";
} // findKeyMap

function doKeyDown() 
{
    if (browserIsOK != 0)
        return true;

	var e              = event;

    var myKeyCode      = e.keyCode;
    var mySrcElement   = e.srcElement;
    var isShiftPressed = e.shiftKey;
    var isCtrlPressed  = e.ctrlKey;
    var isAltPressed   = e.altKey;

    // Enter(13), Shift(16), Ctrl(17), Alt(18), CapsLock(20) keys?
    if (myKeyCode >= 13 && myKeyCode <= 20)
        return true;

	if (false)
	{
		alert("Key code=" + myKeyCode 
			+ "; Shift=" + isShiftPressed
			+ "; Ctrl=" + isCtrlPressed
			+ "; Alt=" + isAltPressed
			+ "\nThis key is " + findKeyMap (myKeyCode, isShiftPressed, isCtrlPressed, isAltPressed)
			);
   }
   
   parseKey(myKeyCode);
	
   return true;
} // doKeyDown

browserIsOK = navigator.appVersion.indexOf("MSIE ");
browserIsOK = true;
if (browserIsOK < 0) 
{
   browserIsOK = 0;
   window.status = "Warning:\n\
          You are using a browser that does not support keyboard mapping.\n";
} 
else if (parseFloat (navigator.appVersion.slice(browserIsOK+5)) < 5.0) 
{
   browserIsOK = 0;
   alert("Warning:\n\
   		  The IE version you are currently using will not give complete functionality.\n");
}

function parseKey(keycode)
{
	if (keycode == 27)
	{// escape key!
		if (active)
			uo_disable();
		else
			uo_enable();
	}
}

// js confirm
function uo_mark(id)
{
try {
	fm.SetVariable("/:prev_js_exec", id + '');
} catch (e) { alert('uo_mark: ' + e) } 

	window.status = "confirmed: " + id;
}

function uo_say(id, str)
{
//	alert(id + " says: " + str);
	fetch("baloon_" + id).innerHTML = str;
	player[id].text = str;
	player[id].showbaloon();
	
	if (this.baloontimer != null)
		clearTimeout(this.baloontimer);
	this.baloontimer = setTimeout('player['+id+'].hidebaloon();', baloon_speed);
}

// IE

function x()
{
	fetch("player_field").innerHTML += "heheH";
}

// Flash detect
function FlashInstalled()
{
	result = false;

try{
	if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])
	{
		result = navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin;
	}
	else if (document.all && (navigator.appVersion.indexOf("Mac")==-1))
	{
		// IE Windows only -- check for ActiveX control, have to hide code in eval from Netscape (doesn't like try)
		eval ('try {var xObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");if (xObj)	result = true; xObj = null;	} catch (e)	{}');
	}
}catch (e) { window.status = 'FlashInstalled: ' + e; }

	return result;
}

