LayoutJigsaw = {}
setmetatable(LayoutJigsaw, Layout)
-- ----------------------------------------------------------------------------
LayoutJigsaw.__index = function(t, k)

	if rawget(t, k) ~= nil then
		return rawget(t, k)
	elseif LayoutJigsaw[k] ~= nil then
		return LayoutJigsaw[k]
	end
	
end
-- ----------------------------------------------------------------------------
-- @param pieces the pieces you need to place
-- @param piecesCorrect will be shown when you place a 'piece' correctly
-- @param piecesFind a 'piece' will be activated if you've found the corresponding 'find' piece
-- @param piecesMirror will become active when a 'piece' is placed in its correct spot
-- @param pieceTransfer a group object that will activate all 'pieces'
-- @param the object pieceTransfer needs to be dragged on
-- @param rotateButton a rotate button
function LayoutJigsaw:setup(pieces, piecesCorrect, piecesFind, piecesMirror, pieceTransfer, objectTransfer, rotateButton)

	-- required
	assert(type(pieces) == "table")
	self.mPieces = pieces
	assert(#self.mPieces > 0)	

	-- optional
	self.mPiecesCorrect = piecesCorrect
	
	-- check
	-- if piecesCorrect is used, it needs to be the same amount
	-- if piecesCorrect is used, it needs to be the same size	
	if (self.mPiecesCorrect) then
		assert(#self.mPiecesCorrect == #self.mPieces)
	
		-- check
		-- pieces and correct need to be equally sized
		for i = 1, #self.mPieces do
--			assert(self.mPieces[i]:width() == self.mPiecesCorrect[i]:width())
--			assert(self.mPieces[i]:height() == self.mPiecesCorrect[i]:height())
		end	
	end
	
	-- optional
	self.mPiecesFind = piecesFind	
	
	-- check
	-- if piecesFind is used, it needs to be the same amount
	if (self.mPiecesFind) then
		assert(type(piecesFind) == "table")	
		assert(#self.mPiecesFind == #self.mPieces)
		
		assert(type(pieceTransfer) == "nil")
		assert(type(objectTransfer) == "nil")
	end
	
	-- optional
	self.mPiecesMirror = piecesMirror
	
	-- check
	-- if piecesMirror is used, it needs to be the same amount
	if (self.mPiecesMirror) then
		assert(type(piecesMirror) == "table")	
		assert(#self.mPiecesMirror == #self.mPieces)
		
		-- all initially inactive
		for i, mirror in ipairs(self.mPiecesMirror) do
			mirror:ss(kInactive)
		end
	end	
	
	-- optional
	self.mPieceTransfer = pieceTransfer
	self.mObjectTransfer = objectTransfer
	
	-- check
	-- if pieceTransfer is used, it needs to an object, not a table
	if (self.mPieceTransfer) then
		assert(type(pieceTransfer) == "table")	
		assert(type(objectTransfer) == "table")	

		assert(type(piecesFind) == "nil")
	end	
	
	-- optional
	self.mRotate = rotateButton
	
	-- initial state
	if (self.mRotate) then
		self.mRotate:ss(kIdle)
		self.mRotate.enabled = false
	end	
	
	-- new variables
	self.mDragging = false
	self.mDraggingGroup = nil
	self.mDraggingPieces = nil
	self.mDragX = nil
	self.mDragY = nil
	self.mSnapDistance = 20
	self.mSnapAngle = 0.0872664626
	self.mPuzzle = true
	
end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onInitialize()

	if (self.mPiecesFind and self.mPiecesCorrect) then
		-- find AND correct are used
		-- inactivate pieces
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kInactive)
		end
		-- inactivate correct
		for i, correct in ipairs(self.mPiecesCorrect) do
			correct:ss(kInactive)
		end
		
	elseif (self.mPiecesFind) then
		-- find is used, correct is NOT used
		-- inactivate pieces
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kInactive)
		end		

	elseif (self.mPieceTransfer and self.mPiecesCorrect) then
		-- transfer AND correct are used
		-- inactivate pieces
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kInactive)
		end
		-- inactivate correct
		for i, correct in ipairs(self.mPiecesCorrect) do
			correct:ss(kInactive)
		end
		
	elseif (self.mPieceTransfer) then		
		-- transfer is used, correct is NOT used
		-- inactivate pieces
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kInactive)
		end				
		
	elseif (self.mPiecesCorrect) then
		-- correct is used, find is NOT used
		-- inactivate correct
		for i, correct in ipairs(self.mPiecesCorrect) do
			correct:ss(kInactive)
		end
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kActive)
		end
		-- enable rotate button
		if (self.mRotate) then
			self.mRotate:ss(kActive)
		end		
	else
		-- correct is NOT used, find is NOT used
		-- activate pieces
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kActive)
		end	
		-- enable rotate button
		if (self.mRotate) then
			self.mRotate:ss(kActive)
		end				
	end
	
	-- store solve x, y
	for i, piece in ipairs(self.mPieces) do
		piece.solveX = piece:x()
		piece.solveY = piece:y()
	end	
	
	-- register all pieces as IOs
	-- so we can click them!
	for i, piece in ipairs(self.mPieces) do
		self.mIOs[piece] = 
		{
			cursors = kCursorSelect
		}	
	end	
	
	-- register transfer object as IO
	if (self.mPieceTransfer and self.mObjectTransfer) then
		self.mIOs[self.mObjectTransfer] =
		{
			links = {self.mPieceTransfer},
			cursor = kCursorDefault,
		}	
		assert(not self.mPieceTransfer.timesUsedCount)
		assert(not self.mPieceTransfer.timesLinkedCount)
		self.mPieceTransfer.timesUsedCount = 0
		self.mPieceTransfer.timesLinkedCount = 1
		assert(not self.mObjectTransfer.linked)
		self.mObjectTransfer.linked = {self.mPieceTransfer}
	end
	
	-- register rotate button
	if (self.mRotate) then
		self.mButtons[self.mRotate] = {}
	end	
	
	-- all pieces form their own group initially
	-- later groups are merged
	for i, piece in ipairs(self.mPieces) do
		piece.pieceIndex = i
		piece.groupIndex = i
	end
	
	-- all pieces rotate around their topleft initially
	for i, piece in ipairs(self.mPieces) do
		piece:relocatePivotPoint("topleft")	
	end
	
	-- now call the base onInitialize,
	-- so our newly created IOs will still be handled	
	Layout.onInitialize(self)

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onShow()

	Layout.onShow(self)
	
	-- if find is used, we now transfer find pieces from inventory
	-- to puzzle
	if (self.mPiecesFind) then
    	for i, piece in ipairs(self.mPiecesFind) do
	        if (piece:isFound()) then
    	        piece:ss(kUsed)
        	    self.mPieces[i]:ss(kActive)
        	end
    	end	
    end

	-- reset rotate button    
	if (self.mRotate) then
		self.mRotate.enabled = false
	end	    

	-- reset any drag    
	self.mDragging = false
	self.mDraggingGroup = nil
	self.mDraggingPieces = nil
	self.mDragX = nil
	self.mDragY = nil   
	
	self:updateOrder() 

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onTick(time)

	Layout.onTick(self, time)

	if (self:isPuzzling()) then
		if (self.mDragging) then
			assert(self.mDragX)
			assert(self.mDragY)
			local mouseX = cursor:getX()
			local mouseY = cursor:getY()
			local deltaX = mouseX - self.mDragX
			local deltaY = mouseY - self.mDragY
	
			assert(self.mDraggingPieces)
			for i, piece in ipairs(self.mDraggingPieces) do
				local pieceX = piece:x()
				local pieceY = piece:y()
				piece:setX(pieceX + deltaX)
				piece:setY(pieceY + deltaY)
			end	
			
			self.mDragX = mouseX
			self.mDragY = mouseY			
		end
	end	

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onMouseDown(x, y)

	if (self:isPuzzling()) then
		if (self.mDragging) then
			assert(self.mDraggingGroup)
			assert(self.mDraggingPieces)
			
			local snapped = false
			local snappedTo = nil
		
			-- find a non placed piece to snap to
			if (not snapped) then
				for i, draggedPiece in ipairs(self.mDraggingPieces) do
					local draggedPieceX = draggedPiece:globalX(0, 0)
					local draggedPieceY = draggedPiece:globalY(0, 0)
					local draggedPieceR = draggedPiece:rotation()
					assert(draggedPieceR >= 0)
					assert(draggedPieceR <= 2 * math.pi)
					
					for j, piece in ipairs(self.mPieces) do
						
						local check = true
						
						-- don't check against self
						if (piece == draggedPiece) then
							check = false
						end
						
						-- don't check against grouped pieces
						if (piece.groupIndex == self.mDraggingGroup) then
							check = false
						end
						
						-- don't check against inactive pieces
						if (not piece:isActive()) then
							check = false
						end
						
						-- don't snap if it's not a neighbor
						
						if (check) then
							local pieceX = piece:globalX(0, 0)
							local pieceY = piece:globalY(0, 0)
							local pieceR = piece:rotation()
							assert(pieceR >= 0)
							assert(pieceR <= 2 * math.pi)				
						
							if (pieceR == draggedPieceR) then
								-- rotation allows a snap
								-- this means they are both in the same orientation
								-- now check distance
								
								local orgDistance = math.distance(piece.solveX, piece.solveY, draggedPiece.solveX, draggedPiece.solveY)
								local curDistance = math.distance(pieceX, pieceY, draggedPieceX, draggedPieceY)
								local deltaDistance = math.abs(curDistance - orgDistance)
								if (deltaDistance < self.mSnapDistance) then
									-- distance allows a snap
									-- this means they are not that far of each other
									-- now check angle
									
									local orgAngle = self:calcAngle(draggedPiece.solveX, draggedPiece.solveY, piece.solveX, piece.solveY)
									local curAngle = self:calcAngle(draggedPieceX, draggedPieceY, pieceX, pieceY)
									
									-- compensate for rotation
									curAngle = curAngle + pieceR
									while (curAngle >= 2 * math.pi) do curAngle = curAngle - (2 * math.pi) end
									while (curAngle < 0) do curAngle = curAngle + (2 * math.pi) end
									
									local deltaAngle = math.abs(curAngle - orgAngle)
									if (deltaAngle < self.mSnapAngle) then
										-- compare to a radian
										-- angle allows a snap
										-- this means they are in the same relative orientation to each other
										snapped = true
										snappedTo = piece
										break
									end						
								end					
							end
						end			
					end		
				end
			end
			
			-- do the snap
			if (snapped and snappedTo) then
				-- cursor
				cursor:green()
				
				-- sound
				self:playConnectSound()
			
				-- first group the dragged piece with the piece we snapped to			
				for i, draggedPiece in ipairs(self.mDraggingPieces) do
					assert(snappedTo.groupIndex)
					draggedPiece.groupIndex = snappedTo.groupIndex
				end
				
				-- then snap the dragged piece
				for i, draggedPiece in ipairs(self.mDraggingPieces) do
					assert(snappedTo.solveX)
					assert(snappedTo.solveY)
					local orgDeltaX = draggedPiece.solveX - snappedTo.solveX
					local orgDeltaY = draggedPiece.solveY - snappedTo.solveY
					local curDeltaX = nil
					local curDeltaY = nil
					
					if (math.abs(draggedPiece:rotation() - 0) < SMALLNUMBER) then
						curDeltaX = orgDeltaX
						curDeltaY = orgDeltaY				
					
					elseif (math.abs(draggedPiece:rotation() - (math.pi / 2)) < SMALLNUMBER) then
						curDeltaX = -1 * orgDeltaY
						curDeltaY = orgDeltaX			
					
					elseif (math.abs(draggedPiece:rotation() - math.pi) < SMALLNUMBER) then
						curDeltaX = -1 * orgDeltaX
						curDeltaY = -1 * orgDeltaY
					
					elseif (math.abs(draggedPiece:rotation() - (math.pi * 1.5)) < SMALLNUMBER) then
						curDeltaX = orgDeltaY
						curDeltaY = -1 * orgDeltaX			
					
					else
						curDeltaX = 0
						curDeltaY = 0
						print(draggedPiece:rotation())
						assert(nil, "invalid rotation for " .. draggedPiece:name())
						
					end
					
                    local draggedPieceX = draggedPiece:x()
                    local draggedPieceY = draggedPiece:y()
                    local snappedPieceX = snappedTo:x()			
                    local snappedPieceY = snappedTo:y()     
                    local globalSnappedX = snappedTo:globalX(0, 0)
                    local globalSnappedY = snappedTo:globalY(0, 0)
                    local selfX = self:x()
                    local selfY = self:y()
                    local newX = globalSnappedX + curDeltaX - selfX
                    local newY = globalSnappedY + curDeltaY - selfY
					
					draggedPiece:setX(newX)
					draggedPiece:setY(newY)
				end
			end
			
			-- find a target position to snap to
			if (not snapped) then
				-- only need to check 1 piece in the group
				-- if that's allowed to snap, they all are
				draggedPiece = self.mDraggingPieces[1]
				assert(draggedPiece.solveX)
				assert(draggedPiece.solveY)
										
				local draggedPieceX = draggedPiece:x()
				local draggedPieceY = draggedPiece:y()
				local draggedPieceR = draggedPiece:rotation()
				assert(draggedPieceR >= 0)
				assert(draggedPieceR <= 2 * math.pi)
				
				if (math.abs(draggedPieceR - 0) < SMALLNUMBER) then
					-- rotation allows a snap
					-- pieces cannot be rotated for them to be snapped to their
					-- target position.
					-- now check distance
					
					local distance = math.distance(draggedPieceX, draggedPieceY, draggedPiece.solveX, draggedPiece.solveY)
					if (distance < self.mSnapDistance) then
						-- distance allows a snap
						snapped = true
					end
				end
				
				if (snapped) then
					-- cursor
					cursor:green()
				
					-- sound
					self:playPlacedSound()
				
					-- place all pieces
					for i, draggedPiece in ipairs(self.mDraggingPieces) do				
						assert(draggedPiece.solveX)
						assert(draggedPiece.solveY)
						draggedPiece:setX(draggedPiece.solveX)
						draggedPiece:setY(draggedPiece.solveY)
						draggedPiece:setRotation(0)
						draggedPiece:ss(kIdle)
						
						if (self.mPiecesCorrect) then
							-- correct is used
							-- this means that when the piece is placed, it becomes inactive
							-- and we show the correct piece instead
							local pieceIndex = draggedPiece.pieceIndex
							self:onPuzzlePiecePlaced(pieceIndex)
						end
						
						if (self.mPiecesMirror) then
							-- mirror is used
							-- this means that when the piece is placed, the mirror becomes
							-- active
							local pieceIndex = draggedPiece.pieceIndex
							
							self.mPiecesMirror[pieceIndex]:ss(kActive)						
						end
					end
					
					-- check if we should call solve
					local solve = true
					for i, piece in ipairs(self.mPieces) do
						if (not (piece:isIdle() or piece:isDone())) then
							solve = false						
						end					
					end
					
					if (solve) then
						self:solve()
					end
				end
			end
			
			if (not snapped) then
				cursor:red()
			end
			
			-- reset rotate
			if (self.mRotate) then
				self.mRotate.enabled = false
			end
			
			-- reset
			self.mDragging = false
			self.mDraggingGroup = nil
			self.mDraggingPieces = nil
			self.mDragX = nil
			self.mDragY = nil
			
			self:updateOrder()
			
			return true
		
		else
			-- check if we should start dragging
			assert(self.mDraggingGroup == nil)
			assert(self.mDraggingPieces == nil)
			
			local previousPiece = nil
			
			for i, piece in ipairs(self.mPieces) do
				if (piece:isActive()) then
					if (piece:isInsideStrict(x, y)) then
						if (previousPiece == nil or isSiblingAfter(previousPiece, piece)) then
							self.mDragging = true
							self.mDraggingGroup = piece.groupIndex
							previousPiece = piece
						end
					end			
				end		
			end
			
			if (self.mDragging) then
				-- set rotate
				if (self.mRotate) then
					self.mRotate.enabled = true
				end
			
				self.mDraggingPieces = {}
				for i, piece in ipairs(self.mPieces) do
					if (piece:isActive()) then
						if (piece.groupIndex == self.mDraggingGroup) then
							table.insert(self.mDraggingPieces, piece)
							
							-- fix rotation
							local rotation = self:calcValidRotation(piece:rotation())
							assert(rotation >= 0)
							assert(rotation <  2*math.pi)
							piece:setRotation(rotation)
						end				
					end			
				end
				
				-- update z order of dragging items
				for i, piece in ipairs(self.mDraggingPieces) do
					piece:moveBehind(self.mRotate)
				end
				
				self.mDragX = x
				self.mDragY = y
				
				assert(self.mDragging)
				assert(self.mDraggingGroup)
				assert(self.mDraggingPieces)
				assert(self.mDragX)
				assert(self.mDragY)
				
				self:updateOrder()
				
				return true
			end
		end
	end
	
	self:updateOrder()
	
	return Layout.onMouseDown(self, x, y)

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onMouseRightDown(x, y)

	if (self.mDragging) then
		playAudioTrack("BUTTON_CLICK_FX")
		self:rotateDraggedPieces()
		return true
	end
	
	return Layout.onMouseRightDown(self, x, y)

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onSolve()

	Layout.onSolve(self)
	
	if (self.mPiecesFind) then
		for i, piece in ipairs(self.mPiecesFind) do
			piece:ss(kUsed)		
		end	
	end
	
	if (self.mPiecesCorrect) then
		for i, piece in ipairs(self.mPiecesCorrect) do
			piece:ss(kIdle)
		end
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kDone)
		end
	end
	
	if (not self.mPiecesCorrect) then
		for i, piece in ipairs(self.mPieces) do
			piece:ss(kIdle)
			piece:setX(piece.solveX)
			piece:setY(piece.solveY)
			piece:setRotation(0)
		end
	end
	
	if (self.mRotate) then
		self.mRotate:ss(kIdle)
	end

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onQuickPlay()

	if (self.mPiecesFind) then
		for i, piece in ipairs(self.mPiecesFind) do
			piece:ss(kUsed)		
		end	
	end
	
	if (self.mPiecesCorrect) then
		for i, piece in ipairs(self.mPiecesCorrect) do
			piece:ss(kInactive)		
		end		
	end	

	for i, piece in ipairs(self.mPieces) do
		piece:ss(kActive)		
	end		
	
	-- enable rotate button
	if (self.mRotate) then
		self.mRotate:ss(kActive)
	end

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onClickButton(button)

	if (button == self.mRotate) then
		self:rotateDraggedPieces()
		return true
	end
	
	return Layout.onClickButton(self, button)

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:rotateDraggedPieces()

	self:calcRotatedPositions(0.5 * math.pi)

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:calcRotatedPositions(rad)

	if (self.mDraggingPieces) then
		pivotX = cursor:getX()
		pivotY = cursor:getY()
		
		for i, draggedPiece in ipairs(self.mDraggingPieces) do
			local localPivotX = draggedPiece:localX(pivotX, pivotY)
			local localPivotY = draggedPiece:localY(pivotX, pivotY)
			
			draggedPiece:relocatePivotPoint(localPivotX, localPivotY)
		end
		
		for i, draggedPiece in ipairs(self.mDraggingPieces) do
			local curRotation = draggedPiece:rotation()
			local newRotation = curRotation + rad

			draggedPiece:animate("RotatePiece" .. i .."_blocking",
				{rotation = math.deg(newRotation), duration = 50}
			)
		end
	end
	
