/******************************************************************************************
	CRTEST PS   (c) 1999-2001 ISL

	menu.c:			Simple little menu thing
******************************************************************************************/

#include "global.h"

#include "main.h"
#include "viewer.h"
#include "platform.h"
#include "collide.h"
#include "inventory.h"
#include "enemies.h"
#include "scenics.h"
#include "charactr.h"

#include "map_view.h"
#include "map_play.h"
// #include <convert.h>	// psx libs, for "atoi" ==== JS : now in global.h

//#include "stilts.h"
#include "station.h"
#include "catapult.h"
#include "startup.h"
#include "credits.h"
#include "fireworks.h"
#include "gametext.h"
#include "fma.h"
#include "options.h"
//#include "fxtest.h"
#include "seesaw.h"
#include "dogchase.h"
#include "crate.h"
#include "engine.h"
#include "language.h"
#include "sound.h"
#include "egglaying.h"
//#include "inventory.h"
#include "card.h"
#include "lev_flow.h"
#include "construct.h"
#include "wings.h"
#include "menu.h"

#include "DC_TIM.h"					// JIM : errr.....we kind of need this

//extern void fxtestStart(void);

int menu_choice = 0;

TIM_HEADER *menuTim=NULL;


void menuDraw();
void menuCycle();

//int menu_n_levels;
char *menu_level_namefile;
//char **menu_level_fnames;
//char **menu_level_idnames;


// Act1, Act2, Act4, Tests, Games, Models, XA
#define N_MENUS 7
int menu_sizes[N_MENUS];
char **menu_text_pointers[N_MENUS];
char **menu_file_pointers[N_MENUS];
int current_menu = 0;

char *menu_extra_names[] =
{
	"Startup!",
#if PREVIEW_VERSION==0
	"Credits",
	"Inventory",
	"Video 16",
#endif
	"Video 24",
#if PREVIEW_VERSION==0
//	"Egg Laying",
//	"Stilts",
	"Catapult",
	"Fireworks",
	"Seesaw",
	"FMA test",
	"Options",
//	"FX Test",
	"Crate",
	"Engine",
#endif
	"Dog Chase",
	"Construct",
	"Wing",
	"Egg Laying2",
};

typedef struct XA_INDEX_STR
{
	char	*name;
	char	tune_no;
//	char	file_no;
//	char	channel;
}XA_INDEX_STR;



XA_INDEX_STR menu_xa_list[] =
{
	{"Intro",		TUNE_1_INTRO},	// 1
	{"McBrief",		TUNE_2_BRIEFING},	// 2
	{"FowlerOK",	TUNE_3_COMPLETE},	// 3
	{"FowlerFail",	TUNE_4_FAILED},	// 4
	{"Coops D",		TUNE_5_COOPS_D},	// 5
	{"Coops N",		TUNE_6_COOPS_N},	// 6
	{"Working",		TUNE_7_WORKING},	// 7
	{"Golf",		TUNE_8_GOLF},	// 8
	{"Cog",			TUNE_9_COG},	// 9
	{"FlyMachine",	TUNE_10_FLYMACHINE},	// 10
	{"MrsT",		TUNE_11_MRST},	// 11
	{"Dies",		TUNE_12_DIES},	// 12
	{"GameOver",	TUNE_13_GAMEOVER},	// 13

	{"GloverAT1",	TUNE_AT1},
	{"GloverAT2",	TUNE_AT2},
	{"GloverAT3",	TUNE_AT3},
	{"GloverAT4",	TUNE_AT4},
	{"GloverAT5",	TUNE_AT5},
	{"GloverAT6",	TUNE_AT6},
	{"GloverAT7",	TUNE_AT7},
	{"GloverAT8",	TUNE_AT8}
};
int menu_n_tracks = sizeof(menu_xa_list) / sizeof(XA_INDEX_STR);

