           infoData[0].forEach (d) ->
             that.cellInfos[d.alphabet] = d.cell
             that.toAlpha[d.cell] = d.alphabet
-        d.resolve
-      deferr.push d.promise
-
-      @layers.forEach (s) ->
-        d = new $.Deferred
-        $.when(
-          $.getJSON(posUrl + '?suffix=' + s),
-          $.getJSON(labelUrl + '?suffix=' + s)
-        ).done (posData, labelData) ->
-          posData[0].forEach (d) ->
-            that.positions[d.cell] = {x: d.posX, y: d.posY}
-            frameName = d.routeName ? 'route_'+d.cell+'_1'
-            if d.routeX && d.routeY
-              that.image.setLayerPoint(s, frameName, {x: d.posX + d.routeX, y: d.posY + d.routeY})
-          labelData[0].forEach (d) ->
-            that.image.setLayerPoint(s, d.imageName, {x: d.posX, y: d.posY})
-          d.resolve
-        deferr.push d.promise
-
-      $.when.apply($,deferr).done ->
-        that.onload()
+
+        if that.layers.length == 0
+          return that.onload()
+
+        labelUrl = that.tag.attr('data-labelpos')
+        layerCount = 0
+        that.layers.forEach (s) ->
+          $.when(
+            $.getJSON(posUrl, {suffix: s}),
+            $.getJSON(labelUrl, {suffix: s})
+          ).done (posData, labelData) ->
+            posData[0].forEach (d) ->
+              that.positions[d.cell] = {x: d.posX, y: d.posY}
+              frameName = d.routeName ? 'route_'+d.cell+'_1'
+              if d.routeX && d.routeY
+                that.image.setLayerPoint(s, frameName, {x: d.posX + d.routeX, y: d.posY + d.routeY})
+            labelData[0].forEach (d) ->
+              that.image.setLayerPoint(s, d.imageName, {x: d.posX, y: d.posY})
+            layerCount++
+            if that.layers.length == layerCount
+              that.onload()
 
   setPoint: (cell, fixed) ->
     if fixed

      that.onclick(event.clientX - @offsetLeft + nX, event.clientY - @offsetTop + nY)

  loadImage: () ->
    imageUrl = @tag.attr('data-src')
    @bgImg = new Image()
    @layerImgs = []
    that = @
    @bgImg.onload = () ->
      that.setImage()
      layerCount = 0
      if layerCount == that.layers.length
        return that.onload()

      that.layers.forEach (s) ->
        img = new Image()
        img.onload = ->
          layerCount++
          if layerCount == that.layers.length
            that.onload()
        img.src = imageUrl + '?suffix=' + s
        that.layerImgs[s] = img
    @bgImg.src = imageUrl

  setImage: () ->
    @ctx.globalAlpha = 1.0
    that = @
    @bgLayers.forEach (name) ->
      frame = that.frames[name]
      that.ctx.drawImage(that.bgImg, frame.pos.x, frame.pos.y, frame.width, frame.height, 0, 0, frame.width, frame.height)

  setLayers: () ->
    that = @


class @SeaMap
  constructor: (idTag) ->
    @tag = $('#' + idTag)
    @positions = []
    @cellInfos = {}
    @toAlpha = []
    @layers = []
    layerUrl = @tag.attr('data-layer')
    if layerUrl
      that = @
      $.getJSON layerUrl, (data) ->
        that.layers = data
        image = new MapImage(idTag, that.layers)
        image.onclick = that.runClick
        that.image = image
        that.load()
    else
      @image = new MapImage(idTag, @layers)
      @image.onclick = @runClick
      @load()

  load: () ->
    posUrl = @tag.attr('data-position')
    infoUrl = @tag.attr('data-cellinfo')
    that = @
    @image.onload = () ->
      $.when(
        $.getJSON(posUrl),
        if infoUrl then $.getJSON(infoUrl) else null
