layoutCryptSecretDoor.timeBeforeAttack = {}
layoutCryptSecretDoor.timeBeforeDent1 = 2000
layoutCryptSecretDoor.timeBeforeDent2 = 1800
layoutCryptSecretDoor.timeBeforeDent3 = 1700
layoutCryptSecretDoor.timeBeforeDoor1 = 1600
layoutCryptSecretDoor.timeLook1 = 100
layoutCryptSecretDoor.timeBeforeAttack[1] = 1500
layoutCryptSecretDoor.timeBeforeAttack[2] = 1400
layoutCryptSecretDoor.timeBeforeAttack[3] = 1300
layoutCryptSecretDoor.timeBeforeAttack[4] = 1200
layoutCryptSecretDoor.timeBeforeDent4 = 900
layoutCryptSecretDoor.timeBeforeDent5 = 800
layoutCryptSecretDoor.timeBeforeDent6 = 700
layoutCryptSecretDoor.timeBeforeDoor2 = 600
layoutCryptSecretDoor.timeLook2 = 100
layoutCryptSecretDoor.timeBeforeAttack[5] = 500
layoutCryptSecretDoor.timeBeforeAttack[6] = 400
layoutCryptSecretDoor.timeBeforeAttack[7] = 300
layoutCryptSecretDoor.timeBeforeAttack[8] = 200
layoutCryptSecretDoor.timeBeforeAttack[9] = 100

layoutCryptSecretDoor.timeBeforeAttack1 = 1000
layoutCryptSecretDoor.timeBeforeAttack2 = 500
layoutCryptSecretDoor.timeBeforeMovieStart = 100

layoutCryptSecretDoor.originalXHookmanEyes = 0
layoutCryptSecretDoor.originalYHookmanEyes = 0
layoutCryptSecretDoor.maxDistXHookmanEyes = 5
layoutCryptSecretDoor.maxDistYHookmanEyes = 5
layoutCryptSecretDoor.timeEyesMovement = 200

layoutCryptSecretDoor.attacks = {}
layoutCryptSecretDoor.attacks[1] = objectIoDoorOpen01Hook01
layoutCryptSecretDoor.attacks[2] = objectIoDoorOpen01Hook02
layoutCryptSecretDoor.attacks[3] = objectIoDoorOpen01Hook03
layoutCryptSecretDoor.attacks[4] = objectIoDoorOpen01Hook04
layoutCryptSecretDoor.attacks[5] = objectIoDoorOpen02Hook01
layoutCryptSecretDoor.attacks[6] = objectIoDoorOpen02Hook02
layoutCryptSecretDoor.attacks[7] = objectIoDoorOpen02Hook03
layoutCryptSecretDoor.attacks[8] = objectIoDoorOpen02Hook04
layoutCryptSecretDoor.attacks[9] = objectIoDoorOpen02Hook05

layoutCryptSecretDoor.puzzleBusy = false
layoutCryptSecretDoor.timeIdle = 0

layoutCryptSecretDoor.timeBeforeAutoRemove = 5000
layoutCryptSecretDoor.timerWaitingForClick = 0
layoutCryptSecretDoor.currentHookHand = nil
layoutCryptSecretDoor.ignoredHookHandsCount = 0
layoutCryptSecretDoor.playPushDoorFirst = true
-- ----------------------------------------------------------------------------
layoutCryptSecretDoor.mExits =
{
}

layoutCryptSecretDoor.mPOIs = 
{
	[objectPoiPeephole] = 
	{
		target = layoutCryptSecretDoorInside,
	}
}

layoutCryptSecretDoor.mHOs = 
{
}