/* XA_INDEX_STR xa_tracks_list[] = 
{
	{"Intro",		0,0},	// 1
	{"McBrief",		1,0},	// 2
	{"FowlerOK",	1,1},	// 3
	{"FowlerFail",	1,2},	// 4
	{"Coops D",		0,1},	// 5
	{"Coops N",		0,2},	// 6
	{"Working",		0,3},	// 7
	{"Golf",		0,4},	// 8
	{"Cog",			0,5},	// 9
	{"FlyMachine",	0,6},	// 10
	{"MrsT",		0,7},	// 11
	{"Dies",		1,3},	// 12
	{"GameOver",	1,4},	// 13

	{"GloverAT1",	2,0},	// 13
	{"GloverAT2",	2,1},	// 13
	{"GloverAT3",	2,2},	// 13
	{"GloverAT4",	2,3},	// 13
	{"GloverAT5",	2,4},	// 13
	{"GloverAT6",	2,5},	// 13
	{"GloverAT7",	2,6},	// 13
	{"GloverAT8",	2,7}	// 13
};
//int menu_n_tracks = sizeof(xa_tracks_list) / sizeof(XA_INDEX_STR);
*/

enum MENU_EXTRA_NUMBERS
{
	MENU_ITEM_STARTUP,
#if PREVIEW_VERSION==0
	MENU_ITEM_CREDITS,
	MENU_ITEM_INVENTORY,
	MENU_ITEM_VIDEO16,
#endif
	MENU_ITEM_VIDEO24,
#if PREVIEW_VERSION==0
//	MENU_ITEM_EGGLAYING,
//	MENU_ITEM_STILTS,
	MENU_ITEM_CATAPULT,
	MENU_ITEM_FIREWORKS,
	MENU_ITEM_SEESAW,
	MENU_ITEM_FMA_TEST,
	MENU_ITEM_OPTIONS,
//	MENU_ITEM_FX_TEST,
	MENU_ITEM_CRATE,
	MENU_ITEM_ENGINE,
#endif
	MENU_ITEM_DOGCHASE,
	MENU_ITEM_CONSTRUCT,
	MENU_ITEM_WING,
	MENU_ITEM_EGGLAYING2,
};

int menu_n_extras = sizeof(menu_extra_names) / sizeof(char *);
int menu_n_items;

/*
extern void videoSetAudioChannel(int channel);

//#define VID_HEIGHT 240
#define VID_HEIGHT 192

StrDataType preview_vid_def =
{
	"\\TEST.STR;1",	//		char		*strName;					// Stream file name. 
//	"\\VIDEO\\GLOVER.STR;1",	//		char		*strName;					// Stream file name. 
	STR_MODE24,			//	short		mode;						// 24-Bit or 16-Bit streaming.
	STR_BORDERS_ON,	//	short		drawBorders;				// != 0 if borders on.
	320,				//		u_short		scrWidth;					// Screen res width.
	0,					//	u_short		x;							// X,Y position. 
	(256-VID_HEIGHT)/2,		//	u_short		y;
	320,				//	u_short		width;						// Stream width and height.
	VID_HEIGHT,				//	u_short		height;
	(1201-3),			//	u_long		endFrame;					// Last frame No.
	0,					//	u_long		vlcBufSize;					// Size of each VLC buffer (including header) - 0 means use max.
	127					//	u_short		volume;						// Left and Right ADPCM volume.
};
*/
// return 1 if debounced X
/*
short menuVidKeyhandler()
{
	static int channel = 1;
	if(padData.debounce[0] & (PAD_START + PAD_SELECT + PAD_CROSS + PAD_CIRCLE))
		return 1;

#if PREVIEW_VERSION == 0
	if(padData.debounce[0] & (PAD_TRIANGLE))
	{
		channel = (channel + 1) & 3;
		videoSetAudioChannel(channel);
	}
#endif

	return 0;
}
*/
// ============== Menu TIM backdrop =================
#ifdef _WINDOWS
#define LoadImage LoadImagePS
#endif
void uploadTimToVRAM(ULONG *addr)
{
}
/*
typedef struct {
	u_long	tag;
	u_long	code[5];
} DR_MOVE;
extern void SetDrawMove(DR_MOVE *p, RECT *rect, int x, int y) ;
*/

