(function ($, window, document, undefined) {
    var $activeElm, preventShow, extend = $.extend, ms = $.mobiscroll, instances = ms.instances, userdef = ms.userdef, util = ms.util, pr = util.jsPrefix, has3d = util.has3d, getCoord = util.getCoord, constrain = util.constrain, isString = util.isString, isOldAndroid = /android [1-3]/i.test(navigator.userAgent), animEnd = 'webkitAnimationEnd animationend', empty = function () { }, prevdef = function (ev) { ev.preventDefault(); }; ms.classes.Widget = function (el, settings, inherit) {
        var $ariaDiv, $ctx, $header, $markup, $overlay, $persp, $popup, $wnd, $wrapper, buttons, btn, doAnim, hasButtons, isModal, lang, modalWidth, modalHeight, posEvents, preset, preventPos, s, scrollLock, setReadOnly, theme, wasReadOnly, wndWidth, wndHeight, that = this, $elm = $(el), elmList = [], posDebounce = {}; function onBtnStart(ev) {
            if (btn) { btn.removeClass('dwb-a'); }
            btn = $(this); if (!btn.hasClass('dwb-d') && !btn.hasClass('dwb-nhl')) { btn.addClass('dwb-a'); }
            if (ev.type === 'mousedown') { $(document).on('mouseup', onBtnEnd); }
        }
        function onBtnEnd(ev) {
            if (btn) { btn.removeClass('dwb-a'); btn = null; }
            if (ev.type === 'mouseup') { $(document).off('mouseup', onBtnEnd); }
        }
        function onShow(prevFocus) {
            if (!prevFocus) { $popup.focus(); }
            that.ariaMessage(s.ariaMessage);
        }
        function onHide(prevAnim) {
            var activeEl, value, type, focus = s.focusOnClose; $markup.remove(); if ($activeElm && !prevAnim) {
                setTimeout(function () {
                    if (focus === undefined || focus === true) {
                        preventShow = true; activeEl = $activeElm[0]; type = activeEl.type; value = activeEl.value; try { activeEl.type = 'button'; } catch (ex) { }
                        $activeElm.focus(); activeEl.type = type; activeEl.value = value;
                    } else if (focus) {
                        if (instances[$(focus).attr('id')]) { ms.tapped = false; }
                        $(focus).focus();
                    }
                }, 200);
            }
            that._isVisible = false; event('onHide', []);
        }
        function onPosition(ev) {
            clearTimeout(posDebounce[ev.type]); posDebounce[ev.type] = setTimeout(function () {
                var isScroll = ev.type == 'scroll'; if (isScroll && !scrollLock) { return; }
                that.position(!isScroll);
            }, 200);
        }
        function show(beforeShow) {
            if (!ms.tapped) {
                if (beforeShow) { beforeShow(); }
                if ($(document.activeElement).is('input,textarea')) { $(document.activeElement).blur(); }
                $activeElm = $elm; that.show();
            }
            setTimeout(function () { preventShow = false; }, 300);
        }
        function event(name, args) { var ret; args.push(that); $.each([userdef, theme, preset, settings], function (i, v) { if (v && v[name]) { ret = v[name].apply(el, args); } }); return ret; }
        that.position = function (check) {
            var w, l, t, anchor, aw, ah, ap, at, al, arr, arrw, arrl, dh, scroll, sl, st, totalw = 0, minw = 0, css = {}, nw = Math.min($wnd[0].innerWidth || $wnd.innerWidth(), $persp.width()), nh = $wnd[0].innerHeight || $wnd.innerHeight(); if ((wndWidth === nw && wndHeight === nh && check) || preventPos) { return; }
            if (that._isFullScreen || /top|bottom/.test(s.display)) { $popup.width(nw); }
            if (event('onPosition', [$markup, nw, nh]) === false || !isModal) { return; }
            sl = $wnd.scrollLeft(); st = $wnd.scrollTop(); anchor = s.anchor === undefined ? $elm : $(s.anchor); if (that._isLiquid && s.layout !== 'liquid') { if (nw < 400) { $markup.addClass('dw-liq'); } else { $markup.removeClass('dw-liq'); } }
            if (!that._isFullScreen && /modal|bubble/.test(s.display)) { $wrapper.width(''); $('.mbsc-w-p', $markup).each(function () { w = $(this).outerWidth(true); totalw += w; minw = (w > minw) ? w : minw; }); w = totalw > nw ? minw : totalw; $wrapper.width(w).css('white-space', totalw > nw ? '' : 'nowrap'); }
            modalWidth = that._isFullScreen ? nw : $popup.outerWidth(); modalHeight = that._isFullScreen ? nh : $popup.outerHeight(true); scrollLock = modalHeight <= nh && modalWidth <= nw; that.scrollLock = scrollLock; if (s.display == 'modal') { l = Math.max(0, sl + (nw - modalWidth) / 2); t = st + (nh - modalHeight) / 2; } else if (s.display == 'bubble') {
                scroll = true; arr = $('.dw-arrw-i', $markup); ap = anchor.offset(); at = Math.abs($ctx.offset().top - ap.top); al = Math.abs($ctx.offset().left - ap.left); aw = anchor.outerWidth(); ah = anchor.outerHeight(); l = constrain(al - ($popup.outerWidth(true) - aw) / 2, sl + 3, sl + nw - modalWidth - 3); t = at - modalHeight; if ((t < st) || (at > st + nh)) { $popup.removeClass('dw-bubble-top').addClass('dw-bubble-bottom'); t = at + ah; } else { $popup.removeClass('dw-bubble-bottom').addClass('dw-bubble-top'); }
                arrw = arr.outerWidth(); arrl = constrain(al + aw / 2 - (l + (modalWidth - arrw) / 2), 0, arrw); $('.dw-arr', $markup).css({ left: arrl });
            } else { l = sl; if (s.display == 'top') { t = st; } else if (s.display == 'bottom') { t = st + nh - modalHeight; } }
            t = t < 0 ? 0 : t; css.top = t; css.left = l; $popup.css(css); $persp.height(0); dh = Math.max(t + modalHeight, s.context == 'body' ? $(document).height() : $ctx[0].scrollHeight); $persp.css({ height: dh }); if (scroll && ((t + modalHeight > st + nh) || (at > st + nh))) { preventPos = true; setTimeout(function () { preventPos = false; }, 300); $wnd.scrollTop(Math.min(t + modalHeight - nh, dh - nh)); }
            wndWidth = nw; wndHeight = nh;
        }; that.attachShow = function ($elm, beforeShow) {
            elmList.push($elm); if (s.display !== 'inline') {
                if (setReadOnly) { $elm.on('mousedown.dw', function (ev) { ev.preventDefault(); }); }
                if (s.showOnFocus) { $elm.on('focus.dw', function () { if (!preventShow) { show(beforeShow); } }); }
                if (s.showOnTap) { that.tap($elm, function () { show(beforeShow); }); }
            }
        }; that.select = function () { if (!isModal || that.hide(false, 'set') !== false) { that._fillValue(); event('onSelect', [that._value]); } }; that.cancel = function () { if (!isModal || that.hide(false, 'cancel') !== false) { event('onCancel', [that._value]); } }; that.clear = function () {
            event('onClear', [$markup]); if (isModal && !that.live) { that.hide(false, 'clear'); }
            that.setValue(null, true);
        }; that.enable = function () { s.disabled = false; if (that._isInput) { $elm.prop('disabled', false); } }; that.disable = function () { s.disabled = true; if (that._isInput) { $elm.prop('disabled', true); } }; that.show = function (prevAnim, prevFocus) {
            var html; if (s.disabled || that._isVisible) { return; }
            if (doAnim !== false) {
                if (s.display == 'top') { doAnim = 'slidedown'; }
                if (s.display == 'bottom') { doAnim = 'slideup'; }
            }
            that._readValue(); event('onBeforeShow', []); html = '<div lang="' + s.lang + '" class="mbsc-' + s.theme + ' dw-' + s.display + ' ' +
                (s.cssClass || '') +
                (that._isLiquid ? ' dw-liq' : '') +
                (isOldAndroid ? ' mbsc-old' : '') +
                (hasButtons ? '' : ' dw-nobtn') + '">' +
                '<div class="dw-persp">' +
                (isModal ? '<div class="dwo"></div>' : '') +
                '<div' + (isModal ? ' role="dialog" tabindex="-1"' : '') + ' class="dw' + (s.rtl ? ' dw-rtl' : ' dw-ltr') + '">' +
                (s.display === 'bubble' ? '<div class="dw-arrw"><div class="dw-arrw-i"><div class="dw-arr"></div></div></div>' : '') +
                '<div class="dwwr">' +
                '<div aria-live="assertive" class="dw-aria dw-hidden"></div>' +
                (s.headerText ? '<div class="dwv">' + (isString(s.headerText) ? s.headerText : '') + '</div>' : '') +
                '<div class="dwcc">'; html += that._generateContent(); html += '</div>'; if (hasButtons) {
                    html += '<div class="dwbc">'; $.each(buttons, function (i, b) {
                        b = isString(b) ? that.buttons[b] : b; if (b.handler === 'set') { b.parentClass = 'dwb-s'; }
                        if (b.handler === 'cancel') { b.parentClass = 'dwb-c'; }
                        b.handler = isString(b.handler) ? that.handlers[b.handler] : b.handler; html += '<div' + (s.btnWidth ? ' style="width:' + (100 / buttons.length) + '%"' : '') + ' class="dwbw ' + (b.parentClass || '') + '"><div tabindex="0" role="button" class="dwb' + i + ' dwb-e ' + (b.cssClass === undefined ? s.btnClass : b.cssClass) + (b.icon ? ' mbsc-ic mbsc-ic-' + b.icon : '') + '">' + (b.text || '') + '</div></div>';
                    }); html += '</div>';
                }
            html += '</div></div></div></div>'; $markup = $(html); $persp = $('.dw-persp', $markup); $overlay = $('.dwo', $markup); $wrapper = $('.dwwr', $markup); $header = $('.dwv', $markup); $popup = $('.dw', $markup); $ariaDiv = $('.dw-aria', $markup); that._markup = $markup; that._header = $header; that._isVisible = true; posEvents = 'orientationchange resize'; that._markupReady(); event('onMarkupReady', [$markup]); if (isModal) {
                $(window).on('keydown.dw', function (ev) { if (ev.keyCode == 13) { that.select(); } else if (ev.keyCode == 27) { that.cancel(); } }); if (s.scrollLock) { $markup.on('touchstart touchmove mousewheel DOMMouseScroll', function (ev) { if (scrollLock) { ev.preventDefault(); } }); }
                if (pr !== 'Moz') { $('input,select,button', $ctx).each(function () { if (!this.disabled) { $(this).addClass('dwtd').prop('disabled', true); } }); }
                posEvents += ' scroll'; ms.activeInstance = that; $markup.appendTo($ctx); if (has3d && doAnim && !prevAnim) { $markup.addClass('dw-in dw-trans').on(animEnd, function () { $markup.off(animEnd).removeClass('dw-in dw-trans').find('.dw').removeClass('dw-' + doAnim); onShow(prevFocus); }).find('.dw').addClass('dw-' + doAnim); }
            } else if ($elm.is('div')) { $elm.html($markup); } else { $markup.insertAfter($elm); }
            event('onMarkupInserted', [$markup]); that.position(); $wnd.on(posEvents, onPosition); $markup.on('selectstart mousedown', prevdef).on('click', '.dwb-e', prevdef).on('keydown', '.dwb-e', function (ev) { if (ev.keyCode == 32) { ev.preventDefault(); ev.stopPropagation(); $(this).click(); } }); setTimeout(function () {
                $.each(buttons, function (i, b) { that.tap($('.dwb' + i, $markup), function (ev) { b = isString(b) ? that.buttons[b] : b; b.handler.call(this, ev, that); }, true); }); if (s.closeOnOverlay) { that.tap($overlay, function () { that.cancel(); }); }
                if (isModal && !doAnim) { onShow(prevFocus); }
                $markup.on('touchstart mousedown', '.dwb-e', onBtnStart).on('touchend', '.dwb-e', onBtnEnd); that._attachEvents($markup);
            }, 300); event('onShow', [$markup, that._tempValue]);
        }; that.hide = function (prevAnim, btn, force) {
            if (!that._isVisible || (!force && !that._isValid && btn == 'set') || (!force && event('onClose', [that._tempValue, btn]) === false)) { return false; }
            if ($markup) {
                if (pr !== 'Moz') { $('.dwtd', $ctx).each(function () { $(this).prop('disabled', false).removeClass('dwtd'); }); }
                if (has3d && isModal && doAnim && !prevAnim && !$markup.hasClass('dw-trans')) { $markup.addClass('dw-out dw-trans').find('.dw').addClass('dw-' + doAnim).on(animEnd, function () { onHide(prevAnim); }); } else { onHide(prevAnim); }
                $wnd.off(posEvents, onPosition);
            }
            delete ms.activeInstance;
        }; that.ariaMessage = function (txt) { $ariaDiv.html(''); setTimeout(function () { $ariaDiv.html(txt); }, 100); }; that.isVisible = function () { return that._isVisible; }; that.setValue = empty; that._generateContent = empty; that._attachEvents = empty; that._readValue = empty; that._fillValue = empty; that._markupReady = empty; that._processSettings = empty; that.tap = function (el, handler, prevent) {
            var startX, startY, moved; if (s.tap && !isOldAndroid) {
                el.on('touchstart.dw', function (ev) {
                    if (prevent) { ev.preventDefault(); }
                    startX = getCoord(ev, 'X'); startY = getCoord(ev, 'Y'); moved = false;
                }).on('touchmove.dw', function (ev) { if (Math.abs(getCoord(ev, 'X') - startX) > 20 || Math.abs(getCoord(ev, 'Y') - startY) > 20) { moved = true; } }).on('touchend.dw', function (ev) {
                    var that = this; if (!moved) { ev.preventDefault(); handler.call(that, ev); }
                    ms.tapped = true; setTimeout(function () { ms.tapped = false; }, 500);
                });
            }
            el.on('click.dw', function (ev) {
                if (!ms.tapped) { handler.call(this, ev); }
                ev.preventDefault();
            });
        }; that.option = function (opt, value) {
            var obj = {}; if (typeof opt === 'object') { obj = opt; } else { obj[opt] = value; }
            that.init(obj);
        }; that.destroy = function () {
            that.hide(true, false, true); $.each(elmList, function (i, v) { v.off('.dw'); }); if (that._isInput && setReadOnly) { el.readOnly = wasReadOnly; }
            event('onDestroy', []); delete instances[el.id];
        }; that.getInst = function () { return that; }; that.trigger = event; that.init = function (ss) {
        that.settings = s = {}; extend(settings, ss); extend(s, ms.defaults, that._defaults, userdef, settings); theme = ms.themes[s.theme] || ms.themes.mobiscroll; lang = ms.i18n[s.lang]; event('onThemeLoad', [lang, settings]); extend(s, theme, lang, userdef, settings); preset = ms.presets[that._class][s.preset]; s.buttons = s.buttons || (s.display !== 'inline' ? ['set', 'cancel'] : []); s.headerText = s.headerText === undefined ? (s.display !== 'inline' ? '{value}' : false) : s.headerText; if (preset) { preset = preset.call(el, that); extend(s, preset, settings); }
            if (!ms.themes[s.theme]) { s.theme = 'mobiscroll'; }
            that._isLiquid = (s.layout || (/top|bottom/.test(s.display) ? 'liquid' : '')) === 'liquid'; that._processSettings(); $elm.off('.dw'); doAnim = isOldAndroid ? false : s.animate; buttons = s.buttons; isModal = s.display !== 'inline'; setReadOnly = s.showOnFocus || s.showOnTap; $wnd = $(s.context == 'body' ? window : s.context); $ctx = $(s.context); that.context = $wnd; that.live = true; $.each(buttons, function (i, b) { if (b === 'set' || b.handler === 'set') { that.live = false; return false; } }); that.buttons.set = { text: s.setText, handler: 'set' }; that.buttons.cancel = { text: (that.live) ? s.closeText : s.cancelText, handler: 'cancel' }; that.buttons.clear = { text: s.clearText, handler: 'clear' }; that._isInput = $elm.is('input'); hasButtons = buttons.length > 0; if (that._isVisible) { that.hide(true, false, true); }
            if (isModal) {
                that._readValue(); if (that._isInput && setReadOnly) {
                    if (wasReadOnly === undefined) { wasReadOnly = el.readOnly; }
                    el.readOnly = true;
                }
                that.attachShow($elm);
            } else { that.show(); }
            $elm.on('change.dw', function () {
                if (!that._preventChange) { that.setVal($elm.val(), true, false); }
                that._preventChange = false;
            }); event('onInit', []);
        }; that.buttons = {}; that.handlers = { set: that.select, cancel: that.cancel, clear: that.clear }; that._value = null; that._isValid = true; if (!inherit) { instances[el.id] = that; that.init(settings); }
    }; ms.classes.Widget.prototype._defaults = { lang: 'en', setText: 'Set', selectedText: 'Selected', closeText: 'Close', cancelText: 'Cancel', clearText: 'Clear', disabled: false, closeOnOverlay: true, showOnFocus: true, showOnTap: true, display: 'modal', scrollLock: true, tap: true, btnClass: 'dwb', btnWidth: true, focusOnClose: false }; ms.themes.mobiscroll = { rows: 5, showLabel: false, headerText: false, btnWidth: false, selectedLineHeight: true, selectedLineBorder: 1, dateOrder: 'MMddyy', weekDays: 'min', checkIcon: 'ion-ios7-checkmark-empty', btnPlusClass: 'mbsc-ic mbsc-ic-arrow-down5', btnMinusClass: 'mbsc-ic mbsc-ic-arrow-up5', btnCalPrevClass: 'mbsc-ic mbsc-ic-arrow-left5', btnCalNextClass: 'mbsc-ic mbsc-ic-arrow-right5' }; $(window).on('focus', function () { if ($activeElm) { preventShow = true; } }); $(document).on('mouseover mouseup mousedown click', function (ev) { if (ms.tapped) { ev.stopPropagation(); ev.preventDefault(); return false; } });
})(jQuery, window, document);