-- ----------------------------------------------------------------------------
layoutBoathouseEnigma.mExits =
{
}
layoutBoathouseEnigma.mPOIs = 
{
	[objectPoiCardJigsaw] = 
	{
		target = layoutBoathouseCardJigsaw
	}
}
layoutBoathouseEnigma.mHOs = 
{
}
layoutBoathouseEnigma.mIOs =
{
	[objectIoEnigmaHandleArea] = 
	{
		links = {objectHoEnigmaHandle},
		cursor = kCursorDefault,
	},
	[objectIoEnigmaEmptyPaperrollHolder] = 
	{
		links = {objectHoPaperroll},
		cursor = kCursorDefault,
	},
	[objectIoEnigmaButtonArea] =
	{
		links = {objectHoButton2},
		cursor = kCursorDefault,
	},
	[objectIoStillwaterPaper1] =
	{
		cursor = kCursorDefault,
	},
	[objectIoStillwaterPaper2] =
	{
		cursor = kCursorDefault,
	},
	[objectIoStillwaterPaperWrong] =
	{
		cursor = kCursorDefault,
	},
	[objectIoStillwaterPaper1Close] =
	{
		cursor = kCursorDefault,
	},
	[objectIoStillwaterPaper2Close] =
	{
		cursor = kCursorDefault,
	},
	[objectIoStillwaterPaperWrongClose] =
	{
		cursor = kCursorDefault,
	},
	[objectIoCardOpening] =
	{
		links = {objectHoCardMachineCard},
		cursor = kCursorDefault,
	},
}
layoutBoathouseEnigma.mButtons =
{
	[objectEnigmaButton1] =
	{
	},
	[objectEnigmaButton2] =
	{
	},
	[objectEnigmaHandle] =
	{
	},
}
layoutBoathouseEnigma.mPapers =
{
	objectIoStillwaterPaper1,
	objectIoStillwaterPaper2,
	objectIoStillwaterPaperWrong,	
}
layoutBoathouseEnigma.mPapersCloseUp =
{
	objectIoStillwaterPaper1Close,
	objectIoStillwaterPaper2Close,
	objectIoStillwaterPaperWrongClose,	
}
layoutBoathouseEnigma.mCardInserted 		= nil

layoutBoathouseEnigma.kAnimCardJigsawDelay 	= 1900
layoutBoathouseEnigma.mTimerPaperStart = 7000
layoutBoathouseEnigma.mTimerPaper1 = layoutBoathouseEnigma.mTimerPaperStart
layoutBoathouseEnigma.mTimerPaper2 = layoutBoathouseEnigma.mTimerPaperStart
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onConstruct()

	Layout.onConstruct(self)
	
	objectIoStillwaterPaper1:ss(kInactive)
	objectIoStillwaterPaper1Close:ss(kInactive)
	objectIoStillwaterPaper2:ss(kInactive)
	objectIoStillwaterPaper2Close:ss(kInactive)
	
	objectIoStillwaterPaperWrongClose:ss(kInactive)
	objectCardIn:ss(kInactive)
	objectEnigmaButton2:ss(kInactive)
	objectEnigmaHandle:ss(kInactive)
	objectEnigmaHandleShadow:ss(kInactive)
	objectEnigmaHandleBack:ss(kInactive)
	objectSliderPaperroll:ss(kInactive)
	objectFilledPaperrollHolder:ss(kInactive)
	objectGreenLightButton1:ss(kInactive)
	objectGreenLightButton2:ss(kInactive)
	objectCardInsert:ss(kInactive)
	objectCardInsertCastShadow:ss(kInactive)

	objectEnigmaButton1:ss(kActive)
	
	objectIoStillwaterPaperWrong:ss(kActive)
	objectIoEnigmaHandleArea:ss(kIdle)
	objectIoEnigmaEmptyPaperrollHolder:ss(kIdle)
	objectIoEnigmaButtonArea:ss(kIdle)
	
	objectPoiCardJigsaw:ss(kInactive)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onInitialize()

	Layout.onInitialize(self)
	
	objectEnigmaHandle:relocatePivotPoint(47, 47.5)
	objectEnigmaHandleBack:relocatePivotPoint(52, 52)
	objectEnigmaHandleShadow:relocatePivotPoint(47, 47.5)

	objectIoStillwaterPaper1:setClipRect(130, 152, 370, 300)
	objectIoStillwaterPaper2:setClipRect(130, 152, 370, 300)
	
	objectCardInsert:relocateAnchorPoint("top")
	objectCardInsertCastShadow:relocateAnchorPoint("top")
	
	objectCardInsert:setClipRect(205, 92, 225, 50)
	objectCardInsertCastShadow:setClipRect(205, 92, 225, 50)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onShow()

	Layout.onShow(self)
	
	self:closeCloseUp()