void drawBGTim()
{
#if PALMODE==1
	static RECT rect = {512,256,512,256};
#else
	static RECT rect = {512,256+8,512,256-16};
#endif
//	static RECT rect = {512,256+8,512,256-8};
	displayPageType *targetpage;
	DR_MOVE *si = (void *)currentDisplayPage->primPtr;
	POLY_F4 *si4;

	targetpage = (currentDisplayPage==displayPage)?(&displayPage[1]):(&displayPage[0]);


//	printf("%d\n",currentDisplayPage->dispenv.disp.y,currentDisplayPage->dispenv.screen.y);


/*
	BEGINPRIM(si4, POLY_F4);
	si4->r0 = 0;
	si4->g0 = 64;
	si4->b0 = 0;

	si4->x0 = si4->x2 = -130;
	si4->x1 = si4->x3 =  130;

	si4->y0 = si4->y1 =    0;
	si4->y2 = si4->y3 =   100;

	setPolyF4(si4);
	ENDPRIM(si4, 1023, POLY_F4);
*/

// Test Borders


/*
	BEGINPRIM(si4, POLY_F4);
	si4->r0 = 0;
	si4->g0 = 64;
	si4->b0 = 0;

	si4->x0 = si4->x2 = -256;
	si4->x1 = si4->x3 = -240;
#if PALMODE==1
	si4->y0 = si4->y1 =  -127;
	si4->y2 = si4->y3 =   127;
#else
	si4->y0 = si4->y1 =  -119;
	si4->y2 = si4->y3 =   119;
#endif

	setPolyF4(si4);
	ENDPRIM(si4, 1023, POLY_F4);

	BEGINPRIM(si4, POLY_F4);
	si4->r0 = 0;
	si4->g0 = 64;
	si4->b0 = 0;

	si4->x0 = si4->x2 = 240;
	si4->x1 = si4->x3 = 256;

	si4->y0 = si4->y1 =  -127;
	si4->y2 = si4->y3 =   127;

	setPolyF4(si4);
	ENDPRIM(si4, 1023, POLY_F4);







	BEGINPRIM(si4, POLY_F4);
	si4->r0 = 0;
	si4->g0 = 64;
	si4->b0 = 0;

	si4->x0 = si4->x2 = -240;
	si4->x1 = si4->x3 = 240;

	si4->y0 = si4->y1 =  -119;
	si4->y2 = si4->y3 =  -110;

	setPolyF4(si4);
	ENDPRIM(si4, 1023, POLY_F4);

	BEGINPRIM(si4, POLY_F4);
	si4->r0 = 0;
	si4->g0 = 64;
	si4->b0 = 0;

	si4->x0 = si4->x2 = -240;
	si4->x1 = si4->x3 = 240;

	si4->y0 = si4->y1 =  110;
	si4->y2 = si4->y3 =  119;

	setPolyF4(si4);
	ENDPRIM(si4, 1023, POLY_F4);
*/




 //	BEGINPRIM(si, DR_MOVE);
// disp or screen. dunno which
 //	SetDrawMove(si,&rect,0,targetpage->dispenv.disp.y);
 //	ENDPRIM(si, 1023, DR_MOVE);	// 0 or 1023






}
#ifdef _WINDOWS
#undef LoadImage 
#endif

// ====================================================

