/**
 * Flash Proxy wrapper
 * @version 1.0
 * @author: Dwight Brown <dwightb at construct7.com>
 */
	 
;(function($)
{
	var _ready = false;
	var _queue = [];
	
	$.flashproxy =
	{
		enableQueue: true,
		debug:       false,
		swf:         "flashproxy.swf",
		target:      "flash-proxy",
		xpi:         "expressInstall.swf",
		flash:       "9.0.0",
		params:      { allowScriptAccess: "always", allowNetworking: "external", menu: "false" },
		attributes:  { id: "FlashProxy", name: "FlashProxy" },
		
		init: function()
		{
			log("jQuery.flashproxy.init()");
			
			if ( !swfobject )
			{
				log("SWFObject not available");
				return false;
			}

			var flashvars =
			    {
			    	onReady:  "jQuery.flashproxy.onReady",
			    	onLoaded: "jQuery.flashproxy.onFinish",
			    	onError:  "jQuery.flashproxy.onError"
			    };

			if ( $("#"+this.target).length == 0 && $("#"+this.attributes.id).length == 0 )
			{
				$("body").append('<div style="position:absolute;top:-32px;left:-32px;"><div id="'+this.target+'"></div></div>');
				
				try
				{
					swfobject.embedSWF( this.swf, this.target, "32", "32", this.flash, this.xpi, flashvars, this.params, this.attributes );
				}
				catch ( error )
				{
					log( "FlashProxy Error: ", error );
					return false;
				}
			}
			
			return true;
		},
		
		get: function( uri )
		{
			log("jQuery.flashproxy.get('"+uri+"')");
			
			if ( !_ready && this.enableQueue )
			{
				this.queue( uri );
				return true;
			}
			else if ( _ready )
			{
				try
				{
					this.proxy().getData( uri );
				}
				catch(err)
				{
					log("Get Error:",err);
				}
				return true;
			}
			
			return false;
		},
		
		proxy: function()
		{
			log("jQuery.flashproxy.proxy()");
			
			var name = this.attributes.name || this.target;
			
			if ( navigator.appName.indexOf("Microsoft") != -1 )
			{
				return window[name];
         	}
         	else
         	{
            	return document[name];
			}
			
			//return $("#" + this.target).get( 0 );
		},
		
		queue: function( uri )
		{
			log("jQuery.flashproxy.queue()");
			
			_queue.push( uri );
		},
		
		processQueue: function()
		{
			log("jQuery.flashproxy.processQueue()");
			
			if ( _queue.length < 1 ) return;

			for ( i in _queue )
			{
				this.get( _queue[i] );
			}
		},
		
		onReady: function()
		{
			log("jQuery.flashproxy.onReady()");
			
			_ready = true;
			this.processQueue();
		},
		
		onFinish: function( data )
		{
			log("jQuery.flashproxy.onFinish()");
			log("data",data);
		},
		
		onError: function( error )
		{
			log("jQuery.flashproxy.onError()");
			log("error",error);
		}
	};
	
	function log()
	{
		if ( !$.flashproxy.debug ) return;
		
		if ( arguments.length > 1 )
		{
			console.log( arguments );
		}
		else
		{
			console.log( arguments[0] );
		}
	};
	
	/* Avoid errors in browser that do not support console.log() */
	if ( !window.console )
	{
		window.console = {};
		console.log = function()
		{
			var msg = '';
			for ( var i = 0; i < arguments.length; ++i )
			{
				if ( i > 0 ) cmd += "\n";
				cmd += arguments[i].toString();
			}		
			alert( msg );
		};
	}
})(jQuery);