var _isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var _isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var _isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var _isFirefox = (navigator.userAgent.indexOf("Firefox") != -1) ? true : false;
var _isNetscape = (navigator.userAgent.indexOf("Netscape") != -1) ? true : false;
var _isNetscape7 = (navigator.userAgent.indexOf("Netscape/7") != -1) ? true : false;
var _isSafari = (navigator.userAgent.indexOf("Safari") != -1) ? true : false;
var _isAvant = (navigator.userAgent.indexOf("Avant Browser") != -1) ? true : false;


//***** static variables ******************************************************

var _movie = null;     // reference to the flash movie

var _isLoaded = false; // true if everything loaded

var _isReady = false;  // true if ready to communicate with flash app

var _keyCode = 0;      // keycode picked up from keydown event

var _extEdit;          // reference to the external editor

// need this _junk for Safari - don't use control codes

var _junk = "@o$m#&";  // some unlikely string so Safari will fire the select event

//***** initialization *********************************************************

function checkReady ()
// Called by the flash app to see if javascript is ready. When it is ready, call
// setFocusNeeds (bNeedsDocFocus, bNeedsMouseDown, bNeedsMouseTimer, bNeedsActivate);
// Note: Only Netscape needs a timer to block unwanted messages.
{
	console.log("checkReady()!");
    if (_isLoaded && (typeof _movie.setAppFocus == 'function'))
    {
        _isReady = true;
        _movie.setFocusNeeds (
          !_isOpera && !_isNetscape,  // bNeedsDocFocus
           _isNetscape || _isSafari,  // bNeedsMouseDown
           _isNetscape,               // bNeedsMouseTimer
          !_isOpera && !_isNetscape); // bNeedsActivate
        _setFocus ();

    }
    return _isReady;
}

function doLoad ()
// Do the initial setup and set _isLoaded to true. If document.getElementById is
// not available then this is some obsolete browser and _isLoaded and _isReady
// will never be set to true. In this rare case the flash app will timeout
// in its call to checkReady and proceed with no external keystroke handling.
// I don't think that flash version 9 runs in these obsolete browsers anyway
// and flash 9 AS3 is a requirement.
{
	console.log("doLoad() reached!");
    if (typeof document.getElementById != "undefined")
    { 
		console.log("setting movie...");
        _movie = _isIE ? window["Director"] : document["Director"];
		
        _isLoaded = true;
    }
}

//***** main focus handling ****************************************************

function setDocumentFocus ()
// When one of the browser windows such as the 'address bar' has focus, this
// function makes sure it removes the focus from that window and gives it to the
// flash app. This is called from the app in response to a mouse down or activation
// event if bNeedsDocFocus was set in checkReady. i.e. Is is not called for Opera
// or Netscape but they are still checked here. 
{
	console.log("setDocumentFocus()!");
    if (_isFirefox || _isSafari)
        _movie.focus();
    else if (_isIE || _isAvant)
        document.focus ();
    else if (!_isOpera && !_isNetScape)
        window.focus ();
}

//***** end Safari only ********************************************************

function flashAppReady() {
	console.log("flash app is ready!");
	checkReady();
	doLoad();
	setDocumentFocus();
}