 #
 # Image Picker source
 # by Rodrigo Vera
+# added limit-functionality by Jason M. Batchelor
 #
+type = (obj) ->
+  if obj == undefined or obj == null
+    return String obj
+  classToType = new Object
+  for name in "Boolean Number String Function Array Date RegExp".split(" ")
+    classToType["[object " + name + "]"] = name.toLowerCase()
+  myClass = Object.prototype.toString.call obj
+  if myClass of classToType
+    return classToType[myClass]
+  return "object"
 
 jQuery.fn.extend({
   imagepicker: (opts = {}) ->

  constructor: (select_element, @opts={}) ->
    @select         = jQuery(select_element)
    @multiple       = @select.attr("multiple") == "multiple"
    @build_and_append_picker()

  build_and_append_picker: () ->

      if imagepicker_option.value() in @selected_values()
        imagepicker_option.option.prop("selected", false)
      else
        if @limit > 0 && (@selected_values().length < @limit)
          imagepicker_option.option.prop("selected", true)
        else
          if type(@limit_reached) is "function"
            @limit_reached.call();
    else
      if @has_implicit_blanks() && imagepicker_option.is_selected()
        @select.val("")
