let frame = 0;
let alpha = 255;
let sound = ObjSound_Create();
ObjSound_Load(sound, GetCurrentScriptDirectory~"sfx/se_select00.wav");
ObjSound_SetVolumeRate(sound, 65);

@Initialize
{
	SetAutoDeleteObject(true);
	TBackground();
	TMenu();
}

@MainLoop
{
	yield;
}

@Finalize
{
}

task TBackground
{
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	ObjPrim_SetTexture(obj, target);
	Obj_SetRenderPriority(obj, 0.1);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 640, 480);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, 320, 240, 0);
	
	loop(32){
		ObjRender_SetColorHSV(obj, 0, 255-alpha, 255);
		ObjRender_SetAlpha(obj, alpha);
		alpha -= 1;
		yield;
	}
	
	while(true){
		ObjSprite2D_SetSourceRect(obj, 0, 0, 640, 480);
		frame++;
		yield;
	}
	
}

task TMenu
{
	let selectIndex = 0;
	task TMenuItem(let index, let mx, let my, let text)
	{
		function CreateTextObject(let mx, let my, let text)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 28);
			ObjText_SetFontType(obj, "Impress BT");
			ObjText_SetFontBold(obj, false);
			ObjText_SetFontColorTop(obj, 255, 128, 128);
			ObjText_SetFontColorBottom(obj, 128, 64, 64);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj, 0, 0, 0);
			ObjText_SetFontBorderWidth(obj, 2);
			ObjText_SetMaxWidth(obj, 150);
			ObjText_SetHorizontalAlignment(obj, ALIGNMENT_CENTER);
			Obj_SetRenderPriorityI(obj, 10);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
		}

		let objText = CreateTextObject(mx, my, text);
		let objSelect = CreateTextObject(mx, my, text);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);
		loop
		{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
		}
	}

	let mx = GetStgFrameWidth/2-45;
	let my = GetStgFrameHeight/2-50;
	let texts = ["Save Replay", "Exit", "Retry"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu)
	{
		TMenuItem(iText, mx, my, texts[iText]);
		my += 32;
	}
	
	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	let frameKeyHold = 0;
	loop
	{
		if(GetVirtualKeyState(VK_OK) == KEY_PULL)
		{
			let listResult = [RESULT_SAVE_REPLAY, RESULT_END, RESULT_RETRY];
			SetScriptResult(listResult[selectIndex]);
			CloseScript(GetOwnScriptID());
			return;
		}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			ObjSound_Play(sound);
			selectIndex--;
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			ObjSound_Play(sound);
			selectIndex++;
		}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 20 || (frameKeyHold > 20 && (frameKeyHold % 10 == 0)))
			{
				ObjSound_Play(sound);
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 20 || (frameKeyHold > 20 && (frameKeyHold % 10 == 0)))
			{
				ObjSound_Play(sound);
				selectIndex++;
			}
		}
		else
		{
			frameKeyHold = 0;
		}

		if(selectIndex < 0) 
		{
			selectIndex = countMenu - 1;
		}
		else
		{
			selectIndex %= countMenu;
		}

		yield;
	}
}