layoutLivingroom.showedScaryShadow = false
-- ----------------------------------------------------------------------------
layoutLivingroom.mExits =
{
	[objectLivingroomBasementhatch] = 
	{
		target = layoutBasement,
		mirrors = {objectLivingroomBasementhatchLit},
		arrowX = 1590,
		arrowY = 559,
		arrowAngle = 1.2 * math.pi,
		labelX = 1526,
		labelY = 605,
		labelAnchorpoint = ANCHOR_CENTER_RIGHT,
		sparkleX = 510,
    	sparkleY = 636,	
	},
	[objectLivingroomBedroomdoorLit] = 
	{
		target = layoutBedroom,
		mirrors = {objectLivingroomBedroomdoorOpenLit},
		arrowX = 1400,
		arrowY = 366,
		arrowAngle = 0.4 * math.pi,
		labelX = 1525,
		labelY = 305,
		labelAnchorpoint = ANCHOR_CENTER,
	},
	[objectLivingroomPorchdoorOpenUnlit] = 
	{
		target = layoutPorch,
		mirrors = {objectLivingroomPorchdoorOpenLit},
		arrowX = 296,
		arrowY = 572,
		arrowAngle = 1.6 * math.pi,
		labelX = 228,
		labelY = 489,
		labelAnchorpoint = ANCHOR_CENTER_LEFT,
	},
}

layoutLivingroom.mPOIs = 
{
	[objectLivingroomCupboard] = 
	{
		target = layoutLivingroomCupboard,
	},
	[objectLivingroomPhone] = 
	{
		target = layoutLivingroomPhone,
	},	
	[objectPoiLivingroomPaperLit] = 
	{
		target = layoutLivingroomPaper,
		mirrors = {objectPoiLivingroomPaperUnlit},
	},
	[objectLivingroomPictureLit] = 
	{
		target = layoutLivingroomPicture,
	},
	[objectLivingroomTelevision] = 
	{
		target = layoutLivingroomTelevision,
	},
	[objectLivingroomRadiostationlistLit] = 
	{
		target = layoutLivingroomRadioStations,
		mirrors = {objectLivingroomRadiostationlistUnlit},
	},
}

