     @drawBar()   if @getOption('drawBar')
     @addLabels() if @getOption('showLabels')
     @attachEvents()
+    window.asd = @
 
 class KDSliderBarHandleView extends KDCustomHTMLView
-  constructor:(options = {}, data = {})->
+  constructor:(options = {})->
     options.tagName   = "a"
     options.cssClass  = "handle"
-    data.value       ?= 0
+    options.value    ?= 0
     options.draggable =
       axis            : "x"
 
-    super options, data
+    super options
 
   attachEvents:->
     {maxValue, minValue, width} = @parent.getOptions()
-    {value}                     = @getData()
+    value                       = @getOption "value"
 
     @on "DragStarted", ->
-      value = @getData().value
+      value = @getOption "value"
     @on "DragInAction", ->
-      relPos       = @dragState.position.relative.x
-      valueChange  = ((maxValue-minValue)*relPos)/width
-      @setValue value+valueChange
-    @on "DragFinished", ->
-      @snap() if @parent.getOption "snap"
+      relPos             = @dragState.position.relative.x
+      valueChange        = ((maxValue - minValue) * relPos) / width
+      @setValue value + valueChange
+      @snap() if @parent.getOption "snapOnDrag"
+    @on "DragFinished", -> @snap() if @parent.getOption "snap"
 
   getPosition:->
     {maxValue, minValue} = @parent.getOptions()
-    {value}              = @getData()
+    {value}              = @getOptions()
     sliderWidth          = @parent.getWidth()
 
-    percentage = ((value-minValue)*100)/(maxValue-minValue)
-    position   = (sliderWidth/100)*percentage
-
+    percentage = ((value - minValue) * 100) / (maxValue - minValue)
+    position   = (sliderWidth / 100) * percentage
     return "#{position}px"
 
+
   setValue:(value)->
-    {leftLimit, rightLimit} = @getData()
+    {leftLimit, rightLimit} = @getOptions()
 
-    if !(value<leftLimit) and !(value>rightLimit)
-      @data.value = value
-    
-    else if value > rightLimit
-      @data.value = rightLimit
+    value = Math.min value, rightLimit if typeof rightLimit is "number"
+    value = Math.max value, leftLimit  if typeof leftLimit  is "number"
 
-    else if value < leftLimit
-      @data.value = leftLimit
+    @options.value = value
 
     @setX "#{@getPosition()}"
     @parent.drawBar() if @parent.getOption('drawBar')


  createHandles:->
    for value in @getOption "handles"
      @handles.push @addSubView handle = new KDSliderBarHandleView  {}, {value}

    sortRef = (a,b) ->
      return -1 if a.getData().value < b.getData().value
      return  1 if a.getData().value > b.getData().value
      return  0
    @handles.sort sortRef

  drawBar:->
    positions = []
    for handle in @handles
      positions.push handle.getRelativeX()

    len       =  positions.length
    left      = (parseInt(positions.first) if len>1) or 0
    right     =  parseInt(positions.last)
    diff      =  right - left

    unless @bar
      @addSubView @bar = new KDCustomHTMLView

    @bar.setWidth diff
    @bar.setX "#{left}px"

  _ಠcreateLabel : (value) =>
    {maxValue, minValue, interval, showLabels} = @getOptions()

    pos = ((value - minValue) * 100) / (maxValue - minValue)
    @labels.push @addSubView label = new KDCustomHTMLView
      cssClass    : "sliderbar-label"
      partial     : "#{value}"
    label.setX "#{pos}%"

  addLabels:->
    {maxValue, minValue, interval, showLabels} = @getOptions()

    if Array.isArray showLabels
    then @_ಠcreateLabel value for value in showLabels
    else @_ಠcreateLabel value for value in [minValue..maxValue] by interval

  getValues:-> handle.getOptions().value for handle in @handles

  setLimits:->
    {maxValue, minValue, interval}      = @getOptions()

    if @handles.length is 1
      @handles.first.options.leftLimit  = minValue
      @handles.first.options.rightLimit = maxValue
    else
      for handle, i in @handles
        options            = handle.getOptions()
        options.leftLimit  = @handles[i-1]?.options.value + interval or minValue
        options.rightLimit = @handles[i+1]?.options.value - interval or maxValue

  attachEvents:->
    @on "click", (event) ->
      {maxValue, minValue} = @getOptions()
      sliderWidth          = @getWidth()
      clickedPos           = event.pageX - @getBounds().x
      clickedValue         = ((maxValue - minValue) * clickedPos) / sliderWidth + minValue
      snappedValue         = @handles.first.getSnappedValue value: clickedValue
      closestHandle        = null
      mindiff              = null

      for handle in @handles
        {value}            = handle.getOptions()
        diff               = Math.abs(clickedValue - value)
        if (diff < mindiff) or not mindiff
          mindiff          = diff
          closestHandle    = handle

      closestHandle.setValue snappedValue

