/* ***JAVASCRIPT PART OF API
===========================================================================
==                          SimBEOS API version 1.0                      ==
==                         Copyright 2002, ISHLAB.RU                     ==
===========================================================================
*/
// Defining a Flash object
Flash = new Object();
// Defining default properties of Flash object
Flash._d = "|||";
Flash.onFSCommand = function() {
}
Flash.onInitialize = function() {
}
Flash.HAS_SCRIPT = false;
isMSIE = navigator.appName.indexOf("Microsoft") != -1;
isNN = navigator.appName.indexOf("Netscape") != -1;
// Creating fscommand support for MSIE
if (isMSIE) {
	document.write("<SCRIPT FOR=\"SWFlashObject\" EVENT=\"FSCommand(command,args)\">\n");
	document.write("SWFlashObject_DoFSCommand(command, args);\n");
	document.write("</SCRIPT>\n");
//	document.write('<object id="WB" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0"></object>')
}
// Defining fscommand event
function SWFlashObject_DoFSCommand(command, args) {
	if (command == "initAPI") {
		_fo = (isMSIE) ? SWFlashObject : document.SWFlashObject;
		Flash.HAS_SCRIPT = true;
		_setSimBEOS();
	} else {
		p = new String(args).split(Flash._d);
		c = new String(command).toLowerCase();
		switch (c) {
		case "alert" :
			alert(args);
			break;
		case "back" :
			if(args!="") {
				history.go(args*(-1));
			} else {
				history.back();
			}
			break;
		case "callsysfunc" :
			var a = new Array();
			for (var i = 2; i<9; i++) {
				a.push(p[i]);
			}
			_fo.SetVariable("Browser._tmp", p[0]+Flash._d+eval(p[1])(a[0], a[1], a[2], a[3], a[4], a[5]));
			break;
		case "callusrfunc" :
			var a = new Array();
			for (var i = 2; i<p.length; i++) {
				a.push(p[i]);
			}
			_fo.SetVariable("Browser._tmp", p[0]+Flash._d+eval(p[1]).apply(this, a));
			break;
		case "close" :
			//if (!isMSIE)
			//	self.close()
			//document.WB.ExecWB(45,2)
			window.close();
			break;
		case "forward" :
			if(args!="") {
				history.go(args);
			} else {
				history.forward();
			}
			break;
		case "getvar" :
			_fo.SetVariable("Browser._tmp", p[0]+Flash._d+eval(p[1]));
			break;
		case "killfocus" :
			if(args!="") {
				eval(args).blur();
			} else {
				window.blur();
			}
			break;
		case "moveby" :
			window.moveBy(p[0], p[1]);
			break;
		case "moveto" :
			window.moveTo(p[0], p[1]);
			break;
		case "navigate" :
			window.navigate(p[0]);
			break;
		case "open" :
			window.open(p[0], p[1], p[2]);
			break;
		case "replace" :
			locatiom.replace(p[0]);
			break;
		case "resizeby" :
			window.resizeBy(p[0], p[1]);
			break;
		case "resizeto" :
			window.resizeTo(p[0], p[1]);
			break;
		case "setfocus" :
			if(args!="") {
				eval(args).focus();
			} else {
				window.focus();
			}
			break;
		case "scrollby" :
			window.scrollBy(p[0], p[1]);
			break;
		case "scrollto" :
			window.scrollTo(p[0], p[1]);
			break;
		case "setvar" :
			eval(p[1]+"='"+p[2]+"'");
			_fo.SetVariable("Browser._tmp", p[0]+Flash._d+eval(p[1]));
			break;
		case "execscript" :
			_fo.SetVariable("Broswer._tmp", p[0]+Flash._d+new Function(p[1]).call(this));
			break;
		default :
			Flash.onFSCommand(command, args);
		}
	}
}
// Defining common methods
function _initEvents() {
	window.onblur = function() {
		_fireEvent("onKillFocus");
	}
	window.onfocus = function() {
		_fireEvent("onSetFocus");
	}
	document.onmousedown = function(e) {
		_setE(e);
		_setSimBEOSProps();
		var _b = null;
		if (isMSIE) {
			_b = window.event.button;
		}
		if (isNN) {
			_b = e.which;
		}
		_fireEvent("onMouseDown", _b);
	}
	document.onmouseup = function(e) {
		_setE(e);
		_setSimBEOSProps();
		var _b = null;
		if (isMSIE) {
			_b = window.event.button;
		}
		if (isNN) {
			_b = e.which;
		}
		_fireEvent("onMouseUp", _b);
	}
	document.onmousewheel = function() {
		_setE();
		_setSimBEOSProps();
		var _d = null;
		if (isMSIE) {
			_d = window.event.wheelDelta;
		}
		_fireEvent("onMouseWheel", _d);
	}
	if(isMSIE) {
		_fo.onmouseover = function() {
			_setE();
			_setSimBEOSProps();
			_fireEvent("onMouseOver");
		}
		_fo.onmouseout = function() {
			_setE();
			_setSimBEOSProps();
			_fireEvent("onMouseOut");
		}
	}

	if(isNN) {
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = function(evnt) {
		_setE(evnt);
		_setSimBEOSProps();
		_fireEvent("onMouseMove");
	}
	window.onload = function() {
		_fireEvent("onPageLoad");
	}
	window.onresize = function(evnt) {
		_setE(evnt);
		_setSimBEOSProps();
		_fireEvent("onResize");
	}
	window.onscroll = function(evnt) {
		_setE(evnt);
		_setSimBEOSProps();
		_fireEvent("onScroll");
	}
}
function _parsetype(v) {
	return typeof (v)+Flash._d+v;
}
function _setSimBEOS() {
	_fo.SetVariable("Browser._inst_", _parsetype(true));
	_fo.SetVariable("Browser._host_", _parsetype(document.location.host));
	_fo.SetVariable("Browser._hostname_", _parsetype(document.location.hostname));
	_fo.SetVariable("Browser._isMSIE_", _parsetype(isMSIE));
	_fo.SetVariable("Browser._isNN_", _parsetype(isNN));
	_fo.SetVariable("Browser._name_", _parsetype(navigator.appName));
	_fo.SetVariable("Browser._path_", _parsetype(document.location.pathname));
	_fo.SetVariable("Browser._port_", _parsetype(document.location.port));
	_fo.SetVariable("Browser._query_", _parsetype(document.location.search));
	_fo.SetVariable("Browser._referrer_", _parsetype(document.referrer));
	_fo.SetVariable("Browser._url_", _parsetype(document.location.href));
	_fo.SetVariable("Browser._version_", _parsetype(navigator.appVersion));
	Flash.onInitialize();
	_fo.SetVariable("Browser._init_",true);
	setInterval("_setSimBEOSProps()",1);
	_initEvents();
}
function _fireEvent(e, p) {
	if (Flash.HAS_SCRIPT) {
		if (arguments.length>1) {
			_fo.SetVariable("Browser._evt", e+Flash._d+p);
		} else {
			_fo.SetVariable("Browser._evt", e);
		}
	}
}
function _setSimBEOSProps() {
	var x;
	var y;
	var width;
	var height;
	var swidth;
	var sheight;
	var sleft;
	var stop;
	var ftop;
	var fleft;
	if (isMSIE) {
		x = window.screenLeft;
		y = window.screenTop;
		width = document.body.offsetWidth;
		height = document.body.offsetHeight;
		swidth = document.body.scrollWidth;
		sheight = document.body.scrollHeight;
		sleft = document.body.scrollLeft;
		stop = document.body.scrollTop;
		fleft = _fo.offsetLeft;
		ftop = _fo.offsetTop;
	}
	if (isNN) {
		x = window.screenX;
		y = window.screenY;
		width = window.innerWidth;
		height = window.innerHeight;
		fleft=0;
		ftop=0;
	}
	_fo.SetVariable("Browser._x_", _parsetype(x));
	_fo.SetVariable("Browser._y_", _parsetype(y));
	_fo.SetVariable("Browser._width_", _parsetype(width));
	_fo.SetVariable("Browser._height_", _parsetype(height));
	_fo.SetVariable("Browser._swidth_", _parsetype(swidth));
	_fo.SetVariable("Browser._sheight_", _parsetype(sheight));
	_fo.SetVariable("Browser._sx_", _parsetype(sleft));
	_fo.SetVariable("Browser._sy_", _parsetype(stop));
	_fo.SetVariable("Browser._fx_", _parsetype(fleft));
	_fo.SetVariable("Browser._fy_", _parsetype(ftop));
}
function _setE(evnt) {
	var xmouse;
	var ymouse;
	if (isMSIE) {
		xmouse = window.event.x;
		ymouse = window.event.y;
	}
	if(isNN) {
		xmouse=evnt.pageX;
		ymouse=evnt.pageY;
	}
	_fo.SetVariable("Browser._xmouse_", _parsetype(xmouse));
	_fo.SetVariable("Browser._ymouse_", _parsetype(ymouse));
}

// Defining methods for the Flash object
Flash.callFunction = function() {
	var func = arguments[0];
	if (arguments.length>1) {
		var args = arguments[1];
		var prms = "";
		for (var i = 0; i<args.length; i++) {
			prms += Flash._d+args[i];
		}
		_fo.SetVariable("Browser._fnc", func+prms);
	} else {
		_fo.SetVariable("Browser._fnc", func);
	}
	return _fo.GetVariable("Browser._fnc");
}
Flash.callGlobalFunction = function() {
	var func = arguments[0];
	if (arguments.length>1) {
		var args = arguments[1];
		var prms = "";
		for (var i = 0; i<args.length; i++) {
			prms += Flash._d+args[i];
		}
		_fo.SetVariable("Browser._gfnc", func+prms);
	} else {
		_fo.SetVariable("Browser._gfnc", func);
	}
	return _fo.GetVariable("Browser._gfnc");
}
Flash.linkJSEvent = function() {
	var jsobject = arguments[0];
	var jseventname = new String(arguments[1]).toLowerCase();
	var aseventname = arguments[2];
	var passvalue = arguments[3];
	var eventfunc;
	var callback = jsobject[jseventname];
	if (callback != null) {
		eventfunc = callback;
	} else {
		eventfunc = function () {
		}
	}
	if (arguments.length<4) {
		jsobject[jseventname] = new Function("e","___ecf="+eventfunc+"; ___ecf(); _setSimBEOSProps(); _setE(e); _fireEvent('"+aseventname+"');");
	} else {
		jsobject[jseventname] = new Function("e","___ecf="+eventfunc+"; ___ecf(); _setSimBEOSProps(); _setE(e); _fireEvent('"+aseventname+"',eval('"+passvalue+"'));");
	}
}
Flash.getPlayer = function() {
	return _fo;
}
Flash.setPlayer = function() {
	_fo = arguments[0];
	Flash.HAS_SCRIPT = true;
	_setSimBEOS();
}

function move(x,y){
	if (document.layers){
		var z=document.layers.flashHolder
		z.left=x
		z.top=y
	} else {
		var z=document.all.flashHolder.style
		z.pixelLeft=x
		z.pixelTop=y
	}
}
