 class PuzzleApp
 
   constructor: () ->
+    @puzzle_view = new PuzzleView(this)
     @grid_model = new PuzzleGridModel
-    @puzzle_view = new PuzzleView
-    @hex_draw = new HexDraw
+    @hex_draw = new HexDraw(this)
     @pattern = new PuzzlePattern(this)
-    @mask = new MissingPiecesMask(this,@pattern)
-    @mask.draw_mask()
-
-    @piece = new PuzzlePiece(this,@pattern)
+    @mask = new MissingPiecesMask(this)
+    @piece = new PuzzlePiece(this)
+
+
+#    alert(" T W E L V E !!!")
+#    @puzzle_view.temp_draw_piece_mask(0,0)
 
 
 


class PuzzleView

  constructor: () ->

    @backgrounds = ["#cc5050","#80cc50","#50b4cc",
                    "#b450cc","#cc8050","#50cc50",
                    "#5080cc","#cc50b4","#ccb450",
                    "#50cc80","#5050cc","#cc5080",
                    "#b4cc50","#50ccb4","#8050cc"]
    @mask_color = @backgrounds[0] # FIXME (temporarily using same color without rotating)

    @canvas = document.getElementById("puzzle-widget")
    @context = @canvas.getContext("2d")

    @img = document.getElementById("photo")
    @context.drawImage(@img,100,30)



class PuzzleGridModel


  constructor: (piece) ->
    @piece = piece
    @hex_draw = @piece.puzzle.hex_draw
    @img = document.createElement('canvas')
    @img.id = "piece-mask"


  draw_pattern: () ->
    @img.width = @piece.box.width
    @img.height = @piece.box.height
    @hexes = @piece.hexes
    @hex_draw.set_context("canvas")
#    @hex_draw.set_context("piece_mask")
    for hx in @hexes
      aa = hx[0]
      bb = hx[1]
      @hex_draw.fill_hex_ab_xy(aa,bb,6,9,3)



class PieceImage

  constructor: (puzzle_piece) ->

    @piece = puzzle_piece
    @puzzle = @piece.puzzle
#    @img = @puzzle.puzzle_view.piece_mask
    @img = document.createElement('canvas')
    @img.id = "piece-mask"
    @img.width = @piece.box.width
    @img.height = @piece.box.height
    @piece_image_context = @img.getContext("2d")


# FIXME Nothing references this method yet.
draw_piece:  (a,b) ->
    # TODO I think the books said there are more than one kind of image object.
    @pc_img = document.getElementById("piece")
    if @grid_model.in_range(a,b)
      xy = @grid_model.get_xy(a,b)
