----------------------------------------------------------------


local driverInSeat = false
local smokeOpacity_throttleOn = 0.5
local smokeOpacity_throttleOff = 0.1
local fireSize_big = 7
local fireSize_small = 3

function seatChildAddedHandler(child)
	if child.Name=="SeatWeld" then
		print("Turn car ON")
		driverInSeat = true
		if(hasSmoke) then 
			smoke.Enabled = true 
		end
		smoke.Opacity = smokeOpacity_throttleOff
		colorAll(headlights, lightColor_on)
		fire.Size = fireSize_big
		if(hasFire) then 
			fire.Enabled = true
		end
		wait(0.3)
		fire.Size = fireSize_small
		if seat.Throttle==0 then
			fire.Enabled = false
		end
	end
end

function seatChildRemovedHandler(child)
	if child.Name=="SeatWeld" then
		print("Turn car OFF")
		driverInSeat = false
		smoke.Enabled = false
		colorAll(headlights, lightColor_off)
	end
end

function showBigFire()
	fire.Size = fireSize_big
	if(hasFire) then
		fire.Enabled = true
	end
	wait(0.3)
	if seat.Throttle==1 then
		fire.Size = fireSize_small
	end
	wait(1)
	if seat.Throttle==1 then
		fire.Enabled = false
	end
end

function forwardFunction()
	--print("forwardFunction()")
end

function backwardFunction()
	--print("backwardFunction()")
end

function colorAll(parts, color)
	for i=1, #parts do
		parts[i].BrickColor = color
	end
end

function seatChangedHandler(prop)
	if prop=="Throttle" then
		if seat.Throttle==1 then
			-- Throttle Forward
			colorAll(brakelights, brakeColor_off)
			smoke.Opacity = smokeOpacity_throttleOn
			local co = coroutine.create(showBigFire)
			coroutine.resume(co)
			local co2 = coroutine.create(forwardFunction)
			coroutine.resume(co2)
		elseif seat.Throttle==0 then
			-- Throttle Off
			colorAll(brakelights, brakeColor_on)
			smoke.Opacity = smokeOpacity_throttleOff
			fire.Enabled = false
			wait(0.9)
			colorAll(brakelights, brakeColor_off)
		elseif seat.Throttle==-1 then
			-- Throttle Reverse
			colorAll(brakelights, lightColor_on)
			smoke.Opacity = smokeOpacity_throttleOff
			fire.Enabled = false
			local co = coroutine.create(backwardFunction)
			coroutine.resume(co)
		end
	end
end
