/*
 DirProxy Class v0.3
 Based on FlashProxy.js (part of Flash / JavaScript Integration Kit,
 http://weblogs.macromedia.com/flashjavascript/)

 The DirProxy object is what proxies function calls between JavaScript and Director Shockwave.
 It handles all argument serialization issues.

*/

/**
 * Instantiates a new DirProxy object. Pass in a uniqueID and the name (including the path)
 * of the Flash proxy SWF. The ID is the same ID that needs to be passed into your Director content as lcId.
 */
function DirProxy(uid, proxySwfName)
{
	this.uid = uid;
	this.proxySwfName = proxySwfName;
	this.flashSerializer = new FlashSerializer(false);

	//IE 5 Mac
	if (!Array.prototype.push) Array.prototype.push=  function(val){this[this.length]=val;}
	if (!Function.prototype.apply) {
		Function.prototype.apply = function(oScope, args) {
		  var sarg = [];
		  var rtrn, call;
		  if (!oScope) oScope = window;
		  if (!args) args = [];
		  for (var i = 0; i < args.length; i++) sarg[i] = "args["+i+"]";
		  call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
		  oScope.__applyTemp__ = this;
		  rtrn = eval(call);
		  oScope.__applyTemp__ = null;
		  return rtrn;
		}
	}
}

/**
 * Call a function in your Director Shockwave content. Arguments should be:
 * 1. Director (Lingo/JS) function name to call
 * 2. any number of additional arguments of type object, array, string, number, boolean, date, null, or undefined.
 */
DirProxy.prototype.call = function()
{

    if (arguments.length == 0)
    {
        throw new Exception("Dir Proxy Exception", "The first argument should be the function name followed by any number of additional arguments.");
    }

    var qs = 'lcId=' + escape(this.uid) + '&functionName=' + escape(arguments[0]);

    if (arguments.length > 1)
    {
        var justArgs = new Array();
        for (var i = 1; i < arguments.length; ++i)
        {
            justArgs.push(arguments[i]);
        }
        qs += '&args=' + escape(this.flashSerializer.serialize(justArgs));
    }


	var dirProxyDivName = '_dir_proxy_' + this.uid;

	if(!document.getElementById(dirProxyDivName))
	{
		var newDirProxyDivTarget = document.createElement("div");
		newDirProxyDivTarget.id = dirProxyDivName;
		document.body.appendChild(newDirProxyDivTarget);
	}

	var dirProxyDivTarget = document.getElementById(dirProxyDivName);
	var isSafari = /Safari/.test(navigator.userAgent);
	var ft = new FlashTag(this.proxySwfName, 1, 1, isSafari);// force reload in safari!
	ft.setVersion('6,0,65,0');
	ft.setFlashvars(qs);

	dirProxyDivTarget.innerHTML = ft.toString();
		//alert("innerHTML: " + dirProxyDivTarget.innerHTML);

}

/**
 * This is the function that proxies function calls from Director Shockwave to JavaScript.
 * It is called implicitly.
 */
DirProxy.callJS = function()
{

    var functionToCall = eval(arguments[0]);
    var argArray = new Array();
    for (var i = 1; i < arguments.length; ++i)
    {
        argArray.push(arguments[i]);
    }
    functionToCall.apply(functionToCall, argArray);
}

