(function(){"use strict";var t=100;u8.Base.Widgets.Busy=function(t,i){if(!t){if(!i.alignTo){i=$.extend({},i);var e=i.parent,s=null;while(e){s=e.element;if(!s&&e.getContent){s=e.getContent()[0];if(s){s=s.parentNode}if(!i.busyElement){i.busyElement=e.getContainer()}}if(s){break}e=e.element}if(!s){s=$w("current")}i=$.extend({},i,{alignTo:s})}var n=$w(i.alignTo);t=n.$("<div style='display:none; position:absolute;'></div>").appendTo(n.document.body)[0];this._removeElementOnDispose=true}this._state="hidden";this.Widget(t,$.extend({},u8.Base.Widgets.Busy.defaults,i));var o=this.$(this.element).addClass("u8-busy");if(this.alignTo){o.addClass("aligned")}this._isCreated=true;if("value"in this){var a=this.value;delete this.value;this.setValue(a)}else if(this._state=="shown"){this._updateState()}};u8.Base.Widgets.Busy.defaults={alignTo:null,modal:true,delay:1e3,disposeOnHide:true};u8.Base.Widgets.Busy.makeClass({name:"Busy",deriveFrom:u8.Base.Widgets.Widget});$.extend(u8.Base.Widgets.Busy.prototype,{show:function(){if(this._state=="shown"){return this}this._state="shown";if(this._isCreated){this._updateState()}return this},hide:function(){if(this._state=="hidden"){return this}this._state="hidden";if(this._isCreated){this._updateState()}return this},is:function(t){return this._state==t},dispose:function(){if(this._isDisposed){return}if(this._removeElementOnDispose){this.$(this.element).remove()}this._hide(true);this.Widget_dispose()},getValue:function(){return this._state=="shown"},setValue:function(t){if(this._isDisposed){return}if(t){this.show()}else{this.hide()}},update:function(){this._createView()},_updateState:function(){if(this._state=="shown"){if(this.modal){this._showModal()}if(this.delay>0){this._show.bind(this).defer(this.delay)}else{this._show()}}else{this._hide()}},_show:function(){if(this._state!="shown"||this._isDisposed){return}this._createView();if(this.alignTo){this._align();this._alignTimerId=setInterval(this._onAlignTimer.bind(this),t);this.$(this.element).show()}},_hide:function(t){if(this._isDisposed){return}if("_alignTimerId"in this){clearInterval(this._alignTimerId);delete this._alignTimerId}this._hideModal();if(t){return}this.$(this.element).stop().hide();if(this.disposeOnHide){this.dispose()}},_align:function(){if(this._isDisposed){return}var t=this._alignedPosition;var e=this._getAlignedPosition();if(!i(e,t)){this._alignedPosition=e;this.$(this.element).css(e)}if(this._$modal){t=this._modalPosisition;e=this._getModalPosition();if(e&&!i(e,t)){this._modalPosisition=e;this._$modal.css(e)}}},_createView:function(){var t=this.html;if(!t&&this.text){t=u8.Base.Html.escape(this.text)}var i=this.$(this.element).empty().addClass("u8-busy");this.$("<img/>").wheel(this.size).appendTo(i);this.$("<span/>").addClass("text").html(t||"&nbsp;").appendTo(i)},_showModal:function(){var t=this._getModalPosition();if(!t){return}if(!this._$modal){this._$modal=this.$("<div class='u8-busy-modal'></div>").on("click",this._onModalClick.bind(this)).css(t).appendTo(this.element.ownerDocument.body)}else{this._$modal.css(t)}},_hideModal:function(){if(!this._$modal){return}this._$modal.remove();this._$modal=null},_getAlignedPosition:function(){if(!this.alignTo){return null}var t=this.alignTo||this.element.ownerDocument.body;var i=this.$(this.element);var e=i.outerWidth();var s=i.outerHeight();if(u8.Base.Dom.isWindow(t)){return{position:"fixed",left:"50%",top:"50%",marginLeft:-Math.floor(e*.5),marginTop:-Math.floor(s*.5)}}var n=this.$(t);var o=n.offset();var a=n.outerWidth();var h=n.outerHeight();var d=Math.floor(o.left+(a-e)*.5)+"px";var l=Math.floor(o.top+(h-s)*.5)+"px";return{left:d,top:l}},_getModalPosition:function(){var t=this.busyElement||this.alignTo||this.element.ownerDocument.body;if(u8.Base.Dom.isWindow(t)){return{position:"fixed",left:0,right:0,top:0,bottom:0}}var i=this.$(t);var e=i.offset();var s=i.outerWidth();var n=i.outerHeight();if(n<=0){return undefined}return{left:e.left,top:e.top,width:s,height:n}},_onModalClick:function(){this._$modal.toggleClass("clicked")},_onAlignTimer:function(){this._align()}});function i(t,i){return u8.tools.equals(t,i)}u8.Base.Widgets.Tools.busy=function(t,i){var e=new u8.Base.Widgets.Busy(null,$.extend({parent:t},i));e.show();return e};u8.Base.Widgets.Widget.prototype.busy=function(t){return u8.Base.Widgets.Tools.busy(this,t)}})();