 
 module.exports = class KDListView extends KDView
 
-  constructor:(options = {}, data)->
+  constructor: (options = {}, data) ->
 
-    options.type       or= "default"
-    options.lastToFirst ?= no
-    options.cssClass     = if options.cssClass? then "kdlistview kdlistview-#{options.type} #{options.cssClass}" else "kdlistview kdlistview-#{options.type}"
+    options.type        or= "default"
+    options.lastToFirst  ?= no
+    options.cssClass      = if options.cssClass? then "kdlistview kdlistview-#{options.type} #{options.cssClass}" else "kdlistview kdlistview-#{options.type}"
 
-    @items = [] unless @items
+    super options, data
 
-    super options,data
-
-  empty:->
-
-    for item,i in @items
-      item.destroy() if item?
     @items = []
 
-  keyDown:(event)->
 
-    event.stopPropagation()
-    event.preventDefault()
+  empty: ->
+
+    item.destroy()  for item, i in @items when item
+
+    @items = []
+
+
+  keyDown: (event) ->
+
+    KD.utils.stopDOMEvent event
     @emit "KeyDownOnList", event
 
-  addItem:(itemData, index, animation)->
+
+  addItem: (itemData, index) ->
 
     {itemChildClass, itemChildOptions} = @getOptions()
 
-    if index? and typeof index isnt 'number'
+    if index? and 'number' isnt typeof index
       itemOptions = index
-      index = null
+      index       = null
     else
       {itemOptions} = @getOptions()
 

    itemOptions.childOptions or= itemChildOptions

    itemInstance = new (@getOptions().itemClass ? KDListItemView) itemOptions, itemData
    @addItemView itemInstance, index, animation

    return itemInstance

  removeItem:(itemInstance, itemData, index)->

    if index?
      @emit 'ItemIsBeingDestroyed', { view : @items[index], index : index }


    return @items


  scrollDown: ->

    clearTimeout @_scrollDownTimeout

    @_scrollDownTimeout = KD.utils.wait 50, =>
      scrollView    = @$().closest(".kdscrollview")
      slidingView   = scrollView.find '> .kdview'
      slidingHeight = slidingView.height()

      scrollView.animate
        scrollTop : slidingHeight
      ,
        duration  : 200
        queue     : no


  doIHaveToScroll: ->

    if @getOptions().autoScroll
      scrollView = @$().closest(".kdscrollview")[0]
      if scrollView.length and scrollView.scrollHeight > scrollView.outerHeight()
      then yes
      else @isScrollAtBottom scrollView
    else no

  isScrollAtBottom: (scrollView) ->

    slidingView       = scrollView.find('> .kdview')[0]

    scrollTop         = scrollView.scrollTop
    slidingHeight     = slidingView.clientHeight
    scrollViewheight  = scrollView.clientHeight

    return slidingHeight - scrollViewheight is scrollTop

