
Ext.define("Ext.ux.container.Widget", {

	alias: [
		"widget.ux.container.widget",
		"widget.widgetcontainer"
	],

	extend: "Ext.form.Panel",
	layout: "fit",

	config: {
		params: null,
		src: null,
		widget: true,
		attr: null
	},

	initComponent: function () {

		var i_widget = this;

		i_widget.title = ip2.i18n(i_widget.title);
		i_widget.callParent();

		var o_params = Ext.apply({}, {
			owner: i_widget,
			url: i_widget.getSrc(),
			params: i_widget.getParams(),
			config: null,
			attr: {}
		});

		i_widget.on({
			added: {
				fn: function(i_panel) {
					setTimeout(function() {
						i_widget.handleLoad(o_params);
					}, 10);
				}
			},
			close: {
				fn: function (i_self) {

					var i_component = i_self.down();
					if (i_component) {
						if (Ext.isFunction(i_component.onClose)) {
							i_component.onClose(i_component, i_self);
						}
					}
				}
			}
		});

		return i_widget;
	},

	handleLoad: function (o_params) {

		var i_widget = this;
		var m_source = i_widget.getSrc();

		if (Ext.isObject(m_source)) {
			i_widget.componentReady(o_params, m_source);
		}

		if (Ext.isString(m_source)) {

			var s_split = m_source.split(".");
			var s_extension = s_split[s_split.length - 1];

			if (s_extension === "js" || s_extension === "widget") {

				i_widget.ajaxLoad(o_params);
			}
			else {
				i_widget.iFrameLoad(o_params);
			}
		}

		return i_widget;
	},

	iFrameLoad: function (o_params) {

		var i_widget = this;
		var s_url = o_params.url;

		var o_config = {
			xtype: "webcontainer",
			src: s_url
		};

		i_widget.componentReady(o_params, o_config);

		return i_widget;
	},

	ajaxLoad: function (o_params) {

		var i_widget = this;

		if (!o_params.attr) {
			o_params.attr = {};
		}

		var o_config = ip2.getEvaled({
			url: o_params.url
		});

		var o_clone = Ext.clone(o_config);

		Ext.applyIf(o_clone, {
			xtype: "panel",
			attr: o_params.attr
		});

		i_widget.componentReady(o_params, o_clone);
	},

	componentReady: function (o_params, o_cmp) {

		var i_widget = this;
		var i_owner = o_params.owner;

		if (i_owner && i_owner.dataParent) {
			Ext.apply(o_cmp, {
				entity: i_owner.dataParent.entity
			});
		}

		if (o_params.callback) {
			o_params.callback.call(i_widget, o_cmp, o_params.attr, resp);
			return;
		}

		// No owner container was sent along, we make a window as container

		var i_component = Ext.widget(o_cmp.xtype, o_cmp);
		var i_child = i_owner.add(i_component);

		if (Ext.isFunction(i_child.onInitialize)) {
			i_child.onInitialize(i_child, i_owner);
		}
		if (Ext.isFunction(i_widget.onInitialize)) {
			i_widget.onInitialize(i_child, i_owner);
		}

		// ON OPEN
		if (Ext.isFunction(i_child.onOpen)) {
			i_child.onOpen(i_child, i_owner);
		}
		if (Ext.isFunction(i_widget.onOpen)) {
			i_widget.onOpen(i_child, i_owner);
		}

		// ON LOAD
		if (Ext.isFunction(i_child.onLoad)) {
			i_child.onLoad(i_child, i_owner);
		}
		if (Ext.isFunction(i_widget.onLoad)) {
			i_widget.onLoad(i_child, i_owner);
		}

		// ON READY
		if (Ext.isFunction(i_child.onReady)) {
			i_child.onReady(i_child, i_owner);
		}
		if (Ext.isFunction(i_widget.onReady)) {
			i_widget.onReady(i_child, i_owner);
		}

		if (Ext.isFunction(i_child.expand)) {
			i_child.expand();
		}

		i_widget.fireEvent("ready", i_widget);

		var i_widgetCmp = i_widget.getWidgetComponent();

		if (i_widgetCmp) {
			i_widget.mon(i_widgetCmp, "afterlayout", i_widget.doLoaded, i_widget);
		}
	},

	doLoaded: function() {

		var i_widget = this;
		var i_widgetCmp = i_widget.getWidgetComponent();
		var a_fields = i_widgetCmp.query("field");
		var a_tree = i_widgetCmp.query("treepanel");
		var a_grid = i_widgetCmp.query("gridpanel");

		if (a_fields.length > 0 || a_tree.length > 0) {

			i_widget.fireEvent("loaded", i_widget);
			i_widget.mun(i_widgetCmp, "afterlayout", i_widget.doLoaded, i_widget);

			if (i_widget.readOnly) {

				Ext.Array.each(a_fields, function(i_field) {
					if (i_field.setReadOnly) {
						setTimeout(function() {
							i_field.setReadOnly(true);
						}, 1);
					}
				});

				Ext.Array.each(a_tree, function(i_tree) {
					i_tree.disable();
				});

				Ext.Array.each(a_grid, function(i_grid) {
					i_grid.disable();
				});
			}
		}
	},

	getWidgetComponent: function () {

		return this.items.items[0];
	},

	getWidgetCmp: function () {

		return this.getWidgetComponent();
	}
});
