 
 Growlyflash.defaults =
   align:   'right'  # horizontal aligning (left, right or center)
-  delay:   4000     # auto-dismiss timeout (0 to disable auto-dismiss)
+  delay:   4000     # auto-dismiss timeout (false to disable auto-dismiss)
   dismiss: yes      # allow to show close button
   spacing: 10       # spacing between alerts
   target:  'body'   # selector to target element where to place alerts
   title:   no       # switch for adding a title
   type:    null     # bootstrap alert class by default
   class:   ['alert', 'growlyflash', 'fade']
+  
+  # customizable callback to set notification position before it shows
+  before_show: (options) -> 
+    @el.css @calc_css_position()
 
 Growlyflash.KEY_MAPPING =
   alert:   'warning'


class Growlyflash.Alert
  constructor: (@flash, options) ->
    {@title, @align, @delay, @dismiss, @msg, @spacing, @target, @type, @class} = options
    
    @el = ($ '<div>', class: @_classes().join(' '), html: "#{@_dismiss()}#{@_title()}#{@msg}").appendTo(@target)
    @el.css(@_calc_position()).toggleClass('in')
    @el.delay(@delay).fadeOut(-> ($ @).remove()) if @delay > 0
  
  _classes: -> 
    @class.concat ("alert-#{type}" for type in [@type] when type?), ["growlyflash-#{@align}"]

    return "" unless @title?
    """<strong>#{@type.charAt(0).toUpperCase()}#{@type.substring(1)}!</strong>"""
  
  calc_top_offset: ->
    amount = parseInt(@el.css 'top')
    (@el.siblings '.growlyflash').each (_, el) =>
      amount = Math.max(amount, parseInt(($ el).css 'top') + ($ el).outerHeight() + @spacing)
    amount
  
  calc_css_position: ->
    css = {}
    css.top        = "#{@calc_top_offset()}px"
    css.marginLeft = "-#{@el.width() / 2}px" if @align is 'center'
    css

$.growlyflash = (flash, options = {}) ->
  settings = $.extend on, {}, Growlyflash.defaults, msg: flash.msg, type: flash.type, options
  alert = new Growlyflash.Alert(flash, settings)
  if flash instanceof Growlyflash.FlashStruct then flash else alert
