layoutWoods1Tirechange.panelPlaced = false
layoutWoods1Tirechange.addedNut = {}
layoutWoods1Tirechange.addedNut[1] = false
layoutWoods1Tirechange.addedNut[2] = false
layoutWoods1Tirechange.addedNut[3] = false
layoutWoods1Tirechange.addedNut[4] = false
layoutWoods1Tirechange.mFirstTime = true
-- ----------------------------------------------------------------------------
layoutWoods1Tirechange.mExits =
{
}

layoutWoods1Tirechange.mPOIs = 
{
}

layoutWoods1Tirechange.mHOs = 
{
	[objectHoTrunkfloorNut4] = 
	{
	}
}

layoutWoods1Tirechange.mIOs =
{
	[objectIoLocationCarjack] = 
	{
		links = {objectHoJackstand},
	},
	[objectIoLocationPanel] = 
	{
		links = {objectHoPanel01},
	},
	[objectIoLocationWheelOff] = 
	{
		links = {objectHoLugwrench}
	},
	[objectIoLocationWheelOn] = 
	{
		links = {objectHoTire},
	},
	[objectIoLocationNuts1] = 
	{
	},
	[objectIoLocationNuts2] = 
	{
	},
	[objectIoLocationNuts3] = 
	{
	},
	[objectIoLocationNuts4] = 
	{
	}
}
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onConstruct()
	
	Layout.onConstruct(self)

end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onInitialize()

	Layout.onInitialize(self)
	
	objectCarReady:ss(kInactive)
	objectCarNut1Loose:ss(kInactive)
	objectCarNut2Loose:ss(kInactive)
	objectCarNut3Loose:ss(kInactive)
	objectCarNut4Loose:ss(kInactive)
	objectCarNut1Ready:ss(kInactive)
	objectCarNut2Ready:ss(kInactive)
	objectCarNut3Ready:ss(kInactive)
	objectCarNut4Ready:ss(kInactive)
	objectCarLiftedNewWheel:ss(kInactive)
	objectCarLiftedWithoutWheel:ss(kInactive)
	objectCarLifted03:ss(kInactive)
	objectIoLocationWheelOff:ss(kInactive)
	objectIoLocationNuts1:ss(kInactive)
	objectIoLocationNuts2:ss(kInactive)
	objectIoLocationNuts3:ss(kInactive)
	objectIoLocationNuts4:ss(kInactive)
	objectIoLocationWheelOn:ss(kInactive)
	objectCarLifted02:ss(kInactive)
	objectCarLifted01:ss(kInactive)
	objectCarLifted01FailedHole:ss(kInactive)
	objectCarLifted01Failed:ss(kInactive)
	objectCarPanelLifted00:ss(kInactive)
	objectCarLifted00:ss(kInactive)
	objectCarPanel:ss(kInactive)
	objectHoTrunkfloorNut4:ss(kInactive)
	objectIoLocationPanel:ss(kInactive)
	
	objectIoLocationCarjack:ss(kActive)
	
	objectHoTire:ss(kHidden)
	objectHoLugwrench:ss(kHidden)
	objectHoJackstand:ss(kHidden)
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onShow()

	Layout.onShow(self)

	if (self.mFirstTime) then	
		think("MESSAGE_DISCOVER_FLAT_TIRE")	
		self.mFirstTime = false
	end
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onUsedAll(object, props)
	
	if (object == objectIoLocationCarjack) then							--> Car jack placed (without panel placed)

		objectCarLifted00:setAlpha(0)
		objectCarLifted00:ss(kIdle)		
		objectCarLifted00:animate("placeJackOnSand_blocking",
			{ alpha=0									},
			{ alpha=1,		duration=kTimeFadeSwitch	}
		)
		
		objectCarDown:ss(kIdle)		
		objectCarDown:animate(
			{ alpha=0,		duration=kTimeFadeSwitch	}
		)
		
		return true
		
	elseif (object == objectIoLocationPanel) then						--> Panel placed
		
		objectCarPanel:setAlpha(0)
		objectCarPanel:ss(kIdle)		
		objectCarPanel:animate("placePanel_blocking",
			{ alpha=0									},
			{ alpha=1,		duration=kTimeFadeSwitch	}
		)
		
		objectCarLifted01FailedHole:ss(kIdle)		
		objectCarLifted01FailedHole:animate(
			{ alpha=0,		duration=kTimeFadeSwitch	}
		)
		
		return true
		
	elseif (object == objectIoLocationWheelOn) then						--> New wheel placed
		
		objectCarLiftedNewWheel:setAlpha(0)
		objectCarLiftedNewWheel:ss(kIdle)		
		objectCarLiftedNewWheel:animate("placeTire_blocking",
			{ alpha=0									},
			{ alpha=1,		duration=kTimeFadeSwitch	}
		)
		
		objectCarLiftedWithoutWheel:ss(kIdle)		
		objectCarLiftedWithoutWheel:animate(
			{ alpha=0,		duration=kTimeFadeSwitch	}
		)
		
		return true
		
	elseif (object == objectIoLocationWheelOff) then				-- Let's take off that wheel off shall we
		-- we need the wrench later on for the nuts
		-- but they're not linked (no use animation)
		objectHoLugwrench:ss(kFound)
	
		objectCarLiftedWithoutWheel:setAlpha(0)
		objectCarLiftedWithoutWheel:ss(kIdle)		
		objectCarLiftedWithoutWheel:animate("removeWheel_blocking",
			{ alpha=0									},
			{ alpha=1,		duration=kTimeFadeSwitCh	}
		)
		
		objectCarLifted03:ss(kIdle)		
		objectCarLifted03:animate(
			{ alpha=0,		duration=kTimeFadeSwitch	}
		)	
		
	end
	
	return Layout.onUsedAll(self, object, props)

