layoutBoathouseCardMachine.mHOs = 
{
	[objectHoCardMachineCard] = 
	{
		siblings = {objectHoCardMachineCardPocket, objectCardMachineCardShadow},
	},
	[objectHoChisel] =
	{
		siblings = {objectHoChiselLit, objectHoChiselPocket},
	},
	[objectHoButton2] =
	{
		siblings = {objectHoButton2Lit, objectHoButton2Pocket},
	},
	[objectHoCardPiece02] = 
	{
		siblings = {objectHoCardPiece02Lit, objectHoCardPiece02Pocket},
	},
	[objectHoCardPiece03] = 
	{
		siblings = {objectHoCardPiece03Lit},
	},		
}
layoutBoathouseCardMachine.mButtons =
{
	[objectCardMachineButton] =
	{
	}
}
layoutBoathouseCardMachine.mSwitches = 
{
	{
		on  		= { objectIoSwitchVerticalLeft01L, objectIoSwitchVerticalRight16L },
		off 		= { objectIoSwitchVerticalLeft01R, objectIoSwitchVerticalRight16R },
		lights 		= { {5, 1}, {5, 2}, {5, 5}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchVerticalLeft02L, objectIoSwitchVerticalRight15L },
		off 		= { objectIoSwitchVerticalLeft02R, objectIoSwitchVerticalRight15R },
		lights 		= { {4, 2}, {4, 4}, {4, 5}, {4, 6}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchVerticalLeft03L, objectIoSwitchVerticalRight14L },
		off 		= { objectIoSwitchVerticalLeft03R, objectIoSwitchVerticalRight14R },
		lights 		= { {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 6}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchVerticalLeft04L, objectIoSwitchVerticalRight13L },
		off 		= { objectIoSwitchVerticalLeft04R, objectIoSwitchVerticalRight13R },
		lights 		= { {2, 2}, {2, 5}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchVerticalLeft05L, objectIoSwitchVerticalRight12L },
		off 		= { objectIoSwitchVerticalLeft05R, objectIoSwitchVerticalRight12R },
		lights 		= { {1, 1}, {1, 3}, {1, 4}, {1,6}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchHorizontalTop06U, objectIoSwitchHorizontalBottom22U },
		off 		= { objectIoSwitchHorizontalTop06D, objectIoSwitchHorizontalBottom22D },
		lights 		= { {2, 1}, {4, 1}, {5, 1}, },
		solution 	= false
	},
	{
		on  		= { objectIoSwitchHorizontalTop07U, objectIoSwitchHorizontalBottom21U },
		off 		= { objectIoSwitchHorizontalTop07D, objectIoSwitchHorizontalBottom21D },
		lights 		= { {1, 2}, {3, 2}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchHorizontalTop08U, objectIoSwitchHorizontalBottom20U },
		off 		= { objectIoSwitchHorizontalTop08D, objectIoSwitchHorizontalBottom20D },
		lights 		= { {1, 3}, {2, 3}, {4, 3}, {5, 3}, },
		solution 	= false
	},
	{
		on  		= { objectIoSwitchHorizontalTop09U, objectIoSwitchHorizontalBottom19U },
		off 		= { objectIoSwitchHorizontalTop09D, objectIoSwitchHorizontalBottom19D },
		lights 		= { {2, 4}, {4, 4}, {5, 4}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchHorizontalTop10U, objectIoSwitchHorizontalBottom18U },
		off 		= { objectIoSwitchHorizontalTop10D, objectIoSwitchHorizontalBottom18D },
		lights 		= { {1, 5}, {2, 5}, {3, 5}, {5, 5}, },
		solution 	= true
	},
	{
		on  		= { objectIoSwitchHorizontalTop11U, objectIoSwitchHorizontalBottom17U },
		off 		= { objectIoSwitchHorizontalTop11D, objectIoSwitchHorizontalBottom17D },
		lights 		= { {2, 6}, {3, 6}, {5, 6}, },
		solution 	= true
	},
}
layoutBoathouseCardMachine.mLights = 
{
	{ objectLightOnR1C1, objectLightOnR1C2, objectLightOnR1C3, objectLightOnR1C4, objectLightOnR1C5, objectLightOnR1C6 },
	{ objectLightOnR2C1, objectLightOnR2C2, objectLightOnR2C3, objectLightOnR2C4, objectLightOnR2C5, objectLightOnR2C6 },
	{ objectLightOnR3C1, objectLightOnR3C2, objectLightOnR3C3, objectLightOnR3C4, objectLightOnR3C5, objectLightOnR3C6 },
	{ objectLightOnR4C1, objectLightOnR4C2, objectLightOnR4C3, objectLightOnR4C4, objectLightOnR4C5, objectLightOnR4C6 },
	{ objectLightOnR5C1, objectLightOnR5C2, objectLightOnR5C3, objectLightOnR5C4, objectLightOnR5C5, objectLightOnR5C6 },
}
layoutBoathouseCardMachine.mStart = 
{
	{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 6},
	{2, 4}, {2, 6},
	{3, 1}, {3, 3}, {3, 4},
	{4, 4},
	{5, 1}, {5, 2}, {5, 4}, {5, 5}, {5, 6},
}
layoutBoathouseCardMachine.mPuzzle = true
layoutBoathouseCardMachine.mPuzzleActive = false
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onConstruct()

	Layout.onConstruct(self)
	
	for i, group in ipairs(self.mSwitches) do
		
		group.all = {}
		
		for j, switch in ipairs(group.on) do
			table.insert(group.all, switch)
		end
		
		for j, switch in ipairs(group.off) do
			table.insert(group.all, switch)
		end
		
		group.switched = false
		
	end
	
	objectCardMachineButton:ss(kIdle)
	objectMapPiece:ss(kInactive)
	
	objectGreenLightOn:setAlpha(0)
	objectRedLightOn:setAlpha(1)	
    
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onInitialize()

	Layout.onInitialize(self)
	
	self:resetPuzzle()
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onShow()
	
	Layout.onShow(self)
	
	if (self.mPuzzleActive or self:isQuickPlay()) then
		objectCardMachineButton:ss(kActive)	
	else
		objectCardMachineButton:ss(kIdle)
	end
	
	if (self.mPuzzleActive) then
		if (layoutBoathouseMap:isSeen()) then
			objectMapPiece:ss(kIdle)
			objectMapPieceGone:ss(kIdle)
		end
	end
	
	if (not self.mPuzzleActive) then
		think("MESSAGE_HMM_PUNCHCARDS")
	end
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onStartStart()
	
	Layout.onStartStart(self)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onQuickPlay()

	Layout.onQuickPlay(self)

