--Key controls
mouse.KeyDown:connect(function(key)
	if key == "n" then
	--	if screen:FindFirstChild("CarPlay").Visible == true then
			if script.Parent.Position == (UDim2.new(1, 0, 0.5, -110)) then
		script.Parent:TweenPosition(UDim2.new(1, -376,0.5, -110), "InOut", "Quint", 0.5, false)
			else
				script.Parent:TweenPosition(UDim2.new(1,0,0.5, -110), "InOut", "Quint", 0.5, false)
	--	end
		end
	end
end)

for i, app in ipairs(script.Parent.CarPlay.Homescreen.Apps:GetChildren()) do
	if app:IsA("ImageButton") then
		app.MouseButton1Down:connect(function()
			if app.AppEnabled.Value == true then
				if app.Name == "Messages" or app.Name == "Phone" or app.Name == "BMW" or app.Name == "Podcasts" then
				else
					LaunchApp(app.Name)
				if app.Name == "Music" then
					CreatePlaylists()
				end
				end
				end
		end)
	end
end

side.HomeButton.MouseButton1Down:Connect(function()
	LaunchHome()
end)

music.Library.NowPlaying.NowPlaying.MouseButton1Down:connect(function()
	TransitionTo("Library", "NowPlaying")
	AddRecentApp("NowPlaying")
end)

music.Playlists.Back.Back.MouseButton1Down:connect(function()
	TransitionTo("Playlists", "Library")
end)

music.Playlists.NowPlaying.NowPlaying.MouseButton1Down:Connect(function()
	TransitionTo("Playlists", "NowPlaying")
	AddRecentApp("NowPlaying")
end)

script.Parent.CarPlay.NowPlaying.Back.Back.MouseButton1Down:connect(function()
	AddRecentApp("Music")
	script.Parent.CarPlay.Music.Visible = true
	script.Parent.CarPlay.Music.Playlists.Visible = false
	handler:FireServer("MusicFix")
	TransitionTo("NowPlaying", "Library")
	CreatePlaylists()
end)

script.Parent.CarPlay.NowPlaying.Play.Play.MouseButton1Down:connect(function()
	if sound.IsPlaying == false then
		filter:FireServer("resumeSong")
		handler:FireServer("Action", "Play")
		nowplaying.Play.ImageRectOffset = Vector2.new(150, 0)
	else
		handler:FireServer("Action", "Pause")
		nowplaying.Play.ImageRectOffset = Vector2.new(0, 0)
		filter:FireServer("pauseSong")
	end
end)

songData.Changed:connect(function()
	local Playlist = script.Parent.CarPlay_Data.Playlists[script.Parent.CarPlay_Data.NowPlayingData.CurrentPlaylist.Value]
	local playlistnum = (#Playlist:GetChildren())
			if songData.Value == 0 then
		songData.Value = playlistnum
			end
		if songData.Value > playlistnum then
			songData.Value = 1
		end
		PlayMusicUsingSongValue(songData.Value, script.Parent.CarPlay_Data.NowPlayingData.CurrentPlaylist.Value)
end)

script.Parent.CarPlay.NowPlaying.Rewind.Rewind.MouseButton1Down:connect(function()
	songData.Value = songData.Value - 1
end)

script.Parent.CarPlay.NowPlaying.FastForward.FastForward.MouseButton1Down:connect(function()
	songData.Value = songData.Value + 1
end)

sound.Ended:Connect(function()
	songData.Value = songData.Value + 1
end)

script.Parent.CarPlay.Maps.MapFrame.Overview.MouseButton1Down:connect(function()
	TransitionTo("MapFrame", "Overview")
end)

script.Parent.CarPlay.Maps.Overview.Back.Back.MouseButton1Down:connect(function()
	TransitionTo("Overview", "MapFrame")
end)

script.Parent.CarPlay.BMW.SettingsHome.Back.Back.MouseButton1Down:connect(function()
	LaunchHome()
end)

script.Parent.CarPlay.Settings.SettingsHome.Back.Back.MouseButton1Down:connect(function()
	LaunchHome()
end)

script.Parent.CarPlay.Settings.SettingsHome["24HrFormat"].Toggle.MouseButton1Down:connect(function()
	if script.Parent.CarPlay_Data.SettingsData["24HourFormat"].Value == true then
	script.Parent.CarPlay_Data.SettingsData["24HourFormat"].Value = false
	else
		script.Parent.CarPlay_Data.SettingsData["24HourFormat"].Value = true
		end
end)

script.Parent.CarPlay.Settings.SettingsHome.RecentApps.Toggle.MouseButton1Down:connect(function()
	if script.Parent.CarPlay_Data.SettingsData.RecentApps.Value == true then
	script.Parent.CarPlay_Data.SettingsData.RecentApps.Value = false
	else
		script.Parent.CarPlay_Data.SettingsData.RecentApps.Value = true
		end
end)

script.Parent.CarPlay_Data.SettingsData.RecentApps.Changed:connect(function()
if script.Parent.CarPlay_Data.SettingsData.RecentApps.Value == true then	
	RecentAppEnabled = true
	script.Parent.CarPlay.Settings.SettingsHome.RecentApps.Toggle.Text = "ON"
else
	RecentAppEnabled = false
	side.Status:TweenPosition(UDim2.new(0.111, 0,0.485, 0), "InOut", "Quint", 0.5, true)
	side.B1:TweenPosition(UDim2.new(0.028, 0,0.433, 0), "InOut", "Quint", 0.5, true)
	script.Parent.CarPlay.Settings.SettingsHome.RecentApps.Toggle.Text = "OFF"
	side.RecentApps:ClearAllChildren()
end
end)

while wait(.5) do
	script.Parent.CarPlay.NowPlaying.MusicLen.Text = "-"..(math.floor(sound.TimeLength/60-sound.TimePosition/60))..":"..math.floor(math.fmod(math.abs(sound.TimeLength-sound.TimePosition),60))
	handler:FireServer("UpdateDuration", "-"..(math.floor(sound.TimeLength/60-sound.TimePosition/60))..":"..math.floor(math.fmod(math.abs(sound.TimeLength-sound.TimePosition),60)))
	script.Parent.CarPlay.Maps.Overview.Speed.Text = (math.floor(car.DriveSeat.Velocity.magnitude*((10/12) * (60/88)))).. " mph"
	local placeId = game.PlaceId
	local Asset = game:GetService("MarketplaceService"):GetProductInfo(placeId)
	script.Parent.CarPlay.Maps.Overview.GameName.Text = Asset.Name
	script.Parent.CarPlay.Maps.Overview.Latitude.Text = "Latitude: "..math.floor(antenna.Position.X)
	script.Parent.CarPlay.Maps.Overview.Attitude.Text = "Attitude: "..math.floor(antenna.Position.Y)
	script.Parent.CarPlay.Maps.Overview.Longitude.Text = "Longitude: "..math.floor(antenna.Position.Z)
	
	script.Parent.CarPlay.NowPlaying.MusicLen.Changed:connect(function()
	if string.len(script.Parent.CarPlay.NowPlaying.MusicLen.Text) == 4 then
		script.Parent.CarPlay.NowPlaying.MusicLen.Text = "-"..(math.floor(sound.TimeLength/60-sound.TimePosition/60))..":0"..math.floor(math.fmod(math.abs(sound.TimeLength-sound.TimePosition),60))
	end
end)
end