-- ----------------------------------------------------------------------------
layoutBasementCorner.mExits =
{
}
layoutBasementCorner.mPOIs = 
{
}
layoutBasementCorner.mHOs = 
{
	[objectBasementDialface] = 
	{
		siblings = {objectBasementDialfaceDark, objectBasementDialfaceLit, objectBasementDialfacePocket},
	},
}
layoutBasementCorner.mIOs =
{
}
-- ----------------------------------------------------------------------------
layoutBasementCorner.mBasementCornerScaleSmall = 1
layoutBasementCorner.mBasementCornerScaleLarge = 1.5
layoutBasementCorner.mBasementCornerScaleSmallSpeed = 2.5
layoutBasementCorner.mBasementCornerScaleLargeSpeed = 2.0
layoutBasementCorner.mBasementCornerStillTime = 500
layoutBasementCorner.mBasementCornerFrameTime = 500
layoutBasementCorner.mBasementCornerAlpha = 0.9  	
layoutBasementCorner.mPuzzle = true
layoutBasementCorner.mScareState = 0
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onInitialize()
	
	Layout.onInitialize(self)

	objectBasementDarkcornerCandlelight:ss(kIdle)
	objectBasementDarkcornerCandlelight:animate("Candlelight_infinite",
		{ startframe=0, endframe=4, frametime=self.mBasementCornerFrameTime, loops="forever"	}
	)

	objectBasementDarkcornerCandlelight:setAnchorPoint("bottom")
	local cx = objectBasementDarkcornerBackgroundDark:x()
	local cy = objectBasementDarkcornerBackgroundDark:y()
	local cw = objectBasementDarkcornerBackgroundDark:width()
	local ch = objectBasementDarkcornerBackgroundDark:height()
	assert(cx and type(cx) == "number")
	assert(cy and type(cy) == "number")
	assert(cw and type(cw) == "number")
	assert(ch and type(ch) == "number")
	objectBasementDarkcornerBackgroundDark:setAlpha(self.mBasementCornerAlpha)	
	objectBasementDarkcornerBackgroundDark:setClipRect(cx, cy, cw, ch)

	objectBasementDarkcornerBackgroundLit2:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	objectBasementDialface:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	objectBasementDialfaceLit:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	objectBasementDialfacePocket:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	objectScareJackboxClosed:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	objectScareJackboxStart:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	objectScareJackboxStart:ss(kInactive)
	objectScareJackboxStartDark:ss(kInactive)
	objectScareJackboxSwing:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	objectScareJackboxSwing:ss(kInactive)
	objectScareJackboxSwingDark:ss(kInactive)
	objectBasementDarkcornerCandlelight:becomeChildOf(objectBasementDarkcornerBackgroundDark)
	
	self:update()

end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onShow()
	
	Layout.onShow(self)

	if (objectBedroomCandle:isFound() and objectBasementMatchbox:isFound()) then
		objectBedroomCandle:ss(kDone)
		objectBasementMatchbox:ss(kDone)
		objectBedroomCandleLit:ss(kFound)
	end	
	
	if (objectBedroomCandle:isIdle()) then
		self:update()	
		-- first time we get here since the goal is active. Put candle in shoppinglist:
		objectBedroomCandle:ss(kActive)
		think("MESSAGE_NEED_CANDLE")
	end			
	
end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onHide()

	Layout.onHide(self)

end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onQuickPlay()

	Layout.onQuickPlay(self)

	objectBedroomCandle:ss(kDone)
	objectBasementMatchbox:ss(kDone)
	objectBedroomCandleLit:ss(kFound)

end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onSolve()

	Layout.onSolve(self)

	objectBedroomCandle:ss(kDone)
	objectBasementMatchbox:ss(kDone)
	objectBedroomCandleLit:ss(kDone)
	objectBasementDialface:ss(kFound)	
	objectBasementDarkcornerCandlelight:ss(kDone)
	objectBasementDarkcornerBackgroundLit2:ss(kDone)	
	objectScareJackboxSwing:ss(kDone)	
	
end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onSolveStart()

	Layout.onSolveStart(self)

	self:animate("CloseCorner_blocking",
		{ delay = 200 },
		{ call = layoutBasementCorner.hide },
		{ call = layoutBasementCorner.solveFinish }
	)	

