<?xml version="1.0" encoding="iso-8859-1" ?>

<mx:MovieClip xmlns:mx="http://www.macromedia.com/2003/mxml"
 width="100" height="50" usePreloader="false">
	<mx:Script>
	<![CDATA[
		var lc:LocalConnection;
		var lconid:String;
		var app:String;
		var regInterval:Number;
		var handshakeTries = 0;

		function onLoad():Void
		{
			if (lc == undefined)
				initApp();
		}
		
		function onEnterFrame():Void
		{
			if (lc == undefined)
				initApp();
		}
		
		function parseRequestVars()
		{
			var str;
			if (_global.requestVars == undefined) _global.requestVars = new Object();
			for(var prop in _root){
				str = new String(prop);
				var ind = str.indexOf("$_");
				if(str.indexOf("$_") > -1) {
					var newProp = str.substr(str.indexOf("$_") + 2);
					_global.requestVars[newProp] = _root[prop];
				}
			}
		}

		// Init function is called to begin a handshake between the main movie
		// and this utility swf
		function register(id:String)
		{
			// Return the handshake
			_root.lc.send(id, "registered");
		}

		function registered()
		{
			if (_root.regInterval != undefined)
			{
				clearInterval(_root.regInterval);
				_root.regInterval = undefined;

				// Handshake complete, let's send the loadState message.
				_root.lc.send(_root.app + _root.lconid, "loadState", _global.requestVars);
			}
		}
		
		function initHandshake()
		{
			_root.lc.send(_root.app + _root.lconid, "register");

			_root.handshakeTries++;

			// Timeout after a while...
			if (_root.handshakeTries > 100)
			{
				clearInterval(_root.regInterval);
				_root.regInterval = undefined;
			}
		}
		
		function initApp()
		{			
			parseRequestVars();

			lconid = _global.requestVars.lconid;
			delete _global.requestVars.lconid;

			//open up the connection
			lc = new LocalConnection();

			lc.allowDomain = function (sendingDomain)
			{
				return true;
			}
			lc.onStatus = function (infoObject)
			{
			}
			lc["register"] = register;
			lc["registered"] = registered;

			lc.connect("history" + lconid);
			
			if (_global.requestVars.app != undefined)
			{
				app = _global.requestVars.app;

				delete _global.requestVars.app;
				
				// Rather than send the message here, we're going to wait
				// until we get a handshake from the main movie. This is because
				// sometimes the history swf is up and running before the main
				// movie.
				regInterval = setInterval(initHandshake, 50);
				initHandshake();
			}
			else
			{
				_root.lc.send("init" + _root.lconid, "loadInitialState");
			}			
		}
	]]>
	</mx:Script>
</mx:MovieClip>