end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onAnimationFinish(animationName)

	Layout.onAnimationFinish(self, animationName)

	if (self:isTop()) then
		if (self.mDraggingPieces) then
			for i, draggedPiece in ipairs(self.mDraggingPieces) do
				if (animationName == "RotatePiece" .. i .. "_blocking") then		
					-- set rotation in range [0..2pi]
					local rotation = draggedPiece:rotation()
					-- fix rotation
					rotation = self:calcValidRotation(rotation)
					assert(rotation >= 0)
					assert(rotation <  2*math.pi)
					draggedPiece:setRotation(rotation)
							
					-- relocate pivot points		
					draggedPiece:relocatePivotPoint("topleft")
					
					-- only one
					break
				end
			end	
		end
	end

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onKeyDown(key)

	if (key == vkSpace) then
		if (self.mDragging) then
			playAudioTrack("BUTTON_CLICK_FX")	
			self:rotateDraggedPieces()
			return true
		end
	end
	
	return Layout.onKeyDown(self, key)

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:onPuzzlePiecePlaced(pieceIndex)
	
	self.mPieces[pieceIndex]:ss(kDone)
	self.mPiecesCorrect[pieceIndex]:ss(kIdle)
	
end

-- ----------------------------------------------------------------------------
function LayoutJigsaw:playConnectSound()
	playAudioTrack("PLACE_OBJECT")
