FlashProxy.instances = {};
FlashProxy.options = {};
FlashProxy.options.swfLocation = (window.location.href.toLowerCase().indexOf("https") == 0 
                                  ? "https://" + location.hostname + "/stc/swf/FlashProxy.swf"
                                  : "http://"  + location.hostname + "/stc/swf/FlashProxy.swf");

function FlashProxy(tagOptions){
	this.queue = [];
	this.dispatch = FlashProxy.bind(this, this.waiter, this.dispatch);
	this.addListener = FlashProxy.bind(this, this.waiter, this.addListener);
	
	var self = this;
	var swfLocation = FlashProxy.options.swfLocation;
	
	var uid = "id_" + new Date().getTime();
	FlashProxy.instances[uid] = this;
	
	tagOptions = tagOptions || {};
	tagOptions.width = tagOptions.width || 0;
	tagOptions.height = tagOptions.height || 0;
	tagOptions.bgcolor = tagOptions.bgcolor || "#FFFFFF";
	
	var tag = [];
	tag.push('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ');
	tag.push(' codebase="https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" ');
	tag.push(' width="' + tagOptions.width + '" ');
	tag.push(' height="' + tagOptions.height + '" ');
	tag.push(' id="' + uid+ '"');
	tag.push(' align="middle">');
	
	tag.push('<param name="movie" value="' + swfLocation + '" />');
	tag.push('<param name="quality" value="high" />');
	tag.push('<param name="bgcolor" value="' + tagOptions.bgcolor + '" />');
	tag.push('<param name="FlashVars" value="id=' + uid + '"/>');
	tag.push('<param name="allowScriptAccess" value="always"/>');
	
	tag.push('<embed src="' + swfLocation + '" FlashVars="id=' + uid + '" ');
	tag.push(' allowScriptAccess="always" ');
	tag.push(' quality="high" ');
	tag.push(' bgcolor="' + tagOptions.bgcolor + '" ');
	tag.push(' width="' + tagOptions.width + '" ');
	tag.push(' height="' + tagOptions.height + '" ');
	tag.push(' name="' + uid + '" align="middle" ');
	tag.push(' type="application/x-shockwave-flash" ');
	tag.push(' pluginspage="https://www.macromedia.com/go/getflashplayer" />');
	
	tag.push('</object>');
	
	document.body.appendChild(document.createElement("div")).innerHTML = tag.join(""); 
	
	var ua = navigator.userAgent
	this.flash = ua.indexOf("MSIE") == -1 ? document[uid] : window[uid];
	
	var intervalId = setInterval(function(){
		if(self.flash.dispatch){
			clearInterval(intervalId);
			var func;
			while(func = self.queue.shift()){
				func();
			}
			self.dispatch = FlashProxy.prototype.dispatch;
			self.addListener = FlashProxy.prototype.addListener;
		}
	}, 50);
}

FlashProxy.prototype.waiter = function(func){
	var args = Array.prototype.slice.call(arguments);
	args.unshift(this);
	
	this.queue.push(
		FlashProxy.bind.apply(null, args));
	return null;
}

FlashProxy.prototype.dispatch = function(target, prop, args, store){
	return this.flash.dispatch(target, prop, args, store);
}

FlashProxy.prototype.addListener = function(event, func){
	this[event] = func;
	this.flash.addListener(event);
}


FlashProxy.onTrigger = function(uid, event){
	FlashProxy.instances[uid][event](Array.prototype.slice.call(arguments, 2));
}

FlashProxy.setMethods = function(obj, target, methods){
	if( ! FlashProxy.flash) {
		FlashProxy.flash = new FlashProxy();
	}
	
	FlashProxy.createProxyMethods(FlashProxy.flash, obj, target, methods);
}

FlashProxy.createProxyMethods = function(flash, obj, target, methods){
	for(var i in methods){
		obj[methods[i]] = (function(prop){
			return function(){
				return flash.dispatch(target, prop, Array.prototype.slice.call(arguments))
			}
		})(methods[i]);
	}
}

FlashProxy.prototype.setMethods = function(methods){
	FlashProxy.createProxyMethods(this, this, "target", methods);
}

FlashProxy.bind  = function(obj, method){
	var args = Array.prototype.slice.call(arguments, 2);
	return function() {
		return method.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
	}
}

