--[[
Animations of in-game Ambients

11 Mar 09	tp		Created

--]]



function doOpeningPanAmbient()
	doGullSFX()
	doWaveSFX()
end



function doFlySwallow(aName, aEveryMin, aEveryMax, aReturnTime, aScaleMin, aScaleMax, aSection, aFlipHorizontal)
	local aSprite = scene:newChildSprite(
		{alpha = 0.0, x = math.Random(-60,-80), y = math.Random(20,140), scale = math.Random (aScaleMin, aScaleMax), image = "Swallow1", section = aSection, name = aName})
		
	aSprite:setFlipImageHorizontal(aFlipHorizontal)
			
	aSprite:doEvery(math.Random(aEveryMin,aEveryMax), function()
		aSprite:animate(
			-- the swallow flys in from the left, quickly
			{alpha = 1.0, x = math.Random(-60,-120), y = math.Random(20,120)},
			{x = math.Random(860,920), y = math.Random(20,120), duration = math.Random(1300,1800), startFrame = 0, endFrame = 37, frameTime = math.Random(33,38), loops = 2}  -- a quick straight line, the strip has a dive sequence
		)
		return aReturnTime
		end
	)
end

function doBEACHGetBackpack()
	local backpack = scene:child("96backpackanimation1")
	backpack:moveBehind( scene:child("GAME_UI_BLUEPRINT") )

	scene:child("4showbackpack"):setAlpha(0)
	scene:child("4showbackpack"):setVisible(true)
	scene:child("3pictureandname"):setAlpha(0)
	scene:child("3pictureandname"):setVisible(true)
	scene:child("2storypicturename"):setAlpha(0)
	scene:child("2storypicturename"):setVisible(true)
	scene:child("22actionelectricaltape"):setAlpha(0)
	scene:child("22actionelectricaltape"):setVisible(true)
	
	scene:child("7backpack"):setVisible(false)
	
	local fadeDuration = 50
	if backpack ~= nil then
		backpack:animate(
			{visible=true, x = 70, y = 90, scale=0.47, rotation=-162},
			{x = 113, y = 342, scale=1.0, drotation=162, duration = 500},
			{
				{sprite=scene:child("4showbackpack"), alpha=1, duration=fadeDuration},
				{sprite=scene:child("3pictureandname"), alpha=1, duration=fadeDuration},
				{sprite=scene:child("2storypicturename"), alpha=1, duration=fadeDuration},
				{sprite=scene:child("22actionelectricaltape"), alpha=1, duration=fadeDuration},
				{alpha=0, duration=fadeDuration},
				{visible=false}
			}
		)
	end
end
		
local function doBEACH()
	doGullSFX()
	doWaveSFX()
	
	-- Set up the swallows
	for i=1, 20 do
		doFlySwallow("swallow_diver_"..i, 16000, 20000, 55000, 0.3, 1.0, "scenegame_1_BEACH_AMB", true)
		scene:child("swallow_diver_"..i):moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	end

	scene:child("96backpackanimation1"):setVisible(false)
end

function doSHPRKGodRay(aName)
	local godRay = scene:newChildSprite(
		{x = math.Random (-60, 100), y = math.Random (-60, -10), image = "Godray", section = "scenegame_2_SHPRK_AMB", name = name, pivotpoint = "top", alpha = 0, rotation = math.Random(-30,-50)})
		
	godRay:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	
	godRay:doEvery(math.Random(50,2000), function()
		local durationLowerLimit = 6100
		local durationUpperLimit = 8000
		godRay:animate(
			-- the ray rotates & fades in/out, at random speeds, angles and durations
			{alpha = math.Random (0.6,1.0), rotation = math.Random(-30,-50), duration = math.Random(durationLowerLimit,durationUpperLimit), style = "easeInOut"},
			{alpha = 0.0, rotation = math.Random(-30,-50), duration = math.Random(durationLowerLimit,durationUpperLimit), style = "easeInOut"} 
		)
		return math.Random (7000,9000)
		end
	)
end

function doSHPRKFlashCamera()
	local cameraFlash = scene:newChildSprite(
		{x = 174, y = 346, image = "CameraFlash", section = "scenegame_2_SHPRK_AMB", name = "CameraFlash", pivotpoint = "center", alpha = 0, scale = 0.0})
			
	local eel = scene:child("76eel1")	-- get the eel sprite from the scene pipeline
	
	ghf.cancelAnimation("eelSwim")
	
	if eel ~= nil and cameraFlash ~= nil then
		cameraFlash:setAnchorPoint("center")
		cameraFlash:animate(
			{alpha=1.0, scale=2, rotation=180, duration=500},
			{delay=100},
			{scale=10},
			{delay=200},
			{
				{alpha=0, duration=1000},
				{sprite=eel, frametime=40, x=400, duration=1200},
			},
			{delay=1200},
			{sprite=eel, visible=false}
		)
	end		
end

