Ext.define("Ext.ux.container.Icecap", {
	extend: "Ext.panel.Panel",
	alias: ["widget.icecap"],
	loadComplete: false,
	cmd: "",
	cfg: "",
	initCmd: "",
	config: {
		iframe: null,
		iframeId: ""
	},
	icecapclose: false,
	initComponent: function () {

		var i_self = this;
		i_self.callParent();

		var s_url = "";

		var a_parts = [];

		a_parts.push("func=" + i_self.cmd);

		if (i_self.cfg) {
			a_parts.push("config=" + i_self.cfg);
		}

		if (i_self.initCmd) {
			a_parts.push("initFunc=" + i_self.initCmd);
		}

		s_url = "/icecap/icecap.aspx?" + a_parts.join("&");

		var i_component = Ext.widget("component", {
			id: "icecap-frame-" + i_self.id,
			xtype: "component",
			autoEl: {
				tag: "iframe",
				style: "height: 100%; width: 100%; border: none;",
				src: s_url
			},
			listeners: {
				load: {
					element: "el",
					fn: function () {

						var i_widget = i_self.up("ip2widget");

						var i_frame = Ext.getCmp(i_self.getIframeId());
						if (i_frame.hidden) {
							i_frame.setVisible(true);
						}
						if (i_widget) {
							i_widget.ownerCt.unmask();
						}
						else {
							i_self.setLoading(false);
						}

						i_self.loadComplete = true;
						i_self.setIframe(i_frame);
						try {
							var i_content = i_frame.el.dom.contentWindow;
							i_content.widget = i_self;
							i_content.ip2 = ip2 || {};
						}
						catch (err) {}
					}
				},
				render: function () {
					var i_widget = i_self.up("ip2widget");
					if (i_widget) {
						if (i_widget.ownerCt) {
							if (!i_widget.ownerCt.hidden) {
								i_widget.ownerCt.mask(ip2.i18n({
									en: "Loading...",
									da: "Indlæser..."
								}));
							}
						}
					}
					else {
						i_self.setLoading(ip2.i18n({
							en: "Loading...",
							da: "Indlæser..."
						}));
					}
				}
			}
		});

		i_self.on({
			added: {
				fn: function() {
					var i_owner = i_self.up().up();
					if (i_owner) {
						i_owner.addListener("hide", function () {
							i_self.unmask();
						}, i_owner);
						i_owner.addListener("show", function () {
							if (!i_self.loadComplete) {
								var i_widget = i_self.up("ip2widget");
								if (i_widget) {
									i_widget.ownerCt.mask(ip2.i18n({
										en: "Loading...",
										da: "Indlæser..."
									}));
								}
							}
						}, i_owner);
					}
				}
			},
			"panel-removeTab": {
				fn: function() {
					this.close();
				}
			}
		});

		i_self.on({
			beforeclose: {
				fn: function(i_panel) {
					if (!i_self.icecapclose) {

						var i_iframe = i_self.getIframe() || Ext.getCmp(i_self.getIframeId());

						i_iframe.el.dom.contentWindow.postMessage("icecap-close", "*");
						return false;
					}
					else {
						return true;
					}
				}
			}
		});

		i_self._iframe = i_component;
		i_self.setIframeId("icecap-frame-" + i_self.id);

		i_self.add(i_component);
	}
});