void menuStart()
{
	int len;
	int n;
	int i;

#ifdef _WINDOWS
	TIM_HEADER *pcTim;
#endif

	int work_menu = 0;


	gameCalledFrom = GAMECALLEDFROM_DEBUG;

	sound_XA_PauseForLoading();	// It'll probably be playing the (intro->day) session from the language screen...

	{
		dcTim_Load(&menuTim, "SHELL\\BACKDROP.TIM");
	}

	{
		menu_extra_names[MENU_ITEM_STARTUP] = TEXTSTR(TEXTSTR_VIEWSEL_STARTUP);
#if PREVIEW_VERSION==0
		menu_extra_names[MENU_ITEM_CREDITS] = TEXTSTR(TEXTSTR_VIEWSEL_CREDITS);
		menu_extra_names[MENU_ITEM_INVENTORY] = TEXTSTR(TEXTSTR_VIEWSEL_INVENTORY);
		menu_extra_names[MENU_ITEM_VIDEO16] = TEXTSTR(TEXTSTR_VIEWSEL_VID16);
#endif
		menu_extra_names[MENU_ITEM_VIDEO24] = TEXTSTR(TEXTSTR_VIEWSEL_VID24);

#if PREVIEW_VERSION==0
//		menu_extra_names[MENU_ITEM_EGGLAYING] = TEXTSTR(TEXTSTR_VIEWSEL_EGG_GAME);
//		menu_extra_names[MENU_ITEM_STILTS] = TEXTSTR(TEXTSTR_VIEWSEL_STILTS_GAME);
		menu_extra_names[MENU_ITEM_FIREWORKS] = TEXTSTR(TEXTSTR_VIEWSEL_FIREWORKS_GAME);
#endif
	}

#if PREVIEW_VERSION==0
	menu_level_namefile = fileLoad("VIEWER\\LEVELS.TXT",&len);
#else
	menu_level_namefile = fileLoad("VIEWER\\LEVELSM.TXT",&len);
#endif

	if(menu_level_namefile[len-2] != 13)
	{
		menu_level_namefile[len-2] = 13;	// just make sure there's a (13,10) at the end
	}


// Quick scan...
	for(i = 0; i < N_MENUS; i++)
	{
		menu_text_pointers[i] = NULL;
		menu_file_pointers[i] = NULL;
		menu_sizes[i] =0;
	}

	for(i = 0; i < len; i++)
	{
		if(i == 0 || menu_level_namefile[i-1] == 13 || menu_level_namefile[i-1] == 10)
		{
			if(menu_level_namefile[i]=='A' &&menu_level_namefile[i+1]=='1')
				menu_sizes[0]++;
			else if(menu_level_namefile[i]=='A' &&menu_level_namefile[i+1]=='2')
				menu_sizes[1]++;
			else if(menu_level_namefile[i]=='A' &&menu_level_namefile[i+1]=='4')
				menu_sizes[2]++;
			else
				menu_sizes[3]++;
		}

		if(menu_level_namefile[i] == 13)	// (13,10)
		{
			menu_level_namefile[i] = 0;
		}
	}

	for(i = 0; i < 4; i++)
	{
		menu_text_pointers[i] = MALLOC(menu_sizes[i] * 4);
		menu_file_pointers[i] = MALLOC(menu_sizes[i] * 4);
		menu_sizes[i] =0;
	}

	for(i = 0; i < len; i++)
	{
		if(i == 0 || menu_level_namefile[i-1] == 13 || menu_level_namefile[i-1] == 10)
		{
			if(menu_level_namefile[i]=='A' &&menu_level_namefile[i+1]=='1')
			{
				(menu_file_pointers[0])[menu_sizes[0]] = &menu_level_namefile[i];
				(menu_text_pointers[0])[menu_sizes[0]] = &menu_level_namefile[i];
				work_menu = 0;
				menu_sizes[0]++;
			}
			else if(menu_level_namefile[i]=='A' &&menu_level_namefile[i+1]=='2')
			{
				(menu_file_pointers[1])[menu_sizes[1]] = &menu_level_namefile[i];
				(menu_text_pointers[1])[menu_sizes[1]] = &menu_level_namefile[i];
				work_menu = 1;
				menu_sizes[1]++;
			}
			else if(menu_level_namefile[i]=='A' &&menu_level_namefile[i+1]=='4')
			{
				(menu_file_pointers[2])[menu_sizes[2]] = &menu_level_namefile[i];
				(menu_text_pointers[2])[menu_sizes[2]] = &menu_level_namefile[i];
				work_menu = 2;
				menu_sizes[2]++;
			}
			else
			{
				(menu_file_pointers[3])[menu_sizes[3]] = &menu_level_namefile[i];
				(menu_text_pointers[3])[menu_sizes[3]] = &menu_level_namefile[i];
				work_menu = 3;
				menu_sizes[3]++;
			}
		}
		if(menu_level_namefile[i] == 9)
		{
			switch(menu_level_namefile[i+1])
			{
				case 'm':
				case 'M':
					(menu_text_pointers[work_menu])[menu_sizes[work_menu]-1] = &menu_level_namefile[i+2];
//					DB("%d %d: %s\n",work_menu,menu_sizes[work_menu]-1,&menu_level_namefile[i+2]);
					break;
			}
			menu_level_namefile[i] = 0;
		}
	}

	viewerParseFile();


//	menu_n_items = menu_n_levels + viewer_n_models + menu_n_extras + 3;
//	menu_all_text = MALLOC(menu_n_items * sizeof(char *));
//	for(i = 0; i < menu_n_items; i++)
//		menu_all_text[i] = "";	// a nice little constant string

	menu_sizes[4] = menu_n_extras;
	menu_text_pointers[4] = MALLOC(menu_sizes[4] * 4);
	for(i = 0; i < menu_n_extras; i++)
	{
		menu_text_pointers[4][i] = menu_extra_names[i];
	}

	menu_sizes[5] = viewer_n_models;
	menu_text_pointers[5] = MALLOC(menu_sizes[5] * 4);
	for(i = 0; i < viewer_n_models; i++)
	{
		menu_text_pointers[5][i] = viewer_model_idnames[i];
	}

	menu_sizes[6] = menu_n_tracks;
	menu_text_pointers[6] = MALLOC(menu_sizes[6] * 4);
	for(i = 0; i < menu_n_tracks; i++)
	{
		menu_text_pointers[6][i] = menu_xa_list[i].name;
	}


// in case someone changed the text file while we were out...
	if(menu_choice >= menu_sizes[current_menu])
		menu_choice = menu_sizes[current_menu]-1;


	draw_routine = menuDraw;
	cycle_routine = menuCycle;


	sound_XA_Resume(-1);


}