layoutCryptSecretDoor.mIOs =
{
	[objectIoDoorOpen01Hook01] = 
	{
		cursors = kCursorSelected,
		mirrors = {	objectIoDoorOpen01Hook02, 
					objectIoDoorOpen01Hook03,
					objectIoDoorOpen01Hook04,
					objectIoDoorOpen02Hook01, 
					objectIoDoorOpen02Hook02, 
					objectIoDoorOpen02Hook03, 
					objectIoDoorOpen02Hook04, 
					objectIoDoorOpen02Hook05
				  },
	}
}
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:onConstruct()
	
	Layout.onConstruct(self)
	objectDoorClosed:ss(kInactive)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:onInitialize()

	Layout.onInitialize(self)
	
	objectDoorClosedDent01:ss(kInactive)
	objectDoorClosedDent02:ss(kInactive)
	objectDoorClosedDent03:ss(kInactive)
	objectDoorOpen01:ss(kInactive)
	objectDoorOpen02:ss(kInactive)
	objectDoorOpenDent01:ss(kInactive)
	objectDoorOpenDent02:ss(kInactive)
	objectDoorOpenDent03:ss(kInactive)
	objectDoorOpening:ss(kInactive)
	objectHookmanPeepholeEyes:ss(kInactive)
	objectIoDoorOpen01Hook01:ss(kInactive)
	objectIoDoorOpen01Hook02:ss(kInactive)
	objectIoDoorOpen01Hook03:ss(kInactive)
	objectIoDoorOpen01Hook04:ss(kInactive)
	objectIoDoorOpen02Hook01:ss(kInactive)
	objectIoDoorOpen02Hook02:ss(kInactive)
	objectIoDoorOpen02Hook03:ss(kInactive)
	objectIoDoorOpen02Hook04:ss(kInactive)
	objectIoDoorOpen02Hook05:ss(kInactive)
	objectPoiPeephole:ss(kActive)
	
	self.originalXHookmanEyes = objectHookmanPeepholeEyes:x()
	self.originalYHookmanEyes = objectHookmanPeepholeEyes:y()
	
	self.timerWaitingForClick = 0
	self.currentHookHand = nil
	self.ignoredHookHandsCount = 0

end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:onShow()

	Layout.onShow(self)
	
	self:child("BUTTON_LAYOUT_CLOSE"):setEnabled(not self.puzzleBusy)
	self:child("BUTTON_LAYOUT_CLOSE"):setVisible(not self.puzzleBusy)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:onQuickPlay()

    Layout.onQuickPlay(self)
    
    self:startActionPuzzle()

end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:startActionPuzzle()
	layoutCrypt.shouldHearBanging = false-- Stop the banging sound in the crypt
	
	objectPoiPeephole:ss(kInactive)
	objectDoorClosed:ss(kIdle)
	objectDoorOpening:ss(kIdle)
	objectHookmanPeepholeEyes:ss(kIdle)
	
	self:onAnimationFinish("hookmanEyes")
	self.puzzleBusy = true
	
	self:child("BUTTON_LAYOUT_CLOSE"):setEnabled(false)
	self:child("BUTTON_LAYOUT_CLOSE"):setVisible(false)	
	
	objectDoorClosed:animate("actionDent1_blocking",
		{ delay=self.timeBeforeDent1 },
		{ command="playTrack track=DOORBANG_1", target=self }
	)
	
	self:shakeDoorBreaking()	
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:onAnimationFinish(animationName)

	Layout.onAnimationFinish(self, animationName)
	
	if (animationName == "actionDent1_blocking") then