end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:playPlacedSound()
	playAudioTrack("PLACE_OBJECT")
end

-- ----------------------------------------------------------------------------
function LayoutJigsaw:onUsedAll(object, props)

	Layout.onUsedAll(self, object, props)

	if (object == self.mObjectTransfer) then
		-- dropped the group
		-- activate all pieces
		for i, piece in ipairs(self.mPieces) do
			if (piece:isInactive()) then
				piece:ss(kActive)
				piece:setAlpha(0)
				piece:animate("PlacePiece" .. i .. "_blocking",
					{ alpha=0						 },
					{ alpha=1,	duration=kTimeFadeIn }, 
					{ delay=kTimeDelay				 }
				)
			end
		end	
		-- enable rotate button
		if (self.mRotate) then
			self.mRotate:ss(kActive)
		end				
	end

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:calcAngle(x1, y1, x2, y2)

    -- returns an angle between two points in radians.
    -- angle is as follows:
    --
    --            y
    --               
    --        1.5 * PI
    --            |
    --            |
    --            |
    -- PI --------x-------- 0 x
    --            |
    --            |
    --            |
    --        0.5 * PI
    --
    local dx = x2 - x1
    local dy = y2 - y1

    local r = math.atan2(dy, dx)
    if (r < 0) then
		r = math.abs(r)
    elseif (r > 0) then
        r = (2*math.pi) - r
    end
    
	assert(r >= 0.0)
    assert(r <= 2*math.pi)

	return r

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:calcValidRotation(rad)

	local rotdeg = math.deg(rad)
	local valid = {0, 90, 180, 270, 360}
	local smallestDiff = math.huge
	local smallestAngle = math.huge
	for i, r in ipairs(valid) do
	  local diff = math.abs(r - rotdeg)
	  if (diff < smallestDiff) then
	    smallestDiff = diff
	    smallestAngle = r
	  end
	end
	if (smallestAngle == 360) then
		smallestAngle = 0
	end
	return math.rad(smallestAngle)	

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:closeOnClick(x, y)

	if (Layout.closeOnClick(self, x, y)) then
		-- don't close when we're dragging
		if (self.mDragging) then
			return false
		end
		
		-- don't close when we're trying to pick up an item
		-- outside the poi
		local object = self:calcObjectBelowMouse(x, y)
		if (object) then
			return false
		end
		
		return true
	end
	
	return false

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:calcCursorProperties()

	Layout.calcCursorProperties(self)
	
	local showSelectedCursor = false
	
	if (self.mDragging) then
		if (self.mRotate) then
			if (not self.mRotate:isInside(cursor:getX(), cursor:getY())) then
				showSelectedCursor = true
			end
		else
			showSelectedCursor = true
		end
	end
	
	if (showSelectedCursor) then
		-- show selected cursor when dragging
		-- even when a blocking rotate animation is busy
		cursor:setCursor(kCursorSelected)		
	end

end
-- ----------------------------------------------------------------------------
function LayoutJigsaw:updateOrder()
	for i, piece in ipairs(self.mPieces) do
		if (piece:isIdle()) then
			piece:moveInFrontOf(self.mObjectTransfer)
		end
	end
end