layoutLivingroom.mHOs = 
{
}
layoutLivingroom.mMoonlightDustEffects = {}
layoutLivingroom.mMoonlightDustSettings = {
	["left1"] = 
	{
		startX = 204,
		startY = 670,
		endX = 404,
		endY = 670,
		durationMin = 1000,
		durationMax = 1600,
	},
	["left2"] = 
	{
		startX = 367,
		startY = 352,
		endX = 1020,
		endY = 568,
		durationMin = 1000,
		durationMax = 1600,
	}
	,
	["right"] =
	{
		startX = 1024,
		startY = 640,
		endX = 1600,
		endY = 640,
		durationMin = 1000,
		durationMax = 1600,
	}	
}
layoutLivingroom.mMoonlightDustActive = true
-- ----------------------------------------------------------------------------
function layoutLivingroom:onConstruct()
	
	Layout.onConstruct(self)
	
	objectLivingroomPorchdoorOpenUnlit:ss(kActive)
	
	objectPorchSuitcaseInside:ss(kInactive)
	objectLivingroomBackgroundLeftUnlit2:ss(kInactive)
	objectLivingroomBackgroundLeftLit:ss(kInactive)
	objectLivingroomBackgroundRightLit:ss(kInactive)
	objectLivingroomBackgroundLeftUnlit:ss(kInactive)
	objectLivingroomBackgroundRightUnlit:ss(kInactive)
	objectLivingroomBedroomdoorLit:ss(kInactive)
	objectLivingroomBedroomdoorOpenLit:ss(kInactive)
	objectLivingroomPorchdoorOpenLit:ss(kInactive)
	objectLivingroomCorkboardLit:ss(kInactive)
	objectLivingroomRadiofrontplate:ss(kInactive)
	objectLivingroomRadiofrontplateSmall:ss(kInactive)
	objectLivingroomRadiostationlistLit:ss(kInactive)
	objectLivingroomPhone:ss(kInactive)
	objectLivingroomPhoneBroken:ss(kInactive)
	objectLivingroomShadow:ss(kInactive)
	objectLivingroomShadow2:ss(kInactive)
	objectLivingroomTelevision:ss(kInactive)
	objectLivingroomTelevisionAnimation:ss(kInactive)
	objectLivingroomCupboard:ss(kInactive)
	objectPhonenumberPiece2:ss(kInactive)
	objectPhonenumberPiece3:ss(kInactive)
	objectPhonenumberPiece4:ss(kInactive)
	objectLivingroomBasementhatch:ss(kInactive)
	objectLivingroomBasementhatchLit:ss(kInactive)
    objectPorchSuitcaseInside:ss(kInactive)
    objectPoiLivingroomPaperLit:ss(kInactive)
    objectPoiLivingroomPaperUnlit:ss(kActive)
    objectLivingroomPaperLit:ss(kInactive)
    objectLivingroomPaperUnlit:ss(kIdle)
    objectLivingroomPictureLit:ss(kInactive)
    
    for name, settings in pairs(self.mMoonlightDustSettings) do
    	self.mMoonlightDustEffects[name] = self:newChildParticle( { name = "PARTICLE_LIVINGROOM_MOONLIGHT_DUST_" .. string.upper(name), x=0, y=0, library="PARTICLE_MOONLIGHT_DUST_LIVINGROOM", section="particles", play=true, removeWhenFinished=false } )
    	self:animMoonlightDust(name)
    end
      	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:onInitialize()

	Layout.onInitialize(self)
	
	objectPorchSuitcaseInsideUnlit:ss(kInactive)
	objectPorchSuitcaseInside:ss(kInactive)
	
	self:animPaperUnlit()

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

	-- after finding all clothes for your suitcase,
	-- it's moved inside the livingroom	
	if (objectPorchSuitcaseOpen:isDone()) then
		if (not objectPorchSuitcaseClosed:isDone()) then
			objectPorchSuitcaseInsideUnlit:ss(kIdle)
			objectPorchSuitcaseClosed:ss(kDone)	
		end
	end
	
	allowScrolling(goalOpenShutters:isFinished())
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:onHide()
	
	Layout.onHide(self)	
	
	allowScrolling(layoutShedShutterManual.scrollingAllowed)
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:flickerRadioLight()
	
	local fadeOut = math.random(40, 100)
	local outDelay = math.random(10, 800)
	local fadeIn = math.random(75, 150)
	local inDelay = math.random(400, 1000)

	objectLivingroomBedroomdoorOpenLit:animate("flickerRadioLightLivingroom",
		{ alpha=0,						duration=fadeOut			},
		--{ sound="SOUND_RADIO_GLOW:default"						},
		--{ command="playTrack track=HUT_RADIO_GLOW",		target=self	},
		{ delay=outDelay											},
		{ alpha=1,						duration=fadeIn				},
		--{ sound="SOUND_RADIO_GLOW:default"						},
		--{ command="playTrack track=HUT_RADIO_GLOW",		target=self	},
		{ delay=inDelay												}
	)
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:animMoonlightDust(part)
	
	if (self.mMoonlightDustActive) then
		local effect   = self.mMoonlightDustEffects[part]
		local settings = self.mMoonlightDustSettings[part]
	
		local sx = settings.startX
		local sy = settings.startY
		local ex = settings.endX
		local ey = settings.endY
		local duration = math.random(settings.durationMin, settings.durationMax)
	
		animParticleEmitterLocationLine(effect, { sx=sx, sy=sy, ex=ex, ey=ey, duration=duration } )
		
		local animID = "animLivingroomMoonlightDust_" .. part
    
    	-- ugly hack code below
    	self:animate(animID,
	    	{ delay=duration		},
   			{ call=function() self.animMoonlightDust(self, part) end }
    	)
    end
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:animPaperUnlit()

	if (objectLivingroomPaperUnlit:isIdle()) then
		objectLivingroomPaperUnlit:animate("flapPaperLivingroomUnlit",
			{ delay = math.random(4500, 8000) },
			{ startframe=0, endframe=13, frametime=75 }
		)		
	end

end
-- ----------------------------------------------------------------------------
function layoutLivingroom:animPaperLit()

	if (objectLivingroomPaperLit:isIdle()) then
		objectLivingroomPaperLit:animate("flapPaperLivingroomLit",
			{ delay = math.random(4500, 8000) },
			{ startframe=0, endframe=13, frametime=75 }
		)	
	end	

