var DEBUG = false;

/*class*/ function Remoting (target/*:String*/, method/*:String*/) {
this.id = "Remoting" + Math.round(Math.random() * 100000000000000000);
this.method = method || "get";
this.target = target || "";

if (this.method == "get") {
if (DEBUG) {
document.write('<iframe id="' + this.id + '" name="' + this.id + '" style="width: 640px; height: 480px;"></iframe>');
} else {
document.write('<iframe id="' + this.id + '" name="' + this.id + '" style="position: absolute; visibility: hidden;"></iframe>');
}

this.iframe = document.getElementById(this.id);
this.iframe.Remoting = this;
} else {
document.write('<form id="' + this.id + 'Form" name="' + this.id + 'Form" target="' + this.id + '"style="position: absolute; visibility: hidden;"></form>');
this.form = document.getElementById(this.id + "Form");
this.form.method = "post";
}
}

Remoting.__load = function (frameWindow/*:Window*/, action/*:String*/, data/*:Object*/)/*:Void*/ {
if (!frameWindow.Remoting) {
frameWindow.Remoting = document.getElementById(frameWindow.name).Remoting;
}

if (action == "__redirect") {
frameWindow.parent.location = data;
return;
}

if (action == "__reload") {
frameWindow.parent.location.reload();
return;
}

try {
if (frameWindow.Remoting.owner) {
frameWindow.Remoting.owner[action](data);
} else {
frameWindow.Remoting[action](data);
}
} catch (e) {

}
}

/*private*/ Remoting.prototype.noCache = 0;

Remoting.prototype.destroy = function ()/*:Void*/ {
this.iframe.parentNode.removeChild(this.iframe);
}

/*depicated*/ Remoting.prototype.sendTo = function (url/*:String*/, data/*:Object*/,
cache/*:Boolean*/)/*:Void*/ {
if (this.method == "get") {
url += ((url.indexOf("?") != -1)
? "&"
: "?") +
(cache
? ""
: ("nocache" + this.noCache++));
if (data) {
for (var key in data) {
url += "&" + key + "=" + data[key];
}
}
this.iframe.src = url;
} else {
if (data) {
while (this.form.firstChild) {
this.form.removeChild(this.form.firstChild);
}

this.form.action = url;
for (var key in data) {
var hidden = document.createElement("input");
hidden.type = "hidden";
hidden.name = key;
hidden.value = data[key];
this.form.appendChild(hidden);
}
this.form.submit();
}
}
}

/*depicated*/ Remoting.prototype.send = function (data/*:Object*/, cache/*:Boolean*/)/*:Void*/ {
	this.sendTo(this.target, data, cache);
}

Remoting.prototype.invoke = function (/*AnyParams*/)/*:Void*/ {
if (typeof this[arguments[0] + "Result"] != "function") {
throw "Found '" + arguments[0] + "Result'.";
}

var data = new Object();
for (var i = 0; i < arguments.length; i++) {
data["params[" + i + "]"] = arguments[i];
}
this.sendTo(this.target, data);
}

/*depricated*/ Exchanger = Remoting;// TODO
/*depricated*/ var EXCHANGER_JS = true; // TODO

var REMOTING_JS = true;