local function doSHPRK()

	local	eel = scene:child("76eel1")	-- get the eel sprite from the scene pipeline

	if eel ~= nil then
		eel:setX(110)
		eel:setY(305)
		eel:animate(
			{startframe = 0, frametime = 70, loops="forever", type="pingpong"}
		)			
		eel:doEvery(500, function()		-- repeat this stuff
			if not scene:isTargetTriggered("76eel1") then
				local dur1 = math.Random( 7000,8000 )
				local dur2 = math.Random( 4000,5000 )
				local totalDuration = dur1  + dur2
				eel:animate("eelSwim", 
					{x = math.Random(93,107), y = math.Random(298,310), duration = dur1, style="easeInOut"},	-- move eel to the right
					{delay = math.Random(100,500)},																			-- delay a bit
					{play = "SFX_AMB_ocean_eel:scenegame_2_SHPRK_AMB"}, 
					{x = math.Random(108,115), y = math.Random(304,310), duration = dur2, style="easeInOut"}	-- move eel to the left
				)
				return totalDuration		-- this number is delay before repeating; must be larger than duration of stuff we do above.
				end
			end
		)
	end
	
	-- Matt's God Rays
	for i=1, 8 do
		doSHPRKGodRay("GodRay_"..i)
	end
	-- end Matt's god rays
	
	
	local	fish1 = scene:newChildSprite(
	{x = -50, y = 150, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish1"})
	fish1:moveBehind( scene:child("83bottomleftformationbehindsuitcase") )							--2nd furthest zone
	fish1:animate(
			{startframe = 0, frametime = 15, loops="forever"}									-- loop frame animation
	)
	fish1:doEvery(500, function()		-- repeat this stuff
		fish1:animate(
			{x =   math.Random(-70,-50), y =  math.Random(125,225), scale = math.Random(0.8,0.5), alpha = 1.0 },
			{delay = math.Random(5000,20000)},
			{play = "SFX_AMB_ocean_fish:scenegame_2_SHPRK_AMB"}, 
			{x = math.Random(750,860), y =  math.Random(-100,-50), scale =  math.Random(0.5,0.3), alpha = .7, duration = 30000}			-- move fish1 to the right
		)
		return math.Random(60000, 70000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)	
	
	local	fish2 = scene:newChildSprite(
	{x = -50, y = 25, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish2"})
	fish2:moveBehind( scene:child("89lunchbox") )													--furthest zone
	fish2:animate(
			{startframe = 0, frametime = 15, loops="forever"}									-- loop frame animation
	)
	fish2:doEvery(500, function()		-- repeat this stuff
		fish2:animate(
			{x =   math.Random(-70,-50), y =  math.Random(0,40), scale = math.Random(0.7,0.4), alpha = 1.0 },
			{delay = math.Random(3000,10000)},
			{play = "SFX_AMB_ocean_fish:scenegame_2_SHPRK_AMB"}, 
			{x = math.Random(750,860), y =  math.Random(25,75), scale =  math.Random(0.5,0.3), alpha = .7, duration = 30000} -- move fish2 to the right and loop frame animation
		)
		return math.Random(50000, 60000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)	
		
	local	fish3 = scene:newChildSprite(
	{x = -50, y = 150, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish3"})
	fish3:moveBehind( scene:child("GAME_UI_BLUEPRINT") )							--2nd furthest zone
	fish3:animate(
			{startframe = 0, frametime = 15, loops="forever"}									-- loop frame animation
	)
	fish3:doEvery(500, function()		-- repeat this stuff
		fish3:animate(
			{x =   math.Random(350,450), y =  math.Random(600,650), scale = math.Random(0.9,1.0), alpha = 1.0 },
			{delay = math.Random(5000,70000)},
			{play = "SFX_AMB_ocean_fish:scenegame_2_SHPRK_AMB"}, 
			{x = math.Random(800,850), y =  math.Random(350,450), scale =  math.Random(0.8,0.9), alpha = 1.0, duration = 40000 }			-- move fish1 to the right
		)
		return math.Random(50000, 70000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)
	
--		

	local	fish4 = scene:newChildSprite(
	{x = -50, y = 150, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish4"})
	fish4:moveBehind( scene:child("83bottomleftformationbehindsuitcase") )							--2nd furthest zone
	fish4:animate(
			{startframe = 0, frametime = 15, loops="forever"}									-- loop frame animation
	)
	fish4:doEvery(500, function()		-- repeat this stuff
		fish4:animate(
			{x =   math.Random(-70,-50), y =  math.Random(225,300), scale = math.Random(0.9,0.7), alpha = 1.0 },
			{delay = math.Random(2000,50000)},
--			{play = "SFX_AMB_ocean_fish:scenegame_2_SHPRK_AMB"}, 
			{x = math.Random(800,860), y =  math.Random(100,350), scale =  math.Random(0.7,0.6), alpha = .7, duration = 30000}			-- move fish1 to the right
		)
		return math.Random(40000, 60000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)	
		
	-- right to left fish
	
	local	fish5 = scene:newChildSprite(
	{x = -50, y = 150, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish5", flip="horizontal"})
	fish5:moveBehind( scene:child("83bottomleftformationbehindsuitcase") )							--2nd furthest zone
	fish5:animate(
			{startframe = 0, frametime = 15, loops="forever"}						-- loop frame animation
	)
	fish5:doEvery(500, function()		-- repeat this stuff
		fish5:animate(
			{x =   math.Random(850,870), y =  math.Random(50,100), scale = math.Random(0.8,0.5), alpha = 1.0 },
			{delay = math.Random(5000,20000)},
			{x = math.Random(-70,-50), y =  math.Random(-50,150), scale =  math.Random(0.5,0.3), alpha = .7, duration = 30000}
		)
		return math.Random(40000, 80000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)
	
		local	fish6 = scene:newChildSprite(
	{x = -50, y = 25, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish6", flip="horizontal"})
	fish6:moveBehind( scene:child("89lunchbox") )													--furthest zone
	fish6:animate(
			{startframe = 0, frametime = 15, loops="forever"}									-- loop frame animation
	)
	fish6:doEvery(500, function()		-- repeat this stuff
		fish6:animate(
			{x =   math.Random(800,850), y =  math.Random(0,50), scale = math.Random(0.7,0.4), alpha = 1.0 },
			{delay = math.Random(3000,10000)},
			{x = math.Random(-50,-75), y =  math.Random(25,75), scale =  math.Random(0.5,0.3), alpha = .7, duration = 30000}
		)
		return math.Random(30000, 70000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)
			
	local	fish7 = scene:newChildSprite(
	{x = -50, y = 150, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish7", flip="horizontal"})
	fish7:moveBehind( scene:child("GAME_UI_BLUEPRINT") )							--2nd furthest zone
	fish7:animate(
			{startframe = 0, frametime = 15, loops="forever"}									-- loop frame animation
	)
	fish7:doEvery(500, function()		-- repeat this stuff
		fish7:animate(
			{x =   math.Random(550,650), y =  math.Random(600,650), scale = math.Random(0.9,1.0), alpha = 1.0 },
			{delay = math.Random(5000,10000)},
			{play = "SFX_AMB_ocean_fish:scenegame_2_SHPRK_AMB"}, 
			{x = math.Random(-100,-50), y =  math.Random(300,400), scale =  math.Random(0.8,0.9), alpha = 1.0, duration = 35000 }			-- move fish1 to the right
		)
		return math.Random(55000, 65000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)
		local	fish8 = scene:newChildSprite(
	{x = -50, y = 150, image = "FishStrip", section = "scenegame_2_SHPRK_AMB", name = "fish8", flip="horizontal"})
	fish8:moveBehind( scene:child("83bottomleftformationbehindsuitcase") )							--2nd furthest zone
	fish8:animate(
			{startframe = 0, frametime = 15, loops="forever"}									-- loop frame animation
	)
	fish8:doEvery(500, function()		-- repeat this stuff
		fish8:animate(
			{x =   math.Random(800,860), y =  math.Random(100,350), scale = math.Random(0.7,0.6), alpha = 1.0 },
			{delay = math.Random(2000,50000)},
--			{play = "SFX_AMB_ocean_fish:scenegame_2_SHPRK_AMB"}, 
			{x = math.Random(-70,-50), y =  math.Random(225,300), scale =  math.Random(0.9,0.7), alpha = .8, duration = 35000}			-- move fish1 to the right
		)
		return math.Random(40000, 60000)		-- this number is delay before repeating; must be larger than duration of stuff we do above.
		end
	)	
	
end

function doLIVNGStartFire( preroll )
	p = scene:newChildSprite({class = "ParticleEffect", x = 672, y = 457, library = "PARTICLE_FIRE", section = "scenegame_3_LIVNG_AMB", name = "fire", play = true})
	if preroll then
		p:addInitialTime(3000)
	end
	ghf.startLoading("scenegame_3_LIVNG_AMB", false)
	ghf.loopSound("SFX_BBQ_Loop:scenegame_3_LIVNG_AMB")
end

function doLIVNGStopFire()
	local fire = scene:child("fire")
	if fire ~= nil then
		fire:animate({alpha=0.0, duration = 100}, {visible=false})
	end
	ghf.stopLoop("SFX_BBQ_Loop:scenegame_3_LIVNG_AMB")
	scene:newChildSprite({class = "ParticleEffect", x = 672, y = 457, library = "PARTICLE_SMOKE", section = "scenegame_3_LIVNG_AMB", name = "smoke", play = true})
end

function doLIVNGDoBarrelWater()
	scene:newChildSprite({class = "ParticleEffect", x = 455, y = 387, library = "PARTICLE_WATER", section = "scenegame_3_LIVNG_AMB", name = "water", play = true})
	local puddle = scene:newChildSprite({x=395, y=415, image="ANI_PUDDLE", section="scenegame_3_LIVNG_AMB", name="puddle"})
	if puddle ~= nil then
		puddle:animate(
			{delay = 40}, 
			{visible=true, startframe = 0, frametime = 200, loops="forever"},
			{delay = 7500},
			{alpha=0.0, duration = 100}
		)
	end
end

local function doLIVNG()

	if scene:isTargetTriggered("25dogwaterbowlfull") then
		scene:child("89storycookschedule"):setVisible(false)
	end

	if scene:isTargetTriggered("30firewoodsoaked") then
        if not scene:isTargetTriggered("31firewoodlighted") then
            -- PLAY FIRE
			doLIVNGStartFire( true )       
	    else
            -- PLAY SMOKE 
            p = scene:newChildSprite({class = "ParticleEffect", x = 672, y = 457, library = "PARTICLE_SMOKE", section = "scenegame_3_LIVNG_AMB", name = "smoke", play = true})
			p:addInitialTime(8000)
        end
   end
	
	local	frontClouds = scene:child("96ambientclouds2")	-- get the cloud sprite from the scene pipeline
	if frontClouds ~= nil then
		frontClouds:doEvery(0, function()		-- repeat this stuff
			frontClouds:animate(
				{x = 810, duration = 70000},	-- move clouds left to right
				{x = 0-173}					
			)
			return 71000		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
	
	local	backClouds = scene:child("72clouds")	-- get the cloud sprite from the scene pipeline
	if backClouds ~= nil then
		backClouds:doEvery(0, function()		-- repeat this stuff
			backClouds:animate(
				{x = 810, duration = 130000},	-- move clouds left to right
				{x = 0-297}
			)
			return 131000		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
	
	local	frontPlant = scene:child("95ambientplants")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("left")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( -8, 10 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -5, 10 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( 10, -7 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -5, 10 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	-- moves upper middle palm
	frontPlant = scene:child("109ambientsmallplant")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("right")
		frontPlant:doEvery(0, function()							-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewy = (math.Random( 2, 4 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewy = (math.Random( -4, 2 )*0.01), duration = dur2, style="easeIn" },
				{skewy = (math.Random( -4, -2 )*0.01), duration = dur3, style="easeOut" },
				{skewy = (math.Random( -2, 4 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
			-- moves bush at second house
	local	frontPlant = scene:child("108ambientsmallbush")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,3000 )
			local dur2 = math.Random( 2000,3000 )
			local dur3 = math.Random( 2000,3000 )
			local dur4 = math.Random( 2000,3000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 2, -1 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -2, -1 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( 1, -2 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -2, 1 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
				-- moves right palm
	local	frontPlant = scene:child("110ambientpalmright")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("top")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,6000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,6000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 1, 12 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -2, 6 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -4, 5 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -2, 8 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
					-- moves left palm
	local	frontPlant = scene:child("111ambientpalmleft")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,6000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,6000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 1, 10 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -2, 8 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -4, 5 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -2, 8 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
			-- moves grasses
	local	frontPlant = scene:child("73grass")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 1, 8 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -2, 8 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( 8, -2 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -2, 8)*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	-- Set up the swallows
	for i=1, 20 do
		doFlySwallow("swallow_diver_"..i, 21000, 22000, 35000, 0.3, 1.0, "scenegame_3_LIVNG_AMB", true)
		scene:child("swallow_diver_"..i):moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	end

end

local function doRUINS()
-- crawling bugs...

local ant_1 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		ant_1:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		ant_1:doEvery(math.Random(15000,55000), function()
		ant_1:setPivotPoint("center")
		ant_1:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 755, y = 640, rotation = -120, scale = .6},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 16, frameTime = 33},
				{x = 688, y = 406, duration = 6336},
			},
			-- wiggles antennae, cleans self
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			-- "walks" around and faces another direction
			{
				{startFrame=0, endFrame = 11, loops = 2, frameTime = 33},
				{rotation = 40, duration = 792},
			},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			-- "walks" around and faces another direction
			{
				{startFrame=0, endFrame = 11, loops = 1, frameTime = 33},
				{rotation = -45, duration = 396},
			},
			-- walks offscreen in new direction
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			
			{
				{startFrame=0, endFrame = 11, loops = 12, frameTime = 33},
				{x = 836, y = 206, duration = 4752},
			},
			
			{delay = 3000}

		)
		return math.Random(90000, 140000)
		end
	)
local ant_2 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		ant_2:moveBehind( scene:child("32upfrontvine") )
		
		ant_2:doEvery(math.Random(10000,45000), function()
		ant_2:setPivotPoint("center")
		ant_2:setAnchorPoint("center")
		ant_2:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = -5, y = 449, rotation = -20, scale = .5},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 16, frameTime = 33},
				{x = 106, y = 439, duration = 6336},
			},
			-- wiggles antennae, cleans self
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 40)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 40), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 40), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			-- "walks" around and faces another direction
			{
				{startFrame=0, endFrame = 11, loops = 1, frameTime = 33},
				{rotation = 40, duration = 396},
			},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 60)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 40)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 40)},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 40)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 40)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			-- "walks" around and faces another direction
			{
				{startFrame=0, endFrame = 11, loops = 1, frameTime = 33},
				{rotation = -5, duration = 396},
			},
			{
				{startFrame=0, endFrame = 11, loops = 12, frameTime = 33},
				{x = 241, y = 420, duration = 4752},
			},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 60)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 60), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 60)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 60), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 60)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 60), direction = "backward"},
			{
				{startFrame=0, endFrame = 11, loops = 1, frameTime = 33},
				{rotation = 75, duration = 396},
			},
			-- walks offscreen in new direction
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			
			{
				{startFrame=0, endFrame = 11, loops = 6, frameTime = 33},
				{x = 303, y = 518, duration = 2376},
			},
			
			{delay = 3000}

		)
		return math.Random(90000, 160000)
		end
	)
	
local trail_ant_1 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_1:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_1:doEvery (math.Random(11000,12000), function()
		trail_ant_1:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_1:setPivotPoint("center")
		trail_ant_1:setAnchorPoint("center")
		trail_ant_1:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_2 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_2:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_2:doEvery(math.Random(14000,15000), function()
		trail_ant_2:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_2:setPivotPoint("center")
		trail_ant_2:setAnchorPoint("center")
		trail_ant_2:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_3 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_3:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_3:doEvery(math.Random(17000,19000), function()
		trail_ant_3:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_3:setPivotPoint("center")
		trail_ant_3:setAnchorPoint("center")
		trail_ant_3:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_4 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_4:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_4:doEvery(math.Random(20000,22000), function()
		trail_ant_4:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_4:setPivotPoint("center")
		trail_ant_4:setAnchorPoint("center")
		trail_ant_4:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_5 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_5:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_5:doEvery(math.Random(23000,25000), function()
		trail_ant_5:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_5:setPivotPoint("center")
		trail_ant_5:setAnchorPoint("center")
		trail_ant_5:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_6 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_6:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_6:doEvery(math.Random(26000,28000), function()
		trail_ant_6:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_6:setPivotPoint("center")
		trail_ant_6:setAnchorPoint("center")
		trail_ant_6:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_7 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_7:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_7:doEvery(math.Random(29000,31000), function()
		trail_ant_7:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_7:setPivotPoint("center")
		trail_ant_7:setAnchorPoint("center")
		trail_ant_7:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_8 = scene:newChildSprite(
		{x = 400, y = 700, image = "Ant_1", section = "scenegame_4_RUINS_AMB", name = "foreground ant 1"})
		
		trail_ant_8:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_8:doEvery(math.Random(32000,34000), function()
		trail_ant_8:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_8:setPivotPoint("center")
		trail_ant_8:setAnchorPoint("center")
		trail_ant_8:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = math.Random (462,468), y = math.Random (512, 518), alpha = .8, rotation = -110, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (430,437), y = math.Random (456,462), duration = 3960},
			},
			{
				{rotation = -130, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 403, y = 421, duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (425,431), y = math.Random (393,400), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (480,486), y = math.Random (386,394), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (500,505), y = math.Random (350,357), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (545,553), y = math.Random (336,344), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (567,573), y = math.Random (300,306), duration = 3960},
			},
			{
				{rotation = 0, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = math.Random (624,628), y = math.Random (297,303), duration = 3960},
			},
			{
				{rotation = -40, duration = 300},
				{startFrame=0, endFrame = 11, loops = 10, frameTime = 33},
				{x = 646, y = 272, duration = 3960},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)



--set up looping ambients here

   	local	backLeftYucca = scene:child("98backleftyuckaandshadow")	-- get the yucca sprite from the scene pipeline
	if backLeftYucca ~= nil then
		backLeftYucca:setPivotPoint("bottom")
		
		local butterfly = scene:child("91butterfly")	
		if butterfly ~= nil then
			butterfly:becomeChildOf(backLeftYucca)					-- Make butterfly a child of yucca
		end
			
		backLeftYucca:doEvery(0, function()							-- repeat this stuff
			local dur1 = math.Random( 2000,3000 )
			local dur2 = math.Random( 3000,4000 )
			local dur3 = math.Random( 3000,4000 )
			local dur4 = math.Random( 3000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			backLeftYucca:animate(
				{skewx = math.Random( -0.025, 0.02 ), duration = dur1, style="easeInOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = math.Random( 0.02, -0.025 ), duration = dur2, style="easeInOut" },
				{skewx = math.Random( -0.03,  0.035 ), duration = dur3, style="easeInOut" },
				{skewx = math.Random( 0.025,  -0.02 ), duration = dur4, style="easeInOut" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	local	LeftCtrPlant = scene:child("56plantshadow")				-- get the shadow sprite from the scene pipeline
	if LeftCtrPlant ~= nil then
		LeftCtrPlant:setPivotPoint("bottom")
		
		local bush = scene:child("55plant")					
		if bush ~= nil then
			bush:becomeChildOf(LeftCtrPlant)							-- make bush a child of the shadow
		end		
		
		local butterfly1 = scene:child("18butterfly")					
		if butterfly1 ~= nil then
			butterfly1:becomeChildOf(LeftCtrPlant)						-- make butterfly 1 a child of the shadow
		end
		
		local butterfly2 = scene:child("22butterfly")					
		if butterfly2 ~= nil then
			butterfly2:becomeChildOf(LeftCtrPlant)						-- make butterfly 2 a child of the shadow
		end
			
		LeftCtrPlant:doEvery(0, function()								-- repeat this stuff
			local dur1 = math.Random( 2000,3000 )
			local totalDuration = dur1
			LeftCtrPlant:animate(
				{skewx = math.Random( -0.025, 0.02 ), duration = dur1, style="easeInOut" }	-- math.Random makes a random number between lower and upper INTEGER
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

	local	farawayPalm = scene:child("106palmfurther")				-- get palm sprite from the scene pipeline
	if farawayPalm ~= nil then
		farawayPalm:setPivotPoint("bottom")
		
		farawayPalm:doEvery(0, function()								-- repeat this stuff
			local dur1 = math.Random( 2000,3000 )
			local totalDuration = dur1
			farawayPalm:animate(
				{skewx = math.Random( -0.035, 0.035 ), duration = dur1, style="easeInOut" }	-- math.Random makes a random number between lower and upper INTEGER
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
		
	local	closerPalm = scene:child("107palmcloser")				-- get palm sprite from the scene pipeline
	if closerPalm ~= nil then
		closerPalm:setPivotPoint("bottom")
		
		closerPalm:doEvery(0, function()								-- repeat this stuff
			local dur1 = math.Random( 2000,2000 )
			local totalDuration = dur1
			closerPalm:animate(
				{skewx = math.Random( -0.025, 0.035 ), duration = dur1, style="easeInOut" }	-- math.Random makes a random number between lower and upper INTEGER
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
		
	local	backRightYucca = scene:child("100backrightyucka")				-- get yucca sprite from the scene pipeline
	if backRightYucca ~= nil then
		backRightYucca:setPivotPoint("bottom")
		
		backRightYucca:doEvery(0, function()									-- repeat this stuff
			local dur1 = math.Random( 3000,3500 )
			local totalDuration = dur1
			backRightYucca:animate(
				{skewx = math.Random( -0.025, 0.02 ), duration = dur1, style="easeInOut" }	-- math.Random makes a random number between lower and upper INTEGER
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	local	backCenterPlant = scene:child("99plantbehindbackruinscenter")	-- get sprite from the scene pipeline
	if backCenterPlant ~= nil then
		backCenterPlant:setPivotPoint("bottom")
		
		backCenterPlant:doEvery(0, function()										-- repeat this stuff
			local dur1 = math.Random( 1500,2500 )
			local totalDuration = dur1
			backCenterPlant:animate(
				{skewx = math.Random( -0.015, 0.015 ), duration = dur1, style="easeInOut" }	-- math.Random makes a random number between lower and upper INTEGER
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
		
	local	vine = scene:child("32upfrontvine")				-- get the vine sprite from the scene pipeline
	if vine ~= nil then
		vine:setPivotPoint("bottom")
		
		vine:doEvery(0, function()								-- repeat this stuff
			local dur1 = math.Random( 2000,3000 )
			local totalDuration = dur1
			vine:animate(
				{skewx = math.Random( -0.035, 0.03 ), duration = dur1, style="easeInOut" }	-- math.Random makes a random number between lower and upper INTEGER
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

--set up custom target stuff here

	local redBeam = scene:child("8redbeam")
	if redBeam ~= nil then
		if scene:isTargetTriggered("eyes") then
			redBeam:setVisible(true)
		else
			redBeam:setVisible(false)
		end
	end
	
	local blueBeam = scene:child("9bluebeamwithglowingresources")
	if blueBeam ~= nil then
		if scene:isTargetTriggered("othertarget") then
			blueBeam:setVisible(true)
		else
			blueBeam:setVisible(false)
		end
	end
	
	scene:child("132yellowlightonclearresource"):setVisible(false)	
	scene:child("115clearresourceinstand"):setVisible(false)	
	scene:child("116coverclearresourceinstand"):setVisible(false)	
	scene:child("117yellowgodrays"):setVisible(false)
	scene:child("63yellowresource"):setVisible(false)
	scene:child("62yellowresourceinstand"):setVisible(false)
	scene:child("26bigpyramid"):setVisible(false)
	scene:child("131ivyoverpyramidbase"):setVisible(false)
	scene:child("25nativepyramiddrawings"):setVisible(false)
	scene:child("127maskovermovingcenter"):setVisible(false)
	scene:child("12coverpyramidshaft"):setVisible(false)
	scene:child("122yellowresourceonpyramid"):setVisible(false)
	scene:child("123legovergreenresource"):setVisible(false)
	scene:child("8redbeam"):setVisible(false)
	scene:child("126beamtoinfinity"):setVisible(false)
	scene:child("11middleofpyramid"):setVisible(false)
	scene:child("10clearresourceinpyramid"):setVisible(false)
	scene:child("118bluegodrays"):setVisible(false)
	scene:child("119greengodrays"):setVisible(false)
	scene:child("9bluebeamwithglowingresources"):setVisible(false)
	scene:child("130greenresource"):setVisible(false)
	scene:child("125raywithoutclearresource"):setVisible(false)
	
		
	local yellowResource = scene:child("63yellowresource")
	local yellowResourceStand = scene:child("62yellowresourceinstand")
	if scene:isTargetTriggered("64resourcestand") and not scene:hasActionItemBeenGotten("63yellowresource") then
		yellowResource:setVisible(true)
		yellowResourceStand:setVisible(true) 
	else
		yellowResource:setVisible(false)
		yellowResourceStand:setVisible(false)
	end
	
	if scene:isTargetTriggered("27pyramidtip") then
		scene:child("26bigpyramid"):setVisible(true)
		scene:child("131ivyoverpyramidbase"):setVisible(true)
		scene:child("25nativepyramiddrawings"):setVisible(true)
		scene:child("127maskovermovingcenter"):setVisible(true)
		scene:child("12coverpyramidshaft"):setVisible(true)
		scene:child("122yellowresourceonpyramid"):setVisible(true)
		scene:child("123legovergreenresource"):setVisible(true)
		yellowResource:setVisible(false)
		yellowResourceStand:setVisible(false)
	else
		scene:child("26bigpyramid"):setVisible(false)
		scene:child("131ivyoverpyramidbase"):setVisible(false)
		scene:child("25nativepyramiddrawings"):setVisible(false)
		scene:child("127maskovermovingcenter"):setVisible(false)
		scene:child("12coverpyramidshaft"):setVisible(false)
		scene:child("122yellowresourceonpyramid"):setVisible(false)
		scene:child("123legovergreenresource"):setVisible(false)
	end
	
	if scene:isTargetTriggered("96targeteyes") then
		scene:child("8redbeam"):setVisible(true)
		scene:child("126beamtoinfinity"):setVisible(true)
		scene:newChildSprite({class = "ParticleEffect", x = 215, y = 148, library = "PARTICLE_BLUEBEAM", section = "scenegame_4_RUINS_AMB", name = "blueBeamParticle", play = true})
					
		if scene:isTargetTriggered("27pyramidtip") then
			scene:child("125raywithoutclearresource"):setVisible(true)
			doRUINSOpenPyramidCenterPanel()
		end
			
	else
		scene:child("8redbeam"):setVisible(false)
		scene:child("126beamtoinfinity"):setVisible(false)
	end
	
	if scene:isTargetTriggered("11middleofpyramid") and not scene:hasActionItemBeenGotten("130greenresource") then
		scene:child("130greenresource"):setVisible(true)
		scene:child("123legovergreenresource"):setVisible(true)
		scene:child("10clearresourceinpyramid"):setVisible(true)
		scene:child("8redbeam"):setVisible(false)
		scene:child("126beamtoinfinity"):setVisible(false)
		scene:child("122yellowresourceonpyramid"):setVisible(false)
	end

end

function doRUINSOpenPyramidCenterPanel()
	-- OPEN CENTER PANEL
	scene:child("127maskovermovingcenter"):setVisible(true)
	scene:child("11middleofpyramid"):setVisible(true)
	scene:child("125raywithoutclearresource"):setVisible(true)

	scene:child("12coverpyramidshaft"):animate(
		{x=415, y=164, visible=true},
		{x=445, y=167, duration = 1000},
		{
			{visible=false},
			{sprite = scene:child("127maskovermovingcenter"), visible = false},
		}
	)
end
		
function doRUINSPanPyramidPiece(aSprite, aPanDistance, aDuration)
	local targetY = aSprite:y()
	local startY = targetY + aPanDistance
	aSprite:animate(
		{visible = true, y = startY}, 
		{y=targetY, duration = aDuration}
	)
end

function doRUINSShakePyramidPiece(aSprite)
	if aSprite ~= nil then
		aSprite:doEvery(50, function()
			local pyramid = scene:child("26bigpyramid") 
			if pyramid:y() > 62 then
				startX = aSprite:x()
				aSprite:animate(
					{x = startX + 1}, 
					{delay=50},
					{x = startX - 1},
					{delay=50},
					{x = startX}
				)
				return 150
			else
				return false
			end
		end
		)
	end
end


function doRUINSRaisePyramid()

	scene:child("131ivyoverpyramidbase"):setVisible(true)

	local pyramid = scene:child("26bigpyramid")

	local panDistance = 149
	local panDuration = 2000

	local drawing = scene:child("25nativepyramiddrawings")	
	local drawingX = drawing:x()				
	if drawing ~= nil then
		doRUINSPanPyramidPiece(drawing, panDistance, panDuration)
		doRUINSShakePyramidPiece(drawing)
	end	

	local yellowResource = scene:child("122yellowresourceonpyramid")					
	if yellowResource ~= nil then
		doRUINSPanPyramidPiece(yellowResource, panDistance, panDuration)
		doRUINSShakePyramidPiece(yellowResource)	
	end	
	
	local yellowResourcePocket = scene:child("123legovergreenresource")					
	if yellowResourcePocket ~= nil then
		doRUINSPanPyramidPiece(yellowResourcePocket, panDistance, panDuration)
		doRUINSShakePyramidPiece(yellowResourcePocket)
	end	

	local shaft = scene:child("12coverpyramidshaft")					
	if shaft ~= nil then
		doRUINSPanPyramidPiece(shaft, panDistance, panDuration)
		doRUINSShakePyramidPiece(shaft)		
	end	
	
	local mask = scene:child("127maskovermovingcenter")					
	if mask ~= nil then
		doRUINSPanPyramidPiece(mask, panDistance, panDuration)
		doRUINSShakePyramidPiece(mask)	
	end	
	
	if scene:isTargetTriggered("96targeteyes") then  
		pyramid:animate(
			{visible=true, y=211},
			{y = 62, duration = panDuration},
			{call = function() doRUINSOpenPyramidCenterPanel() end}
		)
	else
		pyramid:animate(
			{visible=true, y=211},
			{y = 62, duration = panDuration}
		)
	end
	
	doRUINSShakePyramidPiece(pyramid)	
	
end


function doRUINSPresentYellowResource(isYellow)
	
	local yellowResource = scene:child("63yellowresource")
	yellowResource:animate({visible=true, alpha=0.0})
	
	local yellowResourcePocket = scene:child("62yellowresourceinstand")
	yellowResourcePocket:animate({visible=true, alpha=0.0})
	
	-- Show light on clear resource in stand -------------
	
	local yellowLightOn = scene:child("132yellowlightonclearresource")
	local clearResourceInStand = scene:child("115clearresourceinstand")
	local coverClearResourceInStand	= scene:child("116coverclearresourceinstand")

	yellowLightOn:setVisible(true)
	clearResourceInStand:setVisible(true)
	coverClearResourceInStand:setVisible(true)
 
	-- Fade in the god rays -----------
	local godRay1 = scene:child("117yellowgodrays")	
	godRay1:relocateAnchorPoint("center")
	
	godRay1:animate(
		{scale=0.0, visible=true, blendmode="additive"},
		{rotation = "center", scale = 1.0, duration=2000},
		--- Fade in the yellow resource and fade out the blue resource ---
		{
			{sprite = yellowResource, alpha=1.0, duration = 200},
			{sprite = yellowResourcePocket, alpha=1.0, duration = 200},
			{sprite = clearResourceInStand, visible=false},
			{sprite = coverClearResourceInStand, visible=false},
			{sprite = yellowLightOn, visible=false},
		},
		{alpha=0.0, duration=500} 
	)
end

function doRUINSPresentGreenResource()

	scene:child("10clearresourceinpyramid"):setVisible(true)

	local glowingResource = scene:child("9bluebeamwithglowingresources")
	glowingResource:animate({visible=true, alpha=0.0})
	
	-- Fade in the god rays -----------
	local blueGodRay = scene:child("118bluegodrays")	
	blueGodRay:relocateAnchorPoint("center")
	
	local greenGodRay = scene:child("119greengodrays")	
	greenGodRay:relocateAnchorPoint("center")
	greenGodRay:setScale(0.0)

	blueGodRay:animate(
		{scale=0.0, visible=true},
		{rotation = "center", scale=1.0, duration=2000},
		{delay = 2000},
		{alpha=0.0, duration=500},
		{sprite = greenGodRay, visible=true, scale=1.0, rotation = "center", duration=2000},
		--- Fade in blue beam with glowing resource ---
		{
			{sprite = glowingResource, alpha=1.0, duration = 200},
			{sprite = yellowResourcePocket, alpha=1.0, duration = 200},
			{sprite = scene:child("122yellowresourceonpyramid"), visible=false},
			{sprite = scene:child("130greenresource"), visible=true},
			{sprite = yellowLightOn, visible=false}
		},
		{delay=2000},
		{
			{sprite=glowingResource, alpha=0.0, duration=500},
			{sprite=scene:child("8redbeam"), alpha=0.0, duration=500},
			{sprite=scene:child("126beamtoinfinity"), alpha=0.0, duration=500},	
			{sprite=greenGodRay, alpha=0.0, duration=500},
			{sprite=glowingResource, alpha=0.0, duration=500},
			{sprite=scene:child("8redbeam"), alpha=0.0, duration=500},
			{sprite=scene:child("126beamtoinfinity"), alpha=0.0, duration=500},
			{sprite=scene:child("blueBeamParticle"), alpha=0.0, duration=500}
		},
		{delay=3000},
		{sprite=greenGodRay, alpha=0.0, duration=500}
	)
end

function doBobSpriteY(aSprite, aDur1Min, aDur1Max, aDur2Min, aDur2Max, aBottomMin, aBottomMax) 
	aSprite:doEvery(0, function()
		local dur1 = math.Random( aDur1Min,aDur1Max )
		local dur2 = math.Random( aDur2Min,aDur2Max )
		local bottom = math.Random( aBottomMin, aBottomMax )
		local originalY = aSprite:y()
		aSprite:animate(
			{dy = bottom - originalY, duration = dur1, style="easeInOut"},
			{dy = originalY - bottom, duration = dur2, style="easeInOut"}
		)
		return dur1 + dur2
		end
	)
end

function doBobSpriteX(aSprite, aDur1Min, aDur1Max, aDur2Min, aDur2Max, aLeftMin, aLeftMax, aRightMin, aRightMax)
	aSprite:doEvery(0, function()
		local dur1 = math.Random( aDur1Min, aDur1Max )
		local dur2 = math.Random( aDur2Min, aDur2Max )
		local left = math.Random ( aLeftMin, aLeftMax )
		local right = math.Random ( aRightMin, aRightMax )
		aSprite:animate(
			{dx = right - aRightMin, duration = dur1, style="easeInOut"},
			{dx = aRightMin - right, duration = dur2, style="easeInOut"}
		)
		return dur1 + dur2
		end
	)
end

function doBobSpriteRotate(aSprite, aDur1Min, aDur1Max, aDur2Min, aDur2Max, aRotate1Min, aRotate1Max, aRotate2Min, aRotate2Max)
	aSprite:doEvery(0, function()
		local dur1 = math.Random( aDur1Min, aDur1Max )										--Get random duration time	
		local dur2 = math.Random( aDur2Min, aDur2Max )
		aSprite:animate(
			{rotation = math.Random( aRotate1Min, aRotate1Max ), duration = dur1, style="easeInOut"},	--Rotate crate
			{rotation = math.Random( aRotate2Min, aRotate2Max ), duration = dur2, style="easeInOut"}
		)
		return dur1 + dur2	--Return excact duration time so there is no delay
		end
	)
end
		
function doBCAVEBeatGameBlingRay(aIndex)
	local rotationList = {-6.8, 24.7, -64.7, 39.3, -21.6, -48.4, 55.7, -35.1, 11}
	
	local blingRay = scene:newChildSprite({x=209, y=392, rotation=rotationList[aIndex], scale=0.10, visible=false, image="BLING_RAY", section="scenegame_5_BCAVE_AMB", name="blingRay"..aIndex, anchorpoint="bottom", pivotpoint="bottom"})
	local shooterParticle = scene:newChildSprite({class = "ParticleEffect", x = 209, y = 392, rotation=rotationList[aIndex], library = "PARTICLE_ENDGAME_SHOOTER", section = "scenegame_5_BCAVE_AMB", name = "shooterParticle"..aIndex, play = true})

	if blingRay ~= nil then
		local resource = scene:child("41openhatchresource")
		if resource ~= nil then
			blingRay:moveBehind(resource)
		end
		blingRay:animate(
			{visible=true, scale=1, duration=100}
		)
	end

end

function doBCAVEBeatGameBlingGrowGlow()
	local growGlow = scene:newChildSprite({class = "ParticleEffect", x = 209, y = 392, scale=0.75, library = "PARTICLE_ENDGAME_GROWGLOWS", section = "scenegame_5_BCAVE_AMB", name = "shooterParticle", play=true })
	local resource = scene:child("41openhatchresource")
	if growGlow ~= nil and resource ~= nil then
		growGlow:moveBehind(resource)
	end
end

function doBCAVEBeatGameBling()

	local UI = scene:child( "GAME_UI_BLUEPRINT" )
	UI:animate(
		{alpha=0.0, duration=1000}
	)


	local blingGlowDuration = 1500
	local blingGlow = scene:child("133blingglow")

	local resource = scene:child("41openhatchresource")
	if resource ~= nil then
		resource:setVisible(true)
		
		local newRayDuration = 126
		if blingGlow ~= nil then
		
			blingGlow:moveBehind(resource)
				
			blingGlow:setAlpha(0)
			blingGlow:animate(
				{play="FINAL_RESOURCE_SFX:scenegame_5_BCAVE_AMB"},
				{delay=1000},
				{visible=true, alpha=1.0, duration=blingGlowDuration},
				{delay=blingGlowDuration},
				{call=function() doBCAVEBeatGameBlingGrowGlow() end},
				{call=function() doBCAVEBeatGameBlingRay(1) end},
				{delay = newRayDuration},			
				{call=function() doBCAVEBeatGameBlingRay(2) end},
				{delay = newRayDuration},
				{call=function() doBCAVEBeatGameBlingRay(3) end},
				{delay = newRayDuration},
				{call=function() doBCAVEBeatGameBlingRay(4) end},
				{delay = newRayDuration},
				{call=function() doBCAVEBeatGameBlingRay(5) end},
				{delay = newRayDuration},
				{call=function() doBCAVEBeatGameBlingRay(6) end},
				{delay = newRayDuration},
				{call=function() doBCAVEBeatGameBlingRay(7) end},
				{delay = newRayDuration},
				{call=function() doBCAVEBeatGameBlingRay(8) end},
				{delay = newRayDuration},	
				{call=function() doBCAVEBeatGameBlingRay(9) end},
				{delay = newRayDuration}, 
				{delay = 6500}		
			)
		end
	end
end

local function doBCAVE()

	-- Hide the boombox soundwaves
	local soundWaves = scene:child("130soundwaves")
	if soundWaves ~= nil then
		soundWaves:setVisible(false)
	end
	
	--blinking start button
	local	greenstartbutton = scene:child("123ambientgreenbutton")	-- get the startbutton sprite from the scene pipeline
	local	redstartbutton = scene:child("124ambientredbutton")		-- get the startbutton sprite from the scene pipeline
	if not scene:isTargetTriggered("114targetstartbutton") then
		if scene:isTargetTriggered("8throttle") and scene:isTargetTriggered("62boatdashboard") then
			-- greenlight
			redstartbutton:setVisible( false )
			greenstartbutton:setVisible( true )
			greenstartbutton:doEvery(0, function()		-- # tells animation how long to wait before starting
				greenstartbutton:animate(
					{alpha = 0, duration = 150},
					{delay = 400},
					{alpha = 1, duration = 150}
				)
				return 700	-- this number is delay before repeating; must be larger than duration of stuff we do above
				end
			)
		else
			-- redlight
			greenstartbutton:setVisible( false )
			redstartbutton:setVisible( true )
			redstartbutton:doEvery(0, function()		-- # tells animation how long to wait before starting
				redstartbutton:animate(
					{alpha = 0, duration = 150},
					{delay = 400},
					{alpha = 1, duration = 150}
				)
				return 700	-- this number is delay before repeating; must be larger than duration of stuff we do above
				end
			)
		end
	else
		redstartbutton:setVisible( false )
		greenstartbutton:setVisible( false )
	end
	

	-- Bob left crate
	local	leftCrate = scene:child("91crate1")					-- get the crate sprite from the scene pipeline
	if leftCrate ~= nil then
		leftCrate:setPivotPoint("center")						--Set pivot point of crate to the center
			local workGloves = scene:child("44workgloves")	
			if workGloves ~= nil then
				workGloves:becomeChildOf(leftCrate)				--Make gloves a child of the crate without moving the gloves
			end
			
			local canteen = scene:child("30canteen")
			if canteen ~= nil then								--Make canteen a child of the crate without moving the gloves
				canteen:becomeChildOf(leftCrate)
			end
		
		doBobSpriteY(leftCrate, 5000, 7000, 5000, 7000, 118, 120) 
		doBobSpriteX(leftCrate, 5000, 8000, 5000, 8000, 104, 108, 108, 112)
		doBobSpriteRotate(leftCrate, 5000, 10000, 5000, 10000, 0, 3, -3, 0)
	end
	
	-- Bob right crate
	local	rightCrate = scene:child("92crate2")					-- get the crate sprite from the scene pipeline
	if rightCrate ~= nil then
		rightCrate:setPivotPoint("center")							--Set pivot point of crate to the center
			local teeth = scene:child("65chatteringteeth")	
			if teeth ~= nil then
				teeth:becomeChildOf(rightCrate)						--Make gloves a child of the crate without moving the gloves
			end

		doBobSpriteY(rightCrate, 5000, 7000, 5000, 7000, -121, -118) 
		doBobSpriteX(rightCrate, 5000, 8000, 5000, 8000, 104, 108, 108, 112)
		doBobSpriteRotate(rightCrate, 5000, 10000, 5000, 10000, 0, 3, -3, 0)
	end
	
	-- Bob bottle
	local bottle = scene:child("58bottle")					
	if bottle ~= nil then
		bottle:setPivotPoint("center")							
		doBobSpriteY(bottle, 1000, 2000, 1000, 2000, 190, 202) 
		doBobSpriteX(bottle, 2000, 5000, 2000, 4000, 44, 46, 46, 52)
		doBobSpriteRotate(bottle, 1000, 3000, 2000, 5000, 0, 8, -8, 0)
	end
	
	-- Bob jellyfish
	local jellyfish = scene:child("83jellyfish")					
	if jellyfish ~= nil then
		jellyfish:setPivotPoint("center")							
		doBobSpriteY(jellyfish, 1000, 2000, 1000, 2000, 153, 159) 
		doBobSpriteX(jellyfish, 2000, 5000, 2000, 4000, -3, -1, -1, 2)
		doBobSpriteRotate(jellyfish, 1000, 3000, 2000, 5000, 0, 4, -4, 0)
	end
	
	-- Rubber Duck
	local duck = scene:child("82rubberduck")					
	if duck ~= nil then
		duck:setPivotPoint("center")							
		doBobSpriteY(duck, 1000, 1200, 1000, 1200, 286, 292) 
		doBobSpriteX(duck, 2000, 3000, 3000, 4000, 697, 700, 700, 704)
		doBobSpriteRotate(duck, 2000, 3000, 2000, 4000, 0, 4, -4, 0)
	end
	
	-- Paper Boat
	local boat = scene:child("90paperboat")					
	if boat ~= nil then
		boat:setPivotPoint("center")							
		doBobSpriteY(boat, 1000, 1200, 1000, 1200, 232, 236) 
		doBobSpriteX(boat, 2000, 3000, 3500, 4000, 759, 763, 763, 766)
		doBobSpriteRotate(boat, 1000, 3000, 2000, 2500, 0, 4, -4, 0)
	end
	
	-- Film
	local film = scene:child("85film")					
	if film ~= nil then
		film:setPivotPoint("center")							
		doBobSpriteY(film, 1000, 1200, 1000, 1200, 338, 343) 
		doBobSpriteX(film, 2000, 3000, 3500, 4000, 679, 683, 683, 688)
		doBobSpriteRotate(film, 1000, 3000, 2000, 2500, 0, 5, -5, 0)
	end
	
	--Sparkle waters
	local	leftSparkle = scene:child("97watersparkle3")								--Get the sparkle sprint from the scene pipeline
	if leftSparkle ~= nil then
		leftSparkle:doEvery(0, function()
		local dur1 = math.Random( 800,2500 )
		local dur2 = math.Random( 800,2500 )
			leftSparkle:animate(
				{alpha = 0.8, duration = dur1, style="easeInOut"},						--fade sparkles from 80% to 20%
				{alpha = 0.2, duration = dur2, style="easeInOut"}
			)
			return dur1 + dur2															--Return excate duration time so there is no delay
			end
		)
	end
			
	local	rightSparkle = scene:child("98watersparkle2")								--Get the sparkle sprint from the scene pipeline
	if rightSparkle ~= nil then
		rightSparkle:doEvery(0, function()
		local dur1 = math.Random( 800,2500 )
		local dur2 = math.Random( 800,2500 )
			rightSparkle:animate(
				{alpha = 0.8, duration = dur1, style="easeInOut"},						--fade sparkles from 80% to 20%
				{alpha = 0.2, duration = dur2, style="easeInOut"}
			)
			return dur1 + dur2															--Return excate duration time so there is no delay
			end
		)
	end
	local	smallSparkle = scene:child("99watersparkle1")								--Get the sparkle sprint from the scene pipeline
	if smallSparkle ~= nil then
		smallSparkle:doEvery(0, function()
		local dur1 = math.Random( 800,2500 )
		local dur2 = math.Random( 800,2500 )
			smallSparkle:animate(
				{alpha = 0.8, duration = dur1, style="easeInOut"},						--fade sparkles from 80% to 20%
				{alpha = 0.2, duration = dur2, style="easeInOut"}
			)
			return dur1 + dur2															--Return excate duration time so there is no delay
			end
		)
	end
end

function doWTRFLShakePillars(aSprite, aStartX, aEndY)
	aSprite:doEvery(0, function()		-- # tells animation how long to wait before starting
		if aSprite:isVisible() and aSprite:y() > aEndY then
			aSprite:animate(
				{x = math.Random( aStartX-1,aStartX - 2 )},
				{delay=50},
				{x = math.Random( aStartX+1,aStartX + 2 )}
			)
			return 50		-- this number is delay before repeating; must be larger than duration of stuff we do above
			end
		end
	)
end

function doWTRFLTurnOffWaterfall()

	local turnOffDuration = 3000 -- The amount of time it takes for the pillars to reach the top
		
	local waterSprayParticleY = 295
	local waterSprayParticle = scene:newChildSprite({class = "ParticleEffect", x = 420, y = waterSprayParticleY, library = "PARTICLE_SPRAY", section = "scenegame_6_WTRFL_AMB", name = "waterSprayParticle", play = true})
	local rocksParticleY = 255
	local rocksParticle = scene:newChildSprite({class = "ParticleEffect", x = 420, y = rocksParticleY, library = "PARTICLE_ROCKS", section = "scenegame_6_WTRFL_AMB", name = "rocksParticle", play = true})
		
	scene:child("131ambientscrollingwater"):animate({alpha=0.0, duration=turnOffDuration})
	
	local normalButton = scene:child("100button1")
	local pushedButton = scene:child("99button2")
	if normalButton ~= nil and pushedButton ~= nil then
		normalButton:animate(
			{alpha=0, duration=200},
			{visible=false}
		)
		pushedButton:animate(
			{alpha=0, visible=true},
			{alpha=1, duration=200}
		)	
	end
	
	local waterfallTile_2 = scene:child("waterfallTile2")
	if waterfallTile_2 ~= nil then
		waterfallTile_2:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local waterfallParticle = scene:child("waterfallParticle")
	if waterfallParticle ~= nil then
		waterfallParticle:animate({alpha=0.0, duration=turnOffDuration})
		waterfallParticle:moveInFrontOf(scene:child("130waterfallwindow")) 	
	end
	
	local waterSpot1 = scene:child("waterspot1")
	if waterSpot1 ~= nil then
		waterSpot1:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local waterSpot2 = scene:child("waterspot2")
	if waterSpot2 ~= nil then
		waterSpot2:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local waterSpot3 = scene:child("waterspot3")
	if waterSpot3 ~= nil then
		waterSpot3:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local waterSpot4 = scene:child("waterspot4")
	if waterSpot4 ~= nil then
		waterSpot4:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local waterSpray1 = scene:child("waterspray1")
	if waterSpray1 ~= nil then
		waterSpray1:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local waterSpray2 = scene:child("waterspray2")
	if waterSpray2 ~= nil then
		waterSpray2:animate({alpha=0.0, duration=turnOffDuration})
	end

	local waterfallTop = scene:child("waterfalltop")
	if waterfallTop ~= nil then
		waterfallTop:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local rainbow = scene:child("132rainbow")
	if rainbow ~= nil then
		rainbow:animate({alpha=0.0, duration=turnOffDuration})
	end
	
	local pillars = scene:child("127pocketpillars")
	if pillars ~= nil then
		
		local moveAmount = 252 -- The amount of pixels the pillars move to the top
				
		if waterSprayParticle ~= nil then
			waterSprayParticle:moveBehind(pillars)
			waterSprayParticle:addInitialTime(10000)
			waterSprayParticle:animate(
				{y=waterSprayParticleY - moveAmount, duration=turnOffDuration},
				{visible=false}
			)
		end
		
		if rocksParticle ~= nil then
			rocksParticle:moveInFrontOf(pillars)
			rocksParticle:addInitialTime(10000)
			rocksParticle:animate(
				{y=rocksParticleY - moveAmount, duration=turnOffDuration},
				{visible=false}
			)
		end
		
		local startX = pillars:x()
		pillars:moveBehind(scene:child("103pillarwater"))
		pillars:animate(
			{y=267, visible=true}, 
			{call=function() doWTRFLShakePillars(pillars, startX, 35) end},
			{y=15, duration = turnOffDuration}
		)
	end
end

function doWTRFLGetCannister()
	local hook = scene:newChildSprite({x = 154, y = 115, image = "ANI_HOOK", section = "scenegame_6_WTRFL_AMB", name = "hook"})
	hook:moveBehind(scene:child("130wellwall"))
	
	local barcode = scene:newChildSprite({x = 137, y = 207, image = "ANI_BARCODEHOOK", section = "scenegame_6_WTRFL_AMB", name = "barcode"})
	barcode:setVisible(false)
	barcode:moveBehind(scene:child("130wellwall"))
	
	local crank =  scene:newChildSprite({x = 34, y = 172, image = "ANI_CRANK", section = "scenegame_6_WTRFL_AMB", name = "crank"})
	local scrollDuration = 2000
	crank:animate({startFrame=0, endFrame=9, frameTime = 200})	-- Lower the hook
	
	local wellWaterPocket = scene:child("110pocketwellwater2")
	wellWaterPocket:setVisible(false)
	
	local well3 = scene:child("74well3")
	if well3 ~= nil then
		well3:setVisible(false)
	end
	
	hook:animate(
		{y=207, duration = scrollDuration},
		{visible=false},
		{sprite=barcode, visible=true, y=115, duration = scrollDuration},
		-- Pull up the cannister --
		{sprite=crank, frame=8},
		{delay=200},
		{sprite=crank, frame=7},
		{delay=200},
		{sprite=crank, frame=6},
		{delay=200},
		{sprite=crank, frame=5},
		{delay=200},
		{sprite=crank, frame=4},
		{delay=200},
		{sprite=crank, frame=3},
		{delay=200},
		{sprite=crank, frame=2},
		{delay=200},
		{sprite=crank, frame=1},
		{delay=200},
		{sprite=crank, frame=0},
		{delay=200},
		{
			{sprite=barcode, visible=false},
			{sprite=wellWaterPocket, visible=true},
			{sprite=scene:child("0fragmentofcanister"), visible=false}
		}
	)
		
end

function doWTRFLGetWellWater()
			
	local hookWithBucket = scene:newChildSprite({x = 137, y = 115, image = "ANI_WATERHOOK_1", section = "scenegame_6_WTRFL_AMB", name = "hookWithBucket"})
	hookWithBucket:moveBehind(scene:child("130wellwall"))
	
	local hookWithWater = scene:newChildSprite({x = 137, y = 217, image = "ANI_WATERHOOK", section = "scenegame_6_WTRFL_AMB", name = "hookWithWater"})
	hookWithWater:setVisible(false)
	hookWithWater:moveBehind(scene:child("130wellwall"))
	
	local crank =  scene:newChildSprite({x = 34, y = 172, image = "ANI_CRANK", section = "scenegame_6_WTRFL_AMB", name = "crank"})
	local scrollDuration = 2000
	crank:animate({startFrame=0, endFrame=9, frameTime = 200})	
	
	local wellWaterPocket = scene:child("110pocketwellwater2")
	wellWaterPocket:setVisible(false)

	local targetWellWater = scene:child("111targetwellwater")
	if targetWellWater ~= nil then
		targetWellWater:setVisible(false)
	end
	
	hookWithBucket:animate(
		{y=217, duration = scrollDuration},
		{visible=false},
		{sprite=hookWithWater, visible=true, y=115, duration = scrollDuration},
		{sprite=crank, frame=8},
		{delay=200},
		{sprite=crank, frame=7},
		{delay=200},
		{sprite=crank, frame=6},
		{delay=200},
		{sprite=crank, frame=5},
		{delay=200},
		{sprite=crank, frame=4},
		{delay=200},
		{sprite=crank, frame=3},
		{delay=200},
		{sprite=crank, frame=2},
		{delay=200},
		{sprite=crank, frame=1},
		{delay=200},
		{sprite=crank, frame=0},
		{delay=550},
		{
			{sprite=hookWithWater, visible=false},
			{sprite=wellWaterPocket, visible=true},
		}
	)

end

local function doWTRFL()
	
	if scene:isTargetTriggered( "74well3" ) then
		if not scene:hasActionItemBeenGotten("72barcode") then
			scene:child("72barcode"):setVisible(true)
		else
			scene:child("0fragmentofcanister"):setVisible(false)
		end
	end
	
	-- Bob bottle
	local bottle = scene:child("32bottle")					
	if bottle ~= nil and bottle:isVisible() then
		bottle:setPivotPoint("center")							
		doBobSpriteY(bottle, 1000, 2000, 1000, 2000, 299, 303) 
		doBobSpriteX(bottle, 2000, 5000, 2000, 4000, 266, 267, 269, 271)
		doBobSpriteRotate(bottle, 1000, 3000, 2000, 5000, 0, 6, -6, 0)
	end
	
	-- Bob fish
	local fish = scene:child("26fish")					
	if fish ~= nil and fish:isVisible() then
		fish:setPivotPoint("center")							
		doBobSpriteY(fish, 1000, 2000, 1000, 2000, 404, 408) 
		doBobSpriteX(fish, 2000, 5000, 2000, 4000, 296, 297, 295, 297)
		doBobSpriteRotate(fish, 1000, 2000, 2000, 3000, 0, 1, -1, 0)
	end
	
	if not scene:isTargetTriggered( "100button1" ) then
		scene:child("127pocketpillars"):setVisible(false)
	end
		
	if scene:isTargetTriggered( "100button1" ) then
		scene:child("131ambientscrollingwater"):setVisible(false)
		local rainbow = scene:child("132rainbow")
		if rainbow ~= nil then
			rainbow:animate({alpha=0.0, duration=turnOffDuration})
		end
	else

		-- waterfall particle
		local waterfallParticle = scene:newChildSprite(
			{class = "ParticleEffect", x = 430, y = 265, library = "PARTICLE_WTRFL", section = "scenegame_6_WTRFL_AMB", name = "waterfallParticle", play = true})
		waterfallParticle:moveInFrontOf(scene:child("130waterfallwindow")) 	
		waterfallParticle:addInitialTime(10000)
				
		-- Tile scroll the waterfall --------------------------------------------
		local topOfWaterfall = 46 
		local waterfallTile_1 = scene:child("131ambientscrollingwater")
		local waterfallTile_2 = scene:newChildSprite(
			{x = waterfallTile_1:x(), y = topOfWaterfall - waterfallTile_1:height(), image = "131ambientscrollingwater", section = "scenegame_6_WTRFL", name = "waterfallTile2"})
				
		if waterfallTile_1 ~= nil and waterfallTile_2 ~= nil then
			waterfallTile_2:moveBehind( waterfallTile_1 )
			local panDuration = 3000
			local panDistance = waterfallTile_1:height() 
			
			waterfallTile_1:doEvery(0, function()		-- repeat this stuff
				waterfallTile_1:setY(topOfWaterfall)
				waterfallTile_1:animate(
					{dy = panDistance, duration = panDuration} 
				)

				local startY = topOfWaterfall - waterfallTile_2:height()
				waterfallTile_2:setY(startY)
				waterfallTile_2:animate(
					{dy = panDistance, duration = panDuration} 
				)
				return panDuration 	-- this number is delay before repeating; must be larger than duration of stuff we do above.
				end
			)
		end
	end


-- set up 8 predermined colors for a random value pool...
local rcolors = {{red = 0.8, green = 0.7, blue = 0.2}, {red = 0.9, green = 0.65, blue = 0.2}, {red = 0.5, green = 0.65, blue = 0.1},{red = .65, green = 0.45, blue = 0.2}, {red = 0.75, green = 0.3, blue = 0.3}, {red = 0.7, green = 0.7, blue = 0.4}, {red = 0.8, green = 0.3, blue = 0.2}, {red = 0.8, green = 0.2, blue = 0.4}}
-- "bfrandom" is the local tag for the random pool of 8 colors set up in "rcolors" above
--local bfrandom = rcolors[math.Random (1,8)]

local butterfly1 = scene:newChildSprite(
		{x = -500, y = -500, image = "Butterfly1", section = "scenegame_6_WTRFL_AMB", name = "bfly1"})
	butterfly1:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	butterfly1:setFlipImageHorizontal(true)
	butterfly1:setColor(rcolors[math.Random (1,8)])
	butterfly1:doEvery(math.Random(1000,2000), function()
		butterfly1:animate(
			-- the butterfly flys in from the left, and lands at one of 6 chosen locations
			{x = -100, y = math.Random(200,600)}, 
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 133, y = 54, duration = 5742, style = "easeInOut"},  -- comes in from left and lands
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 850, y = math.Random(-100, 400), duration = 5742, style = "easeInOut"}  -- flies off to the right
		)
		return math.Random(60000, 120000)
		end
	)
local butterfly2 = scene:newChildSprite(
		{x = -500, y = -500, image = "Butterfly1", section = "scenegame_6_WTRFL_AMB", name = "bfly1"})
	butterfly2:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	butterfly2:setFlipImageHorizontal(false)
	butterfly2:setColor(rcolors[math.Random (1,8)])
	butterfly2:doEvery(math.Random(1000,90000), function()
		butterfly2:animate(
			-- the butterfly flys in from the right, and lands at one of 6 chosen locations
			{x = 900, y = math.Random(-100,700)}, 
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 209, y = 257, duration = 5742, style = "easeInOut"},  -- comes in from righ and lands
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = -50, y = math.Random(-100, 700), duration = 5742, style = "easeInOut"} -- flies off to the left
		)
		return math.Random(60000, 120000)
		end
	)
local butterfly3 = scene:newChildSprite(
		{x = -500, y = -500, image = "Butterfly1", section = "scenegame_6_WTRFL_AMB", name = "bfly1"})
	butterfly3:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	butterfly3:setFlipImageHorizontal(true)
	butterfly3:setColor(rcolors[math.Random (1,8)])
	butterfly3:doEvery(math.Random(1000,90000), function()
		butterfly3:animate(
			-- the butterfly flys in from the left, and lands at one of 6 chosen locations
			{x = -100, y = math.Random(200,600)}, 
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 720, y = 380, duration = 5742, style = "easeInOut"},  -- comes in from left and lands
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 850, y = math.Random(-100, 400), duration = 5742, style = "easeInOut"}  -- flies off to the right
		)
		return math.Random(60000, 120000)
		end
	)
local butterfly4 = scene:newChildSprite(
		{x = -500, y = -500, image = "Butterfly1", section = "scenegame_6_WTRFL_AMB", name = "bfly1"})
	butterfly4:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	butterfly4:setFlipImageHorizontal(true)
	butterfly4:setColor(rcolors[math.Random (1,8)])
	butterfly4:doEvery(math.Random(1000,90000), function()
		butterfly4:animate(
			-- the butterfly flys in from the left, and lands at one of 6 chosen locations
			{x = -100, y = math.Random(200,600)}, 
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 387, y = 428, duration = 5742, style = "easeInOut"},  -- comes in from left and lands
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 850, y = math.Random(-100, 400), duration = 5742, style = "easeInOut"}  -- flies off to the right
		)
		return math.Random(60000, 120000)
		end
	)
local butterfly5 = scene:newChildSprite(
		{x = -500, y = -500, image = "Butterfly1", section = "scenegame_6_WTRFL_AMB", name = "bfly1"})
	butterfly5:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	butterfly5:setFlipImageHorizontal(false)
	butterfly5:setColor(rcolors[math.Random (1,8)])
	butterfly5:doEvery(math.Random(1000,90000), function()
		butterfly5:animate(
			-- the butterfly flys in from the right, and lands at one of 6 chosen locations
			{x = 900, y = math.Random(-100,700)}, 
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 404, y = 284, duration = 5742, style = "easeInOut"},  -- comes in from righ and lands
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = -50, y = math.Random(-100, 700), duration = 5742, style = "easeInOut"} -- flies off to the left
		)
		return math.Random(60000, 120000)
		end
	)
local butterfly6 = scene:newChildSprite(
		{x = -500, y = -500, image = "Butterfly1", section = "scenegame_6_WTRFL_AMB", name = "bfly1"})
	butterfly6:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	butterfly6:setFlipImageHorizontal(false)
	butterfly6:setColor(rcolors[math.Random (1,8)])
	butterfly6:doEvery(math.Random(1000,90000), function()
		butterfly6:animate(
			-- the butterfly flys in from the right, and lands at one of 6 chosen locations
			{x = 900, y = math.Random(-100,700)}, 
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 696, y = 111, duration = 5742, style = "easeInOut"},  -- comes in from righ and lands
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = -50, y = math.Random(-100, 700), duration = 5742, style = "easeInOut"} -- flies off to the left
		)
		return math.Random(60000, 120000)
		end
	)
local butterfly7 = scene:newChildSprite(
		{x = -500, y = -500, image = "Butterfly1", section = "scenegame_6_WTRFL_AMB", name = "bfly1"})
	butterfly7:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	butterfly7:setFlipImageHorizontal(true)
	butterfly7:setColor(rcolors[math.Random (1,8)])
	butterfly7:doEvery(math.Random(1000,90000), function()
		butterfly7:animate(
			-- the butterfly flys in from the left, and lands at one of 6 chosen locations
			{x = -100, y = math.Random(200,600)}, 
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 617, y = 275, duration = 5742, style = "easeInOut"},  -- comes in from left and lands
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 29, endFrame = 39, frameTime = math.Random(40,110), duration = 1800},  -- flaps wings once slowly
			{delay = math.Random(100, 1500)},
			{startFrame = 0, endFrame = 28, loops = 6, frameTime = 33, x = 850, y = math.Random(-100, 400), duration = 5742, style = "easeInOut"}  -- flies off to the right
		)
		return math.Random(60000, 120000)
		end
	)

	if not scene:isTargetTriggered( "100button1" ) then
	-- pyro particle waterspots1 on waterfall
		local waterspot1 = scene:newChildSprite({class = "ParticleEffect", x = 455, y = 115, library = "PARTICLE_WATERSPOTSLEFT",  section = "scenegame_6_WTRFL_AMB", name = "waterspot1", play = true})
		waterspot1:moveInFrontOf( scene:child("130waterfallwindow") )	
		waterspot1:addInitialTime(10000)
		
	-- pyro particle waterspots2 on waterfall
		local waterspot2 = scene:newChildSprite({class = "ParticleEffect", x = 400, y = 183, library = "PARTICLE_WATERSPOTSRIGHT",  section = "scenegame_6_WTRFL_AMB", name = "waterspot2", play = true})
		waterspot2:moveInFrontOf( scene:child("130waterfallwindow") )	
		waterspot2:addInitialTime(10000)
			
	-- pyro particle waterspots3 on waterfall
		local waterspot3 = scene:newChildSprite({class = "ParticleEffect", x = 459, y = 209, library = "PARTICLE_WATERSPOTSLEFT",  section = "scenegame_6_WTRFL_AMB", name = "waterspot3", play = true})
		waterspot3:moveInFrontOf( scene:child("130waterfallwindow") )	
		waterspot3:addInitialTime(10000)
			
	-- pyro particle waterspots4 on waterfall
		local waterspot4 = scene:newChildSprite({class = "ParticleEffect", x = 396, y = 105, library = "PARTICLE_WATERSPOTSRIGHT",  section = "scenegame_6_WTRFL_AMB", name = "waterspot4", play = true})
		waterspot4:moveInFrontOf( scene:child("130waterfallwindow") )	
		waterspot4:addInitialTime(10000)
			
	-- pyro particle left waterspray edge on waterfall
		local waterspray1 = scene:newChildSprite({class = "ParticleEffect", x = 385, y = 156, library = "PARTICLE_WATEREDGESPRAY",  section = "scenegame_6_WTRFL_AMB", name = "waterspray1", play = true})
		waterspray1:moveInFrontOf( scene:child("130waterfallwindow") )	
		waterspray1:addInitialTime(10000)
			
	-- pyro particle right waterspray edge on waterfall
		local waterspray2 = scene:newChildSprite({class = "ParticleEffect", x = 479, y = 164, library = "PARTICLE_WATEREDGESPRAY",  section = "scenegame_6_WTRFL_AMB", name = "waterspray2", play = true})
		waterspray2:moveInFrontOf( scene:child("130waterfallwindow") )	
		waterspray2:addInitialTime(10000)
		
	-- pyro particle WATERFALL TOP
		local waterfalltop = scene:newChildSprite({class = "ParticleEffect", x = 431, y = 60, library = "PARTICLE_WATERFALLTOP",  section = "scenegame_6_WTRFL_AMB", name = "waterfalltop", play = true})
			waterfalltop:moveInFrontOf( scene:child("130waterfallwindow") )	
			waterfalltop:addInitialTime(10000)
	end
	
-- pyro particle RIPPLES NEAR WATERFALL
	local ripple1 = scene:newChildSprite({class = "ParticleEffect", x = 430, y = 265, library = "PARTICLE_RIPPLE1",  section = "scenegame_6_WTRFL_AMB", name = "ripple1", play = true})
		ripple1:moveBehind( scene:child("95stonesteps") )	
		ripple1:addInitialTime(10000)	
		
-- pyro particle RIPPLES IN CREEK
	local ripple2 = scene:newChildSprite({class = "ParticleEffect", x = 300, y = 330, library = "PARTICLE_RIPPLE2",  section = "scenegame_6_WTRFL_AMB", name = "ripple2", play = true})
		ripple2:moveBehind( scene:child("133creekpocket") )	
		ripple2:addInitialTime(10000)			
	
end

function doINVENPowerComputer()
	local scannerReady = scene:child("119scannerready")
	if scannerReady ~= nil then
		if scene:isTargetTriggered("8poweredcomputer") then 
			scene:child("120serialcableconnected"):setVisible(false)
			scannerReady:setVisible(true)
			scannerReady:doEvery(500, function()		-- # tells animation how long to wait before starting
				scannerReady:animate(
					{alpha = 0, duration = 100},
					{delay = 1000},
					{alpha = 1, duration = 100}
				)
				return 1800		-- this number is delay before repeating; must be larger than duration of stuff we do above
				end
			)
		end
	end
end

function doINVENSerialCableConnected()
	local serialCable = scene:child("120serialcableconnected")
	
	local scannerReady = scene:child("119scannerready")
	if scannerReady ~= nil then
		scannerReady:setVisible(false)
	end
		
	if serialCable ~= nil then
		if scene:isTargetTriggered("9computer") and not scene:isTargetTriggered("5serialcable") then
		    serialCable:setVisible(true)
			serialCable:doEvery(500, function()		-- # tells animation how long to wait before starting
				serialCable:animate(
					{alpha = 0, duration = 100},
					{delay = 1000},
					{alpha = 1, duration = 100}
				)
				return 1800		-- this number is delay before repeating; must be larger than duration of stuff we do above
				end
			)
		end
	end
end

function doINVENBlueScreen()
	local bluescreen = scene:child("8poweredcomputer")
	if bluescreen ~= nil then
		bluescreen:setVisible(true)
	end
end

local function doINVEN()
local	fly = scene:child("111fly")	
	if fly ~= nil then
		fly:moveBehind( scene:child("GAME_UI_BLUEPRINT") )							--move in front of everything
		fly:doEvery(0, function()		
			fly:animate(
				{dx = 10, dy = 10, duration = 150}, --changed these all from 100 to 120 to try and slow the thing down
				{dx = -8, dy = 6, duration = 150}, 
				{dx = 9, dy = -12, duration = 150},
				{dx = -12, dy = 9, duration = 150}, 
				{dx = 4, dy = 3, duration = 150}, 
				{dx = -8, dy = 3, duration = 150}, 
				{dx = 6, dy = -11, duration = 150}, 
				{x = math.Random (20, 760), y = math.Random (30, 520), duration = math.Random( 900, 1100), style="easeInOut" },  -- move out big and random, changed dur from 300, 1000 to try and slow it down.
				{delay = math.Random (200, 400)}
			)
			return 2700  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	if scene:isTargetTriggered("8poweredcomputer") then
		doINVENPowerComputer() 
	end
	
	if scene:isTargetTriggered("9computer") then
		doINVENBlueScreen() -- CHAPTER 8
	end
		
	if scene:isTargetTriggered("7computerch9") then
		doINVENSerialCableConnected()
	end
end


local function doSCLAB()

	-- restore
	doSCLABBinderStuff( (scene:isTargetTriggered( "110clockwithouthands" ) and scene:isTargetTriggered( "113brokenwires" )) )
	
	if scene:isTargetTriggered( "49lightswitchoff" ) and not scene:isTargetTriggered( "66h2obeaker" ) then
		doSCLABRedMode( true )
	end
	
	scene:child("148pocketphotoofstonecarvings"):setVisible(false)
		
	if scene:isTargetTriggered( "89centrifugeopen" ) then
		scene:child("87centrifugeclosed"):setVisible(true)
	else
		scene:child("87centrifugeclosed"):setVisible(false)
	end
	
	if scene:isTargetTriggered("67fullislandweatherposter") and not scene:isTargetTriggered("113brokenwires") then 
		scene:newChildSprite({class = "ParticleEffect", x = 427, y = 119, library = "PARTICLE_SCLAB_SPARKS",  section = "scenegame_8_SCLAB_AMB", name = "wireSparks", play = true})
	end
	
	local hintArrow = scene:child("109hint")
	if hintArrow ~= nil and hintArrow:isVisible() then
		hintArrow:setPivotPoint("center")
		hintArrow:doEvery(0, function()		-- repeat this stuff
			hintArrow:animate(
				{rotation = 0},
				{rotation = 360, duration = 60000}
			)
			return 60000		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
end

function doSCLABTurnOffWireSparks()
	local wireSparks = scene:child("wireSparks")
	if wireSparks ~= nil then
		wireSparks:setVisible(false)
	end
end

function doSCLABOpenCannisterPart_1()
	local leftPoof = scene:newChildSprite({class = "ParticleEffect", x = 667, y = 412, library = "PARTICLE_SCLAB_LPOOF",  section = "scenegame_8_SCLAB_AMB", name = "leftPoof", play = true})
	local rightPoof = scene:newChildSprite({class = "ParticleEffect", x = 667, y = 412, library = "PARTICLE_SCLAB_RPOOF",  section = "scenegame_8_SCLAB_AMB", name = "rightPoof", play = true})
end

function doSCLABOpenCannisterPart_2()
	local rightPoof2 = scene:newChildSprite({class = "ParticleEffect", x = 667, y = 412, library = "PARTICLE_SCLAB_RPOOF",  section = "scenegame_8_SCLAB_AMB", name = "rightPoof2", play = true})
	local smoulder = scene:newChildSprite({class = "ParticleEffect", x = 581, y = 468, library = "PARTICLE_SCLAB_SMOULDER",  section = "scenegame_8_SCLAB_AMB", name = "smoulder", play = true})
end

function doSCLABRedMode( red )
 
	if red then
		print( "doTEMPX doSCLABRedMode ON" )
		scene:animate(
			{command = "RedModeON"}
		)
	else
		print( "doTEMPX doSCLABRedMode OFF" )
		scene:animate(
			{command = "RedModeOFF"}
		)
	end
end

function doSCLABShakeHorizontally(aSprite, aStartX)
	aSprite:animate(
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1},
		{delay=50},
		{x = aStartX - 1},
		{delay=50},
		{x = aStartX + 1}
	)
end

function doSCLABShakeCentrifuge()
	local centrifuge = scene:child("87centrifugeclosed")
	local centrifugeStartX = centrifuge:x()
	
	centrifuge:setVisible(true)
	
	scene:child("89centrifugeopen"):animate({visible=false, x=-500})
	scene:child("88cobweb"):animate({visible=false, x=-500})
	
	scene:child("15razorblade"):becomeChildOf(centrifuge)
			
	local precariousBook = scene:child("32booksprecarious")
	local precariousBookX = precariousBook:x()
	
	local bookFallAnim = scene:newChildSprite({x=170, y=14, image="SCLAB_BookFallAnimStrip", section="scenegame_8_SCLAB_AMB", name="bookFallAnim"})
	bookFallAnim:setVisible(false)
	
	
	-- Shake the centrifuge and the book (Use the binder as a controller)
	local binder = scene:child("107researchbinder")
	binder:animate(
		{call = function() doSCLABShakeHorizontally(centrifuge, centrifugeStartX) end},
		{delay = 1000},
	    {call = function() doSCLABShakeHorizontally(centrifuge, centrifugeStartX) end},
	    {delay = 1000},
	    {call = function() doSCLABShakeHorizontally(centrifuge, centrifugeStartX) end},
		{call = function() doSCLABShakeHorizontally(precariousBook, precariousBookX, true) end},
		{delay = 1000},
	    {call = function() doSCLABShakeHorizontally(centrifuge, centrifugeStartX) end},
		{call = function() doSCLABShakeHorizontally(precariousBook, precariousBookX) end},
		{delay = 500},
		{sprite = centrifuge, x=centrifugeStartX},
		{sprite = precariousBook, x=-500, visible=false}, -- Move the book off the screen
		{play = "SFX_BookFall:scenegame_8_SCLAB_AMB"},
		{sprite = bookFallAnim, visible=true, startFrame = 0, endFrame = 2, frameTime = 125},
		{delay = 400},
		{sprite = bookFallAnim, visible=false},
		{sprite = scene:child("148pocketphotoofstonecarvings"), visible=true},
		{delay = 1300},
		{sprite = scene:child("148pocketphotoofstonecarvings"), visible=false}
	)
		
end

function doSCLABBinderStuff( show )
	scene:child("105lockerdoorclosed"):setVisible(not show)
	scene:child("104lockerdooropen"):setVisible(show)
	if scene:isTargetTriggered( "107researchbinder" ) then
		show = false
	end
	scene:child("107researchbinder"):setVisible(show)
end

function doSCLABBOpenLocker()
	scene:animate(
		{delay=1000},
		{play = "SFX_LockerOpen:scenegame_8_SCLAB_AMB"},
		{call=function() doSCLABBinderStuff(true) end}
	)
end

local function doDOCKS()
	doGullSFX()
	doWaveSFX()
	
	-- Set up the swallows
	for i=1, 20 do
		doFlySwallow("swallow_diver_"..i, 11000, 14000, 55000, 0.3, 0.5, "scenegame_9_DOCKS_AMB", true)
		scene:child("swallow_diver_"..i):moveInFrontOf( scene:child("118ambient") )
	end

	-- only create the spark effect if the solar panel has been tirggered and the exposed wires have not been triggered
	if scene:isTargetTriggered("73solarpanel") and not scene:isTargetTriggered("72exposedwires") then
		scene:newChildSprite({class = "ParticleEffect", x = 534, y = 136, library = "PARTICLE_SPARK", section = "scenegame_9_DOCKS_AMB", name = "spark", play = true})
	end
	
	local	topClouds = scene:child("118ambient")	-- get the cloud sprite from the scene pipeline
	if topClouds ~= nil then
		topClouds:doEvery(0, function()		-- repeat this stuff
			topClouds:animate(
				{x = 810, duration = 80000},	-- move clouds left to right
				{x = 0-173}					
			)
			return 81000		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
	
	local	lowerClouds = scene:child("119ambient")	-- get the cloud sprite from the scene pipeline
	if lowerClouds ~= nil then
		lowerClouds:doEvery(0, function()		-- repeat this stuff
			lowerClouds:animate(
				{x = 810, duration = 88000},	-- move clouds left to right
				{x = 0-106}
			)
			return 88500		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
		
	local	middleClouds = scene:child("120ambient")	-- get the cloud sprite from the scene pipeline
	if middleClouds ~= nil then
		middleClouds:doEvery(0, function()		-- repeat this stuff
			middleClouds:animate(
				{x = 810, duration = 95000},	-- move clouds left to right
				{x = 0-297}
			)
			return 96000		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
	
	local	bottomClouds = scene:child("121ambient")	-- get the cloud sprite from the scene pipeline
	if bottomClouds ~= nil then
		bottomClouds:doEvery(0, function()		-- repeat this stuff
			bottomClouds:animate(
				{x = 810, duration = 110000},	-- move clouds left to right
				{x = 0-293}
			)
			return 	111000	-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
end


local function doTEMPX()

--set up looping ambients here

   	local	TropicalPlantLeft = scene:child("32tropicalplantleft")	-- get the plant sprite from the scene pipeline
	if TropicalPlantLeft ~= nil then
		TropicalPlantLeft:setPivotPoint("bottom")
		
		local Chameleon = scene:child("58lizard")	
		if Chameleon ~= nil then
			Chameleon:becomeChildOf(TropicalPlantLeft)				--Make Chameleon a child of TropicalPlantLeft
		end
			
		local Grasshopper = scene:child("14grasshopper")
		if Grasshopper ~= nil then									--Make Grasshopper a child of TropicalPlantLeft
			Grasshopper:becomeChildOf(TropicalPlantLeft)
		end
			
		local GrasshopperP = scene:child("13leafovergrasshopper")
		if GrasshopperP ~= nil then									--Make Grasshopper Pocket a child of TropicalPlantLeft
			GrasshopperP:becomeChildOf(TropicalPlantLeft)
		end
			
		local GreenLizard = scene:child("27lizard")	
		if GreenLizard ~= nil then
			GreenLizard:becomeChildOf(TropicalPlantLeft)			--Make GreenLizard a child of TropicalPlantLeft
		end
			
		local GreenLizardP = scene:child("26leafoverlizard")	
		if GreenLizardP ~= nil then
			GreenLizardP:becomeChildOf(TropicalPlantLeft)			--Make GreenLizard Pocket a child of TropicalPlantLeft
		end
			
		local BirdOfParadise = scene:child("86birdofparadise")	
		if BirdOfParadise ~= nil then
			BirdOfParadise:becomeChildOf(TropicalPlantLeft)			--Make BirdOfParadise a child of TropicalPlantLeft
		end
			
		
		TropicalPlantLeft:doEvery(0, function()						-- repeat this stuff
			local dur1 = math.Random( 2000,3000 )
--			local dur2 = math.Random( 3000,4000 )
--			local dur3 = math.Random( 3000,4000 )
--			local dur4 = math.Random( 3000,4000 )
			local totalDuration = dur1 --+ dur2 + dur3 + dur4
			TropicalPlantLeft:animate(
				{skewx = math.Random( -0.025, 0.02 ), duration = dur1, style="easeInOut" }--,	-- math.Random makes a random number between lower and upper INTEGER
--				{skewx = math.Random( 0.02, -0.02 ), duration = dur2, style="easeIn" },
--				{skewx = math.Random( -0.03,  0.03 ), duration = dur3, style="easeOut" },
--				{skewx = math.Random( 0.02,  -0.01 ), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

   	local	LeafyPlantLeft = scene:child("31leftleafplant")		-- get the plant sprite from the scene pipeline
	if LeafyPlantLeft ~= nil then
		LeafyPlantLeft:setPivotPoint("bottom")
		
		LeafyPlantLeft:doEvery(0, function()						-- repeat this stuff
			local dur1 = math.Random( 2000,2500 )
			local totalDuration = dur1
			LeafyPlantLeft:animate(
				{skewx = math.Random( 0.02, -0.02 ), duration = dur1, style="easeInOut" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
   	local	TropicalPlantRight = scene:child("41tropicalplantright")	-- get the plant sprite from the scene pipeline
	if TropicalPlantRight ~= nil then
		TropicalPlantRight:setPivotPoint("bottom")
		
			local Corn = scene:child("42corn")	
		if Corn ~= nil then
			Corn:becomeChildOf(TropicalPlantRight)						--Make Corn a child of TropicalPlantLeft
		end
		
		local PlantPocket = scene:newChildSprite(
		{x = 702, y= 232, image = "41tropicalplantright", section = "scenegame_10_TEMPX", name = "PlantPocket"})
		if PlantPocket ~= nil then
			PlantPocket:becomeChildOf(TropicalPlantRight)				--Make plant pocket a child of TropicalPlantLeft to cover Corn
		end
			
		local Caterpillar = scene:child("8caterpillar")	
		if Caterpillar ~= nil then
			Caterpillar:becomeChildOf(TropicalPlantRight)				--Make Caterpillar a child of TropicalPlantLeft
		end
		
		TropicalPlantRight:doEvery(0, function()						-- repeat this stuff
			local dur1 = math.Random( 2500,3000 )
			local totalDuration = dur1
			TropicalPlantRight:animate(
				{skewx = math.Random( -0.025, 0.02 ), duration = dur1, style="easeInOut" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	 	local	TopRightIvy = scene:child("35toprightivy")			-- get the plant sprite from the scene pipeline
		if TopRightIvy ~= nil then
			TopRightIvy:setPivotPoint("top")
		end
			
		local Parrot = scene:child("6parrot")
		if Parrot ~= nil then										--Make parrot a child of the ivy
			Parrot:becomeChildOf(TopRightIvy)
		end
		
		local Emerald = scene:child("17gem")
		if Emerald  ~= nil then										--Make emerald  a child of the ivy
			Emerald :becomeChildOf(TopRightIvy)
		end
				
		TopRightIvy:doEvery(0, function()							-- repeat this stuff
			local dur1 = math.Random( 1500,2000 )
			local totalDuration = dur1
			TopRightIvy:animate(
				{skewx = math.Random( -0.03, 0.03 ), duration = dur1, style="easeInOut" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
		
	if scene:isTargetTriggered("43hamradiowires") and not scene:isTargetTriggered("44hamradiowiresstripped") then
		doTEMPXRadioWires()
	end

--hide all these items that are shown in animation sequences (?)

		scene:child( "140pocketsoundwaves" ):setVisible( false )			--don't show now
		scene:child( "73jawopen" ):setVisible( false )						--don't show now
		scene:child( "72leverswitch" ):setVisible( false )					--don't show now
		scene:child( "135pocketleverswitchinmouth" ):setVisible( false )	--don't show now
		
		
--restore scene items based on triggered targets

	if scene:isTargetTriggered( "78statueheadpiece3" ) then			--Check if 3 of 3 has been triggered
		doTEMPXHeadDrop( false )									--Restore Head to dropped position
		doTEMPXpaperspew( false )									--Restore paper to spewed position
	elseif scene:isTargetTriggered( "79statueheadpiece 2" ) then	--Check if 2 of 3 has been triggered
		doTEMPXHeadRaise( false )									--Restore head to raised position
	elseif scene:isTargetTriggered( "80statueheadpiece1" ) then		--Check if 1 of 3 has been triggered
		print( "doTEMPX doing stuff for isTargetTriggered 80statueheadpiece1" )
		doTEMPXhammerfall( false )									--Restore hammer to fallen position
		doTEMPXHeadDrop( false )									--Restore Head to dropped position
	end

		if scene:isTargetTriggered("78statueheadpiece3") then
			scene:child("96suicasesmashed"):setVisible(true)
		else
			scene:child( "96suicasesmashed" ):setVisible( false )
		end

	scene:child( "95documentsfrombetasite" ):setVisible( false )		--don't show now	
					
--	if scene:isTargetTriggered( "80statueheadpiece1" ) then			-- moot…
--		doTEMPXhammerfall( false )									--Restore hammer to fallen position
--	end

	if scene:isTargetTriggered( "79statueheadpiece 2" ) then				--Checking if 2 of 3 has been for jaw drop (need to restore to this if 3 of 3 has been triggered, too)
		print( "doTEMPX doing stuff for isTargetTriggered 79statueheadpiece 2" )		
		scene:child( "70jawclosed" ):setVisible( false )	
		scene:child( "73jawopen" ):setVisible( true )						--restore to jaw open\
		if scene:hasActionItemBeenGotten( "72leverswitch" ) then
			scene:child( "72leverswitch" ):setVisible( false )					
			scene:child( "135pocketleverswitchinmouth" ):setVisible( false )
		else
			scene:child( "72leverswitch" ):setVisible( true )		
			scene:child( "135pocketleverswitchinmouth" ):setVisible( true )					
		end
		
	else
		print( "doTEMPX doing stuff for NOT isTargetTriggered 79statueheadpiece 2" )
		scene:child( "73jawopen" ):setVisible( false )						--if not triggered, don't show yet
		scene:child( "72leverswitch" ):setVisible( false )					--if not triggered, don't show yet
		scene:child( "135pocketleverswitchinmouth" ):setVisible( false )	--if not triggered, don't show yet
	end
end

function doTEMPXhammerfall( moveHammer )								--make hammer fall, called from GameProcess.lua
	print( "doTEMPXhammerfall" )
	local hammer = scene:child( "117hammer" )							--hammer as child
	local hammerP = scene:child( "116shadowonhammer" )					--hammer shadow as child
	local newY = 400													--y destination coordinate
	local newX = 364													--x destination coordinate--moves a little to left
	if moveHammer then													--when target triggered…
		local dur = 500													--for duration of…
		hammer:animate(		
			{ rotation=-4, visible=true},
			{ delay = 100},
			{ rotation=0 },
			{ delay = 100},
			{ rotation=4 },
			{ delay = 100},
			{ rotation=0},
			{ delay = 100},	
			{ rotation = -4},
			{ delay = 100},
			{ rotation = 0},
			{ delay = 0 },							
			{ y = newY, x = newX, duration = dur, style="easeIn" }
		)
		hammerP:animate(	
			{ rotation=-4, visible=true},
			{ delay = 100},
			{ rotation=0 },
			{ delay = 100},
			{ rotation=4 },
			{ delay = 100},
			{ rotation=0},
			{ delay = 100},	
			{ rotation = -4},
			{ delay = 100},
			{ rotation = 0},
			{ delay = 0 },	
			{ y = newY, x = newX, duration = dur, style="easeIn" }
		)
	else																--else
		hammer:setY( newY )												--hammer down
		hammer:setX( newX )												--hammer down
		hammerP:setY( newY )											--hammer shadow down
		hammerP:setX( newX )											--hammer shadow down
	end
end

function doTEMPXHeadDrop( moveHeadDown )								--make head drop, called from GameProcess.lua
	print( "doTEMPXHeadDrop" )
	local head = scene:child( "101leftstatueheadopen" )					--head as child
	local newX = -1														--x destination coordinate
	local newY = 121													--y destination coordinate

	if moveHeadDown  then												--when target triggered…
		local dur = 1000												--for duration of…
		head:animate(													--move head to destination
			{ y = newY, x = newX, duration = dur, style="easeIn" }
		)
	else																--else
		head:setX( newX )												--head down
		head:setY( newY )												--head down
	end
end

function doTEMPXUseStatueHeadPiece()
	local headPiece = scene:child("75statueheadpiece1")
	if headPiece ~= nil then
		scene:animate(
			{sprite = headPiece, visible=true, alpha=0.0},
			{sprite = headPiece, alpha=1.0, duration=500},
			{delay=1000},
			{call = function() doTEMPXHeadDrop(true) end},
			{delay=1500},
			{sprite=scene:child("115closeddoorleft"), visible=false},
			{call = function() doTEMPXhammerfall( true ) end}
		)
	end
end
	
function doTEMPXCrushSuitcase()
	local headPiece = scene:child("77statueheadpiece3")
	if headPiece ~= nil then
		scene:animate(
			{sprite = headPiece, visible=true, alpha=0.0},
			{sprite = headPiece, alpha=1.0, duration=500},
			{delay=300},
			{call = function() doTEMPXHeadDrop(true) end},
			{delay=100},
			{call=function() doTEMPXpaperspew(true) end}
		)
	end
end
	
function doTEMPXHeadRaise( moveHeadUp )									--raise head
	print( "doTEMPXHeadRaise" )
	local head = scene:child( "101leftstatueheadopen" )					--head as child
	local newX = 1														--x destination coordinate
	local newY = 99														--y destination coordinate

	if moveHeadUp  then													--when target triggered…, called from GameProcess.lua
		local dur = 1000												--for duration…
		head:animate(													--move head to destination
			{ y = newY, x = newX, duration = dur, style="easeIn" }
		)
	else																--else restore instantly, called from Ambients.lua doTEMPX()
		head:setX( newX )												--head up
		head:setY( newY )												--head up
	end
end

function doTEMPXJawDrop( moveJawDown )										--drop jaw, called from GameProcess.lua
	print( "doTEMPXJawDrop" )
	local jaw = scene:child( "70jawclosed" )								--closed jaw as child
	local newX = 379														--x destination coordinate
	local newY = 91															--y destination coordinate

	if moveJawDown  then													--when target triggered…
		scene:animate(														--drop jaw animation
			{sprite = jaw, y = newY, x = newX, duration = 1000, style="easeIn" },			--move closed jaw down
			{sprite = scene:child( "73jawopen" ), visible = true},								--show open jaw
			{sprite = scene:child( "72leverswitch" ),  visible = true},			--show pocket covering stone weight action item
			{sprite = scene:child( "135pocketleverswitchinmouth" ), visible = true},		--show stone weight action item
			{sprite = jaw, alpha=0.0, duration = 600 },						--hide closed jaw
			{delay=600},
			{sprite = jaw, visible=false}									--hide closed jaw
		)
	else																	--else
		scene:child( "70jawclosed" ):setVisible( false )					--hide closed jaw
		scene:child( "73jawopen" ):setVisible( true )						--show open jaw
		scene:child( "72leverswitch" ):setVisible( true )					--show stone weight action item--click takes it away?
		scene:child( "135pocketleverswitchinmouth" ):setVisible( true )		--show pocket covering stone weight action item--data 1 clears it?
	end
end


-- crush briefcase and paper spew

function doTEMPXpaperspew( CrushBriefcase )										
	print( "doTEMPXpaperspew" )
	local briefcase = scene:child( "94suitcasewhole" )
	local crushedBriefcase = scene:child( "96suicasesmashed" )
	local papers = scene:child( "95documentsfrombetasite" )	
	papers:setAnchorPoint({x=20, y=0})
	papers:setScale(0.0)
	if CrushBriefcase  then															-- when target triggered…
		scene:animate(																-- crush and spew animation
			{delay = 1000},															-- wait until head is [almost] all the way down
			{sprite = scene:child( "96suicasesmashed" ), visible = true},			-- show smashed briefcase					
			{sprite = scene:child( "94suitcasewhole" ),  visible = false},			-- hide whole briefcase
			{sprite = scene:child( "95documentsfrombetasite" ), visible = true, scale = 1.0, duration = 100, style="easeIn"} -- grow papers out of smashed case												
		)
	else																	--else
		scene:child( "96suicasesmashed" ):setVisible( false )						--hide smashed briefcase
		scene:child( "94suitcasewhole" ):setVisible( true )							--show whole briefcase
		scene:child( "95documentsfrombetasite" ):setVisible( false )				--hide papers
	end
end

function doTEMPXRadioWires()
	scene:newChildSprite({class = "ParticleEffect", x = 650, y = 337, library = "PARTICLE_TEMPX_WIRES",  section = "scenegame_10_TEMPX_AMB", name = "radioWires", play = true})
end

local function doPLANE()

-- pyro particle smoke
	local smoke = scene:newChildSprite({class = "ParticleEffect", x = 602, y = 262, library = "PARTICLE_SMOKE",  section = "scenegame_11_PLANE_AMB", name = "smoke", play = true})
	smoke:moveBehind( scene:child("126pocketsmoke") )
	
--[[
-- plane wreckage smoke

local	smoke = scene:child("smoke")	-- get the smoke pyro 
	if smoke ~= nil then
		smoke:doEvery(0, function()		-- repeat this stuff
			smoke:animate(
				{class = "ParticleEffect", x = 602, y = 262, library = "PARTICLE_SMOKE",  section = "resource_11_PLANE_Amb", name = "smoke", play = true}			
			)
			end
		)
	end
--]]	

-- mosquitos buzzing about at random

local anim1 = scene:newChildSprite(
		{x = -500, y = -500, image = "Mosquito", section = "scenegame_11_PLANE_AMB", name = "mosquito1"})
	anim1:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	anim1:setFlipImageHorizontal(true)
	anim1:doEvery(3000, function()
		anim1:animate(
			-- the mosquito zips across from left to the right
			{x = -100, y = math.Random(200,600), scale = math.Random(0.2 , 1.2)},   -- the scale is randomized with equal aspect ratio
			{startFrame = 0, loops = 4, frameTime = math.Random(22,50), x = 900, y = math.Random(100,700), duration = math.Random(3500,9500)}
		)
		return math.Random(18000, 32000)
		end
	)
local anim2 = scene:newChildSprite(
		{x = -500, y = -500, image = "Mosquito", section = "scenegame_11_PLANE_AMB", name = "mosquito2"})
	anim2:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	anim2:setFlipImageHorizontal(true)
	anim2:doEvery(5000, function()
		anim2:animate(
			-- the mosquito zips across from left to the right
			{x = -100, y = math.Random(200,600), scale = math.Random(0.2 , 1.2)}, 
			{startFrame = 0, loops = 4, frameTime = math.Random(22,50), x = 900, y = math.Random(100,700), duration = math.Random(3500,9500)}
		)
		return math.Random(22000, 38000)
		end
	)
local anim3 = scene:newChildSprite(
		{x = -500, y = -500, image = "Mosquito", section = "scenegame_11_PLANE_AMB", name = "mosquito3"})
	anim3:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	anim3:setFlipImageHorizontal(true)
	anim3:doEvery(6000, function()
		anim3:animate(
			-- the mosquito zips across from left to the right
			{x = -100, y = math.Random(200,600), scale = math.Random(0.2 , 1.2)}, 
			{startFrame = 0, loops = 4, frameTime = math.Random(22,50), x = 900, y = math.Random(100,700), duration = math.Random(3500,9500)}
		)
		return math.Random(23000, 30000)
		end
	)
local anim4 = scene:newChildSprite(
		{x = -500, y = -500, image = "Mosquito", section = "scenegame_11_PLANE_AMB", name = "mosquito4"})
	anim4:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	anim4:setFlipImageHorizontal(false)
	anim4:doEvery(7000, function()
		anim4:animate(
			-- the mosquito flies upward from bottom
			{x = math.Random(500,800), y = 640, scale = math.Random(0.2 , 1.2)}, 
			{startFrame = 0, loops = 4, frameTime = math.Random(22,50), x = math.Random (-200,450), y = -40, duration = math.Random(7500,9500)}
		)
		return math.Random(18000, 40000)
		end
	)

local anim5 = scene:newChildSprite(
		{x = 900, y = 400, image = "Mosquito", section = "scenegame_11_PLANE_AMB", name = "mosquito5"})
	anim5:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	anim5:setFlipImageHorizontal(true)
	anim5:animate({startFrame = 0, loops = "forever", frameTime = math.Random(22,50)})	-- Start this outside doEvery
	anim5:doEvery(3000, function()
		-- Run the animation continuously (simultaneously with the motion)
		anim5:animate(
			-- the mosquito zips in from the left, hovers a bit, then zips off to the rightt
			{x = -40, y = math.Random (100,500), scale = math.Random(0.2 , 1.2)}, 
			{x = math.Random(200,600), y = math.Random(400,500), duration = math.Random(1500,1800), style = "easeInOut"},
			{delay = 1000},
			{x = 840, y = math.Random(100,700), duration = math.Random(1500,1800), style = "easeInOut"}
		)
		return math.Random(16000, 32000)
		end
	)
local anim6 = scene:newChildSprite(
		{x = -500, y = -500, image = "Mosquito", section = "scenegame_11_PLANE_AMB", name = "mosquito6"})
	anim6:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	anim6:setFlipImageHorizontal(false)		
	anim6:animate({startFrame = 0, loops = "forever", frametime = math.Random(22,50)})
	anim6:doEvery(22000, function()
		anim6:animate(
			-- the mosquito zips in from the right, hovers a bit, then zips off to the left
			{x = 840, y = math.Random(500,600), scale = math.Random(0.2 , 1.2)}, 
			{x = math.Random(200,600), y = math.Random(100,300), duration = 2500, style = "easeInOut"},
			{delay = 1000},
			{x = -40, y = 500, duration = 2500, style = "easeInOut"}
		)
		return math.Random(10000, 32000)
		end
	)
local anim7 = scene:newChildSprite(
		{x = -500, y = -500, image = "Mosquito", section = "scenegame_11_PLANE_AMB", name = "mosquito7"})
	anim7:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	anim7:setFlipImageHorizontal(true)
	anim7:doEvery(56000, function()
		anim7:animate(
			-- the mosquito flies upward from bottom
			{x = math.Random(0,250), y = 640, scale = math.Random(0.2 , 1.2)}, 
			{startFrame = 0, loops = 4, frameTime = math.Random(22,50), x = math.Random (550,900), y = -40, duration = math.Random(2400,3800)}
		)
		return math.Random(22000, 38000)
		end
	)

	-- moves lower left palm
	local	frontPlant = scene:child("122ambientpalms")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("left")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 2, 10 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -2, 2 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -5, -2 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -2, 10 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	-- moves upper right palm
	local	frontPlant = scene:child("118ambientleaves1")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("top")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 1000,4000 )
			local dur2 = math.Random( 1000,4000 )
			local dur3 = math.Random( 1000,4000 )
			local dur4 = math.Random( 1000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( -5, 5 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( 5, -5 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -5, 5 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( 6, -2 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	-- moves upper middle palm
		local	frontPlant = scene:child("103plants2")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 2, 5 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( 4, -2 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -2, 4 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( 3, -2 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
	-- moves vine on wing
		local	frontPlant = scene:child("73vines")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("top")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 1000,3000 )
			local dur2 = math.Random( 1000,3000 )
			local dur3 = math.Random( 1000,3000 )
			local dur4 = math.Random( 1000,3000)
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 3, 4 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -3, 4 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -3, 2 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -3, 2 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
		-- moves upper left palm
	local	frontPlant = scene:child("117ambientleaves2")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( -10, 10 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( 10, -10 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( 10, -3 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -3, 10 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end
	
		-- moves lower left bush
	local	frontPlant = scene:child("121ambientsmallbush")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 2, 3 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -2, 2 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -3, -2 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -2, 3 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

end
-- begin tempi animations...


function doTEMPISnakeSlither()
	local snake = scene:child("snake")
	if snake ~= nil then
		snake:setVisible(false)
	end
	
	local slither = scene:newChildSprite({x = 692, y = 448, image = "Slither", section = "scenegame_12_TEMPI_AMB", name = "slither"})
	slither:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	if slither ~= nil then
		local slitherFrameTime = 100
		slither:animate(	
			{startFrame=0, endFrame=8, frametime=slitherFrameTime},
			{delay=slitherFrameTime * 8},
			{y=640, duration = 50}
		)
	end
end

function doTEMPIDoLaserEyes()
	scene:newChildSprite({class = "ParticleEffect", x = 405, y = 149, library = "PARTICLE_RAY2",  section = "scenegame_12_TEMPI_AMB", name = "particleRay2", play = true})
	scene:newChildSprite({class = "ParticleEffect", x = 361, y = 139, library = "PARTICLE_RAY1",  section = "scenegame_12_TEMPI_AMB", name = "particleRay1", play = true})
	scene:newChildSprite({class = "ParticleEffect", x = 630, y = 145, library = "PARTICLE_BEAM2",  section = "scenegame_12_TEMPI_AMB", name = "particleBeam2", play = true})
	scene:newChildSprite({class = "ParticleEffect", x = 575, y = 135, library = "PARTICLE_BEAM1",  section = "scenegame_12_TEMPI_AMB", name = "particleBeam1", play = true})
end


function doTEMPISnake()
	local snake = scene:newChildSprite({x = 692, y = 426, image = "Snake", section = "scenegame_12_TEMPI_AMB", name = "snake"})
	if snake ~= nil then
		snake:animate({startFrame=0, frameTime = 100, loops="forever"})	
	end
end
			
function doTEMPIRevealWaterPorts()

	ghf.startLoading("scenegame_12_TEMPI_AMB", false)
	ghf.loopSound("SFX_PipeWaterLoop:scenegame_12_TEMPI_AMB")
	
	scene:animate(
		{delay=1000}
	)
end
			
function doTEMPIAttachLeftSpigot()
			
	local water1 = scene:newChildSprite({class = "ParticleEffect", x = 157, y = 276, library = "PARTICLE_WATER2",  section = "scenegame_12_TEMPI_AMB", name = "particleWater2", play = true})
	if water1 ~= nil then
		water1:moveBehind( scene:child("111water2") )
	end
	
	local water2 = scene:newChildSprite({class = "ParticleEffect", x = 573, y = 308, library = "PARTICLE_WATER1RIGHT",  section = "scenegame_12_TEMPI_AMB", name = "particleWater1Right", play = true})
	if water2 ~= nil then
		water2:moveBehind( scene:child("111pocketwater2") )
	end
	
	local leftWater = scene:child("particleLeakyLeft")
	if leftWater ~= nil then
		leftWater:setVisible(false)
	end
end

function doTEMPITurnOffLeakyPipe(aOverflowDelay, aPlaySound)
	local particleWater2 = scene:child("particleWater2")
	if particleWater2 ~= nil then
		particleWater2:setVisible(false)
	end
	local waterSpray scene:newChildSprite({class = "ParticleEffect", x = 180, y = 278, library = "PARTICLE_WATER1",  section = "scenegame_12_TEMPI_AMB", name = "particleWater1", play = true})
	if waterSpray ~= nil then
		waterSpray:moveBehind( scene:child("110pocketwater") )
		waterSpray:setVisible(false)
	end
	
	if aPlaySound == true then
		scene:animate(
			{delay=2000},
			{play = "SFX_StoneDoorOpen:scenegame_10_TEMPX_AMB"}
		)
	end
	
	doTEMPIWaterSprayParticles(0, aOverflowDelay)
end


function doTEMPIWaterSprayParticles(aDelay, aOverflowDelay)
	local waterjug = scene:newChildSprite({class = "ParticleEffect", x = 165, y = 312, library = "PARTICLE_WATERJUG",  section = "scenegame_12_TEMPI_AMB", name = "waterjug"})
	if waterjug ~= nil then
		waterjug:moveBehind( scene:child("111water2") )
		if aDelay == 0 then
			waterjug:addInitialTime(150000)
		end
		waterjug:animate({delay=aOverflowDelay}, {call=function() waterjug:play() end})
	end
		
	local waterspray = scene:newChildSprite({class = "ParticleEffect", x = 165, y = 295, library = "PARTICLE_WATERSPRAY",  section = "scenegame_12_TEMPI_AMB", name = "waterspray"})
	if waterspray ~= nil then
		waterspray:moveBehind( scene:child("111water2") )
		if aDelay == 0 then
			waterspray:addInitialTime(150000)
		end
		waterspray:animate({delay=aDelay}, {call=function() waterspray:play() end})
	end
		
	local waterjugsmall = scene:newChildSprite({class = "ParticleEffect", x = 140, y = 304, library = "PARTICLE_WATERJUG_SMALL",  section = "scenegame_12_TEMPI_AMB", name = "waterjugsmall"})
	if waterjugsmall ~= nil then
		waterjugsmall:moveBehind( scene:child("111water2") )
		if aDelay == 0 then
			waterjugsmall:addInitialTime(150000)
		end	
		waterjugsmall:animate({delay=aOverflowDelay}, {call=function() waterjugsmall:play() end})
	end
		
	local waterspray2 = scene:newChildSprite({class = "ParticleEffect", x = 140, y = 295, library = "PARTICLE_WATERSPRAY",  section = "scenegame_12_TEMPI_AMB", name = "waterspray2"})
	if waterspray2 ~= nil then
		waterspray2:moveBehind( scene:child("111water2") )
		waterspray2:addInitialTime(150000)
		waterspray2:animate({delay=aDelay}, {call=function() waterspray2:play() end})
	end
	
	doTEMPIWaterPourRightPort(0, 0)
	
end

function doTEMPIWaterPourRightPort(aDelay, aOverflowDelay)
	local waterjugsmall2 = scene:newChildSprite({class = "ParticleEffect", x = 565, y = 324, library = "PARTICLE_WATERJUG_SMALL2",  section = "scenegame_12_TEMPI_AMB", name = "waterjugsmall2"})
	if waterjugsmall2 ~= nil then
		waterjugsmall2:moveBehind( scene:child("112water3") )
		if aOverflowDelay == 0 then
			waterjugsmall2:addInitialTime(150000)
		end
		waterjugsmall2:animate({delay=aOverflowDelay}, {call=function() waterjugsmall2:play() end})
	end
	local waterspray3 = scene:newChildSprite({class = "ParticleEffect", x = 565, y = 318, library = "PARTICLE_WATERSPRAY2",  section = "scenegame_12_TEMPI_AMB", name = "waterspray3"})
	if waterspray3 ~= nil then
		waterspray3:moveBehind( scene:child("112water3") )
		if aDelay == 0 then
			waterspray3:addInitialTime(150000)
		end		
		waterspray3:animate({delay=aDelay}, {call=function() waterspray3:play() end})
	end
	local water2 = scene:newChildSprite({class = "ParticleEffect", x = 573, y = 308, library = "PARTICLE_WATER1RIGHT",  section = "scenegame_12_TEMPI_AMB", name = "particleWater1Right"})
	if water2 ~= nil then
		water2:moveBehind( scene:child("111pocketwater2") )
		if aDelay == 0 then
			water2:addInitialTime(150000)
		end			
		water2:animate({delay=aDelay}, {call=function() water2:play() end})
	end
end

function doTEMPIWaterLeakLeftPort(aDelay)
	local water2 = scene:newChildSprite({class = "ParticleEffect", x = 145, y = 280, library = "PARTICLE_WATER1RIGHT",  section = "scenegame_12_TEMPI_AMB", name = "particleLeakyLeft"})
	if water2 ~= nil then
		water2:moveBehind( scene:child("112spoutparticles") )
		local waterport = scene:child("30rightwaterport")
		if waterport ~= nil then
			waterport:moveBehind(water2)
		end
		if aDelay == 0 then
			water2:addInitialTime(150000)
		end
		water2:animate({delay=aDelay}, {call=function() water2:play() end})
	end
end

local function doTEMPI()
	
	if scene:isTargetTriggered("96targeteyes") then
		doTEMPIDoLaserEyes()
	end
	
	if not scene:isTargetTriggered("67potwithsnake") then
		doTEMPISnake()
	end
	
	if scene:isTargetTriggered("27leakypipe") then
		ghf.startLoading("scenegame_12_TEMPI_AMB", false)
	    ghf.loopSound("SFX_PipeWaterLoop:scenegame_12_TEMPI_AMB")
	    doTEMPITurnOffLeakyPipe(0, false)
		doTEMPIWaterPourRightPort(0, 0)
		doTEMPIWaterSprayParticles(0, 0)
	else 
		if scene:isTargetTriggered("30rightwaterport") then 
			ghf.startLoading("scenegame_12_TEMPI_AMB", false)
			ghf.loopSound("SFX_PipeWaterLoop:scenegame_12_TEMPI_AMB")	
			doTEMPIAttachLeftSpigot(0)
			doTEMPIWaterPourRightPort(0, 0)
		else
			if scene:isTargetTriggered("5pillarswitch") then
				ghf.startLoading("scenegame_12_TEMPI_AMB", false)
				ghf.loopSound("SFX_PipeWaterLoop:scenegame_12_TEMPI_AMB")		
				doTEMPIWaterPourRightPort(0, 0)
				doTEMPIWaterLeakLeftPort(0)
			end
		end
	end
-- ant procession...
local ant_1 = scene:newChildSprite(
		{x = -50, y = 508, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		ant_1:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		ant_1:setFlipImageVertical(true)
		ant_1:doEvery(math.Random(15000,30000), function()
		ant_1:setPivotPoint("center")
		ant_1:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = -50, y = 508, rotation = -70, scale = .6},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 16, frameTime = 33},
				{x = 43, y = 256, duration = 6336},
			},
			-- wiggles antennae, cleans self
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			-- "walks" around and faces another direction
			{
				{startFrame=0, endFrame = 11, loops = 2, frameTime = 33},
				{rotation = -190, duration = 792},
			},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			-- "walks" around and faces another direction
			{
				{startFrame=0, endFrame = 11, loops = 1, frameTime = 33},
				{rotation = -115, duration = 396},
			},
			-- walks offscreen in new direction
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 120)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 120)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 120), direction = "backward"},
			
			{
				{startFrame=0, endFrame = 11, loops = 12, frameTime = 33},
				{x = -50, y = 48, duration = 4752},
			},
			
			{delay = 3000}

		)
		return math.Random(90000, 140000)
		end
	)
local trail_ant_1 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_1:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_1:doEvery (math.Random(22000,22500), function()
		trail_ant_1:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_1:setPivotPoint("center")
		trail_ant_1:setAnchorPoint("center")
		trail_ant_1:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (253,263), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_2 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_2:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_2:doEvery (math.Random(23000,23500), function()
		trail_ant_2:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_2:setPivotPoint("center")
		trail_ant_2:setAnchorPoint("center")
		trail_ant_2:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (253,263), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_3 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_3:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_3:doEvery (math.Random(24000,24500), function()
		trail_ant_3:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_3:setPivotPoint("center")
		trail_ant_3:setAnchorPoint("center")
		trail_ant_3:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			-- wiggles antennae, cleans self
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 70)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 70), direction = "backward"},
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 70)},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 70)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 70), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 70)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 70), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 70)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 70), direction = "backward"},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (253,263), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_4 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_4:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_4:doEvery (math.Random(25000,25500), function()
		trail_ant_4:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_4:setPivotPoint("center")
		trail_ant_4:setAnchorPoint("center")
		trail_ant_4:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (243,253), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_5 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_5:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_5:doEvery (math.Random(26000,26500), function()
		trail_ant_5:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_5:setPivotPoint("center")
		trail_ant_5:setAnchorPoint("center")
		trail_ant_5:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (243,253), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_6 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_6:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_6:doEvery (math.Random(27000,27500), function()
		trail_ant_6:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_6:setPivotPoint("center")
		trail_ant_6:setAnchorPoint("center")
		trail_ant_6:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (243,253), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_7 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_7:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_7:doEvery (math.Random(28000,28500), function()
		trail_ant_7:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_7:setPivotPoint("center")
		trail_ant_7:setAnchorPoint("center")
		trail_ant_7:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (243,253), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_8 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_8:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_8:doEvery (math.Random(29000,29500), function()
		trail_ant_8:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_8:setPivotPoint("center")
		trail_ant_8:setAnchorPoint("center")
		trail_ant_8:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (253,263), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)
local trail_ant_9 = scene:newChildSprite(
		{x = 830, y = 181, image = "Ant_1", section = "scenegame_12_TEMPI_AMB", name = "foreground ant 1"})
		
		trail_ant_9:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
		
		trail_ant_9:doEvery (math.Random(30000,30500), function()
		trail_ant_9:setColor({red = 0.5, green = 0.5, blue = 0.5})
		trail_ant_9:setPivotPoint("center")
		trail_ant_9:setAnchorPoint("center")
		trail_ant_9:animate(
			-- the ant climbs up the slab in the foreground lower right
			{x = 830, y = 181, alpha = .8, rotation = 160, scale = .2},  -- climbs up and stops
			{
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (758,768), y = math.Random (195,205), duration = 2772},
			},
			{
				{rotation = 110, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (742,752), y = math.Random (253,263), duration = 2772},
			},
			{
				{rotation = 80, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (762,772), y = math.Random (306,316), duration = 2772},
			},
			{
				{rotation = 60, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = math.Random (782,792), y = math.Random (355,365), duration = 2772},
			},
			-- wiggles antennae, cleans self
			{startFrame = 12, endFrame = 23, frameTime = math.Random (33, 70)},
			{startFrame = 23, endFrame = 12, frameTime = math.Random (33, 70), direction = "backward"},
			{startFrame = 24, endFrame = 35, frameTime = math.Random (33, 70)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 70), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 70)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 70), direction = "backward"},
			{startFrame = 27, endFrame = 35, frameTime = math.Random (33, 70)},
			{startFrame = 35, endFrame = 27, frameTime = math.Random (33, 70), direction = "backward"},
			{
				{rotation = 10, duration = 300},
				{startFrame=0, endFrame = 11, loops = 7, frameTime = 33},
				{x = 830, y = 363, duration = 2772},
			},
			{alpha = 0, duration = 150}
		)
		return 140000
		end
	)	
	-- moves hanging lights
	local	frontPlant = scene:child("33hanginglights")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("top")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( -2, 20 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -2, 20 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -2, -2 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -2, 3 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

	-- moves fern in pot
	local	frontPlant = scene:child("38fern")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("left")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 2000,4000 )
			local dur2 = math.Random( 2000,4000 )
			local dur3 = math.Random( 2000,4000 )
			local dur4 = math.Random( 2000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewy = (math.Random( 2, 5 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewy = (math.Random( -2, 2 )*0.01), duration = dur2, style="easeIn" },
				{skewy = (math.Random( -5, -2 )*0.01), duration = dur3, style="easeOut" },
				{skewy = (math.Random( -2, 3 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

			-- moves grasses
	local	frontPlant = scene:child("64vegetation1")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 1000,4000 )
			local dur2 = math.Random( 1000,4000 )
			local dur3 = math.Random( 1000,4000 )
			local dur4 = math.Random( 1000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( -20, 20)*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -20, 20 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -20, 20 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -20, 15 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

			-- moves grasses
	local	frontPlant = scene:child("102ambientgrass1")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("bottom")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 1000,4000 )
			local dur2 = math.Random( 1000,4000 )
			local dur3 = math.Random( 1000,4000 )
			local dur4 = math.Random( 1000,4000 )
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( -20, 20)*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -20, 20 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -20, 20 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -20, 15 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

	-- moves vine 
		local	frontPlant = scene:child("103ambientvine")	-- get the plant sprite from the scene pipeline
	if frontPlant ~= nil then
		frontPlant:setPivotPoint("top")
		frontPlant:doEvery(0, function()		-- repeat this stuff
			local dur1 = math.Random( 1000,10000 )
			local dur2 = math.Random( 1000,10000)
			local dur3 = math.Random( 1000,10000 )
			local dur4 = math.Random( 1000,10000)
			local totalDuration = dur1 + dur2 + dur3 + dur4
			frontPlant:animate(
				{skewx = (math.Random( 3, 4 )*0.01), duration = dur1, style="easeOut" },	-- math.Random makes a random number between lower and upper INTEGER
				{skewx = (math.Random( -3, 4 )*0.01), duration = dur2, style="easeIn" },
				{skewx = (math.Random( -3, 4 )*0.01), duration = dur3, style="easeOut" },
				{skewx = (math.Random( -3, 4 )*0.01), duration = dur4, style="easeIn" }
			)
			return totalDuration  		-- this number is delay before repeating; must be larger than the total of the longest times above
			end
		)
	end

	
end


function doMPATHBatteryAcid()
	scene:newChildSprite({class = "ParticleEffect", x = 518, y = 260, library = "PARTICLE_ACID",  section = "scenegame_13_MPATH_AMB", name = "acidParticle", play = true})
end


local function doMPATH()
	if not scene:isTargetTriggered("66stone") then
		scene:child("67letter"):setVisible(true)
	else
		local	rollRock	= scene:child("68movedstone")	-- get the sprite from the scene pipeline
		if rollRock ~= nil then
			rollRock:setPivotPoint("bottomright")
			rollRock:setRotation(45)
		end
	end

	local	rightMidFronds = scene:child("26morefronds")	-- get the leftfronds sprite from the scene pipeline
	if rightMidFronds ~= nil then
		rightMidFronds:setPivotPoint("bottomright")
		rightMidFronds:doEvery( 0, function()		-- repeat this stuff
			local dur1 = math.Random( 1000, 3000 )
			local dur2 = math.Random( 800, 1500 )
			rightMidFronds:animate(
				{rotation = math.Random (-.05, -.15), skewy = math.Random (-.02, -.01), duration = dur1, style="easeInOut"},	
				{rotation = math.Random (.15, .05), skewy = math.Random (.01, .02), duration = dur2, style="easeInOut"}		
				-- rotate slightly from the lower right corner, and skew along the y-axis just enough to give it a sense of bounce, repeat every 1-6 seconds
				)
			return dur1 + dur2		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
	
	local	rightFrond = scene:child("18frond")	-- get the sprite from the scene pipeline
	if rightFrond ~= nil then
		rightFrond:setPivotPoint("bottomright")
		rightFrond:doEvery( 0, function()		-- repeat this stuff
			local dur3 = math.Random( 800, 2000 )
			local dur4 = math.Random( 600, 1500 )
			rightFrond:animate(
				{rotation = math.Random (.1,.25), skewy = math.Random (.005, .025), duration = dur3, style="easeInOut"},	
				{rotation = math.Random (-.25, -.03), skewy = math.Random (-.025, -.005), duration = dur4, style="easeInOut"}		
				-- rotate slightly from the lower right corner, and skew along the y-axis just enough to give it a sense of bounce, repeat every 1-6 seconds
				)
			return dur3 + dur4		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
	
	local	leftFrond = scene:child("49leftfronds")	-- get the leftfronds sprite from the scene pipeline
	if leftFrond ~= nil then
		leftFrond:setPivotPoint("bottomleft")
		leftFrond:doEvery( 0, function()		-- repeat this stuff
			local dur5 = math.Random( 600, 2000 )
			local dur6 = math.Random( 800, 1500 )
			leftFrond:animate(
				{rotation = .035, skewy = .005, duration = dur5, style="easeInOut"},	
				{rotation = -.035, skewy = -.005, duration = dur6, style="easeInOut"}		
				-- rotate slightly from the lower right corner, and skew along the y-axis just enough to give it a sense of bounce, repeat every 1-6 seconds
				)
			return dur5 + dur6		-- this number is delay before repeating; must be larger than duration of stuff we do above.
			end
		)
	end
	-- falling leaf #1
	local leaf1 = scene:newChildSprite(
		{x = -500, y = -500, image = "Falling_Leaf", section = "scenegame_13_MPATH_AMB", name = "leaf1"})
	leaf1:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	leaf1:setFlipImageHorizontal(false)		
	leaf1:animate({startFrame = 0, loops = "forever", frametime = math.Random(33,43)})
	leaf1:doEvery(12000, function()
		leaf1:animate(
			-- the leaf falls from the top left somewhere to the bottom right somewhere
			{x = math.Random(-10, 350), y = -10, scale = math.Random(1.0 , 1.4)}, 
			{x = math.Random(450, 660), y = 660, duration = math.Random(4500,7500)}
		)
		return math.Random(25000, 40000)
		end
	)

	-- falling leaf #2
	local leaf2 = scene:newChildSprite(
		{x = -500, y = -500, image = "Falling_Leaf", section = "scenegame_13_MPATH_AMB", name = "leaf2"})
	leaf2:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	leaf2:setFlipImageHorizontal(false)		
	leaf2:animate({startFrame = 0, loops = "forever", frametime = math.Random(33,43)})
	leaf2:doEvery(000, function()
		leaf2:animate(
			-- the leaf falls from the top left somewhere to the bottom right somewhere
			{x = math.Random(450, 850), y = -10, scale = math.Random(1.1 , 1.8)}, 
			{x = math.Random(-10, 350), y = 660, duration = math.Random(4500,7500)}
		)
		return math.Random(25000, 40000)
		end
	)

	-- falling leaf #3
	local leaf3 = scene:newChildSprite(
		{x = -500, y = -500, image = "Falling_Leaf", section = "scenegame_13_MPATH_AMB", name = "leaf3"})
	leaf3:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	leaf3:setFlipImageHorizontal(false)		
	leaf3:animate({startFrame = 0, loops = "forever", frametime = math.Random(33,43)})
	leaf3:doEvery(6000, function()
		leaf1:animate(
			-- the leaf falls from the top left somewhere to the bottom right somewhere
			{x = math.Random(-10, 350), y = -10, scale = math.Random(0.9 , 1.3)}, 
			{x = math.Random(450, 660), y = 660, duration = math.Random(4500,7500)}
		)
		return math.Random(25000, 40000)
		end
	)

	-- falling leaf #4
	local leaf4 = scene:newChildSprite(
		{x = -500, y = -500, image = "Falling_Leaf", section = "scenegame_13_MPATH_AMB", name = "leaf4"})
	leaf4:moveBehind( scene:child("GAME_UI_BLUEPRINT") )
	leaf4:setFlipImageHorizontal(false)		
	leaf4:animate({startFrame = 0, loops = "forever", frametime = math.Random(33,43)})
	leaf4:doEvery(19000, function()
		leaf1:animate(
			-- the leaf falls from the top left somewhere to the bottom right somewhere
			{x = math.Random(450, 850), y = -10, scale = math.Random(0.9 , 1.0)}, 
			{x = math.Random(-10, 360), y = 660, duration = math.Random(4500,7500)}
		)
		return math.Random(25000, 40000)
		end
	)
	
	if scene:isTargetTriggered("63ammobox") then
		doMPATHFlashNoService()
	end

end

function doMPATHRollRock()
	local	rollRock	= scene:child("68movedstone")	-- get the sprite from the scene pipeline
	if rollRock ~= nil then
		local regularRock = scene:child("66stone")
		if regularRock ~= nil then
			regularRock:setVisible(false)
		end
		rollRock:setPivotPoint("bottomright")
		rollRock:animate(
			{visible=true, rotation = 45, duration = 800}
		)
	end
end

function doMPATHFlashNoService()
	local	noService = scene:child("110noservice")	-- get the sprite from the scene pipeline
	if noService ~= nil then
		noService:doEvery(500, function()		-- # tells animation how long to wait before starting
			noService:animate(
				{alpha = 0, duration = 100},
				{delay = 1000},
				{alpha = 1, duration = 100}
			)
			return 1800		-- this number is delay before repeating; must be larger than duration of stuff we do above
			end
		)
	end
end

local function doUNLAB()
	doUnivSFX()

-- pyro particle leaves
	local leaves = scene:newChildSprite({class = "ParticleEffect", x = 364, y = -5, library = "PARTICLE_LEAVES",  section = "scenegame_14_UNLAB_AMB", name = "leaves", play = true})
	leaves:moveBehind( scene:child("202pocketleaves") )
	
-- pyro particle water faucet, to be turned off when target "95faucet" is triggered
	if not scene:isTargetTriggered("95faucet") then
		local faucet = scene:newChildSprite({class = "ParticleEffect", x = 282, y = 112, library = "PARTICLE_FAUCET",  section = "scenegame_14_UNLAB_AMB", name = "faucet", play = true})
		faucet:moveBehind( scene:child("10sink") )
		ghf.startLoading("scenegame_14_UNLAB_AMB", false)
		ghf.loopSound("SFX_SinkLoop:scenegame_14_UNLAB_AMB")
	end
end

function doUNLABTurnOffFaucet()
	faucet = scene:child("faucet")
	if faucet ~= nil then
		faucet:animate({alpha=0.0, duration = 50}, {visible=false})
	end
	ghf.stopLoop("SFX_SinkLoop:scenegame_14_UNLAB_AMB")
end

function doOFFICPrintPaper()
	local	printedPage = scene:child("99storyprintout")	-- get the sprite from the scene pipeline
	if printedPage ~= nil then
		printedPage:animate(
			{visible=true, x = 498, y = 234, duration = 3000}
		)
	end
end

function doOFFICChargeBatteries()
	local char = scene:child("108targetbatterycharger")
	char:animate({alpha=0.0, duration = 50}, {visible=false})
	local bat = scene:child("107actionchargedbatteries")
	bat:animate({visible=true, alpha=0.0}, {alpha=1.0, duration = 50})
end

local function doOFFIC(roundNum)
	doUnivSFX()

	local	printerLight = scene:child("93blinkinglight")	-- get the printerLight sprite from the scene pipeline
	if printerLight ~= nil then
		if printerLight:isVisible() then
			printerLight:doEvery(500, function()		-- # tells animation how long to wait before starting
				printerLight:animate(
					{alpha = 0, duration = 100},
					{delay = 1000},
					{alpha = 1, duration = 100}
				)
				return 1800		-- this number is delay before repeating; must be larger than duration of stuff we do above
				end
			)
		end
	end
	
	local	printerScreen = scene:child("100printererror")	-- get the printerScreen sprite from the scene pipeline
	if printerScreen ~= nil then
		if printerScreen:isVisible() then
			printerScreen:doEvery(0, function()		-- # tells animation how long to wait before starting
				printerScreen:animate(
					{alpha = 0, duration = 400},
					{alpha = 0, duration = 1},
					{alpha = 1, duration = 1},
					{alpha = 1, duration = 1200}
				)
				return 1672	-- this number is delay before repeating; must be larger than duration of stuff we do above
				end
			)
		end
	end
	
	local	mouseScreen = scene:child("134nomouseconnected")	-- get the mouseScreen sprite from the scene pipeline
	if mouseScreen ~= nil then
		if roundNum == 1 then
			mouseScreen:setVisible( false )
		else
			mouseScreen:doEvery(200, function()		-- # tells animation how long to wait before starting
				mouseScreen:animate(
					{alpha = 0, duration = 400},
					{alpha = 0, duration = 1},
					{alpha = 1, duration = 1},
					{alpha = 1, duration = 1200}
				)
				return 1672	-- this number is delay before repeating; must be larger than duration of stuff we do above
				end
			)
		end
	end

end

function doCONDOShakePhone()
	-- Shake the cell phone
	local cellPhone = scene:child("1cellphone")
	if cellPhone ~= nil then
		cellPhone:setVisible(true)
		
		cellPhone:doEvery(1000, function()		-- # tells animation how long to wait before starting
			if cellPhone:isVisible() and scene:isTargetTriggered("4landline") and not scene:isTargetTriggered("1cellphone") then
				if cellPhone:isVisible() then
					local startX = 684
					cellPhone:animate("cellPhoneRing", 
						{x=startX},
						{play = "SFX_Target_cellphoneVibe:scenegame_16_CONDO_AMB"}, 
						{delay=50},
						{x=startX + 1},
						{delay=50},
						{x=startX - 1},	
						{delay=50},
						{x=startX + 1},
						{delay=50},
						{x=startX - 1},
						{delay=50},
						{x=startX + 1},
						{delay=50},
						{x=startX - 1},	
						{delay=50},
						{x=startX + 1},
						{delay=50},
						{x=startX - 1},	
						{delay=50},
						{x=startX + 1},
						{delay=50},
						{x=startX - 1},	
						{delay=50},
						{x=startX + 1},
						{delay=50},
						{x=startX - 1},	
						{delay=50},
						{x=startX + 1},
						{delay=50}
					)
					end
				end
			end
		)
	end
	
	local cellPhoneLight = scene:child("50layer1")
	if cellPhoneLight ~= nil then
		cellPhoneLight:setVisible(true)
		cellPhoneLight:doEvery(1000, function()		-- # tells animation how long to wait before starting
			if cellPhoneLight:isVisible() and scene:isTargetTriggered("4landline") and not scene:isTargetTriggered("1cellphone") then
				local startX = 681
				cellPhoneLight:animate("cellPhoneLightRing",
					{x=startX},
					{x=startX},
					{delay=50},
					{x=startX + 1},
					{delay=50},
					{x=startX - 1},	
					{delay=50},
					{x=startX + 1},
					{delay=50},
					{x=startX - 1},
					{delay=50},
					{x=startX + 1},
					{delay=50},
					{x=startX - 1},	
					{delay=50},
					{x=startX + 1},
					{delay=50},
					{x=startX - 1},	
					{delay=50},
					{x=startX + 1},
					{delay=50},
					{x=startX - 1},	
					{delay=50},
					{x=startX + 1},
					{delay=50},
					{x=startX - 1},	
					{delay=50},
					{x=startX + 1},
					{delay=50}					
				)
				end
			end
		)
	end
end

function doCONDOStartPhoneRing()
	ghf.startLoading("scenegame_16_CONDO_AMB", false)
	ghf.loopSound("SFX_Target_cellphoneRing:scenegame_16_CONDO_AMB")
end

function doCONDOPhone()
	local dialing = scene:child("52dialing")
	if dialing ~= nil then
		dialing:doEvery(1000, function()		-- # tells animation how long to wait before starting
			dialing:animate(
				{visible=true},
				{delay=500},
				{visible=false}
			)
			end
		)
	end

	scene:animate(
		{delay = 1000}, 	
		{call = function() doCONDOStartPhoneRing() end},
		{call = function() doCONDOShakePhone() end}
	)

end

function doCONDOAnswerPhone()
	ghf.cancelAnimation("cellPhoneRing")
	ghf.cancelAnimation("cellPhoneLightRing")
	ghf.stopLoop("SFX_Target_cellphoneRing:scenegame_16_CONDO_AMB")
end
	
function doCONDOFireExtinguisher()
	local fireExtinguisher = scene:child("39fireextinguisher")
	if fireExtinguisher ~= nil then
	    
	    -- Prepare the business card to fly
	    local businessCard = scene:child("9businesscard") 
		businessCard:animate({visible = true, x=195, y=243, scale=0.2})
			
		fireExtinguisher:animate({visible=true, x=630, y=14})
		local fireExtinguisherParticle = scene:newChildSprite({class = "ParticleEffect", x = 659, y = 29, library = "PARTICLE_CONDO_EXTINGUISHER",  section = "scenegame_16_CONDO_AMB", name = "fireExtinguisherParticle", play = true})
		fireExtinguisherParticle:animate(
			{delay = 1500}, 
			{sprite=fireExtinguisher, alpha=0, duration=500},
			{visible=false},
			{call = function() doCONDOAirvent() end},
			{delay = 100},
			{sprite = businessCard, scale = 1.0, x=195, y=143, rotation = 360, style = "easeInOut", duration = 1000},
			{sprite = businessCard, rotation=0, skewx = (math.Random( -2, 8 )*0.01)},
			{sprite = businessCard, x=235, y=43, rotation = 360, style="easeInOut", duration = 700},
			{sprite = businessCard, rotation=0, skewx = (math.Random( -2, 8 )*0.01)},
			{sprite = businessCard, x=210, y=257, rotation = 360, style="easeInOut", duration = 700},
			{sprite = businessCard, skewx=0},
			{sprite = businessCard, x = 211, y=281, duration = 700, style="easeInOut"}
		)
	end
end
				
function doCONDOAirvent()

	local ribbon1 = scene:child("52ribbon1")
	local ribbon2 = scene:child("52ribbon2")
	
	if ribbon1 ~= nil and ribbon2 ~= nil then
		ribbon1:doEvery(160, function()		-- # tells animation how long to wait before starting
			ribbon1:animate(
				{
					{sprite=ribbon2, visible=false},
					{visible=true},
				},
				{delay=80},                    
				{
					{visible=false},
					{sprite=ribbon2, visible=true},
				},
				{delay=80}
			)
			end
		)
	end
end

local function doCONDO()
	doCondoSFX()
	
	if scene:isTargetTriggered("40thermostat") then
		doCONDOAirvent()
	end

	local neonSign = scene:child("47ghmoreorangyambient")	-- get the sprite from the scene pipeline
	if neonSign ~= nil then
		neonSign:doEvery(math.Random (20, 200), function()		-- # tells animation how long to wait before starting
			local dur1 = math.Random( 20, 3000 )
			local dur2 = math.Random( 10, 1500 )
			neonSign:animate(
					{alpha = 0, duration = 5}, -- start at 0% opacity for 1.5 seconds
					{alpha = 0, duration = dur1},
					{alpha = 1, duration = 5},
					{alpha = 1, duration = dur1}
			)
			return 10 + dur1 + dur2	-- this number is delay before repeating; must be larger than duration of stuff we do above
			end
		)
	end
	
	local	flickeringLight = scene:child("46lightonambient")	-- get the sprite from the scene pipeline
	if flickeringLight ~= nil then
		flickeringLight:doEvery(math.Random (20, 100), function()		-- # tells animation how long to wait before starting
			local dur3 = math.Random( 10, 900 )
			local dur4 = math.Random( 5, 2000 )
			flickeringLight:animate(
					{alpha = .5, duration = 1}, -- start at 0% opacity
					{alpha = .55, duration = dur3},    -- eliminate the fade
					{alpha = 1, duration = 1},
					{alpha = 1, duration = dur4 }
			)
			return 2 + dur3 + dur4	-- this number is delay before repeating; must be larger than duration of stuff we do above
			end
		)
	end
end



function animateAmbient(location, roundNum)
	local dispatchTable = {
		BEACH	= doBEACH,
		SHPRK	= doSHPRK,
		LIVNG	= doLIVNG,
		RUINS	= doRUINS,
		BCAVE	= doBCAVE,
		WTRFL	= doWTRFL,
		INVEN	= doINVEN,
		SCLAB	= doSCLAB,	
		DOCKS	= doDOCKS,
		TEMPX	= doTEMPX,
		PLANE	= doPLANE,
		TEMPI	= doTEMPI,
		MPATH	= doMPATH,
		UNLAB	= doUNLAB,
		OFFIC	= doOFFIC,
		CONDO	= doCONDO
	}
	
	dispatchTable[location](roundNum)
end



function doGullSFX()
	scene:doEvery( math.Random(3000,6000), function()
		local gullSFXName = "SFX_AMB_ocean_gull_"..math.Random(1,4)..":scenelevels"
		scene:animate(
			{play = gullSFXName}
		)
		return math.Random(3000,19000)
		end
	)
end


function doWaveSFX()
	scene:doEvery( math.Random(2000,2500), function()
		local waveSFXName = "SFX_AMB_ocean_wave_"..math.Random(1,10)..":scenelevels"
		scene:animate(
			{play = waveSFXName}
		)
		return math.Random(5500,14500)
		end
	)
end


function doCondoSFX()
	scene:doEvery( math.Random(3000,6000), function()
		local citySFXName = "SFX_FB_amb_"..math.Random(2,4)..":FlashbackTransition"
		scene:animate(
			{play = citySFXName}
		)
		return math.Random(3000,19000)
		end
	)
end

function doUnivSFX()
	scene:doEvery( math.Random(3000,6000), function()
		local citySFXName = "SFX_FB_amb_"..math.Random(1,3)..":FlashbackTransition"
		scene:animate(
			{play = citySFXName}
		)
		return math.Random(3000,19000)
		end
	)
end
