 _ = require 'underscore'
 Backbone = require 'backbone'
+Cookies = require 'cookies-js'
 template = -> require('./template.jade') arguments...
-Cookies = require 'cookies-js'
 
 module.exports = class ZigZagBanner extends Backbone.View
   className: 'zig-zag-banner'

      throw new Error('You must pass a name, $target, and message')

    if @backwards
      @$el.addClass('zig-zag-backwards').css "margin-left": @$target.width()

    if @persist
      if Cookies.get("zig_zag_#{@name}")?
        # Has already seen this... ignore
        return @remove()
      else
        # Will see this once until cookie expires
        Cookies.set "zig_zag_#{@name}", true, expires: 60 * 60 * 24 * 365

    @transitionIn()

  render: ->
    @$el.html @template(message: @message)

    newMargin = offset + existingMargin
    @$el.css marginTop: newMargin

  delay: (cb) ->
    _.delay cb, @segmentTransitionLength

  transitionIn: (cb) ->
    @render()
    _.defer =>
      @delay =>
        @$one.addClass 'is-in'
        @delay =>
          @$two.addClass 'is-in'
          @delay =>
            @$three.addClass 'is-in'
            @delay =>
              @$el.addClass 'is-done'
              @delay(cb) if cb?

  transitionOut: (cb) ->
    @delay =>
      @$el.removeClass 'is-done'
      @delay =>
        @$three.removeClass 'is-in'
        @delay =>
          @$two.removeClass 'is-in'
          @delay =>
            @$one.removeClass 'is-in'
            @delay(cb) if cb?

  close: (e) ->
    e?.preventDefault()
    e?.stopPropagation()
    @transitionOut =>
      @remove()