end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onSolve()
	
	Layout.onSolve(self)
	
	for i, group in ipairs(self.mSwitches) do
		
		if ((group.switched and not group.solution) or (not group.switched and group.solution)) then
			
			self:onClickSwitch(group)
			
		end
		
	end
    
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onSolveStart()
	
	Layout.onSolveStart(self)
    
    self:animSolved()
    self.mPuzzleActive = false
    
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onSolveFinish()
	
	Layout.onSolveFinish(self)
	
	objectHoCardMachineCard:ss(kHidden)
	objectHoCardMachineCard:ss(kFound)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onMouseDown(x, y)

	local result = Layout.onMouseDown(self, x, y)
	
	if (self.mPuzzleActive or self:isQuickPlay()) then
		
		-- check if we clicked on a switch
		for i, group in ipairs(self.mSwitches) do
				
			for j, switch in ipairs(group.all) do
					
				switch:setHitmaskThreshold(0)
				if (switch:isInside(x, y)) then
						
					self:onClickSwitch(group)
					
					if (self:checkSolution()) then
						self:animLightGreen()
					else
						self:animLightRed()
					end
					
					return true
												
				end
					
			end
				
		end
	    
	end
	
	return result

end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onClickButton(button)
	
	if (button == objectCardMachineButton) then
		
		if (self:checkSolution()) then
			self:solve()
		end

		return true	
	end
	
	return Layout.onClickButton(self, button)

end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:onClickSwitch(group)
	
	group.switched = not group.switched
	
	-- set all switch in group not visible
	for i, switch in ipairs(group.all) do
		
		switch:setVisible(false)
		
	end
	
	for i, switch in ipairs(group.on) do
		
		switch:setVisible(not group.switched)
		
	end
	
	for i, switch in ipairs(group.off) do
		
		switch:setVisible(group.switched)
		
	end
	
	-- toggle lights
	for i, pos in ipairs(group.lights) do
		
		local light = self.mLights[pos[1]][pos[2]]
		
		light:setVisible(not light:isVisible())
		
	end
	
	-- sound
	playAudioTrack("SLIDE_BLOCK")	
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:checkSolution()
	
	for i, group in ipairs(self.mSwitches) do
		
		if (not group.switched and group.solution) then
			
			return false
			
		end
		
		if (group.switched and not group.solution) then
			
			return false
			
		end
		
	end
	
	return true
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:resetPuzzle()
	
	-- set all switches on
	for i, group in ipairs(self.mSwitches) do
		
		group.switched = false
		
		for j, switch in ipairs(group.all) do
			
			switch:setVisible(false)
			
		end
		
		for j, switch in ipairs(group.on) do
			
			switch:setVisible(true)
			
		end
				
	end
	
	-- reset all the lights
	for i, row in ipairs(self.mLights) do
		
		for j, light in ipairs(row) do
			
			light:setVisible(false)
			
			-- check if this is in our start
			for k, start in ipairs(self.mStart) do
				
				if (start[1] == i and start[2] == j) then
					
					light:setVisible(true)
				
				end
				
			end
			
		end
		
	end
	
	objectGreenLightOn:setAlpha(0)
	objectRedLightOn:setAlpha(1)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:animLightGreen()

	objectGreenLightOn:animate(
		{ command="playTrack track=LED_ON", target=self },
		{ alpha=1, 	duration=150 }
	)
	
	objectRedLightOn:animate(
		{ alpha=0,	duration=150 }
	)
		
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:animLightRed()

	objectGreenLightOn:animate(
		{ alpha=0, 	duration=150 }
	)
	
	objectRedLightOn:animate(
		{ command="playTrack track=LED_OFF", target=self },
		{ alpha=1,	duration=150 }
	)
	
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:animSolved()

	local delay = 0
	
	for i=1, 3 do
		objectGreenLightOn:animate(
			{ delay=delay			},
			{ alpha=1, duration=150 },
			{ delay=250				},
			{ alpha=0, duration=150 },
			{ delay=250				}
		)
		delay=delay+800
	end
	
	local ex = objectHoCardMachineCard:x() - 5
	local ey = objectHoCardMachineCard:y() + 50
	
	objectHoCardMachineCard:animate(
		{ delay=delay				},
		{ command="playTrack track=CARD_MACHINE", target=self },
		{ delay=300				},
		{ x=ex, y=ey, duration=600	}
	)
	
	delay=delay+900

    self:animate("SolveCardMachine_blocking",
    	{ delay=delay									},
    	{ call=layoutBoathouseCardMachine.solveFinish 	}
    )
    
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:isSkippable()
	return self.mPuzzleActive
end
-- ----------------------------------------------------------------------------
function layoutBoathouseCardMachine:isTippable()
	return self.mPuzzleActive
end