 
     return @items
 
+
   scrollDown: ->
 
     clearTimeout @_scrollDownTimeout
-    @_scrollDownTimeout = setTimeout =>
+
+    @_scrollDownTimeout = KD.utils.wait 50, =>
       scrollView    = @$().closest(".kdscrollview")
       slidingView   = scrollView.find '> .kdview'
+      slidingHeight = slidingView.height()
 
-      # scrollTop         = scrollView.scrollTop()
-      slidingHeight     = slidingView.height()
-      # scrollViewheight  = scrollView.height()
-      # scrollDown        = slidingHeight - scrollViewheight - scrollTop
-      scrollView.animate (scrollTop : slidingHeight), (duration: 200, queue: no)
-    , 50
+      scrollView.animate
+        scrollTop : slidingHeight
+      ,
+        duration  : 200
+        queue     : no
+
 
   doIHaveToScroll: ->
 
-    scrollView = @$().closest(".kdscrollview")
     if @getOptions().autoScroll
-      if scrollView.length and scrollView[0].scrollHeight <= scrollView.height()
-        yes
-      else
-        @isScrollAtBottom()
-    else
-      no
+      scrollView = @$().closest(".kdscrollview")[0]
+      if scrollView.length and scrollView.scrollHeight > scrollView.outerHeight()
+      then yes
+      else @isScrollAtBottom scrollView
+    else no
 
-  isScrollAtBottom: ->
+  isScrollAtBottom: (scrollView) ->
 
-    scrollView        = @$().closest(".kdscrollview")
-    slidingView       = scrollView.find '> .kdview'
+    slidingView       = scrollView.find('> .kdview')[0]
 
-    scrollTop         = scrollView.scrollTop()
-    slidingHeight     = slidingView.height()
-    scrollViewheight  = scrollView.height()
+    scrollTop         = scrollView.scrollTop
+    slidingHeight     = slidingView.clientHeight
+    scrollViewheight  = scrollView.clientHeight
 
-    if slidingHeight - scrollViewheight is scrollTop
-      return yes
-    else
-      return no
+    return slidingHeight - scrollViewheight is scrollTop
 


module.exports = class KDListView extends KDView

  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}"

    @items = [] unless @items

    super options,data

  empty:->

    for item,i in @items
      item.destroy() if item?
    @items = []

  keyDown:(event)->

    event.stopPropagation()
    event.preventDefault()
    @emit "KeyDownOnList", event

  addItem:(itemData, index, animation)->

    {itemChildClass, itemChildOptions} = @getOptions()

    if index? and typeof index isnt 'number'
      itemOptions = index
      index = null
    else
      {itemOptions} = @getOptions()


    itemOptions.childOptions or= itemChildOptions

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

    return itemInstance


  removeItem: (itemInstance, itemData, index) ->

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