(function(){"use strict";u8.Base.Widgets.DropDown=function(i,t){this.Widget($w(i),$.extend({},u8.Base.Widgets.DropDown.defaults,t));this._window=u8.application.getMainWindow();this._element=this.$(i);this._input=this.inputElement?this.$(this.inputElement):null;this._$dropdown=null;this._initEvents();if(this.autoCreate){this.create()}};u8.Base.Widgets.DropDown.defaults={closeOnTab:true,textAlign:"left",tagName:"div",popup:{withoutFocus:true}};u8.Base.Widgets.DropDown.firstZIndex=300;$.extend(u8.Base.Widgets.DropDown.prototype,{isVisible:function(){return this._popup&&this._popup.isVisible()},setVisible:function(i){if(i){this._show()}else{this._hide()}return this},create:function(){if(!this._$dropdown){this._create()}},update:function(i){if(i){if(i.reason=="attr"){if(i.properties.alignTo){this._updateSizeAndPosition()}if(i.properties.items){this._updateItems()}return this}else if(i=="position"||i=="width"){this._updateSizeAndPosition();return this}}this._updateItems();return this},setFocus:function(){this._$dropdown.focus()},dispose:function(){if(this._isDisposed){return}if(this._popup){this._popup.close();this._popup=null}this._$dropdown=null;if(this._element){this._element.off(".DropDown");this._element=null}if(this._input){this._input.off(".DropDown");this._input=null}this.element=null;this.Widget_dispose()},createContent:function(){$error("Method DropDown.createContent() is not implemented.")},getContent:function(){return this._$dropdown},setHeight:function(i){this._height=i;this._popup.setSize(this._$dropdown.parent().width(),this._height)},getHeight:function(){return this._height},_show:function(){if(this.isVisible()){return}var i={cancel:false};this.raise("PreShow",i);if(i.cancel){return}if(!this._$dropdown){this._create()}u8.services.popups.autoClose({exclude:this});if(!this._popup.isOpen()){this._popup.open()}this._popup.show();this._updateSizeAndPosition();this._onShow()},_hide:function(){if(!this.isVisible()){return}var i={cancel:false};this.raise("PreHide",i);if(i.cancel){return}if(this._popup){this._popup.hide()}this._onHide()},_create:function(){if(this._popup){this._popup.dispose()}if(this._$dropdown){this._$dropdown.remove()}var i=$.extend({},u8.Base.Widgets.Popup.defaults.theme.css,{content:""});var t=$w("main");this._$dropdown=t.$("<"+this.tagName+"></"+this.tagName+">").addClass("u8-dropdown-contents").attr("tabindex",-1);if(typeof this.textAlign=="string"){this._$dropdown.css("text-align",this.textAlign)}this._popup=new u8.Base.Widgets.Popup(this._$dropdown,$.extend({allowClose:false,allowMaximize:false,allowMove:false,allowResize:false,autoShow:false,autoClose:true,disposeOnClose:false,modal:false,alignTo:this.alignTo||this._element.get(0),position:"-top",possibleOrientations:["bottom","top"],showTitle:false,sizing:"fit",style:"dropdown",minSize:this.minSize||{height:0,width:0},maxSize:this.maxSize,theme:{css:i}},this.popup,{parent:this}));if(u8.session.environment.isEnterpriseVersion===true){this._popup.disposeOnClose=true}this._popup.bind("Close",function(i){i.parent._onHide()}).bind("Dispose",function(i){var t=i.parent;t._popup=null;t._$dropdown=null});this._popup.open();this._$dropdown.on("keydown",this._onDropDownKeyDown.bind(this));this.createContent();this._updateSizeAndPosition();if(!this._height){this._height=this._$dropdown.outerHeight()}this.raise("Create");this._popup.hide()},_updateItems:function(){if(!this._$dropdown){return}this._$dropdown.empty();this._$dropdown.css("width","");this.createContent();this._updateSizeAndPosition()},_updateSizeAndPosition:function(){if(!this._$dropdown||!this._$dropdown.is(":visible")){return}var i=this.alignTo||this._element.get(0);if(this.sizeToElement!==false){var t=this._$dropdown.prop("scrollWidth");var e=$$(i).width();if(e>t){this._$dropdown.css("width",e)}else if(t>e){this._$dropdown.css("width","");t=this._$dropdown.prop("scrollWidth");if(e>t){this._$dropdown.css("width",e)}}}var s=this.popup&&this.popup.position||"-top";this._popup.setPosition(s,i)},_initEvents:function(){this._element.on("keydown.DropDown",this._onElementKeyDown.bind(this));if(this._input){this._input.on("keydown.DropDown",this._onInputKeyDown.bind(this))}},_onShow:function(){this._isShown=true;this.raise("Show",{})},_onHide:function(){if(!this._isShown){return}this._isShown=false;this.raise("Hide",{})},_onElementKeyDown:function(i){if(this._isDisposed){return undefined}if(i.keyCode==40&&i.altKey){this._show();if(this.isVisible()){u8.Base.Dom.focus(this._$dropdown.focus)}return false}if(i.keyCode==38&&i.altKey){this._hide();return false}if(i.keyCode==9&&this.closeOnTab){this._hide()}},_onInputKeyDown:function(i){if(this._isDisposed){return undefined}return this._onElementKeyDown(i)},_onDropDownKeyDown:function(i){if(i.keyCode==9&&this.closeOnTab){this._hide()}}});u8.Base.Widgets.DropDown.makeClass({name:"DropDown",deriveFrom:u8.Base.Widgets.Widget,virtual:["dispose","update"],events:["Create","PreShow","PreHide","Show","Hide"]})})();