     @select @storage.prev @current
     return @
 
-
-
-
   add: (images) ->
     return @ if not images or images.length is 0
 
     for object in images
-      image     = null
-      isElement = if typeof HTMLElement is 'object' then object instanceof HTMLElement else typeof object is 'object' and object.nodeType is 1 and typeof object.nodeName is 'string'
+      image = null
+
+      if typeof HTMLElement is 'object'
+        isElement = object instanceof HTMLElement
+      else
+        isElement = typeof object is 'object' and object.nodeType is 1 and typeof object.nodeName is 'string'
 
       if isElement
-        image  = object
+        image = object
+
         object =
-          orig:  getAttribute(image, 'data-src') or getAttribute(image.parentNode, 'href')
+          orig: getAttribute(image, 'data-src') or getAttribute(image.parentNode, 'href')
           title: getAttribute(image, 'data-title') or getAttribute(image, 'title') or getAttribute(image, 'alt') or getAttribute(image.parentNode, 'title')
           thumb: getAttribute(image, 'src')
 
       addImage @, object, image
 
-    @
-
-
-
+    return @
 
   updateDimensions: ->
     if @options.thumbnails
-      @dimensions =
-        thumbWidth: offsetWidth @thumbnails
-    for i of @storage.images
-      setSize @storage.images[i]
-
-
-
+      @dimensions = thumbWidth: offsetWidth @thumbnails
+
+    for cid, image of @storage.images
+      setSize image
+
+    return
 
   updateSides: (item) ->
     return if isTouch
+
     if not item.size
       item.size = offsetWidth getTarget item.slide, ".#{choco}slide-container"
+
     s = "#{(env.w - item.size) / 2}px"
+
     setStyle @leftside, width: s
     setStyle @rightside, width: s
 
-
-
+    return
 
   toggleThumbnails: ->
     return if isTouch
+
     containers = ['leftside', 'rightside', 'overlay', 'thumbnailsToggle']
+
     if hasClass @thumbnails, choco_hide
       removeClass @thumbnails, choco_hide
+
       for container in containers
         removeClass @[container], choco_no_thumbnails
     else
       addClass @thumbnails, choco_hide
+
       for container in containers
         addClass @[container], choco_no_thumbnails
+
+    return
 
   initTouch: ->
     return false

      for container in ['overlay', 'leftside', 'rightside']
        prepareActionFor @, container

    @add images if images

    instances.push @




  close: ->
    isOpen = false
    opened = null
    if hasClass @overlay, choco_show
      removeClass @overlay, choco_show
      removeClass document.body, choco_body
      removeClass @current.thumbnail, choco_selected if @options.thumbnails
      @current = null
      pushState()
    @




  open: (cid, updateHistory) ->
    return if isOpen
    opened = @
    isOpen = true
    addClass @overlay, choco_show
    addClass document.body, choco_body
    @updateDimensions()
    @select cid, updateHistory
    @




  select: (item, updateHistory = true) ->

    if typeof item is 'number'
      item = @storage.get item

    return false unless item

    getEnv()

    translate @slider, env.shift * item.cid

    if @options.thumbnails

      removeClass @current.thumbnail, choco_selected if @current?

      thumb = item.thumbnail
      addClass thumb, choco_selected

      offset = env.w / 2 - thumb.offsetLeft - offsetWidth(thumb) / 2

      offset = squeeze offset, 0, env.w - @dimensions.thumbWidth
      translate @thumbnails, offset

    if updateHistory
      title = if item.title then item.title else item.hashbang
      pushState title, item.hashbang

    loading = hasClass item.slide, choco_loading
    if loading
      loadImage item, (success) =>
        @updateSides item
    else
      @updateSides item

    @current = item

    true




  next: ->
    @select @storage.next @current

      h = toInt(style 'height') -
          toInt(style 'padding-top') -
          toInt(style 'padding-bottom')

      w = shift -
          toInt(style 'padding-left') -
          toInt(style 'padding-right')

      env.shift = shift * -1
      env.s = {w, h}

    return env

  setSize = (item) ->
    return unless item.w > 0 and item.h > 0

    getEnv()

    s = scale item.w, item.h, env.s.w, env.s.h

    item.img.width = s[0]
    item.img.height = s[1]

    setStyle item.slide, 'padding-top': "#{(env.s.h - s[1]) / 2}px"
    return

  addEvent window, 'resize', ->
    needResize = true

    if isOpen
      opened.updateDimensions()
      opened.select opened.current

    return

  unless isTouch
    addEvent window, 'keyup', (event) ->
      if not isOpen or not hasClass opened.overlay, choco_show
        return

      switch event.keyCode
        when 27 # ESC
          opened.close()