end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onHide()

	self:closeCloseUp()
	
	Layout.onHide(self)	
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onTick(time)

	Layout.onTick(self, time)
	
	-- show christine thought for paper1
	if (objectIoStillwaterPaper1Close:isActive()) then
		if (self.mTimerPaper1 > 0) then
			self.mTimerPaper1 = self.mTimerPaper1 - time
			if (self.mTimerPaper1 <= 0) then
				think("MESSAGE_MEMO_ENIGMA_JIGSAW")	
			end
		end
	else
		if (self.mTimerPaper1 > 0) then	
			if (self.mTimerPaper1 ~= 5000) then
				think("MESSAGE_MEMO_ENIGMA_JIGSAW")
				self.mTimerPaper1 = 0
			end		
		end
	end
	
	-- show christine thought for paper2
	if (objectIoStillwaterPaper2Close:isActive()) then
		if (self.mTimerPaper2 > 0) then
			self.mTimerPaper2 = self.mTimerPaper2 - time
			if (self.mTimerPaper2 <= 0) then
				think("MESSAGE_MEMO_ENIGMA_CARD")
			end
		end
	else
		if (self.mTimerPaper2 > 0) then	
			if (self.mTimerPaper2 ~= 5000) then
				think("MESSAGE_MEMO_ENIGMA_CARD")
				self.mTimerPaper2 = 0
			end		
		end
	end
	

end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onQuickPlay()

	objectIoEnigmaHandleArea:ss(kActive)
	--objectIoEnigmaEmptyPaperrollHolder:ss(kActive)
	objectIoEnigmaButtonArea:ss(kActive)
	objectIoCardOpening:ss(kActive)
	
	objectHoEnigmaHandle:ss(kFound)
	objectHoPaperroll:ss(kFound)
	objectHoButton2:ss(kFound)
	
	objectHoCardMachineCard:ss(kFound)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:animRedLight()
	if (objectRedFlashingLight:isIdle()) then
		objectRedFlashingLight:animate("animRedLight",
			{ alpha=1,	duration=100	},
			{ delay=500					},
			{ alpha=0,	duration=100	},
			{ delay=1000				}
		)
	end
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:animHandle()
	
	playAudioTrack("UNLOCKED")

	objectEnigmaHandle:animate("animHandle_blocking",
		{ drotation=180, 	duration=300 },
		{ drotation=-180,  	duration=700 }
	)
	
	objectEnigmaHandleShadow:animate("animHandleShadow",
		{ drotation=180,	duration=300 },
		{ drotation=-180,	duration=700 }
	)
	
	objectEnigmaHandleBack:animate("animHandleBack",
		{ drotation=180,	duration=300 },
		{ drotation=-180,	duration=700 }
	)
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:animCardJigsaw()

	objectCardInsert:ss(kIdle)
	objectCardInsertCastShadow:ss(kIdle)
	
	local cey = objectCardInsert:y() - 10
	local sey = objectCardInsertCastShadow:y() - 50

	objectCardInsert:animate("animCard_blocking",
		{
			{ scale=0.65, 	duration=400 },
			{ y=cey,		duration=400 }
		}
	)
	
	objectCardInsertCastShadow:animate("animCardShadow",
		{
			{ scale=0.65,	duration=400 },
			{ y=sey,		duration=400 }
		}
	)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:animPaper(objectPaper)
	
	local sy = objectPaper:y() - objectPaper:height()
	local ey = objectPaper:y()
	
	if (objectPaper:isInactive()) then
		objectPaper:setY(sy)
		objectPaper:ss(kActive)
	end
	
	objectPaper:animate("anim" .. objectPaper:name() .. "_blocking",
		{ command="playTrack track=PRINT", target=self },	
		{ y=sy						},
		{ y=ey,		duration=500 	}
	)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onAnimationFinish(animationName)
	
	Layout.onAnimationFinish(self, animationName)
	
	if (animationName == "animRedLight") then
		
		self:animRedLight()
		
	elseif (animationName == "animCard_blocking") then
		
		self:insertCard(objectHoCardPiecesGroup)
		self:animHandle()
		
		objectCardInsert:ss(kInactive)
		objectCardInsertCastShadow:ss(kInactive)
		
	elseif (animationName == "animHandle_blocking") then
		
		if (self.isWorking()) then
			
			-- check which message we need to print
			local objectPaper = nil
			
			if (self.mCardInserted == objectHoCardPiecesGroup) then
				
				objectPaper = objectIoStillwaterPaper1
				
			elseif (self.mCardInserted == objectHoCardMachineCard) then
				
				objectPaper = objectIoStillwaterPaper2
								
			end
		
			if (objectPaper ~= nil) then
				self:animPaper(objectPaper)
				
				-- disable handle
				if (objectEnigmaHandle:isActive()) then
					objectEnigmaHandle:ss(kIdle)
				end
			else
				think("MESSAGE_ENIGMA_EMPTY")
			
			end
		
		end			
		
	elseif (animationName == "animobjectIoStillwaterPaper1_blocking") then
		objectIoStillwaterPaper1Close:ss(kActive)
		playAudioTrack("MANUAL_PAGE")
	
	elseif (animationName == "animobjectIoStillwaterPaper2_blocking") then
		objectIoStillwaterPaper2Close:ss(kActive)
		playAudioTrack("MANUAL_PAGE")
		
	end
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onMouseDown(x, y)

	local result = Layout.onMouseDown(self, x, y)
	
	-- WARNING
	-- not supported for now!
	-- chaning the click behavior messes up things for the step/hint system!
	
