# The Modularity framework written specificially for CoffeeScript.
window.modularity =

  # Checks whether the given condition is true.
  assert: (condition, message) ->
    if !condition or condition?.length is 0
      console.error message
      false
    else
      true


  # Registers the given callback as a listener to the given global event.
  on: (event_type, callback) ->
    return unless modularity.assert(typeof event_type is 'string',
                                    "modularity.on: parameter 'event_type' is empty")
    unless typeof callback is 'function'
      return console.error "modularity.on: parameter 'callback' must be a function,
                            #{callback} (#{typeof callback}) given."
    modularity.global_event_container().on event_type, callback


  # Fires the given global event with the given data payload.
  trigger: (event_type, data) ->
    modularity.assert event_type,
                      'Module.trigger: You must provide the event type to fire.'
    unless typeof event_type is 'string'
      return console.error "Module.trigger: Event type must be a string,
                            #{event_type} (#{typeof event_type}) given."
    modularity.global_event_container().trigger event_type,
                                                data ?= {}


  # Returns the DOM object that is used to fire global events on.
  global_event_container: -> modularity.global_event_container_cache or= $('body')