void menuEnd()
{
	char temp[30];
	int i;

	dcTim_Unload(&menuTim);

	if(current_menu < 4)
	{
		strcpy(temp,menu_file_pointers[current_menu][menu_choice]);
	}

	for(i = 0; i < N_MENUS; i++)
	{
		FREE(menu_text_pointers[i]);
		if(menu_file_pointers[i])
		{
			FREE(menu_file_pointers[i]);
		}
	}
	FREE(menu_level_namefile);

	viewerFreeFile();

	sound_XA_Stop();

	switch(current_menu)
	{
	case 0:
	case 1:
	case 2:
	case 3:
		if(padData.debounce[0] & (PAD_SQUARE + PAD_TRIANGLE))
			mapviewStart(temp);
		else
		{
			if (padData.debounce[0] & (PAD_SELECT))
				fmaStatus.testSelection=TRUE;
			else
				fmaStatus.testSelection=FALSE;
			mapplayStart(temp,0);
		}
		break;
	case 5:
		viewerStart(menu_choice);
		break;

	case 6:

	{
		int file_no;
		int channel;

		sound_XA_Start(menu_xa_list[menu_choice].tune_no);

/*
		file_no = xa_tracks_list[menu_choice].file_no;
		channel = xa_tracks_list[menu_choice].channel;

		XAsetStatus(1);
		music = XAgetFileInfo(xa_file_names[file_no]);	// the ";1" is added internally
		XAstart(1);	// 1 = double speed
		XAplayChannel(music, channel, 1, 90);	// 1 = loop enable, 90 = volume (which should be got from options, really)
		music_playing = 1;
*/
		break;
	}

	case 4:
		switch(menu_choice)
		{

		case MENU_ITEM_STARTUP:
			languageStart();	// language -> memory card check -> startup
//			startupStart();
			break;

#if PREVIEW_VERSION==0
		case MENU_ITEM_CREDITS:
			creditsStart();
			break;
		case MENU_ITEM_INVENTORY:
			inventoryStart();
			break;
		case MENU_ITEM_VIDEO16:
		{
/*
			static RECT VRAMarea1 = {0,0,512,256};
			static RECT VRAMarea2 = {0,256,512,256};

			ClearImage2(&VRAMarea1, 0,0,0);
			ClearImage2(&VRAMarea2, 0,0,0);

			preview_vid_def.mode = STR_MODE16;

			videoPlayStream(&preview_vid_def,PALMODE, &menuVidKeyhandler);
*/
			menuStart();
			break;
		}
#else
#endif
		case MENU_ITEM_VIDEO24:
		{
   //			main_EnsureOverlay(VIDEO_PLAYING_OVERLAY);
   //			video_PlayVid(MOVIENO_BLITZ,0);
			videoPlayStream("BLITZBIG.SFD");
/*
			static RECT VRAMarea1 = {0,0,512,256};
			static RECT VRAMarea2 = {0,256,512,256};

			ClearImage2(&VRAMarea1, 0,0,0);
			ClearImage2(&VRAMarea2, 0,0,0);

			preview_vid_def.mode = STR_MODE24;

			videoPlayStream(&preview_vid_def,PALMODE, &menuVidKeyhandler);
*/
			menuStart();
			break;
		}

#if PREVIEW_VERSION==0
//		case MENU_ITEM_EGGLAYING:
//			stationStart("A4_EGGHUT");	//INTERIOR");
//			break;
//		case MENU_ITEM_STILTS:
//			stiltsStart();
//			break;	// stilts
		case MENU_ITEM_CATAPULT:
			main_EnsureOverlay(ACT2_SUBGAME_OVERLAY);
			catapultStart("DAYTIME");
			break;
		case MENU_ITEM_FIREWORKS:

			main_EnsureOverlay(ACT2_SUBGAME_OVERLAY);
			fireworksStart("A2_FIREWORKS");
			break;
		case MENU_ITEM_FMA_TEST:
			//FMAtest();	// no longer exists
			break;
		case MENU_ITEM_OPTIONS:
			optionsStart(0);
			break;
//		case MENU_ITEM_FX_TEST:
//			fxtestStart();
//			break;
		case MENU_ITEM_SEESAW :
			main_EnsureOverlay(ACT2_SUBGAME_OVERLAY);
			seesawStart();
			break;
		case MENU_ITEM_CRATE:
			main_EnsureOverlay(ACT4_SUBGAME_OVERLAY);
			crateStart();
			break;

		case MENU_ITEM_ENGINE:
			main_EnsureOverlay(ACT4_SUBGAME_OVERLAY);
			engineStart();
			break;
#endif
		case MENU_ITEM_DOGCHASE:
			main_EnsureOverlay(ACT1_SUBGAME_OVERLAY);
			dogchaseStart();
			break;

#ifndef _WINDOWS
		case MENU_ITEM_CONSTRUCT:
			main_EnsureOverlay(ACT4_SUBGAME_OVERLAY);
			constructStart();
			break;

		case MENU_ITEM_WING:
			main_EnsureOverlay(ACT4_SUBGAME_OVERLAY);
			wingStart();
			break;

		case MENU_ITEM_EGGLAYING2:
			main_EnsureOverlay(ACT4_SUBGAME_OVERLAY);
			elStart();
			break;
	 
#endif
		}
		break;

	}
}