end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onAnimationFinish(animationName)
	
	Layout.onAnimationFinish(self, animationName)
	
	if (animationName == "placeJackOnSand_blocking") then
		
		objectCarDown:ss(kDone)
		
		-- TODO: update woods1 tire image
		
	elseif (animationName == "jackingOnSand_blocking") then
		
		objectCarLifted00:ss(kDone)

		-- Automatically remove the car jack from the layout:
		objectCarLifted01FailedHole:setAlpha(0)
		objectCarLifted01FailedHole:ss(kIdle)		
		objectCarLifted01FailedHole:animate("jackingHole_blocking",
			{ alpha=0									},
			{ alpha=1,		duration=kTimeFadeSwitch	}
		)
			
		objectCarLifted01Failed:animate(
			{ alpha=0,		duration=kTimeFadeSwitch	}
		)
		
		-- TODO: update woods1 tire image
		
	elseif (animationName == "jackingHole_blocking") then
		objectCarLifted01Failed:ss(kDone)
		objectHoJackstand:ss(kFound)
		
		-- Houston, we need a panel:
		objectIoLocationPanel:ss(kActive)
		objectHoPanel01:ss(kActive)
		objectIoLocationCarjack:ss(kInactive)
		
		-- update woods1 tire image
		objectTireFlatNoPanel:ss(kIdle)
		
	elseif (animationName == "placePanel_blocking") then
		objectCarLifted01FailedHole:ss(kDone)
		objectIoLocationPanel:ss(kDone)
		
		objectIoLocationCarjack:ss(kActive)
		self.panelPlaced = true

		-- update woods1 tire image
		objectTireFlatNoPanel:ss(kDone)		
		objectTireFlatPanel:ss(kIdle)
		
	elseif (animationName == "placeJackOnPanel_blocking") then
		objectCarPanel:ss(kDone)
		
		-- update woods1 tire image
		objectTireFlatPanel:ss(kDone)
		objectTireFlatPanelOldWheelOn:ss(kIdle)		
		
	elseif (animationName == "panelJack1_blocking") then
		
		objectCarPanelLifted00:ss(kInactive)

		-- TODO: update woods1 tire image
		
	elseif (animationName == "panelJack2_blocking") then
		
		objectCarLifted01:ss(kInactive)

		-- TODO: update woods1 tire image
		
	elseif (animationName == "panelJack3_blocking") then
		
		objectCarLifted02:ss(kInactive)
	--	objectIoLocationCarjack:ss(kDone)		-- We're done jackin' !
		objectIoLocationWheelOff:ss(kActive)	-- Ready for tire removal.

		-- TODO: update woods1 tire image
		
	elseif (animationName == "panelJack4_blocking") then
		
		objectCarLifted03:ss(kInactive)
		think("MESSAGE_JACKING_FANATIC")

		-- TODO: update woods1 tire image
	
	elseif (animationName == "removeWheel_blocking") then
		
		objectCarLifted03:ss(kDone)
		objectIoLocationWheelOff:ss(kDone)		-- We're done removin' them tires !
		objectIoLocationWheelOn:ss(kActive)		-- Ready for tire placement.
		objectHoTrunkfloorNut4:ss(kIdle)

		-- update woods1 tire image
		objectTireFlatPanelOldWheelOn:ss(kDone)
		objectTireFlatPanelOldWheelOff:ss(kIdle)
	
	elseif (animationName == "placeTire_blocking") then
		
		objectCarLiftedWithoutWheel:ss(kDone)
		objectIoLocationWheelOn:ss(kDone)		-- We're done addin' them tires !
		objectIoLocationNuts1:ss(kActive)
		objectIoLocationNuts2:ss(kActive)
		objectIoLocationNuts3:ss(kActive)
		objectIoLocationNuts4:ss(kActive)		-- Ready for nut placement.
		objectHoTrunkfloorNut1:ss(kActive)
		objectHoTrunkfloorNut2:ss(kActive)
		objectHoTrunkfloorNut3:ss(kActive)
		objectHoTrunkfloorNut4:ss(kActive)		-- Nuts added to shopping list
		think("MESSAGE_NUTS_LOCATION")
		objectIoJarUp:ss(kActive)

		-- update woods1 tire image
		objectTireFlatPanelOldWheelOff:ss(kDone)
		objectTireFlatNewWheelOn:ss(kIdle)
	
	elseif (animationName == "fadeInFastenedNut1_blocking") then
		
		objectCarNut1Loose:ss(kDone)
		if (self:allNutsReady()) then
			self:showEndAnimation()
		end

	elseif (animationName == "fadeInFastenedNut2_blocking") then
		
		objectCarNut2Loose:ss(kDone)
		if (self:allNutsReady()) then
			self:showEndAnimation()
		end
		
	elseif (animationName == "fadeInFastenedNut3_blocking") then
		
		objectCarNut3Loose:ss(kDone)
		if (self:allNutsReady()) then
			self:showEndAnimation()
		end
		
	elseif (animationName == "fadeInFastenedNut4_blocking") then
		
		objectCarNut4Loose:ss(kDone)
		if (self:allNutsReady()) then
			self:showEndAnimation()
		end

	elseif (animationName == "tireChangeFinish_blocking") then
		
		objectCarLiftedNewWheel:ss(kDone)		
		objectCarNut1Ready:ss(kDone)
		objectCarNut2Ready:ss(kDone)
		objectCarNut3Ready:ss(kDone)
		objectCarNut4Ready:ss(kDone)
		objectPoiTireFlat:ss(kDone)
		objectTireFlatNewWheelOn:ss(kDone)
		objectTireFlatFixed:ss(kIdle)		
		
		self:solve()

	end
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onSolveStart()

	Layout.onSolveStart(self)
    self:solveFinish()

