Object.extend(beng, {
	/**
		@class
		@name beng.WidgetInstance
		@description
	*/
    WidgetInstance : Class.create(
        /**
            @lends beng.WidgetInstance#
        */
        {
			container   : null,
			descriptor  : null,
			wiid	    : "",
			wvcid       : "",
			wviid       : "",
			namespace   : null,

			/**
				@constructs
			*/
			initialize : function(descriptor, wiid, namespace, wvcid, wviid) {
				// this should be read from WidgetNG, if possible
				this.container = $("container_" + (wviid?wviid:wiid));
				this.descriptor = descriptor;
				this.wiid = wiid;
				this.wvcid = wvcid || "";
				this.wviid = wviid || "";
				this.namespace = namespace || "";
			},

			getWidgetId : function() {
				return this.wiid;
			},

			getWidgetClassId : function() {
				return this.descriptor.wcid;
			},

			/**
				@description
					creates  a popup and calls the notificationview of the widget
				@param {function} callback
					
			*/
			showNotifications : function( callback ) {
				var options = {};
				// at this point we may be  in the "Messages" tab in the controlpanel.
				// When we close the config window of this widget, we may want to reload
				// the message tab. Thats why WidgetManager.showNotifications can pass
				// a callback to do so.
				if ( callback && typeof(callback) == "function" )
					options.callbackFunc = callback;
				
				this.__showPopup(
					createBengRequestUrl(
						'/beng/coma/WidgetMgr.cls',
						{
							action : 'showNotifications',
							wiid   : this.wiid
						}
					),
					options
				);
			},

			configure : function(options) {
			    options = options || {};
			    options.callbackFunc = (typeof options.callbackFunc == "function" ? options.callbackFunc : function() {
			        if (!this.container)
			            this.container = $("container_" + (this.wviid?this.wviid:this.wiid));
                    $$("TABLE[wiid='" + this.wiid + "']").each(
                  		function(viewElem) {		 
							beng.WidgetManager.updateContent(
		                        this.descriptor.wcid,
		                        this.wiid,
		                        {
		                            bodyonly : true
		                        },
		                        viewElem.getAttribute("wvcid"), 
		                        viewElem.getAttribute("wviid")
		                    );
		                }.bind(this)
		            );        

			    }.bind(this));
				this.__showPopup(
					createBengRequestUrl(
						'/beng/coma/WidgetMgr.cls',
						{
							action : 'showConfiguration',
							wiid   : this.wiid,
							wvcid  : this.wvcid,
							wviid  : this.wviid
						},
						{
						    "x-widgetnamespace" : this.namespace
						}
					),
					options
				);
			},

			__showPopup : function(configUrl, options) {
				var popupWnd = getTopWindow().getPopup();
				var width = 500;
				var height = 300;
				var callbackFunc = (typeof options != "function" ? options : Prototype.emptyFunction);
				if (options && typeof options != "function"){
				    width = options.width || width;
				    height = options.height || height;
				    callbackFunc = options.callbackFunc || callbackFunc;
				}
				popupWnd.callbackFunc = callbackFunc;
				popupWnd.popupBox.style.visibility = "hidden";
				popupWnd.load(configUrl,width, height);
			},

			TERM_END : null
		}
	)
});