--	for i, paper in ipairs(self.mPapersCloseUp) do
		
--		if (paper:isActive()) then
			
--			if (not paper:isInside(x, y)) then
			
--				self:closeCloseUp()
			
--			end
			
--			return result
			
--		end
--	end
	
	return result

end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onClickIO(object, x, y)
	
	if (object == objectIoStillwaterPaperWrong) then
		objectIoEnigmaEmptyPaperrollHolder:ss(kActive)
		think("MESSAGE_MEMO_ENIGMA_STUCK")
	end
	
	for i, paper in ipairs(self.mPapersCloseUp) do
		
		if (object == paper) then
			
			-- remove paper
			self:closeCloseUp()
				
			return true, false, false
			
		end
			
	end	
	
	for i, paper in ipairs(self.mPapers) do
		
		if (object == paper) then
			
			-- show close up
			self.mPapersCloseUp[i]:ss(kActive)
				
			playAudioTrack("MANUAL_PAGE")
			return true, false, false
		end
			
	end
	
	return Layout.onClickIO(self, object, x, y)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onClickButton(button)
	
	-- do not respond to buttons if we have a close up active
	for i, paper in ipairs(self.mPapersCloseUp) do
		
		if (paper:isActive()) then
			
			return false
			
		end
			
	end
	
	if (button == objectEnigmaButton1) then
		
		if (objectGreenLightButton1:isInactive()) then
			objectGreenLightButton1:ss(kIdle)
			objectGreenLightButton1Small:ss(kIdle)
		else
			objectGreenLightButton1:ss(kInactive)
			objectGreenLightButton1Small:ss(kInactive)
		end
		
		objectEnigmaButton1:ss(kIdle)
		
		return true
		
	elseif (button == objectEnigmaButton2) then
		
		if (objectGreenLightButton2:isInactive()) then
			objectSliderPaperroll:ss(kIdle)
			objectSliderPaperrollSmall:ss(kIdle)
			objectGreenLightButton2:ss(kIdle)
			objectGreenLightButton2Small:ss(kIdle)
		else
			objectSliderPaperroll:ss(kInactive)
			objectSliderPaperrollSmall:ss(kInactive)
			objectGreenLightButton2:ss(kInactive)
			objectGreenLightButton2Small:ss(kInactive)
		end
		
		objectEnigmaButton2:ss(kIdle)
		
		return true
	
	elseif (button == objectEnigmaHandle) then
		
		self:animHandle()
			
		return true
			
	end
	
	return Layout.onClickButton(self, button)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onUseObject(selectedObject, targetObject, targetProps)

	if (targetObject == objectIoCardOpening) then
		
		self:insertCard(selectedObject)
		
	end
	
	return Layout.onUseObject(self, selectedObject, targetObject, targetProps)

end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:onUsedAll(object, props)

	Layout.onUsedAll(self, object, props)

	if (object == objectIoEnigmaHandleArea) then
		objectEnigmaHandle:ss(kActive)
		objectEnigmaHandleShadow:ss(kIdle)
		objectEnigmaHandleBack:ss(kIdle)
		objectEnigmaHandleSmall:ss(kIdle) -- boathouse
	end
	
	if (object == objectIoEnigmaEmptyPaperrollHolder) then
		objectFilledPaperrollHolder:ss(kIdle)
		objectFilledPaperrollHolderSmall:ss(kIdle)
	end
	
	if (object == objectIoEnigmaButtonArea) then
		objectEnigmaButton2:ss(kActive)
		objectEnigmaButton2Small:ss(kIdle) -- boathouse
	end		

end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:insertCard(object)
		
	self.mCardInserted = object
		
	objectCardIn:ss(kIdle)
	objectIoCardOpening:ss(kIdle)

end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:isWorking()

	if (objectGreenLightButton1:isInactive()) then
		return false
	end
	
	if (objectGreenLightButton2:isInactive()) then
		return false
	end
	
	if (objectEnigmaHandle:isInactive()) then
		return false
	end
	
	if (objectFilledPaperrollHolder:isInactive()) then
		return false
	end
	
	return true

end
-- ----------------------------------------------------------------------------
function layoutBoathouseEnigma:closeCloseUp()

	for i, paper in ipairs(self.mPapersCloseUp) do
		
		if (paper:isActive()) then
			
			paper:ss(kInactive)
			self.mPapers[i]:ss(kInactive)

			playAudioTrack("MANUAL_PAGE")
			
		end
		
	end
	
	layoutBoathouseEnigma.mCardInserted = nil
		
	objectCardIn:ss(kInactive)
	objectIoCardOpening:ss(kActive)
	
	if (objectEnigmaHandle:isIdle()) then
		objectEnigmaHandle:ss(kActive)
	end
	
end