 							loading = (value) ->
 								datasource.loading(value) if datasource.loading
 
+						ridActual = 0
 						first = 1
 						next = 1
 						buffer = []

							else
								doAdjustment()

						finalize = (scrolling, newItems)->
							adjustBuffer scrolling, newItems, ->
								pending.shift()
								if pending.length == 0
									isLoading = false
									loading(false)
								else
									fetch(scrolling)

						fetch = (scrolling) ->
							direction = pending[0]
							#log "Running fetch... #{{true:'bottom', false: 'top'}[direction]} pending #{pending.length}"
							if direction
								if buffer.length && !shouldLoadBottom()
									finalize(scrolling)
								else
									#log "appending... requested #{bufferSize} records starting from #{next}"
									datasource.get next, bufferSize,
									(result) ->
										newItems = []
										if result.length == 0
											eof = true
											adapter.bottomPadding(0)
											log "appended: requested #{bufferSize} records starting from #{next} recieved: eof"
										else
											clipTop()
											for item in result
												newItems.push (insert ++next, item)
											log "appended: requested #{bufferSize} received #{result.length} buffer size #{buffer.length} first #{first} next #{next}"
										finalize(scrolling, newItems)

							else
								if buffer.length && !shouldLoadTop()
									finalize(scrolling)
								else
									#log "prepending... requested #{size} records starting from #{start}"
									datasource.get first-bufferSize, bufferSize,
									(result) ->
										newItems = []
										if result.length == 0
											bof = true
											adapter.topPadding(0)
											log "prepended: requested #{bufferSize} records starting from #{first-bufferSize} recieved: bof"
										else
											clipBottom() if buffer.length
											for i in [result.length-1..0]
												newItems.unshift (insert --first, result[i])
											log "prepended: requested #{bufferSize} received #{result.length} buffer size #{buffer.length} first #{first} next #{next}"
										finalize(scrolling, newItems)

						resizeHandler = ->
							if !$rootScope.$$phase && !isLoading
								adjustBuffer(false)
								$scope.$apply()

						viewport.bind 'resize', resizeHandler

						scrollHandler = ->
							if !$rootScope.$$phase && !isLoading
								adjustBuffer(true)
								$scope.$apply()

						viewport.bind 'scroll', scrollHandler

									next++

							item.scope.$index = first + i for item,i in buffer
							adjustBuffer(null, false, inserted)

		])

