(function (a) {
    a.widget("ui.dropdownchecklist",
     { version: function () { alert("DropDownCheckList v1.1") },
         _appendDropContainer: function (b) {
             var d = a("<div/>");
             d.addClass("ui-dropdownchecklist ui-dropdownchecklist-dropcontainer-wrapper");
             d.addClass("ui-widget");
             d.attr("id", b.attr("id") + "-ddw");
             d.css({ position: "absolute", left: "-33000px", top: "-33000px" });
             var c = a("<div/>");
             c.addClass("ui-dropdownchecklist-dropcontainer ui-widget-content");
             c.css("overflow-y", "auto");
             d.append(c);
             d.insertAfter(b);
             d.isOpen = false; return d
         },
         _isDropDownKeyShortcut: function (c, b) { return c.altKey && (a.ui.keyCode.DOWN == b) },
         _isDropDownCloseKey: function (c, b) { return (a.ui.keyCode.ESCAPE == b) || (a.ui.keyCode.ENTER == b) },
         _keyFocusChange: function (f, i, c) {
             var g = a(":focusable");
             var d = g.index(f);
             if (d >= 0) {
                 d += i; if (c) {
                     var e = this.dropWrapper.find("input:not([disabled])");
                     var b = g.index(e.get(0));
                     var h = g.index(e.get(e.length - 1));
                     if (d < b) { d = h } else { if (d > h) { d = b } }
                 } g.get(d).focus()
             }
         },
         _handleKeyboard: function (d) {
             var b = this; var c = (d.keyCode || d.which);
             if (!b.dropWrapper.isOpen && b._isDropDownKeyShortcut(d, c)) {
                 d.stopImmediatePropagation();
                 b._toggleDropContainer(true)
             }
             else {
                 if (b.dropWrapper.isOpen && b._isDropDownCloseKey(d, c)) {
                     d.stopImmediatePropagation();
                     b._toggleDropContainer(false);
                     b.controlSelector.focus()
                 }
                 else {
                     if (b.dropWrapper.isOpen && (d.target.type == "checkbox") && ((c == a.ui.keyCode.DOWN) || (c == a.ui.keyCode.UP))) {
                         d.stopImmediatePropagation();
                         b._keyFocusChange(d.target, (c == a.ui.keyCode.DOWN) ? 1 : -1, true)
                     }
                     else {
                         if (b.dropWrapper.isOpen && (c == a.ui.keyCode.TAB)) { }
                     }
                 }
             }
         },
         _handleFocus: function (f, d, b) {
             var c = this;
             if (b && !c.dropWrapper.isOpen) {
                 f.stopImmediatePropagation();
                 if (d) {
                     c.controlSelector.addClass("ui-state-hover");
                     if (a.ui.dropdownchecklist.gLastOpened != null) {
                         a.ui.dropdownchecklist.gLastOpened._toggleDropContainer(false)
                     }
                 }
                 else { c.controlSelector.removeClass("ui-state-hover") }
             }
             else {
                 if (!b && !d) {
                     if (f != null) {
                         f.stopImmediatePropagation()
                     }
                     c.controlSelector.removeClass("ui-state-hover");
                     c._toggleDropContainer(false)
                 }
             }
         },
         _cancelBlur: function (c) { var b = this; if (b.blurringItem != null) { clearTimeout(b.blurringItem); b.blurringItem = null } },
         _appendControl: function () {

             var j = this, c = this.sourceSelect, k = this.options;
             var b = a("<span/>");
             b.addClass("ui-dropdownchecklist ui-dropdownchecklist-selector-wrapper ui-widget");
             b.css({ cursor: "default", overflow: "hidden" });
             var f = c.attr("id");
             if ((f == null) || (f == "")) { f = "ddcl-" + a.ui.dropdownchecklist.gIDCounter++ } else { f = "ddcl-" + f } b.attr("id", f)
                               ; var h = a("<span/>");
             h.addClass("ui-dropdownchecklist-selector ui-state-default-selector");
             h.css({ display: "inline-block", overflow: "hidden", "white-space": "nowrap" });
             var d = c.attr("tabIndex");
             if (d == null) { d = 0 } else {
                 d = parseInt(d);
                 if (d < 0) { d = 0 }
             } h.attr("tabIndex", d);
             h.keyup(function (l) {
                 j._handleKeyboard(l)
             });
             h.focus(function (l) { j._handleFocus(l, true, true) });
             h.blur(function (l) { j._handleFocus(l, false, true) });
             b.append(h); if (k.icon != null) {
                 var i = (k.icon.placement == null) ? "left" : k.icon.placement;
                 var g = a("<div/>");
                 g.addClass("ui-icon");
                 g.addClass((k.icon.toOpen != null) ? k.icon.toOpen : "ui-icon-triangle-1-e");
                 g.css({ "float": i });
                 h.append(g)
             } var e = a("<span/>");
             e.addClass("ui-dropdownchecklist-text");
             e.css({ display: "inline-block", "white-space": "nowrap", overflow: "hidden", "padding-top": "3px" });
             h.append(e);
             b.hover(function () { if (!j.disabled) { } }, //h.addClass("ui-state-hover")
                               function () { if (!j.disabled) { } }); //h.removeClass("ui-state-hover")
             b.click(function (l) {
                 if (!j.disabled) {
                     l.stopImmediatePropagation();
                     j._toggleDropContainer(!j.dropWrapper.isOpen)
                 }
             });
             b.insertAfter(c);
             a(window).resize(function () { if (!j.disabled && j.dropWrapper.isOpen) { j._toggleDropContainer(true) } });
             return b
         },
         _createDropItem: function (g, f, o, l, h, e, k) {
             // debugger;
             var m = this, c = this.options, d = this.sourceSelect, p = this.controlWrapper;
             var s = a("<div/>");
             s.addClass("ui-dropdownchecklist-item");
             s.css({ "white-space": "nowrap" });
             var q = h ? ' checked="checked"' : "";
             var j = e ? ' class="inactive"' : ' class="active"';
             var b = p.attr("id");
             var n = b + "-i" + g; var r;
             if (m.isMultiple) { r = a('<input disabled type="checkbox" id="' + n + '"' + q + j + ' tabindex="' + f + '" />') }
             else { r = a('<input disabled type="radio" id="' + n + '" name="' + b + '"' + q + j + ' tabindex="' + f + '" />') } r = r.attr("index", g).val(o);
             s.append(r);
             var i = a("<label for=" + n + "/>");
             i.addClass("ui-dropdownchecklist-text");
             i.css({ cursor: "default" })
                               ; i.text(l);
             if (k) { s.addClass("ui-dropdownchecklist-indent") } s.addClass("ui-state-default");
             if (e) { s.addClass("ui-state-disabled") } i.click(function (t) { t.stopImmediatePropagation() });
             s.append(i);
             s.hover(function (u) {
                 var t = a(this);
                 if (!t.hasClass("ui-state-disabled")) { t.addClass("ui-state-hover") }
             }, function (u) {
                 var t = a(this);
                 t.removeClass("ui-state-hover")
             });
             r.click(function (u) {
                 var t = a(this);
                 u.stopImmediatePropagation();
                 if (t.hasClass("active")) {
                     m._syncSelected(t);
                     m.sourceSelect.trigger("change", "ddcl_internal");
                     if (!m.isMultiple && c.closeRadioOnClick) { m._toggleDropContainer(false) }
                 }
             });
             s.click(function (w) {
                 var v = a(this);
                 w.stopImmediatePropagation();
                 if (!v.hasClass("ui-state-disabled")) {
                     var t = v.find("input");
                     var u = t.attr("checked"); t.attr("checked", !u);
                     m._syncSelected(t);
                     m.sourceSelect.trigger("change", "ddcl_internal");
                     if (!u && !m.isMultiple && c.closeRadioOnClick) { m._toggleDropContainer(false) }
                 } else { v.focus(); m._cancelBlur() }
             });
             s.focus(function (u) { var t = a(this); u.stopImmediatePropagation() });
             s.keyup(function (t) { m._handleKeyboard(t) }); return s
         },
         _createGroupItem: function (f, d) {
             var b = this; var e = a("<div />"); e.addClass("ui-dropdownchecklist-group ui-widget-header");
             if (d) { e.addClass("ui-state-disabled") } e.css({ "white-space": "nowrap" });
             var c = a("<span/>"); c.addClass("ui-dropdownchecklist-text");
             c.css({ cursor: "default" });
             c.text(f);
             e.append(c);
             e.click(function (h) {
                 var g = a(this);
                 h.stopImmediatePropagation();
                 g.focus();
                 b._cancelBlur()
             });
             e.focus(function (h) { var g = a(this); h.stopImmediatePropagation() }); return e
         },
         _appendItems: function () {
             var d = this, g = this.sourceSelect, f = this.dropWrapper;
             var b = f.find(".ui-dropdownchecklist-dropcontainer");
             g.children().each(function (h) {
                
                 var i = a(this);
                 if (i.is("option")) { d._appendOption(i, b, h, false, false) } else {
                     if (i.is("optgroup")) {
                         var j = i.attr("disabled");
                         var l = i.attr("label"); if (l != "") { var k = d._createGroupItem(l, j); b.append(k) } d._appendOptions(i, b, h, true, j)
                     }
                 }
             });
             var c = b.outerWidth();
             var e = b.outerHeight(); return { width: c, height: e }
         },
         _appendOptions: function (g, d, f, c, b) {
             var e = this; g.children("option").each(function (h) {
                // debugger;
                 var i = a(this); var j = (f + "." + h);
                 e._appendOption(i, d, j, c, b)
             })
         }, _appendOption: function (g, b, h, d, m) {
             var l = this; var j = g.text();
             var i = g.val();
             var f = g.attr("selected");
             var e = (m || g.attr("disabled"));
             var c = l.controlSelector.attr("tabindex");
             var k = l._createDropItem(h, c, i, j, f, e, d); b.append(k)
         },
         _syncSelected: function (g) {
             var h = this, i = this.options, c = this.sourceSelect, d = this.dropWrapper;
             var f = d.find("input.active");
             if (i.firstItemChecksAll) {
                 if ((g != null) && (g.attr("index") == 0)) { f.attr("checked", g.attr("checked")) } else {
                     var e = true;
                     var j = null;
                     f.each(function (k) {
                         if (k > 0) {
                             var l = a(this).attr("checked");
                             if (!l) { e = false }
                         } else { j = a(this) }
                     });
                     if (j != null) { j.attr("checked", e) }
                 }
             } f = d.find("input");
             var b = c.get(0).options; f.each(function (k) { a(b[k]).attr("selected", a(this).attr("checked")) });
             h._updateControlText();
             if (g != null) { g.focus() }
         }, _sourceSelectChangeHandler: function (c) {
             var b = this, d = this.dropWrapper;
             d.find("input").val(b.sourceSelect.val());
             b._updateControlText()
         },
         _updateControlText: function () {
             var c = this, g = this.sourceSelect, d = this.options, f = this.controlWrapper;
             var h = g.find("option:first");
             var b = g.find("option");
             var i = c._formatText(b, d.firstItemChecksAll, h);
             var e = f.find(".ui-dropdownchecklist-text");
             e.html(i);
             e.attr("title", i);
             e.attr("id", "spanMonthsValue");

         },
         _formatText: function (b, d, e) {
             var f;
             if (a.isFunction(this.options.textFormatFunction))
             { try { f = this.options.textFormatFunction(b) } catch (c) { alert("textFormatFunction failed: " + c) } }
             else {
                 if (d && (e != null) && e.attr("selected")) { f = e.text() }
                 else {
                     f = "";
                     b.each(function () { if (a(this).attr("selected")) { if (f != "") { f += ", " } f += a(this).text() } }); if (f == "") { f = (this.options.emptyText != null) ? this.options.emptyText : "&nbsp;" }
                 }
             } return f
         },
         _toggleDropContainer: function (e) {
             var c = this; var d = function (f) {
                 if ((f != null) && f.dropWrapper.isOpen) {
                     f.dropWrapper.isOpen = false; a.ui.dropdownchecklist.gLastOpened = null;
                     var h = f.options;
                     f.dropWrapper.css({ top: "-33000px", left: "-33000px" });
                     var g = f.controlSelector;
                     //g.removeClass("ui-state-active");
                     //g.removeClass("ui-state-hover");
                     var j = f.controlWrapper.find(".ui-icon");
                     if (j.length > 0) {
                         j.removeClass((h.icon.toClose != null) ? h.icon.toClose : "ui-icon-triangle-1-s");
                         j.addClass((h.icon.toOpen != null) ? h.icon.toOpen : "ui-icon-triangle-1-e")
                     } a(document).unbind("click", d);
                     f.dropWrapper.find("input.active").attr("disabled", "disabled");
                     if (a.isFunction(h.onComplete)) { try { h.onComplete.call(f, f.sourceSelect.get(0)) } catch (i) { alert("callback failed: " + i) } }
                 }
             };
             var b = function (f) {
                 if (!f.dropWrapper.isOpen) {
                     f.dropWrapper.isOpen = true; a.ui.dropdownchecklist.gLastOpened = f;
                     var i = f.options;
                     f.dropWrapper.css({ top: f.controlWrapper.offset().top + (f.controlWrapper.outerHeight() + 5) + "px", left: f.controlWrapper.offset().left + "px" });
                     var m = f.controlWrapper.parents().map(function () { var n = a(this).css("z-index"); return isNaN(n) ? 0 : n }).get();
                     var h = Math.max.apply(Math, m);
                     if (h > 0) { f.dropWrapper.css({ zIndex: (h + 1) }) } var g = f.controlSelector;
                     //g.addClass("ui-state-active"); 
                     //g.removeClass("ui-state-hover");
                     var j = f.controlWrapper.find(".ui-icon");
                     if (j.length > 0) {
                         j.removeClass((i.icon.toOpen != null) ? i.icon.toOpen : "ui-icon-triangle-1-e");
                         j.addClass((i.icon.toClose != null) ? i.icon.toClose : "ui-icon-triangle-1-s")
                     } a(document).bind("click", function (n) { d(f) });
                     var l = f.dropWrapper.find("input.active");
                     l.removeAttr("disabled");
                     var k = l.get(0); if (k != null) { k.focus() }
                 }
             };
             if (e) { d(a.ui.dropdownchecklist.gLastOpened); b(c) } else { d(c) }
         },
         _setSize: function (b) {
             var m = this.options, f = this.dropWrapper, l = this.controlWrapper;
             var k = b.width; if (m.width != null) { k = parseInt(m.width) } else {
                 if (m.minWidth != null) {
                     var c = parseInt(m.minWidth);
                     if (k < c) { k = c }
                 }
             } var i = this.controlSelector; i.css({ width: k + "px" });
             var g = i.find(".ui-dropdownchecklist-text");
             var d = i.find(".ui-icon"); if (d != null) {
                 k -= (d.outerWidth() + 6);
                 g.css({ width: k + "px" })
             } k = l.outerWidth();
             var j = (m.maxDropHeight != null) ? parseInt(m.maxDropHeight) : -1; var h = ((j > 0) && (b.height > j)) ? j : b.height;
             var e = b.width < k ? k : b.width;
             e = 136;
             a(f).css({ height: h + "px", width: e + "px" });
             f.find(".ui-dropdownchecklist-dropcontainer").css({ height: h + "px" })
         },
         _init: function () {
             var c = this, d = this.options;
             if (a.ui.dropdownchecklist.gIDCounter == null) { a.ui.dropdownchecklist.gIDCounter = 1 } c.blurringItem = null;
             var g = c.element; c.initialDisplay = g.css("display");
             g.css("display", "none");
             c.initialMultiple = g.attr("multiple");
             c.isMultiple = c.initialMultiple;
             if (d.forceMultiple != null) { c.isMultiple = d.forceMultiple } g.attr("multiple", true);
             c.sourceSelect = g;
             var e = c._appendControl();
             c.controlWrapper = e;
             c.controlSelector = e.find(".ui-dropdownchecklist-selector");
             var f = c._appendDropContainer(e); c.dropWrapper = f;
             var b = c._appendItems(); c._updateControlText(e, f, g); c._setSize(b);
             if (d.firstItemChecksAll) { c._syncSelected(null) } if (d.bgiframe && typeof c.dropWrapper.bgiframe == "function") { c.dropWrapper.bgiframe() } c.sourceSelect.change(function (i, h) { if (h != "ddcl_internal") { c._sourceSelectChangeHandler(i) } })
         },
         _refreshOption: function (e, d, c) {
             var b = e.parent(); if (d) {
                 e.attr("disabled", "disabled");
                 e.removeClass("active"); e.addClass("inactive"); b.addClass("ui-state-disabled")
             }
             else {
                 e.removeAttr("disabled"); e.removeClass("inactive"); e.addClass("active");
                 b.removeClass("ui-state-disabled")
             } e.attr("checked", c)
         },
         _refreshGroup: function (c, b) { if (b) { c.addClass("ui-state-disabled") } else { c.removeClass("ui-state-disabled") } },
         refresh: function () {
             var b = this, e = this.sourceSelect, d = this.dropWrapper;
             var c = d.find("input");
             var g = d.find(".ui-dropdownchecklist-group");
             var h = 0;
             var f = 0;
             e.children().each(function (i) {
                 var j = a(this);
                 var l = j.attr("disabled");
                 if (j.is("option")) {
                     var k = j.attr("selected");
                     var n = a(c[f]); b._refreshOption(n, l, k); f += 1
                 }
                 else {
                     if (j.is("optgroup")) {
                         var o = j.attr("label"); if (o != "") { var m = a(g[h]); b._refreshGroup(m, l); h += 1 } j.children("option").each(function (s) {
                             var p = a(this); var r = (l || p.attr("disabled")); var q = p.attr("selected"); var t = a(c[f + s]); b._refreshOption(t, r, q)
                         })
                     }
                 }
             });
             b._updateControlText()
         },
         enable: function () {
             this.controlSelector.removeClass("ui-state-disabled");
             this.disabled = false
         },
         disable: function () { this.controlSelector.addClass("ui-state-disabled"); this.disabled = true },
         destroy: function () {
             a.Widget.prototype.destroy.apply(this, arguments);
             this.sourceSelect.css("display", this.initialDisplay);
             this.sourceSelect.attr("multiple", this.initialMultiple);
             this.controlWrapper.unbind().remove(); this.dropWrapper.remove()
         }
     });
    a.extend(a.ui.dropdownchecklist,
                               { defaults: { width: null, maxDropHeight: null, firstItemChecksAll: false, closeRadioOnClick: false, minWidth: 50, bgiframe: false} })
})(jQuery);