end
-- ----------------------------------------------------------------------------
function layoutLivingroom:killDustAnimations()
	self.mMoonlightDustActive = false
	
	 for name, effect in pairs(self.mMoonlightDustEffects) do
    	effect:stop()
    end
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:onAnimationFinish(animationName)

	if (animationName == "flapPaperLivingroomUnlit") then
		self:animPaperUnlit()
		
	elseif (animationName == "flapPaperLivingroomLit") then
		self:animPaperLit()		

	elseif (animationName == "televisionAnimation") then
		self:showTelevisionAnimation()
	
	elseif ((animationName == "flickerRadioLightLivingroom") and (not goalFixRadio:isFinished())) then
		self:flickerRadioLight()
		
	end
	
	Layout.onAnimationFinish(self, animationName)
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:onScrollFinish()

	Layout.onScrollFinish(self)
	
	if ((self.showedScaryShadow == false) and (goalFindFuses:isStarted()) and (not goalFindFuses:isFinished())) then
		self:showScareLivingroom()
	end

end
-- ----------------------------------------------------------------------------
function layoutLivingroom:showTelevisionAnimation()

	if (objectLivingroomTelevisionAnimation:isIdle()) then
		objectLivingroomTelevisionAnimation:animate("televisionAnimation",
			{ delay = math.random(3000, 4000) },
			{ startframe=0, endframe=22, frametime=50 }
		)
	end

end
-- ----------------------------------------------------------------------------
function layoutLivingroom:showScareLivingroom()

	objectLivingroomShadow:ss(kIdle)
	objectLivingroomShadow2:setAlpha(0)
	objectLivingroomShadow2:ss(kIdle)
	objectLivingroomBackgroundLeftUnlit2:setAlpha(0)
	objectLivingroomBackgroundLeftUnlit2:ss(kIdle)
	
	objectLivingroomShadow:animate("livingRoomScare1_blocking", 
		{ alpha=1		},
		{ command="playTrack track=SHADOW_SCARE_2", target=self	},
		{ startframe=0, 		frametime=80					},
		{ alpha=0, duration=250									}
	)
	
	objectLivingroomShadow2:animate("livingRoomScare2_blocking", 
		{ alpha=0												},
		--{ alpha=1, duration=250								},
		{ delay=200												},
		{ alpha=1												},
		{ startframe=0, 		frametime=80					},
		{ alpha=0, duration=500									}
	)
	
	objectLivingroomBackgroundLeftUnlit2:animate("livingRoomScare3_blocking", 
		{ alpha=0.2												},
		--{ delay=300											},
		{ alpha=1, duration=200									},
		{ delay=100 											},
		{ alpha=0,				duration=200					},
		{ delay=200												}
	)
	
	self.showedScaryShadow = true
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:onStartAudio()
	
	--getAudioManager():stopTrack({group=kOutsideHutAmbientGroup})
	
	playAmbientAudio(kInsideHutAmbientGroup)
	
	if (layoutBedroom ~= nil) then
		layoutBedroom:checkRadioStatic("medium")
	end
	
	-- Play the "inside" ambiences
	if (not getAudioManager():isTrackPlaying("DOOR_CREAK")) then
		local doorTrack = {}
		
		doorTrack.tag = "DOOR_CREAK"
		doorTrack.group = kInsideHutAmbientGroup
		doorTrack.definition = "DOOR_SWEETENER_1"
		doorTrack.volume = 0.9
		
		getAudioManager():playTrack(doorTrack)
	end
	
	if (not getAudioManager():isTrackPlaying("FLOOR_CREAK")) then
		local floorTrack = {}
		
		floorTrack.tag = "FLOOR_CREAK"
		floorTrack.group = kInsideHutAmbientGroup
		floorTrack.definition = "FLOOR_SWEETENER_1"
		floorTrack.volume = 0.8
		
		getAudioManager():playTrack(floorTrack)
	end
	
	-- ticking clock
	local clockTrack = {}
	clockTrack.tag = "CLOCK"
	clockTrack.definition = "LIVINGROOM_CLOCK"
	clockTrack.volume = 0.4
	getAudioManager():playTrack(clockTrack)	
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:onStopAudio()
	stopAudioTrack("CLOCK")
end
-- ----------------------------------------------------------------------------
function layoutLivingroom:shouldPlaceFireflies()

	if (Layout.shouldPlaceFireflies(self)) then
    	return layoutBasementFusebox:isSolved()
    end

end