NewAudio.CreateChannel("Spooky XD")
NewAudio.PlaySound("Spooky XD", "prejoker", true, 1)
NewAudio.SetPitch("Spooky XD", 0.75)
NewAudio.SetVolume("Spooky XD", 0)
Black = CreateSprite("Pixel", "Top")
Black.MoveTo(320, 240)
Black.Scale(640, 480)
Black.color = {0,0,0}
Player.sprite.rotation = 0
Player.sprite.alpha = 0

timer = 539

function Update()
	timer = timer + 1
	if timer == 540 then
		BlackB = CreateSprite("Pixel", "BelowPlayer")
		BG = CreateSprite("Menu/Background", "BelowPlayer")
		Torch = CreateSprite("Menu/Torch/1", "BelowPlayer")
		Door = CreateSprite("Menu/Door Closed", "BelowPlayer")
		Titulo = CreateSprite("Menu/TituloJ", "BelowPlayer")
		Start = CreateSprite("Menu/"..GetAlMightyGlobal("Idioma").."/Start", "BelowPlayer")
		Exit = CreateSprite("Menu/"..GetAlMightyGlobal("Idioma").."/Exit", "BelowPlayer")
		Torch.SetAnimation({"Menu/Torch/1","Menu/Torch/2","Menu/Torch/3","Menu/Torch/4","Menu/Torch/5","Menu/Torch/6"}, 0.15)
		BlackB.color = {0,0,0}
		BlackB.Scale(640, 480)
		BG.Scale(2,2)
		Torch.Scale(4,4)
		Door.Scale(4,4)
		Torch.SetParent(BG)
		Door.SetParent(BG)
		Titulo.SetParent(BG)
		Start.SetParent(BG)
		Exit.SetParent(BG)
		BlackB.MoveTo(320, 240)
		BG.MoveTo(320, 240)
		Torch.MoveTo(-120, 80)
		Door.MoveTo(214, 82)
		Titulo.MoveTo(0, 170)
		Start.MoveTo(10, 60)
		Exit.MoveTo(2, 20)
		Player.sprite.alpha = 0
	elseif timer > 540 and timer <= 660 then
		Player.sprite.alpha = 0
		Black.alpha = (660 - timer)/120
		NewAudio.SetVolume("Spooky XD", 1/120*(timer - 540))
	elseif timer == 661 then
		Player.SetControlOverride(true)
		Player.MoveToAbs(270, Start.absy, true)
		Player.sprite.alpha = 1
		Start.color = {1,1,0}
	elseif timer > 1000 and timer <= 1010 then
		BG.MoveTo(320 + math.random(4, -4), 240 + math.random(4, -4))
	elseif timer == 1011 then
		BG.MoveTo(320, 240)
	elseif timer > 1070 and timer <= 1160 then
		BG.Scale(26 - 24*math.cos(math.rad(timer - 1070)), 26 - 24*math.cos(math.rad(timer - 1070)))
		Torch.Scale(52 - 48*math.cos(math.rad(timer - 1070)), 52 - 48*math.cos(math.rad(timer - 1070)))
		Door.Scale(52 - 48*math.cos(math.rad(timer - 1070)), 52 - 48*math.cos(math.rad(timer - 1070)))
		Titulo.Scale(13 - 12*math.cos(math.rad(timer - 1070)), 13 - 12*math.cos(math.rad(timer - 1070)))
		Start.Scale(13 - 12*math.cos(math.rad(timer - 1070)), 13 - 12*math.cos(math.rad(timer - 1070)))
		Exit.Scale(13 - 12*math.cos(math.rad(timer - 1070)), 13 - 12*math.cos(math.rad(timer - 1070)))
		BG.MoveTo(320 - 2920 + 2920*math.cos(math.rad(timer - 1070)), 240)
		Torch.MoveTo(-60*BG.xscale, 40*BG.yscale)
		Door.MoveTo(107*BG.xscale, 41*BG.yscale)
	elseif timer == 1161 then
		Audio.PlaySound("ueeueeueeuee")
	elseif timer == 1340 or timer == 1346 then
		Player.sprite.alpha = 1
		Audio.PlaySound("st")
	elseif timer == 1343 or timer == 1349 then
		Player.sprite.alpha = 0
	elseif timer == 1352 then
		Player.sprite.alpha = 1
		Audio.PlaySound("st")
		Black.Remove()
		BlackB.Remove()
		BG.Remove()
		Torch.Remove()
		Door.Remove()
		Titulo.Remove()
		Start.Remove()
		Exit.Remove()
		Titulo = nil
		NewAudio.DestroyChannel("Spooky XD")
		Encounter["nextwaves"] = {"Jevil Level"}
		State("ACTIONSELECT")
		State("DEFENDING")
	elseif timer == 2001 then
		Black2 = CreateSprite("Pixel", "Top")
		Black2.Scale(640, 480)
		Black2.MoveTo(320, 240)
		Black2.color = {0,0,0}
		Black2.alpha = 0
	elseif timer > 2060 and timer <= 2180 then
		Black2.alpha = (timer - 2060)/120
		NewAudio.SetVolume("Spooky XD", 1 - Black2.alpha)
	elseif timer > 2240 then
		Black2.Remove()
		Black.Remove()
		BlackB.Remove()
		BG.Remove()
		Torch.Remove()
		Door.Remove()
		Titulo.Remove()
		Start.Remove()
		Exit.Remove()
		Titulo = nil
		NewAudio.DestroyChannel("Spooky XD")
		Encounter["nextwaves"] = {"Menu"}
		SetRealGlobal("Extras", 2)
		State("ACTIONSELECT")
		State("DEFENDING")
	end
	if Titulo then
		if not anim then
			anim = 0
		end
		anim = anim + 1
		Titulo.MoveTo(0, 85*BG.yscale + 5*BG.yscale*math.sin(math.rad(anim)))
		Start.MoveTo(5*BG.xscale, 30*BG.yscale + 5*BG.yscale*math.sin(math.rad(anim - 40)))
		Exit.MoveTo(BG.xscale, 10*BG.yscale + 5*BG.yscale*math.sin(math.rad(anim - 80)))
	end
	if timer == 691 then
		if Input.Up == 1 then
			Start.color = {1,1,0}
			Exit.color = {1,1,1}
			Player.MoveToAbs(270, Start.absy, true)
			Audio.PlaySound("pip")
		elseif Input.Down == 1 then
			Start.color = {1,1,1}
			Exit.color = {1,1,0}
			Player.MoveToAbs(270, Exit.absy, true)
			Audio.PlaySound("pip")
		end
		if Player.absy > 280 then
			Player.MoveToAbs(270, Start.absy, true)
		else
			Player.MoveToAbs(270, Exit.absy, true)
		end
		if Input.Confirm == 1 then
			if Player.absy > 280 then
				timer = 1000
				NewAudio.Stop("Spooky XD")
				Audio.PlaySound("closet impact")
				Door.Set("Menu/Door Open")
				Player.MoveToAbs(320, 240, true)
			else
				timer = 2000
				Audio.PlaySound("confirm")
			end
			Start.color = {1,1,1}
			Exit.color = {1,1,1}
			Player.sprite.alpha = 0
		else
			timer = 690
		end
	end
end