myLib('popup-content').require();

var AjaxPopUp = function (id, _o, _c) {
	this.opts = jQuery.extend({}, AjaxPopUp.defaults, _o);
	this._context = _c;
	this._id = id;
	this.original_url = this.opts.url;
	if (this.opts.url == '') throw new Exception('No url specified!');
	if (this.opts.url.indexOf('?') != -1) {
		this.opts.url += '&_output=iframe';
	} else this.opts.url += '?_output=iframe';
	this.opts.context = this;
	this.opts.success = this.execComplete;
	this.opts.error = this.execError;
	var popup_opts = jQuery.extend({}, {fullscreen: true, width:200, height: 180}, this.opts.popup);
	this.opts.popup = popup_opts;
	this._popup = PopUp.create(this._id, popup_opts);
};
AjaxPopUp.defaults = {
	type: 'GET',
	async: true,
	crossDomain: true
}
AjaxPopUp.prototype= {
	show: function () {
		this._popup.setContent('<iframe src="'+this.opts.url+'" class="register-dialog" style="height: 465px;"></ifreame>').show();
		//jQuery.ajax(this.opts);
	},
	execComplete: function (response, status, ajaxObj) {
		if (ajaxObj.getResponseHeader('DZStatus') != 'success') {
			this._popup.hide();
			if (typeof(this.opts.onError) == 'function') this.opts.onError.apply(this._context, [-1, ajaxObj]);
			else {
				window.location.href = this.opts.url.substring(0, this.opts.url.indexOf('&_output'));
			}
			return false;
		}
		if (typeof(this.opts.beforeShow) == 'function') this.opts.beforeShow.apply(this._context, [response, status, ajaxObj]);
		this._popup.setContent(response).show();
		if (typeof(this.opts.afterShow) == 'function') this.opts.afterShow.apply(this._context, [response, status, ajaxObj]);
	},
	execError: function (ajaxObj, textStatus, errorThrown) {
		if (typeof(this.opts.onError) == 'function') {
			this._popup.hide();
			this.opts.onError.apply(this._context, [textStatus, ajaxObj, errorThrown]);
		} else {
			document.location.href = this.original_url;
			//SysMessages.alert('Unable to load the requested url.');
		}
	}
}
myLib('popup-ajax').register();