//#define MENU_PANEL_X (0)
#define MENU_PANEL_X (3)

//dr_load (17)
void menuDraw()
{
	int i;
	int item;
	char *ptr;
	int menu_panel_y;
	char *headings[N_MENUS] = {"Act 1","Act 2","Act 4","Tests","Games","Models","XA"};

	//drawBGTim();
	dcTim_Draw(0, 0, 640, 480, menuTim);

	menu_panel_y = 50 - (11 * 7)/2;

	ptr = headings[current_menu];
	fontPrint(font, MENU_PANEL_X - fontExtentW(font, ptr)/2,menu_panel_y - 20, ptr, 128,128,64);


	fontPrintCentred(font,180,110,__TIME__, 128,100,53);
	fontPrintCentred(font,-180,110,__DATE__,128,100,53);

	ptr = menu_text_pointers[current_menu][menu_choice];
	fontPrint(font, MENU_PANEL_X - fontExtentW(font, ptr)/2,menu_panel_y+7*5, ptr, 128,128,128);

	for(i = 0;i < 11; i++)
	{
		item = (i + menu_choice + menu_sizes[current_menu] + 1 - 5) % (menu_sizes[current_menu] + 1);
		if(item >= menu_sizes[current_menu])
			ptr = " ";
		else
			ptr = menu_text_pointers[current_menu][item];

		if(item != menu_choice)
			fontPrint(font, MENU_PANEL_X - fontExtentW(font, ptr)/2,menu_panel_y+7*i, ptr, 32,32,32);
	}
}


