RemotingConstants = {};
RemotingConstants._listeners = [];
RemotingConstants.addListener = function(movieconn, remoteinstance){
	RemotingConstants._listeners[movieconn] = remoteinstance;
	if(RemotingConstants[movieconn]){
		RemotingConstants.setConnection(movieconn, RemotingConstants[movieconn]);
	}
}
// RemotingConstants.setConnection() is called by Flash to tell javascript it's ready to receive messages
RemotingConstants.setConnection = function(movieconn, connectvalue){
	RemotingConstants[movieconn] = connectvalue;
	if(RemotingConstants._listeners[movieconn]){
		RemotingConstants._listeners[movieconn].flashMovieDidLoad(RemotingConstants[movieconn]);
	}
}
// RemotingConstants.setAvailability() is called by gateway.swf to notify the remoting instance a new call can be made.
RemotingConstants.setAvailability = function(movieconn){
	RemotingConstants._listeners[movieconn].newCallAllowable();
}
RemotingConstants.incomingFlashCall = function(movieconn, func, val){
	RemotingConstants._listeners[movieconn].callToListeners(func, val);
}
RemotingConstants.streamFlashCall = function(movieconn, val){
	RemotingConstants._listeners[movieconn].streamValue(val);
}
RemotingConstants.endStreamFlashCall = function(movieconn, func){
	RemotingConstants._listeners[movieconn].endStreamValue(func);
}


//------------------------------------------
// FlashRemoting Class
//------------------------------------------
FlashGateway = function(movieid){
	this.movie = movieid;
	this.rconnection = null;
	this.movieconn = this.movie+"_connection"
	this.callarray = [];
	this.callallowed = true;
	this.listener = null;
	this.streamval = "";
	this.gateway = null;
	var ua = navigator.userAgent.toLowerCase(); 
	this.is_pc_ie = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );
	RemotingConstants.addListener(this.movieconn, this);
}
//------------------------------------------
// Public Functions
//------------------------------------------
FlashGateway.prototype.addListener = function(obj){
	this.listener = obj;
}
FlashGateway.prototype.callFunction = function(func, val){
	this.callarray.unshift( [func, val] );
	if(this.rconnection != null) this.sendToFlash();
}
//------------------------------------------
// Private Functions
//------------------------------------------
FlashGateway.prototype.streamValue = function(val){
	this.streamval += val;
}
FlashGateway.prototype.endStreamValue = function(func){
	//this.streamval = this.streamval.split("#singlequote#").join("'").split("#return#").join("\r").split("#newline#").join("\n");
	this.callToListeners(func, this.streamval);
	this.streamval = "";
}
FlashGateway.prototype.callToListeners = function(func, val){
	val = unescape(val);
	if(this.listener != null){
		this.listener[func](val);
	}else{
		eval(func)(val);
	}
}
FlashGateway.prototype.newCallAllowable = function(){
	this.callallowed = true
	this.sendToFlash();
}
FlashGateway.prototype.flashMovieDidLoad = function(connectname){
	this.rconnection = connectname;
	this.sendToFlash();
}
FlashGateway.prototype.sendToFlash = function(){
	if(this.callarray.length > 0 && this.callallowed == true){
	
		this.callallowed = false;
		var func_val = this.callarray.pop();
		if(this.is_pc_ie){
			document[this.movie].SetVariable("/_global/RemotingConstants:IESpeedCall", func_val[0]+"|||"+func_val[1]);
			this.newCallAllowable();
		}else{
			if(this.gateway == null){
				var divholder = document.createElement("div");
				divholder.setAttribute("id", this.movieconn);
				this.gateway = document.body.appendChild(divholder);
			}
			this.gateway.innerHTML = "";
			var gatewaydata = func_val[0]+"|||"+func_val[1];
			var divinfo = "<embed src='gateway.swf' FlashVars='lc="+this.rconnection+"&mc="+this.movieconn+"&rq="+escape(gatewaydata)+"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
			this.gateway.innerHTML = divinfo;
		}
		
	}
}