/**
 * The FlashProxy object is what proxies function calls between JavaScript and Flash.
 * It handles all argument serialization issues. */

/**
 * Instantiates a new FlashProxy 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 Flash content as lcId. 
*/

function FlashProxy(uid, proxySwfName) {
   this.uid = uid;
   this.proxySwfName = proxySwfName;
   this.flashSerializer = new FlashSerializer(false);}

/** 
 * Call a function in your Flash content.  Arguments should be: 
 * 1. ActionScript function name to call, 
 * 2. any number of additional arguments of type object, 
 *    array, string, number, boolean, date, null, or undefined. 
*/

FlashProxy.prototype.call = function() {
    if (arguments.length == 0){throw new Exception("Flash 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 += ('&' + this.flashSerializer.serialize(justArgs));}

var divName = '_flash_proxy_' + this.uid;    

if(!document.getElementById(divName)){
    var newTarget = document.createElement("div");
        newTarget.id = divName;
        document.body.appendChild(newTarget);}

    var target = document.getElementById(divName);
    var ft = new FlashTag(this.proxySwfName, 1, 1);
    ft.setVersion('6,0,65,0');
    ft.setFlashvars(qs);
    target.innerHTML = ft.toString();}

/**
 * This is the function that proxies function calls from Flash to JavaScript.
 * It is called implicitly. */

FlashProxy.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);}