zul.wgt.Popup=zk.$extends(zul.Widget,{_visible:!1,isOpen:function(){return this.isVisible()},open:function(a,b,c,d){this._fakeParent=zk.$(a);var e=this._posInfo(a,b,c),f=this.$n();f=jq(f);0!=arguments.length&&(this._openInfo=arguments);this._shallToggle=d&&"toggle"==d.type;f.css({position:"absolute"}).zk.makeVParent();zWatch.fire("onVParent",this);e&&f.zk.position(e.dim,e.pos,d);this.setFloating_(!0);this.setTopmost();this.openAnima_(a,b,c,d)},openAnima_:function(a,b,c,d){this.afterOpenAnima_(a,b,
c,d)},afterOpenAnima_:function(a,b,c,d){b=this.$n();c=d&&d.sendOnOpen;a=zk.Widget.$(a);this._adjustOffsets(a);this.setVisible(!0);d&&d.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})));if(d=this._openInfo)this.position.apply(this,d),this._adjustOffsets(a);if(this.shallStackup_()&&b)if(this._stackup){var e;(d=this._stackup.style).top=(e=b.style).top;d.left=e.left;d.zIndex=
e.zIndex;d.display="block"}else this._stackup=jq.newStackup(b,b.id+"-stk");c&&this.fire("onOpen",{open:!0,reference:a});jq(b).addClass(this.$s("open"))},_adjustOffsets:function(a){if(a&&a.desktop&&this.desktop){a=zk(a).dimension(!0);var b=zk(this).dimension(!0);a&&b&&(this._adjustLeft=b.left-a.left,this._adjustTop=b.top-a.top);this._keepVisible=!0}},shallStackup_:function(){return zk.eff.shallStackup()},position:function(a,b,c,d){(a=this._posInfo(a,b,c))&&zk(this.$n()).position(a.dim,a.pos,d)},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,d){var e;if(c)if(a){if("string"==typeof a&&(a=zk.Widget.$(a)),a)if(e=zul.Widget.isInstance(a)?a.$n():a){var f=c;e=zk(e).dimension(!0)}else return{pos:c}}else return{pos:c};else jq.isArray(b)&&(e={left:zk.parseInt(b[0]),top:zk.parseInt(b[1]),width:0,height:0});if(e)return c=zk(this.$n()),e.top+=c.sumStyles("t",
jq.margins),e.left+=c.sumStyles("l",jq.margins),{pos:f,dim:e}},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}]}});
