zul.wgt.Popup=zk.$extends(zul.Widget,{_visible:!1,isOpen:function(){return this.isVisible()},open:function(a,b,c,e){var d=this._posInfo(a,b,c),f=this.$n();f=jq(f);0!=arguments.length&&(this._openInfo=arguments);this._shallToggle=e&&"toggle"==e.type;f.css({position:"absolute"}).zk.makeVParent();zWatch.fire("onVParent",this);d&&f.zk.position(d.dim,d.pos,e);this.setFloating_(!0);this.setTopmost();this.openAnima_(a,b,c,e)},openAnima_:function(a,b,c,e){this.afterOpenAnima_(a,b,c,e)},afterOpenAnima_:function(a,
b,c,e){b=this.$n();c=e&&e.sendOnOpen;if((a=zk.Widget.$(a))&&a.desktop&&this.desktop){var d=zk(a).dimension(!0),f=zk(this).dimension(!0);d&&f&&(this._adjustLeft=f.left-d.left,this._adjustTop=f.top-d.top);this._keepVisible=!0}this.setVisible(!0);e&&e.disableMask||!this.isListen("onOpen",{asapOnly:!0})||(this.mask&&this.mask.destroy(),c&&(this.mask=new zk.eff.Mask({id:this.uuid+"-mask",anchor:b}),zWatch.listen({onResponse:this})));(e=this._openInfo)&&this.position.apply(this,e);this.shallStackup_()&&
b&&(this._stackup?((e=this._stackup.style).top=(d=b.style).top,e.left=d.left,e.zIndex=d.zIndex,e.display="block"):this._stackup=jq.newStackup(b,b.id+"-stk"));c&&this.fire("onOpen",{open:!0,reference:a});jq(b).addClass(this.$s("open"))},shallStackup_:function(){return zk.eff.shallStackup()},position:function(a,b,c,e){(a=this._posInfo(a,b,c))&&zk(this.$n()).position(a.dim,a.pos,e)},onScroll:function(a){a&&(this.isInView_()?(a=this.getPositionArgs_(),!this.isOpen()&&this._keepVisible?this.open.apply(this,
a):this.position.apply(this,a)):this.isOpen()&&this.close({keepVisible:!0}))},_posInfo:function(a,b,c,e){var d;if(c)if(a){if("string"==typeof a&&(a=zk.Widget.$(a)),a)if(d=zul.Widget.isInstance(a)?a.$n():a){var f=c;d=zk(d).dimension(!0)}else return{pos:c}}else return{pos:c};else jq.isArray(b)&&(d={left:zk.parseInt(b[0]),top:zk.parseInt(b[1]),width:0,height:0});if(d)return c=zk(this.$n()),d.top+=c.sumStyles("t",jq.margins),d.left+=c.sumStyles("l",jq.margins),{pos:f,dim:d}},onResponse:function(){this.mask&&
this.mask.destroy();var a=this._openInfo;a&&(this.position.apply(this,a),this._openInfo=null);zWatch.unlisten({onResponse:this});this.mask=null},close:function(a){this._stackup&&(this._stackup.style.display="none");this._shallToggle=!1;try{if((zk.ie||zk.ff||zk.safari)&&zk.currentFocus){var b=zk.currentFocus.getInputNode?zk.currentFocus.getInputNode():zk.currentFocus.$n();jq.nodeName(b,"input")&&((zk.ff||zk.safari)&&jq.isAncestor(this.$n(),b)?jq(b).blur():zk.ie&&document.activeElement!==b&&zk(b).focus())}}catch(c){zk.debugLog(c.message||
c)}this.closeAnima_(a);a&&a.keepVisible||(this._keepVisible=!1,this._fakeParent=null)},closeAnima_:function(a){this.afterCloseAnima_(a)},afterCloseAnima_:function(a){this.setVisible(!1);var b=this.$n();zk(b).undoVParent();zWatch.fireDown("onVParent",this);this.setFloating_(!1);a&&a.sendOnOpen&&this.fire("onOpen",{open:!1});if(11>zk.ie){var c=this;setTimeout(function(){c.replaceHTML(b)},50)}jq(b).removeClass(this.$s("open"))},onFloatUp:function(a,b){this._keepVisible=!1;if(this.isVisible()){var c=
this._openInfo;this._shallToggle&&c&&b&&(void 0===b.triggerByClick||c[3].which==b.triggerByClick&&zUtl.isAncestor(this._openInfo[0],a.origin))||this._doFloatUp(a)}},_doFloatUp:function(a){if(this.isVisible()){a=a.origin;for(var b;a;a=a.parent){if(a==this){b||this.setTopmost();return}if(a==this.parent&&a.ignoreDescendantFloatUp_(this))return;b=b||a.isFloating_()}this.close({sendOnOpen:!0})}},setFloatZIndex_:function(a,b){this.$supers("setFloatZIndex_",arguments);this._stackup&&(this._stackup.style.zIndex=
b)},onVParent:function(a){this._shallToggle&&this._doFloatUp(a)},bind_:function(){this.$supers(zul.wgt.Popup,"bind_",arguments);zWatch.listen({onFloatUp:this,onShow:this,onVParent:this,afterSize:this,onScroll:this});this.setFloating_(!0)},unbind_:function(){zk(this.$n()).undoVParent();this._stackup&&(jq(this._stackup).remove(),this._stackup=null);this._openInfo&&(this._openInfo=null);this._shallToggle=null;zWatch.unlisten({onFloatUp:this,onShow:this,onVParent:this,afterSize:this,onScroll:this});this.setFloating_(!1);
this.$supers(zul.wgt.Popup,"unbind_",arguments)},afterSize:function(){this.reposition()},reposition:function(){if(this._fakeParent)this.position.apply(this,this.getPositionArgs_());else{var a=this._openInfo;if(a){var b=this._posInfo(a[0],a[1],a[2]);b&&jq(this.$n()).zk.position(b.dim,b.pos,a[3])}}},onShow:function(a){a.fire(this.firstChild);zk(this).redoCSS(-1,{fixFontIcon:!0})},setHeight:function(a){this.$supers("setHeight",arguments);this.desktop&&zUtl.fireShown(this)},setWidth:function(a){this.$supers("setWidth",
arguments);this.desktop&&zWatch.fireDown("onShow",this)},prologHTML_:function(a){},epilogHTML_:function(a){},isInView_:function(){return this._fakeParent?zk(this._fakeParent).isRealScrollIntoView(!0):!1},getPositionArgs_:function(){var a=this._fakeParent,b=zk(a).dimension(!0);return[a,[b.left+this._adjustLeft,b.top+this._adjustTop],null,{dodgeRef:!1}]}});