#define AUTOREPEAT_BIG		15
#define AUTOREPEAT_MEDIUM	7
#define AUTOREPEAT_SMALL	2
#define AUTOREPEAT_DELTA	1

void menuCycle()
{
	int menu_items;
	static int delay = AUTOREPEAT_BIG;
	static int countdown = AUTOREPEAT_BIG;


	if(padData.debounce[0] & PAD_RIGHT)
	{
		current_menu++;
		if(current_menu >= N_MENUS)
			current_menu = 0;
		if(menu_choice >= menu_sizes[current_menu])
			menu_choice = menu_sizes[current_menu]-1;
	}
	if(padData.debounce[0] & PAD_LEFT)
	{
		current_menu--;
		if(current_menu < 0)
			current_menu = N_MENUS-1;
		if(menu_choice >= menu_sizes[current_menu])
			menu_choice = menu_sizes[current_menu]-1;
	}

	menu_items = menu_sizes[current_menu];

	if(padData.debounce[0] & PAD_UP || ((padData.digital[0] & PAD_UP) && countdown == 0))
	{
		menu_choice--;
		if(menu_choice < 0)
			menu_choice = menu_items-1;

		if(delay == AUTOREPEAT_BIG)
			delay = AUTOREPEAT_MEDIUM;
		else if(delay > AUTOREPEAT_SMALL)
			delay -= AUTOREPEAT_DELTA;
		countdown = delay;
	}
	if(padData.debounce[0] & PAD_DOWN || ((padData.digital[0] & PAD_DOWN) && countdown == 0))
	{
		menu_choice++;
		if(menu_choice >= menu_items)
			menu_choice = 0;

		if(delay == AUTOREPEAT_BIG)
			delay = AUTOREPEAT_MEDIUM;
		else if(delay > AUTOREPEAT_SMALL)
			delay -= AUTOREPEAT_DELTA;
		countdown = delay;
	}

	if(!(padData.digital[0] & (PAD_UP+PAD_DOWN)))
	{
		delay = AUTOREPEAT_BIG;
		countdown = AUTOREPEAT_BIG;
	}

	if(countdown)
		countdown--;
	if(padData.debounce[0] & (PAD_CROSS+PAD_CIRCLE+PAD_SQUARE+PAD_TRIANGLE+PAD_START+PAD_SELECT))
	{
		menuEnd();
	}
}