end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onClickIO(object, x, y)
	
	-- After first usage, objectIoLocationCarjack is reactivated so it can receive the objectHoJackstand
	-- again. As onUsedAll won'tbe called anymore, we solve it like this:
	if (self.panelPlaced == true and gSelection == objectHoJackstand and object == objectIoLocationCarjack) then
		
		objectHoJackstand:ss(kUsed)
		
		objectCarPanelLifted00:setAlpha(0)							--> Placed carjack with panel placed
		objectCarPanelLifted00:ss(kIdle)		
		objectCarPanelLifted00:animate("placeJackOnPanel_blocking",
			{ alpha=0									},
			{ alpha=1,		duration=kTimeFadeSwitch	}
		)
			
		objectCarPanel:animate(
			{ alpha=0,		duration=kTimeFadeSwitch	}
		)
		
	end
	
	if (object == objectIoLocationCarjack and self:jackIsPlaced()) then			-- Let's jack this baby up!
		
		if (objectCarLifted00:isIdle()) then									-- Jackin' in sand, not so bright
			objectCarLifted01Failed:setAlpha(0)
			objectCarLifted01Failed:ss(kIdle)		
			objectCarLifted01Failed:animate("jackingOnSand_blocking",
				{ alpha=0									},
				{ alpha=1,		duration=kTimeFadeSwitch	},
				{ delay=700 }
			)
				
			objectCarLifted00:animate(
				{ alpha=0,		duration=kTimeFadeSwitch	}
			)
			
		elseif (objectCarPanelLifted00:isIdle()) then								-- Jackin' on panel, step 1

			objectCarLifted01:setAlpha(0)
			objectCarLifted01:ss(kIdle)		
			objectCarLifted01:animate("panelJack1_blocking",
				{ command="playTrack track=CAR_JACK", target=self },
				{ alpha=0										  },
				{ alpha=1,		duration=kTimeFadeSwitch		  }
			)
			
			objectCarPanelLifted00:animate(
				{ alpha=0,		duration=kTimeFadeSwitch	}
			)
			
		elseif (objectCarLifted01:isIdle()) then									-- Jackin' on panel, step 2
			objectCarLifted02:setAlpha(0)
			objectCarLifted02:ss(kIdle)		
			objectCarLifted02:animate("panelJack2_blocking",
				{ command="playTrack track=CAR_JACK", target=self },
				{ alpha=0										  },
				{ alpha=1,		duration=kTimeFadeSwitch		  }
			)
			
			objectCarLifted01:animate(
				{ alpha=0,		duration=kTimeFadeSwitch	}
			)
		
		elseif (objectCarLifted03:isIdle()) then									-- Jackin' on panel, step 4, fall back down
			objectIoLocationWheelOff:ss(kInactive)	-- Not Ready for tire removal anymore.
			objectCarPanelLifted00:setAlpha(0)
			objectCarPanelLifted00:ss(kIdle)		
			objectCarPanelLifted00:animate("panelJack4_blocking",
				{ command="playTrack track=CAR_FALL", target=self },
				{ alpha=0										  },
				{ alpha=1,		duration=kTimeFadeSwitch		  }
			)
			
			objectCarLifted03:animate(
				{ alpha=0,		duration=kTimeFadeSwitch	}
			)
		
		elseif (objectCarLifted02:isIdle()) then									-- Jackin' on panel, step 3
			objectCarLifted03:setAlpha(0)
			objectCarLifted03:ss(kIdle)		
			objectCarLifted03:animate("panelJack3_blocking",
				{ command="playTrack track=CAR_JACK", target=self },
				{ alpha=0										  },
				{ alpha=1,		duration=kTimeFadeSwitch		  }
			)
			
			objectCarLifted02:animate(
				{ alpha=0,		duration=kTimeFadeSwitch	}
			)
			
		end
	
	elseif (object == objectIoLocationNuts1) then		
		if (self.addedNut[1] == false) then
			if (self:applyNut()) then												-- Adding nut to spot 1
				self:fadeInNut(objectCarNut1Loose, "fadeInNut1_blocking")
				self.addedNut[1] = true
				playAudioTrack("PLACE_OBJECT")
				
			end
		else
			if (gSelection == objectHoLugwrench) then								-- Fasten nut in spot 1
				objectIoLocationNuts1:ss(kDone)											-- No need for this anymore
				self:crossFadeImages(	objectCarNut1Loose, 
										objectCarNut1Ready, 
										"fadeInFastenedNut1_blocking" )
				playAudioTrack("PLACE_OBJECT")
				
			end
		end
		return true
		
	elseif (object == objectIoLocationNuts2) then		
		if (self.addedNut[2] == false) then
			if (self:applyNut()) then												-- Adding nut to spot 2
				self:fadeInNut(objectCarNut2Loose, "fadeInNut2_blocking")
				self.addedNut[2] = true
				playAudioTrack("PLACE_OBJECT")
				
			end
		else
			if (gSelection == objectHoLugwrench) then								-- Fasten nut in spot 2
				objectIoLocationNuts2:ss(kDone)											-- No need for this anymore
				self:crossFadeImages(	objectCarNut2Loose, 
										objectCarNut2Ready, 
										"fadeInFastenedNut2_blocking" )
				playAudioTrack("PLACE_OBJECT")
				
			end
		end
		return true
		
	elseif (object == objectIoLocationNuts3) then		
		if (self.addedNut[3] == false) then
			if (self:applyNut()) then												-- Adding nut to spot 3
				self:fadeInNut(objectCarNut3Loose, "fadeInNut3_blocking")
				self.addedNut[3] = true
				playAudioTrack("PLACE_OBJECT")
				
			end
		else
			if (gSelection == objectHoLugwrench) then								-- Fasten nut in spot 3
				objectIoLocationNuts3:ss(kDone)											-- No need for this anymore
				self:crossFadeImages(	objectCarNut3Loose, 
										objectCarNut3Ready, 
										"fadeInFastenedNut3_blocking" )
				playAudioTrack("PLACE_OBJECT")
				
			end
		end
		return true
		
	elseif (object == objectIoLocationNuts4) then		
		if (self.addedNut[4] == false) then
			if (self:applyNut()) then												-- Adding nut to spot 4
				self:fadeInNut(objectCarNut4Loose, "fadeInNut4_blocking")
				self.addedNut[4] = true
				playAudioTrack("PLACE_OBJECT")
				
			end
		else
			if (gSelection == objectHoLugwrench) then								-- Fasten nut in spot 4
				objectIoLocationNuts4:ss(kDone)											-- No need for this anymore
				self:crossFadeImages(	objectCarNut4Loose, 
										objectCarNut4Ready, 
										"fadeInFastenedNut4_blocking" )
				playAudioTrack("PLACE_OBJECT")
				
			end
		end
		return true
	
	end
	
	return Layout.onClickIO(self, object, x, y)
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:jackIsPlaced()
	
	return ( not(objectCarDown:isIdle() or objectCarPanel:isIdle() or 
			 objectCarReady:isIdle() or objectCarLifted01FailedHole:isIdle()) )
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:applyNut()
	
	if (gSelection == objectHoTrunkfloorNut1) then
		objectHoTrunkfloorNut1:ss(kUsed)
		return true
		
	elseif (gSelection == objectHoTrunkfloorNut2) then
		objectHoTrunkfloorNut2:ss(kUsed)
		return true
	
	elseif (gSelection == objectHoTrunkfloorNut3) then
		objectHoTrunkfloorNut3:ss(kUsed)
		return true
		
	elseif (gSelection == objectHoTrunkfloorNut4) then
		objectHoTrunkfloorNut4:ss(kUsed)
		return true
		
	end
	
	return false
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:fadeInNut(object, animID)
	object:setAlpha(0)
	object:ss(kIdle)		
	object:animate(animID,
		{ alpha=0									},
		{ alpha=1,		duration=kTimeFadeSwitch	}
	)
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:crossFadeImages(oldObject, newObject, animID)
	newObject:setAlpha(0)
	newObject:ss(kIdle)
	newObject:animate(animID,
		{ alpha=0									},
		{ alpha=1,		duration=kTimeFadeSwitch	}
	)
	
	oldObject:ss(kIdle)		
	oldObject:animate(
		{ alpha=0,		duration=kTimeFadeSwitch	}
	)
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:allNutsReady()

	return (objectCarNut1Loose:isDone() and objectCarNut2Loose:isDone() and 
			objectCarNut3Loose:isDone() and objectCarNut4Loose:isDone())

end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:showEndAnimation()
	
	objectHoLugwrench:ss(kDone)
	crossFadeImages(objectCarLiftedNewWheel, objectCarReady, "tireChangeFinish_blocking")
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:onHide()

	Layout.onHide(self)
	
	layoutWoods1:checkShadowScare()
	
end
-- ----------------------------------------------------------------------------
function layoutWoods1Tirechange:isSkippable()

	return false

end