 
     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
 

    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 }

          item.destroy()
          return


  removeItemByData: (itemData) -> @removeItem null, itemData


  removeItemByIndex: (index) -> @removeItem null, null, index


  destroy: ->

    item.destroy() for item in @items

    super


  addItemView: (itemInstance, index = 0) ->

    {lastToFirst} = @getOptions()

    @emit 'ItemWasAdded', itemInstance, index

    if index?
      actualIndex = if lastToFirst then @items.length - index - 1 else index
      @items.splice actualIndex, 0, itemInstance
    else
      if lastToFirst
      then @items.unshift itemInstance
      else @items.push itemInstance

    @appendItemAtIndex itemInstance, index

    return itemInstance


  appendItem: (itemInstance) -> @appendItemAtIndex itemInstance


  appendItemAtIndex: (itemInstance, index = 0) ->

    itemInstance.setParent @

    scroll        = @doIHaveToScroll()
    {lastToFirst} = @getOptions()
    $item         = itemInstance.$()

    if index is 0

      if lastToFirst
      then @$().prepend $item
      else @$().append $item

    else if index > 0

      if lastToFirst
        index = @items.length - index - 2
        @items[index].$().before $item
      else
        index -= 1
        @items[index].$().after  $item

    @scrollDown() if scroll

    itemInstance.emit 'viewAppended'  if @parentIsInDom

    null


  getItemIndex:(targetItem)->
    for item, index in @items
      return index if item is targetItem
    return -1


  moveItemToIndex:(item, newIndex)->

