 # of the given `name` from the page.
 # It's the widget responsibility to clean up its dependencies during
 # the `dispose` call.
-widgets.release = (names...) ->
+agt.widgets.release = (names...) ->
   names = Object.keys(__instances__) if names.length is 0
   for name in names
     __instances__[name].each (value) -> value.dispose()
 
 # Activates all the widgets instances of type `name`.
-widgets.activate = (names...) ->
+agt.widgets.activate = (names...) ->
   names = Object.keys(__instances__) if names.length is 0
   for name in names
     __instances__[name].each (value) -> value.activate()
 
 # Deactivates all the widgets instances of type `name`.
-widgets.deactivate = (names...) ->
+agt.widgets.deactivate = (names...) ->
   names = Object.keys(__instances__) if names.length is 0
   for name in names
     __instances__[name].each (value) -> value.deactivate()

    res = $(element)[name](options)
    block?(res, options)

widgets.delete = (name) ->
  __subscriptions__[name]?.forEach (subscription) -> subscription.off()
  widgets.release(name)
  delete __widgets__[name]

widgets.reset = (names...) ->
  names = Object.keys(__instances__) if names.length is 0
  widgets.delete(name) for name in names

widgets.widgetsFor = (element, widget) ->
  if widget?
    __instances__[widget].get(element)
  else
    instances.get(element) for instances in __instances__ when instances.hasKey(element)

widgets.subscribe = (name, to, evt, handler) ->
  __subscriptions__[name] ||= []
  to.addEventListener(evt, handler)
  subscription = off: -> to.removeEventListener(evt, handler)


# The `widgets` function is both the main module and the function
# used to register the widgets to apply on a page.
agt.widgets = (name, selector, options={}, block) ->
  unless __widgets__[name]?
    throw new Error "Unable to find widget '#{name}'"

