var DEBUG = false;

/*class*/ function Exchanger (id/*:String*/) {
	//assert(id, "Exchanger\nNo id specified");
	this.id = id;
		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);
	 //assert(this.iframe.tagName == "IFRAME", "Exchanger\nthis.iframe")
	 this.iframe.exchanger = this;
}

Exchanger.__load = function (frameWindow/*:Window*/, action/*:String*/, data/*:Object*/)/*:Void*/ {
	//trace(action,  "Exchanger.__load: action");
	//trace(data, "Exchanger.__load: data");

	if (!frameWindow.exchanger)
		frameWindow.exchanger = document.getElementById(frameWindow.name).exchanger;

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

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

	try {
		if (frameWindow.exchanger.owner)
			frameWindow.exchanger.owner[action](data);
		else
			frameWindow.exchanger[action](data);
	} catch (e) {
		//assert(false, "Exchanger.__load\nNo " + action + " event handler exists");
	}
}

Exchanger.prototype.fakeId = 0;

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

Exchanger.prototype.redirect = function (href/*:String*/)/*:Void*/ {

}

Exchanger.prototype.sendTo = function (url/*:String*/, data/*:Object*/)/*:Void*/ {
	url += ((url.indexOf("?") != -1) ? "&" : "?") + "fakeId" + this.fakeId++;
	if (data) for (var key in data) url += "&" + key + "=" + data[key];
	this.iframe.src = url;
}

var EXCHANGER_JS;