/*
Macromedia(r) Flash(r) JavaScript Integration Kit
** BETA VERSION 0.1 : JQuery Flash JavaScript Gateway
## MUST BE USED WITH JQuery.flash,js
updated by designerzen. giving full credits to macromedia or whoever it is now.
Macromedia
*/
//var uid = new Date().getTime(); 
//var flashProxy = new FlashProxy(uid, "http://www.saffrolla.com/wp-content/themes/saffrolla/js/swfjs/JavaScriptFlashGateway.swf");


/* Create a new Exception object.  
 name: The name of the exception.  message: The exception message. */
function Exception(name, message)
{
    if (name)
        this.name = name;
    if (message)
        this.message = message;
}

/* Set the name of the exception. */
Exception.prototype.setName = function(name)
{
    this.name = name;
}

/* Get the exception's name.  */
Exception.prototype.getName = function()
{
    return this.name;
}

/* Set a message on the exception.  */
Exception.prototype.setMessage = function(msg)
{
    this.message = msg;
}

/* Get the exception message.  */
Exception.prototype.getMessage = function()
{
    return this.message;
}

/**
 * The FlashSerializer serializes JavaScript variables of types object, array, string,
 * number, date, boolean, null or undefined into XML. 
 */

/**
 * Create a new instance of the FlashSerializer.
 * useCdata: Whether strings should be treated as character data. If false, strings are simply XML encoded.
 */
function FlashSerializer(useCdata)
{
    this.useCdata = useCdata;
}

/**
 * Serialize an array into a format that can be deserialized in Flash. Supported data types are object,
 * array, string, number, date, boolean, null, and undefined. Returns a string of serialized data.
 */
FlashSerializer.prototype.serialize = function(args)
{
    var qs = new String();
    switch(typeof(args))
    {
        case 'undefined':
       	return 'undefined';
		
	    case 'object':
			if (args instanceof Date)
            {
                return args.getTime();
            }
            else // array or object
            {
                try
                {
                    return this._serializeXML(args);
                }
                catch (exception)
                {
                    throw new Exception("FlashSerializationException",
                                        "The following error occurred during complex object serialization: " + exception.getMessage());
                }
            }
            break;
      
	    default:
			return args;
		 }

}

FlashSerializer.prototype.getType = function(args)
{
	switch(typeof(args))
        {
            case 'undefined':
			   return 'undf';

            case 'string':
                return 'str';

            case 'number':
                return 'num';

            case 'boolean':
                return 'bool';

            case 'object':
                if (args[i] == null)
                {
                    return 'null';
                }
                else if (args[i] instanceof Date)
                {
                    return 'date';
                }
                else // array or object
                {
                    try
                    {
                       return 'xser';
                    }
                    catch (exception)
                    {
                        throw new Exception("FlashSerializationException",
                                            "The following error occurred during complex object serialization: " + exception.getMessage());
                    }
                }
                break;
			  }
}

/**
 * Private
 */
FlashSerializer.prototype._serializeXML = function(obj)
{
    var doc = new Object();
    doc.xml = '<fp>'; 
    this._serializeNode(obj, doc, null);
    doc.xml += '</fp>'; 
    return doc.xml;
}

/**
 * Private
 */
FlashSerializer.prototype._serializeNode = function(obj, doc, name)
{
    switch(typeof(obj))
    {
        case 'undefined':
            doc.xml += '<undf'+this._addName(name)+'/>';
            break;
        case 'string':
            doc.xml += '<str'+this._addName(name)+'>'+this._escapeXml(obj)+'</str>';
            break;
        case 'number':
            doc.xml += '<num'+this._addName(name)+'>'+obj+'</num>';
            break;
        case 'boolean':
            doc.xml += '<bool'+this._addName(name)+' val="'+obj+'"/>';
            break;
        case 'object':
            if (obj == null)
            {
                doc.xml += '<null'+this._addName(name)+'/>';
            }
            else if (obj instanceof Date)
            {
                doc.xml += '<date'+this._addName(name)+'>'+obj.getTime()+'</date>';
            }
            else if (obj instanceof Array)
            {
                doc.xml += '<array'+this._addName(name)+'>';
                for (var i = 0; i < obj.length; ++i)
                {
                    this._serializeNode(obj[i], doc, null);
                }
                doc.xml += '</array>';
            }
            else
            {
                doc.xml += '<obj'+this._addName(name)+'>';
                for (var n in obj)
                {
                    if (typeof(obj[n]) == 'function')
                        continue;
                    this._serializeNode(obj[n], doc, n);
                }
                doc.xml += '</obj>';
            }
            break;
        default:
            throw new Exception("FlashSerializationException",
                                "You can only serialize strings, numbers, booleans, objects, dates, arrays, nulls and undefined");
            break;
    }
}

/**
 * Private
 */
FlashSerializer.prototype._addName= function(name)
{
    if (name != null)
    {
        return ' name="'+name+'"';
    }
    return '';
}

/**
 * Private
 */
FlashSerializer.prototype._escapeXml = function(str)
{
    if (this.useCdata)
        return '<![CDATA['+str+']]>';
    else
        return str.replace(/&/g,'&amp;').replace(/</g,'&lt;');
}

/**
 * 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()
{
	// BEGIN ERROR CHECKING
    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.");
    }
	// END ERROR CHECKING
	
	var fv = new Object();
 	fv.lcId = this.uid;
	fv.functionName = arguments[0];
	       
    if (arguments.length > 1)
    {
        for (var i = 1; i < arguments.length; ++i)
        {
			var id = i-1;
			fv['t'+id] = 	this.flashSerializer.getType(arguments[i]);
			fv['d'+id] =this.flashSerializer.serialize(arguments[i]);
        }
    }

	 var divName = '_flash_proxy_' + this.uid;
 	// ** clear this div
	$('#'+divName).empty();
    // ** save this to the div
	$('#'+divName).flashembed(
		{
		    src: this.proxySwfName,
		    width: 1,    height: 1,
			
		}, fv
	);
}

/**
 * This is the function that proxies function calls from Flash to JavaScript.
 * It is called implicitly.
 */
FlashProxy.callJS = function()
{
if (arguments[0] != undefined)
	{
	    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);
	}
}

 jQuery(document).ready(function(){
 	var divName = '_flash_proxy_' + uid;
  	// if the flash element's div does not exist...
	$("body").append("<div id='"+divName+"'></div>");

	$('#'+divName).css( 'position' , 'absolute' );
	$('#'+divName).css( 'left' , '0px' );
	$('#'+divName).css( 'top' , '-500px' );
	$('#'+divName).css( 'width' , '1px' );
	$('#'+divName).css( 'height' , '1px' );
	$('#'+divName).css( 'overflow' , 'hidden' );
});
  