﻿PopupBase = Class.extend({

	setupHtml: function () {

		this.cnvKey = '#popup-canvas';
		this.bdrKey = '#popup-border';
		this.clsKey = '#popup-close';

		if (jQuery(this.cnvKey).length == 0) {
			var baseHTML
			= '<div id="popup-canvas"></div>'
			+ '<div id="popup-border"></div>'
			+ '<div id="popup-close"></div>'
			;
			jQuery(baseHTML).appendTo('body').css({ zIndex: 1000, position: 'absolute' }).hide();
		}
	},

	init: function (config) {

		if (!this.config) this.config = {};
		jQuery.extend(this.config, config);

		this.setupHtml();
		var popup = this;

		jQuery(this.html).appendTo('body').css({ zIndex: 1000, position: 'absolute' }).hide();
		jQuery(this.cnvKey).click(function () { popup.hide(); });
		jQuery(this.clsKey).click(function () { popup.hide(); });
	},

	show: function (callback) {

		var winH = jQuery(window).height();
		var winW = jQuery(window).width();

		// - dialog panel:

		var dlg = jQuery(this.dlgKey);

		var dlgW = dlg.outerWidth();
		var dlgH = dlg.outerHeight();
		var dlgT = dlg.offset().top;
		var dlgB = dlg.outerHeight() + dlgT;

		var scrollT = jQuery('html').scrollTop() + jQuery('body').scrollTop();

		var setT = (winH / 2) - (dlgH / 2) + scrollT;
		var setL = (winW / 2) - (dlgW / 2);

		dlg.css({ top: setT, left: setL });

		// - border panel:

		var bdr = jQuery(this.bdrKey);

		bdr.height(dlgH + 24);
		bdr.width(dlgW + 24);

		bdr.css({ top: setT - 12, left: setL - 12 });

		// - close panel:

		var cls = jQuery(this.clsKey);
		cls.css({ top: setT - 28, left: setL + dlgW - 8 });

		// - canvas panel:

		var cnv = jQuery(this.cnvKey);
		cnv.css('top', scrollT);
		cnv.fadeTo(200, .5, function () {
			dlg.fadeIn();
			bdr.fadeTo(200, .45);
			cls.fadeIn();

			if (typeof (callback) == 'function')
				callback();

		});

		if (typeof (this.config.onshow) == 'function')
			this.config.onshow();
	},

	hide: function (callback) {

		var dlg = jQuery(this.dlgKey); var cnv = jQuery(this.cnvKey);
		var bdr = jQuery(this.bdrKey); var cls = jQuery(this.clsKey);
		var popup = this;

		bdr.fadeOut(200);
		cls.fadeOut(200);
		dlg.fadeOut(200, function () {
			//dlg.removeClass('not-found-dialog-visible');
			dlg.hide();
			cnv.fadeOut(200, function () {
				if (typeof (callback) == 'function')
					callback();

				var config = popup.config;
				if (typeof (config.onhide) == 'function')
					config.onhide();
			});
		});
	}
});

IFramePopupBase = PopupBase.extend({

	show: function (config) {

		jQuery.extend(this.config, config);

		var id = (config && config.id) || 0;
		var src = (id > 0) ? this.config.url + id : this.config.url;
		jQuery(this.frmKey).attr('src', src);
		this._super();
	}
});