end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onClickHO(object, x, y)

	if (object == objectBasementDialface) then
		self:solve()
	end
	
	return Layout.onClickHO(self, object, x, y)

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

	Layout.onTick(self, time)
	
	self:update()	
	
	if (objectBedroomCandleLit:isSelected()) then
		if (not objectScareJackboxClosed:isDone()) then
			local cx = objectScareJackboxClosed:centerX()
			local cy = objectScareJackboxClosed:centerY()
			local d  = math.distance(cx, cy, cursor:getX(), cursor:getY())
			if (d < 50) then
				objectScareJackboxClosed:ss(kDone)
				objectScareJackboxStart:ss(kIdle)
				objectScareJackboxStart:animate("scareJackboxStart",
					{ command="playTrack track=JACK_IN_BOX", target=self },
					{ startframe=0, endframe=5,	frametime=150 }
				)
				objectScareJackboxClosedDark:ss(kDone)
				objectScareJackboxStartDark:ss(kIdle)
				objectScareJackboxStartDark:animate("scareJackboxStartDark",
					{ startframe=0, endframe=5,	frametime=150 }
				)		
				self.mScareState = 1		
			end	
		end
	end
	
end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:update()

	if (self:isPuzzling()) then
		self:updateLightCoords()
		self:updateLightVisibility()		
		self:updateLightClipRects()
	else
	
	
	end
	
end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:updateLightCoords()

	local x = cursor:getX()
	local y = cursor:getY() + objectBasementDarkcornerCandlelight:height()/2
	
	objectBasementDarkcornerCandlelight:setX(x - self:x())
	objectBasementDarkcornerCandlelight:setY(y - self:y())

end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:updateLightVisibility()

	if (objectBedroomCandleLit:isSelected()) then
		objectBasementDialface:ss(kActive)
		objectBasementDarkcornerCandlelight:ss(kIdle)
		objectBasementDarkcornerBackgroundLit2:ss(kIdle)
		
		cursor:setCampfireCursorVisible(true)
		cursor:setCursorOffsetX(0)
		cursor:setCursorOffsetY(0)
		cursor:setSelectionOffsetX(0)
		cursor:setSelectionOffsetY(75)
		
		if (self.mScareState == 2) then
			objectScareJackboxSwing:ss(kIdle)
		end
		if (self.mScareState == 1) then
			objectScareJackboxStart:ss(kIdle)
		end
		if (self.mScareState == 0) then
			objectScareJackboxClosed:ss(kIdle)
		end				
	else
		objectBasementDialface:ss(kHidden)
		objectBasementDarkcornerCandlelight:ss(kInactive)
		objectBasementDarkcornerBackgroundLit2:ss(kInactive)	
		
		if (self.mScareState == 2) then
			objectScareJackboxSwing:ss(kInactive)
		end
		if (self.mScareState == 1) then
			objectScareJackboxStart:ss(kInactive)
		end
		if (self.mScareState == 0) then
			objectScareJackboxClosed:ss(kInactive)
		end			
	end
	
end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:updateLightClipRects()

	local w = objectBasementDarkcornerCandlelight:width()
	local h = objectBasementDarkcornerCandlelight:height()
	local x = objectBasementDarkcornerCandlelight:globalX(0, 0)
	local y = objectBasementDarkcornerCandlelight:globalY(0, 0)
	w = objectBasementDarkcornerCandlelight:globalX(w, h) - x
	h = objectBasementDarkcornerCandlelight:globalY(w, h) - y
	
	x = x - self:x()
	y = y - self:y()
	w = w - 4
	h = h - 4

	objectBasementDarkcornerCandlelight:setClipRect(x, y, w, h)
	objectBasementDialface:setClipRect(x, y, w, h)
	objectBasementDialfaceLit:setClipRect(x, y, w, h)
	objectBasementDialfacePocket:setClipRect(x, y, w, h)
	objectBasementDarkcornerBackgroundLit2:setClipRect(x, y, w, h)
	objectScareJackboxClosed:setClipRect(x, y, w, h)
	objectScareJackboxStart:setClipRect(x, y, w, h)
	objectScareJackboxSwing:setClipRect(x, y, w, h)
	
end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:calcCursorProperties()

	Layout.calcCursorProperties(self)
	
	if (objectBedroomCandleLit:isSelected()) then
		cursor:setCursor(kCursorPointer)
	end

end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:isSkippable()
	return objectBedroomCandleLit:isFound() or objectBedroomCandleLit:isSelected()
end
-- ----------------------------------------------------------------------------
function layoutBasementCorner:onAnimationFinish(animationName)

	Layout.onAnimationFinish(self, animationName)
	
	if (animationName == "scareJackboxStart") then
		objectScareJackboxStart:ss(kDone)
		objectScareJackboxSwing:ss(kIdle)
		objectScareJackboxSwing:animate("scareJackboxSwing",
			{ startframe=0,	endframe=8, frametime=80, loops="forever", type="pingpong" }
		)
		self.mScareState = 2
	end
	
	if (animationName == "scareJackboxStartDark") then
		objectScareJackboxStartDark:ss(kDone)
		objectScareJackboxSwingDark:ss(kIdle)
		objectScareJackboxSwingDark:animate("scareJackboxSwingDark",
			{ startframe=0,	endframe=8, frametime=80, loops="forever", type="pingpong" }
		)
		self.mScareState = 2
	end	
	
end