/*	unFocusFlashCommunicator, version 0.8 (alpha) (2006/03/10)
	Copyright: 2005, Kevin Newman (http://www.unfocus.com/)
	License: http://creativecommons.org/licenses/LGPL/2.1/ */
if (typeof unFocus == "undefined") var unFocus = {};
if (!unFocus.Flash) unFocus.Flash = {};

// set up function for IE to use to create the fscommand catcher
if (window.ActiveXObject && window.print && !window.opera)
	// Communicator.vbs
	document.write('<scr'+'ipt type="text/vbscript"\>Sub unFocusCreateFSCommand (n)\nExecuteGlobal "Sub " & n & ',
		'"_FSCommand(ByVal c, ByVal a):" & "call " & n & "_','DoFSCommand(c, a):" & "End Sub"\nEnd Sub</scr'+'ipt\>');

// do  initialization stuff
unFocus.Flash.Communicator = function(_HTMLObj, _communicatorSWF) {
	/* provides reference for methods attatched to other objects 
	(like _CatchFSCommand) it will compress well too ;-) */
	var _this = this,
		_setVariableQueue = [],
		_comDivId = "unFocusFlashCommunicatorDiv",
		//_comSwfId = 'unFocusFlashCommunicatorSwf',
		_comDivRef,
		_connId,
		_movieObj;
	
	//var _movieId = (typeof _movieObj 'String')?_movieObj:_movieObj.getId();
	// a little emulated overloading here
	var _movieId;
	if (typeof _HTMLObj === "string") {
		_movieId = _HTMLObj;
	} else if (_HTMLObj.getId) {
		_movieId = _HTMLObj.getId();
		_this.setupHTML(_HTMLObj);
	}
	
	/*
	Private Method: _getFlashReference
		Tries to get a reference to the flash element
	*/
	function _getFlashReference() {
		// check embeds first, in case static html is used, in which case getElementById would 
		// return a reference to the object tag
		if (document[_movieId]) {
			_movieObj = document[_movieId];
		} else if (document.embeds && document.embeds[_movieId]){
			_movieObj = document.embeds[_movieId];
		} else {
			_movieObj = document.getElementById(_movieId);
		}
	}
	
	/* Private Method: _routeFSCommand
		Set up FSCommand catch functions - catch, and route fscommand calls.
		:NOTE: "this" keyword is scoped to window, so use the _this ref instead */
	function _routeFSCommand($command, $arguments) {
		// do the command
		// :NOTE: Safari places "FSCommand:" in front of anything sent from flash with fscommand for some reason..
		// This might be due to Safari using getURL with FSCommand as the protocol, which might be causeing some 
		// of the strange behavior with refreshing, and multiple calls. :TODO: look into this to see what versions
		// of Flash and Safari are doing this - also note, this might have been solved in newer Safari and Flash versions.
		if ($command.indexOf('FSCommand:') != -1) $command = $command.substring(10, $command.length);
		
		
		
		switch ($command) {
			case "unFocusFlashTestFSCommand":
					/* get reference to the flash Object */
					if (!_movieObj) _getFlashReference();
					// if we are here, then fscommand works!
					_this.setVariable("unFocusJavascriptCommunication","unFocusJavascriptUseFSCommand");
				break;
			case "unFocusFlashSetConnID":
					_connId = $arguments;
					_cycleSetVariableQueue();
				break;
			default:
				// call user's DoFSCommand
				//_DoFSCommand($command, $arguments);
				_this.notifyListeners("FSCommand", [$command,$arguments]);
				// call DoFSCommand (will be ignored by stub method, if not defined by user)
				_this.DoFSCommand($command, $arguments);
		}
	}
	// these all route to _routeFSCommand
	window[_movieId + "_"+"DoFSCommand"] = _routeFSCommand;
	// catch function for IE
	if (typeof unFocusCreateFSCommand != "undefined") unFocusCreateFSCommand(_movieId);
	
	// stub DoFSCommand - to be overwritten by user for classic style DoFSCommand usage.
	_this.DoFSCommand = new Function;

	/* Private Method: _cycleSetVariableQueue
		If a call to setVariable is made before the flash movie and all participants are ready for it,
		it is added to a Queue. This method clears that Queue when things become ready */
	function _cycleSetVariableQueue() {
		var _localQueue = _setVariableQueue;
		_setVariableQueue = []; // empty the array
		if (_localQueue.length) {
			// we may need a delay here, to give the comSwf time to load (or just pile up the comSwfs in the html)
			for (var i = 0; i < _setVariableQueue.length; i++) {
				_this.setVariable(_localQueue[i].$name, _localQueue[i].$value);
			}
		}
	}
	
	/* Method: setVariable
		Sets the variable in flash. This method emulates Macromedia's setVariable method exactly even when it
		isn't supported natively (it does that using the LocalConnection technique). */
	_this.setVariable = function($name, $value) {
		if (!_movieObj) _getFlashReference();
		// when the native function is available, use that
		if (typeof _movieObj.SetVariable != "undefined" && !window.opera) {
			// overwrite the emulated (default) setVariable function
			_this.setVariable = function($name, $value) {
				_movieObj.SetVariable($name, $value);
			};
			_this.SetVariable = _this.setVariable;
			//_cycleSetVariableQueue();
			// now run this call
			_this.setVariable($name, $value);
		// check _connId - if there is none, then the following will not work. _connId is sent out from 
		// the swf file when it fails to detect fscommand (and thus fails the test for SetVariable).
		} else if (_connId) {
			///// do setup ...
			// create the div to hold the communication flash movie(s)
			if (!document.getElementById(_comDivId)) { // in case user sets it up manually
				var _comDivRef = document.createElement("div");
				_comDivRef.id = _comDivId;
				_comDivRef.style.position = "absolute";
				_comDivRef.style.top = "-900px";
				//_historyFrame.runtimeStyle.display = 'none';
				document.body.insertBefore(_comDivRef,document.body.firstChild);
			}
			
			// set up the html object to reuse for sending in the data
			var $element = new unFocus.Flash.HTML();
			$element.setSrc(_communicatorSWF);
			$element.setWidth(1);
			$element.setHeight(1);
			$element.setVersion("6");
			$element.setMinorRevision("29");
			$element.setAllowscriptaccess("always");
			
			///// overwrite the original function:
			_this.setVariable = function($name, $value) {
				// :NOTE: this cannot be used until the receiving flash movie passes out the connectionID
				// do localconnection thingy
				$element.setFlashvars("cid="+_connId+"&cmd=setVariable&vName="+$name+"&vValue="+$value);
				
				_comDivRef.innerHTML = $element.getHTML();
			};
			_this.SetVariable = _this.setVariable;
			//_cycleSetVariableQueue();
			// run this call
			_this.setVariable($name, $value);
		} else {
			// add to queue
			_setVariableQueue.push({$name:$name,$value:$value});
		}
	};
	
	/* 
	Method: SetVariable
		Provides an alias to setVariable for consistancy with Flash.
	*/
	_this.SetVariable = _this.setVariable;
	
	_this.getHTML = function() {
		return _HTMLObj;
	};
};

unFocus.Flash.Communicator.prototype = new unFocus.Utilities.EventManager("FSCommand");

unFocus.Flash.Communicator.prototype.setupHTML = function($htmlObj) {
	$htmlObj.addFlashvar("unFocusFlashMovieId",$htmlObj.getId());
};