--		objectHookmanPeepholeEyes:ss(kInactive)
	
		objectDoorClosedDent01:ss(kIdle)
		objectDoorClosed:ss(kDone)
		
		objectDoorClosedDent01:animate("actionDent2_blocking",
			{ delay=self.timeBeforeDent2 },
			{ command="playTrack track=DOORBANG_2", target=self }
		)
		
	elseif (animationName == "actionDent2_blocking") then
		objectDoorClosedDent02:ss(kIdle)
		objectDoorClosedDent01:ss(kDone)
		
		objectDoorClosedDent02:animate("actionDent3_blocking",
			{ delay=self.timeBeforeDent3 },
			{ command="playTrack track=DOORBANG_3", target=self }
		)
	
	elseif (animationName == "actionDent3_blocking") then
		objectDoorClosedDent03:ss(kIdle)
		objectDoorClosedDent02:ss(kDone)
		
		objectDoorClosedDent03:animate("actionDoor1_blocking",
			{ delay=self.timeBeforeDoor1 },
			{ command="playTrack track=DOORBANG_1", target=self }
		)
	
	elseif (animationName == "actionDoor1_blocking") then
		objectHookmanPeepholeEyes:ss(kIdle)

		objectDoorOpen01:ss(kIdle)
		objectDoorClosedDent03:ss(kDone)
		
		objectDoorOpen01:animate("actionLook1_blocking",
			{ delay=self.timeLook1 }
		)		
		
	elseif (animationName == "actionLook1_blocking") then
	
		objectDoorOpen01:animate("attack1_blocking",
			{ delay=self.timeBeforeAttack1 }
		)	
		
	elseif (animationName == "actionDent4_blocking") then
		objectDoorOpenDent01:ss(kIdle)
		objectDoorOpen01:ss(kDone)
		objectHookmanPeepholeEyes:animate("appear_eyes_blocking",
			{ alpha=1, duration = 100 }
		)
		
		objectDoorOpenDent01:animate("actionDent5_blocking",
			{ delay=self.timeBeforeDent5 },
			{ command="playTrack track=DOORBANG_2", target=self }
		)
		
	elseif (animationName == "actionDent5_blocking") then
		objectDoorOpenDent02:ss(kIdle)
		objectDoorOpenDent01:ss(kInactive)
		
		objectDoorOpenDent02:animate("actionDent6_blocking",
			{ delay=self.timeBeforeDent6 },
			{ command="playTrack track=DOORBANG_3", target=self }
		)
	
	elseif (animationName == "actionDent6_blocking") then
		objectDoorOpenDent03:ss(kIdle)
		objectDoorOpenDent02:ss(kInactive)
		
		objectDoorOpenDent03:animate("actionDoor2_blocking",
			{ delay=self.timeBeforeDoor2 },
			{ command="playTrack track=DOORBANG_1", target=self }
		)		
		
	elseif (animationName == "actionDoor2_blocking") then
		objectHookmanPeepholeEyes:ss(kIdle)
	
		objectDoorOpen02:ss(kIdle)
		objectDoorOpenDent03:ss(kInactive)
		
		objectDoorOpen02:animate("actionLook2_blocking",
			{ delay=self.timeLook2 }
		)		
		
	elseif (animationName == "actionLook2_blocking") then
	
		objectDoorOpen02:animate("attack2_blocking",
			{ delay=self.timeBeforeAttack2 }
		)				
		
	elseif (animationName == "attack1_blocking") then
		objectHookmanPeepholeEyes:animate("remove_eyes_blocking",
			{ alpha=0, duration = 100 }
		)
		objectIoDoorOpen01Hook01:ss(kActive)
		self:registerHookPlaced(objectIoDoorOpen01Hook01)
		
		-- HELP Unwanted guests
		queueHelpDialog(31, 148, 55)
		queueHelpArrow(32, 31, 790, 370)		
		
	elseif (animationName == "attack2_blocking") then
		objectHookmanPeepholeEyes:animate("remove_eyes_blocking",
			{ alpha=0, duration = 100 }
		)
		print("\n LALALALALALA \n")
		objectIoDoorOpen02Hook01:ss(kActive)	
		self:registerHookPlaced(objectIoDoorOpen02Hook01)	

	elseif (animationName == "hookmanEyes") then
		
		local targetX = self.originalXHookmanEyes + (math.random(-self.maxDistXHookmanEyes, self.maxDistXHookmanEyes)) 
		local targetY = self.originalYHookmanEyes + (math.random(-self.maxDistYHookmanEyes, self.maxDistYHookmanEyes)) 
		
		objectHookmanPeepholeEyes:animate("hookmanEyes", 
			{ x = targetX, y = targetY, duration = self.timeEyesMovement }
		)
		
	elseif (animationName == "newAttack_blocking") then

		self:shakeDoorStop()

		for i = #(self.attacks), 1, -1 do
			if (self.attacks[i]:isDone()) then
				if (i == #(self.attacks)) then	-- Action puzzle finished!
					self.attacks[i]:animate("actionPuzzleFinished_blocking", 
						{ delay=self.timeBeforeMovieStart }
					)
					break
				end
				
				if (i == 4) then
					if (self.ignoredHookHandsCount > 3) then
						-- All hands / hooks were automatically removed; start end movie now:
						self.attacks[i]:animate("actionPuzzleFinished_blocking", 
							{ delay=1 }
						)
					else
						-- start next batch of dents
						objectDoorClosed:animate("actionDent4_blocking",
							{ delay=self.timeBeforeDent4 },
							{ command="playTrack track=DOORBANG_1", target=self }
						)
						self.timerWaitingForClick = 0
					end
				else
					-- another hook
					self.attacks[i + 1]:ss(kActive)
					self:registerHookPlaced(self.attacks[i + 1])
				end
			
				break
			end
			
		end

	elseif(animationName == "actionPuzzleFinished_blocking") then
		self.puzzleBusy = false	
	end
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:onClickIO(object, x, y)

	for i = 1, #self.attacks do
		if (object == self.attacks[i]) then	
			if (i == 1) then
				self.timeIdle = 7500
			else
				self.timeIdle = 0
			end
			self:withstandAttack(self.attacks[i], i)
			return true
		end
	end
	
	return Layout.onClickIO(self, object, x, y)

end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:withstandAttack(object, index)
	
	playAudioTrack("HITHOOK")
	self:shakeDoorBreaking()
	object:ss(kDone)
	object:animate("newAttack_blocking",
		{ delay=self.timeBeforeAttack[index] }
	)
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:closeOnClick(x, y) 

	if (Layout.closeOnClick(self, x, y)) then
		return not self.puzzleBusy
	end

end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:getCurrentDoor()

	if (objectDoorOpen02:isIdle()) then
		return objectDoorOpen02
		
	elseif (objectDoorOpen01:isIdle()) then
		return objectDoorOpen01
	
	elseif (objectDoorClosed:isIdle()) then
		return objectDoorClosed
	end
	
	return nil
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:shakeDoorBanging()

	ghf.cancelAnimation("animShakeDoorBanging")
	
	print(" -- layoutCryptSecretDoor:shakeDoorBanging() \n ")

	local door = self:getCurrentDoor()
	
	if (door) then
		door:animate("animShakeDoorBanging",
			{ startframe=0, endframe=1,	frametime=75 			}
		)
	end
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:shakeDoorBreaking()

	ghf.cancelAnimation("animShakeDoorBreaking")
	
	print(" -- layoutCryptSecretDoor:shakeDoorBreaking() \n ")

	local door = self:getCurrentDoor()
	
	if (door) then
		
		local track = "PUSHDOOR_1"
		
		if (self.playPushDoorFirst) then
			self.playPushDoorFirst = false
		else
			self.playPushDoorFirst = true
			track = "PUSHDOOR_2"
		end
		
		local command = "playTrack track=" .. track
			
		door:animate("animShakeDoorBreaking",
			{ command=command, target=self 						},
			{ startframe=0, endframe=1,	frametime=75 			}
		)

	end
	
end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:shakeDoorStop()

	ghf.cancelAnimation("animShakeDoorBanging")
	ghf.cancelAnimation("animShakeDoorBreaking")

end
-- ----------------------------------------------------------------------------
function layoutCryptSecretDoor:registerHookPlaced(hookHand)

	playAudioTrack("HITHOOK")
	self.currentHookHand = hookHand
	self.timerWaitingForClick = self.timeBeforeAutoRemove

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

	Layout.onTick(self, time)

	if (self.timeIdle > 0) then
		self.timeIdle = self.timeIdle - time
		if (self.timeIdle <= 0) then
			-- auto solve the puzzle
			self:animate("actionPuzzleFinished_blocking",
				{ delay=1 }
			)
		end	
	end
	
	if (self.timerWaitingForClick > 0) then
		self.timerWaitingForClick = self.timerWaitingForClick - time
		if (self.timerWaitingForClick <= 0) then
			-- automatically remove hook / hand:
			print("---> Autoclick <------------ \n ")
			if (self.currentHookHand ~= nil) then
				self:onClickIO(self.currentHookHand, 0, 0)
				self.currentHookHand = nil
				self.ignoredHookHandsCount = self.ignoredHookHandsCount + 1
			else
				print("----> layoutCryptSecretDoor:onTick: Tried to remove hand / hook, but there wasn't any! \n")
			end
		end	
	end

end
