(function(d) {
  d.fn.styler = function(g) {
    g = d.extend({
      browseText: "\u0412\u044b\u0431\u0440\u0430\u0442\u044c",
      singleSelectzIndex: "1000",
      selectVisibleOptions: 0,
      width: 50,
      maxHeight: 105,
      position: ""
    }, g);
    return this.each(function() {
      var a = d(this),
        l = "",
        q = "";
      void 0 !== a.attr("id") && "" != a.attr("id") && (l = ' id="' + a.attr("id") + '"');
      void 0 !== a.attr("class") && "" != a.attr("class") && (q = " " + a.attr("class"));
      a.is(":checkbox") ? a.css({
        position: "absolute",
        left: -9999
      }).each(function() {
        if (1 > a.next("span.jq-checkbox").length) {
          var b = d("<span" + l + ' class="jq-checkbox' + q + '" style="display: inline-block"><span></span></span>');
          a.after(b);
          a.is(":checked") && b.addClass("checked");
          a.is(":disabled") && b.addClass("disabled");
          b.click(function() {
            if (!b.is(".disabled")) return a.is(":checked") ? (a.prop("checked", !1), b.removeClass("checked")) : (a.prop("checked", !0), b.addClass("checked")), a.change(), !1
          });
          a.parent("label").add('label[for="' + a.attr("id") + '"]').click(function(a) {
            b.click();
            a.preventDefault()
          });
          a.change(function() {
            a.is(":checked") ? b.addClass("checked") : b.removeClass("checked")
          }).keydown(function(d) {
            !a.parent("label").length || 13 != d.which && 32 != d.which || b.click()
          }).focus(function() {
            b.is(".disabled") || b.addClass("focused")
          }).blur(function() {
            b.removeClass("focused")
          });
          a.on("refresh", function() {
            a.is(":checked") ? b.addClass("checked") : b.removeClass("checked");
            a.is(":disabled") ? b.addClass("disabled") : b.removeClass("disabled")
          })
        }
      }) : a.is(":radio") ? a.css({
        position: "absolute",
        left: -9999
      }).each(function() {
        if (1 > a.next("span.jq-radio").length) {
          var b = d("<span" + l + ' class="jq-radio' + q + '" style="display: inline-block"><span></span></span>');
          a.after(b);
          a.is(":checked") && b.addClass("checked");
          a.is(":disabled") && b.addClass("disabled");
          b.click(function() {
            if (!b.is(".disabled")) return d('input[name="' + a.attr("name") + '"]').prop("checked", !1).next().removeClass("checked"), a.prop("checked", !0).next().addClass("checked"), a.change(), !1
          });
          a.parent("label").add('label[for="' + a.attr("id") + '"]').click(function(a) {
            b.click();
            a.preventDefault()
          });
          a.change(function() {
            d('input[name="' + a.attr("name") + '"]').next().removeClass("checked");
            a.next().addClass("checked")
          }).focus(function() {
            b.is(".disabled") || b.addClass("focused")
          }).blur(function() {
            b.removeClass("focused")
          });
          a.on("refresh", function() {
            a.is(":checked") ? (d('input[name="' + a.attr("name") + '"]').next().removeClass("checked"), b.addClass("checked")) : b.removeClass("checked");
            a.is(":disabled") ? b.addClass("disabled") : b.removeClass("disabled")
          })
        }
      }) : a.is(":file") ? a.css({
        position: "absolute",
        top: "-50%",
        right: "-50%",
        fontSize: "200px",
        opacity: 0
      }).each(function() {
        if (1 > a.parent("span.jq-file").length) {
          var b = d("<span" + l + ' class="jq-file' + q + '" style="display: inline-block; position: relative; overflow: hidden"></span>'),
            x = d('<div class="name" style="float: left; white-space: nowrap"></div>').appendTo(b);
          d('<div class="browse" style="float: left">' + g.browseText + "</div>").appendTo(b);
          a.after(b);
          b.append(a);
          a.is(":disabled") && b.addClass("disabled");
          a.change(function() {
            x.text(a.val().replace(/.+[\\\/]/, ""))
          }).focus(function() {
            b.addClass("focused")
          }).blur(function() {
            b.removeClass("focused")
          }).click(function() {
            b.removeClass("focused")
          }).on("refresh", function() {
            a.is(":disabled") ? b.addClass("disabled") : b.removeClass("disabled")
          })
        }
      }) : a.is("select") && a.each(function() {
        if (1 > a.next("span.jqselect").length) {
          var b = function() {
            function b(a) {
              a.bind("mousewheel DOMMouseScroll", function(a) {
                var b = null;
                "mousewheel" == a.type ? b = -1 * a.originalEvent.wheelDelta : "DOMMouseScroll" == a.type && (b = 40 * a.originalEvent.detail);
                b && (a.preventDefault(), d(this).scrollTop(b + d(this).scrollTop()))
              })
            }

            function s() {
              for (i = 0; i < f.length; i++) {
                var a = liClass = optgroupClass = optionClass = "";
                void 0 !== f.eq(i).attr("selected") && (liClass = "selected sel");
                f.eq(i).is(":disabled") && (liClass = "disabled");
                f.eq(i).is(":selected:disabled") && (liClass = "selected sel disabled");
                void 0 !== f.eq(i).attr("class") && (optionClass = " " + f.eq(i).attr("class"));
                a = '<li class="' + liClass + optionClass + '">' + f.eq(i).text() + "</li>";
                f.eq(i).parent().is("optgroup") && (void 0 !== f.eq(i).parent().attr("class") && (optgroupClass = " " + f.eq(i).parent().attr("class")), a = '<li class="' + liClass + optionClass + " option" + optgroupClass + '">' + f.eq(i).text() + "</li>", f.eq(i).is(":first-child") && (a = '<li class="optgroup' + optgroupClass + '">' + f.eq(i).parent().attr("label") + "</li>" + a));
                r += a
              }
            }

            function y() {
              d(".dropdown").css({
                'maxHeight': g.maxHeight
              });
              var e = d("<span" + l + ' class="jq-selectbox jqselect' + q + '" style="display: inline-block; position: relative; z-index:' + g.singleSelectzIndex + "; width:" + g.width + 'px"><div class="select" style="float: left; width:' + g.width + 'px"><div class="text"></div><b class="trigger"><i class="arrow"></i></b></div></span>');
              a.after(e).css({
                position: "absolute",
                left: -9999
              });
              var k = e.find("div.select"),
                h = e.find("div.text"),
                t = f.filter(":selected");
              t.length ? h.text(t.text()) : h.text(f.filter(":first").text());
              if (a.is(":disabled")) e.addClass("disabled");
              else {
                s();
                var c = d('<div class="dropdown" style="position: absolute; overflow: auto; overflow-x: hidden; width:' + g.width + 'px; maxHeight:' + g.maxHeight + '"><ul style="list-style: none">' + r + "</ul></div>");
                e.append(c);
                var m = c.find("li"),
                  u = e.outerHeight();
                "auto" == c.css("left") && c.css({
                  left: 0
                });
                "auto" == c.css("top") && c.css({
                  top: u
                });
                var n = m.outerHeight(),
                  v = c.css("top");
                c.hide();
                k.click(function() {
                  var a = d(window),
                    f = e.offset().top,
                    h = a.height() - u - (f - a.scrollTop()),
                    p = g.selectVisibleOptions,
                    k = 6 * n,
                    l = n * p;
                  0 < p && 6 > p && (k = l);
                  0 > h || h < k ? (c.height("auto").css({
                    top: "auto",
                    bottom: v
                  }), c.outerHeight() > f - a.scrollTop() - 20 && (c.height(Math.floor((f - a.scrollTop() - 20) / n) * n), 0 < p && 6 > p ? c.height() > k && c.height(k) : 6 < p && c.height() > l && c.height(l))) : h > k && (c.height("auto").css({
                    bottom: "auto",
                    top: v
                  }), c.outerHeight() > h - 20 && (c.height(Math.floor((h - 20) / n) * n), 0 < p && 6 > p ? c.height() > k && c.height(k) : 6 < p && c.height() > l && c.height(l)));
                  d("span.jqselect").css({
                    zIndex: g.singleSelectzIndex - 1
                  }).removeClass("focused");
                  e.css({
                    zIndex: g.singleSelectzIndex
                  });
                  c.is(":hidden") ? (d("div.dropdown:visible").hide(), c.show(), e.addClass("opened")) : (c.hide(), e.removeClass("opened"));
                  m.filter(".selected").length && c.scrollTop(c.scrollTop() + m.filter(".selected").position().top - c.innerHeight() / 2 + m.filter(".selected").outerHeight() / 2);
                  b(c);
                  return !1
                });
                m.hover(function() {
                  d(this).siblings().removeClass("selected")
                });
                var w = m.filter(".selected").text();
                m.filter(":not(.disabled):not(.optgroup)").click(function() {
                  var b = d(this),
                    e = b.text();
                  if (w != e) {
                    var n = b.index();
                    b.is(".option") && (n -= b.prevAll(".optgroup").length);
                    b.addClass("selected sel").siblings().removeClass("selected sel");
                    f.prop("selected", !1).eq(n).prop("selected", !0);
                    w = e;
                    h.text(e);
                    a.change()
                  }
                  c.hide()
                });
                c.mouseout(function() {
                  c.find("li.sel").addClass("selected")
                });
                a.focus(function() {
                  e.addClass("focused")
                }).blur(function() {
                  e.removeClass("focused")
                }).keyup(function() {
                  h.text(f.filter(":selected").text());
                  m.removeClass("selected sel").eq(f.filter(":selected").index()).addClass("selected sel")
                });
                d(document).on("click", function(a) {
                  d(a.target).parents().hasClass("selectbox") || (c.hide().find("li.sel").addClass("selected"), e.removeClass("focused opened"))
                })
              }
            }

            function z() {
              var e = d("<span" + l + ' class="jq-select-multiple jqselect' + q + '" style="display: inline-block"></span>');
              a.after(e).css({
                position: "absolute",
                left: -9999
              });
              s();
              e.append("<ul>" + r + "</ul>");
              var k = e.find("ul"),
                h = e.find("li").attr("unselectable", "on").css({
                  "-webkit-user-select": "none",
                  "-moz-user-select": "none",
                  "-ms-user-select": "none",
                  "-o-user-select": "none",
                  "user-select": "none"
                }),
                g = a.attr("size"),
                c = k.outerHeight(),
                m = h.outerHeight();
              void 0 !== g && 0 < g ? k.css({
                height: m * g
              }) : k.css({
                height: 4 * m
              });
              c > e.height() && (k.css("overflowY", "scroll"), b(k));
              a.is(":disabled") ? (e.addClass("disabled"), f.each(function() {
                d(this).is(":selected") && h.eq(d(this).index()).addClass("selected")
              })) : h.filter(":not(.disabled):not(.optgroup)").click(function(b) {
                var c = d(this);
                b.ctrlKey || c.addClass("selected");
                b.shiftKey || c.addClass("first");
                b.ctrlKey || b.shiftKey || c.siblings().removeClass("selected first");
                b.ctrlKey && (c.is(".selected") ? c.removeClass("selected first") : c.addClass("selected first"), c.siblings().removeClass("first"));
                if (b.shiftKey) {
                  var e = !1,
                    g = !1;
                  c.siblings().removeClass("selected").siblings(".first").addClass("selected");
                  c.prevAll().each(function() {
                    d(this).is(".first") && (e = !0)
                  });
                  c.nextAll().each(function() {
                    d(this).is(".first") && (g = !0)
                  });
                  e && c.prevAll().each(function() {
                    if (d(this).is(".selected")) return !1;
                    d(this).not(".disabled, .optgroup").addClass("selected")
                  });
                  g && c.nextAll().each(function() {
                    if (d(this).is(".selected")) return !1;
                    d(this).not(".disabled, .optgroup").addClass("selected")
                  });
                  1 == h.filter(".selected").length && c.addClass("first")
                }
                f.prop("selected", !1);
                h.filter(".selected").each(function() {
                  var a = d(this),
                    b = a.index();
                  a.is(".option") && (b -= a.prevAll(".optgroup").length);
                  f.eq(b).prop("selected", !0)
                });
                a.change()
              })
            }
            var f = a.find("option"),
              r = "";
            a.is("[multiple]") ? z() : y()
          };
          b();
          a.on("refresh", function() {
            a.next().remove();
            b()
          })
        }
      })
    })
  }
})(jQuery);
