// 
// string allocation/managment

#ifndef CGAME
	#include "ui_local.h"
#endif
#ifdef _XBOX
#include "../client/client.h"
#endif
#include "../Xbox/XBLive.h"

#include "ui_shared.h"
#include "../game/bg_public.h"
#include "../game/anims.h"
#include "../ghoul2/G2.h"
extern stringID_table_t animTable [MAX_ANIMATIONS+1];
extern void UI_UpdateCharacterSkin( void );

#ifdef _XBOX
#include "../cgame/cg_local.h"
#include "../renderer/modelmem.h"
#include "../ghoul2/ghoul2_shared.h"
#endif


#define SCROLL_TIME_START					500
#define SCROLL_TIME_ADJUST					150
#define SCROLL_TIME_ADJUSTOFFSET			40
#define SCROLL_TIME_FLOOR					20

typedef struct scrollInfo_s {
	int nextScrollTime;
	int nextAdjustTime;
	int adjustValue;
	int scrollKey;
	float xStart;
	float yStart;
	itemDef_t *item;
	qboolean scrollDir;
} scrollInfo_t;

#ifdef _XBOX
extern char *Cvar_VariableString( const char *var_name );
extern const char *RE_ShaderNameFromIndex(int index);
char *G2API_GetGLMName(void *pGhoul2, int modelIndex);
#endif


#ifndef CGAME	// Defined in ui_main.c, not in the namespace
extern vmCvar_t	ui_char_color_red;
extern vmCvar_t	ui_char_color_green;
extern vmCvar_t	ui_char_color_blue;
extern vmCvar_t	se_language;

// Some extern functions hoisted from the middle of this file to get all the non-cgame,
// non-namespace stuff together
extern void UI_SaberDrawBlades( itemDef_t *item, vec3_t origin, vec3_t angles );

extern void UI_SaberLoadParms( void );
extern qboolean ui_saber_parms_parsed;
extern bool gDoNotMakeUic;	// Used to signal complex menus that can't be turned into UIC files
extern void UI_CacheSaberGlowGraphics( void );

#endif //

#include "../namespace_begin.h"

#ifdef CGAME

extern int trap_Key_GetCatcher( void ) ;
extern void trap_Key_SetCatcher( int catcher );
extern void trap_Cvar_Set( const char *var_name, const char *value );

#endif

//JLF DEMOCODE
#ifdef _XBOX

//support for attract mode demo timer
#define DEMO_TIME_MAX  45000 //g_demoTimeBeforeStart
int g_demoLastKeypress = 0;  //milliseconds
bool  g_ReturnToSplash = false;
bool g_runningDemo = false;

void G_DemoStart();
void G_DemoEnd();
void G_DemoFrame();
void G_DemoKeypress();

void PlayDemo();
//void UpdateDemoTimer();
bool TestDemoTimer();
//END DEMOCODE

//JLF used by sliders
#define TICK_COUNT 16

//JLF MORE PROTOTYPES
qboolean Item_HandleSelectionNext(itemDef_t * item);
qboolean Item_HandleSelectionPrev(itemDef_t * item);
qboolean Item_HandleAction(itemDef_t * item);

#endif	// _XBOX

qboolean Item_SetFocus(itemDef_t *item, float x, float y);

static scrollInfo_t scrollInfo;

static void (*captureFunc) (void *p) = 0;
static void *captureData = NULL;

displayContextDef_t *DC = NULL;

static itemDef_t *g_bindItem = NULL;
static itemDef_t *g_editItem = NULL;

menuDef_t Menus[MAX_MENUS];      // defined menus
int menuCount = 0;               // how many

menuDef_t *menuStack[MAX_OPEN_MENUS];
int openMenuCount = 0;

static qboolean debugMode = qfalse;

#define DOUBLE_CLICK_DELAY 300
static int lastListBoxClickTime = 0;

void Item_RunScript(itemDef_t *item, const char *s);
void Item_SetupKeywordHash(void);
void Menu_SetupKeywordHash(void);
itemDef_t *Menu_SetPrevCursorItem(menuDef_t *menu);
itemDef_t *Menu_SetNextCursorItem(menuDef_t *menu);
static void Item_TextScroll_BuildLines ( itemDef_t* item );
void Menu_SetItemText(const menuDef_t *menu,const char *itemName, const char *text);
extern qboolean ItemParse_asset_model_go( itemDef_t *item, const char *name,int *runTimeLength );
extern qboolean ItemParse_model_g2anim_go( itemDef_t *item, const char *animName );

void UIC_SaveMenu( FILE *f, menuDef_t *menu );
void UIC_LoadMenu( FILE *f, menuDef_t *menu );

#ifdef CGAME
#define MEM_POOL_SIZE  128 * 1024
#define UI_ALLOCATION_TAG	TAG_CG_UI_ALLOC
#else
//#define MEM_POOL_SIZE  1024 * 1024
#define MEM_POOL_SIZE  2048 * 1024
#define	UI_ALLOCATION_TAG	TAG_UI_ALLOC
#endif

#ifndef _XBOX
static char		memoryPool[MEM_POOL_SIZE];
#endif // _XBOX

static int		allocPoint, outOfMemory;


typedef struct  itemFlagsDef_s {
	char *string;
	int value;
}	itemFlagsDef_t;

itemFlagsDef_t itemFlags [] = {
"WINDOW_INACTIVE",		WINDOW_INACTIVE,
NULL,					(int) NULL
};

char *styles [] = {
"WINDOW_STYLE_EMPTY",
"WINDOW_STYLE_FILLED",
"WINDOW_STYLE_GRADIENT",
"WINDOW_STYLE_SHADER",
"WINDOW_STYLE_TEAMCOLOR",
"WINDOW_STYLE_CINEMATIC",
NULL
};

char *alignment [] = {
"ITEM_ALIGN_LEFT",
"ITEM_ALIGN_CENTER",
"ITEM_ALIGN_RIGHT",
NULL
};

char *types [] = {
"ITEM_TYPE_TEXT",
"ITEM_TYPE_BUTTON",
"ITEM_TYPE_RADIOBUTTON",
"ITEM_TYPE_CHECKBOX",
"ITEM_TYPE_EDITFIELD",
"ITEM_TYPE_COMBO",
"ITEM_TYPE_LISTBOX",
"ITEM_TYPE_MODEL",
"ITEM_TYPE_OWNERDRAW",
"ITEM_TYPE_NUMERICFIELD",
"ITEM_TYPE_SLIDER",
"ITEM_TYPE_YESNO",
"ITEM_TYPE_MULTI",
"ITEM_TYPE_BIND",
"ITEM_TYPE_TEXTSCROLL",
NULL
};


extern int MenuFontToHandle(int iMenuFont);




/*
===============
UI_Alloc
===============
*/
void *UI_Alloc( int size ) {
	allocPoint += size;
	return Z_Malloc(size, UI_ALLOCATION_TAG, qfalse, 4);
}

/*
===============
UI_InitMemory
===============
*/
void UI_InitMemory( void ) {
	allocPoint = 0;
	outOfMemory = qfalse;
	Z_TagFree(UI_ALLOCATION_TAG);
}

qboolean UI_OutOfMemory() {
	return outOfMemory;
}





#define HASH_TABLE_SIZE 2048
/*
================
return a hash value for the string
================
*/
static long hashForString(const char *str) {
	int		i;
	long	hash;
	char	letter;

	hash = 0;
	i = 0;
	while (str[i] != '\0') {
		letter = tolower((unsigned char)str[i]);
		hash+=(long)(letter)*(i+119);
		i++;
	}
	hash &= (HASH_TABLE_SIZE-1);
	return hash;
}

typedef struct stringDef_s {
	struct stringDef_s *next;
	const char *str;
} stringDef_t;

static int strPoolIndex = 0;
static char strPool[STRING_POOL_SIZE];

static int strHandleCount = 0;
static stringDef_t *strHandle[HASH_TABLE_SIZE];


const char *String_Alloc(const char *p) {
	int len;
	long hash;
	stringDef_t *str, *last;
	static const char *staticNULL = "";

	if (p == NULL) {
		return NULL;
	}

	if (*p == 0) {
		return staticNULL;
	}

	hash = hashForString(p);

	str = strHandle[hash];
	while (str) {
		if (strcmp(p, str->str) == 0) {
			return str->str;
		}
		str = str->next;
	}

	len = strlen(p);
	if (len + strPoolIndex + 1 < STRING_POOL_SIZE) {
		int ph = strPoolIndex;
		strcpy(&strPool[strPoolIndex], p);
		strPoolIndex += len + 1;

		str = strHandle[hash];
		last = str;
		while (last && last->next) 
		{
			last = last->next;
		}

		str  = (stringDef_t *) UI_Alloc(sizeof(stringDef_t));
		str->next = NULL;
		str->str = &strPool[ph];
		if (last) {
			last->next = str;
		} else {
			strHandle[hash] = str;
		}
		return &strPool[ph];
	}

	//Increase STRING_POOL_SIZE.
	assert(0);
	return NULL;
}

void String_Report() {
	float f;
	Com_Printf("Memory/String Pool Info\n");
	Com_Printf("----------------\n");
	f = strPoolIndex;
	f /= STRING_POOL_SIZE;
	f *= 100;
	Com_Printf("String Pool is %.1f%% full, %i bytes out of %i used.\n", f, strPoolIndex, STRING_POOL_SIZE);
	f = allocPoint;
	f /= MEM_POOL_SIZE;
	f *= 100;
	Com_Printf("Memory Pool is %.1f%% full, %i bytes out of %i used.\n", f, allocPoint, MEM_POOL_SIZE);
}

/*
=================
String_Init
=================
*/
void String_Init() {
	int i;
	for (i = 0; i < HASH_TABLE_SIZE; i++) {
		strHandle[i] = 0;
	}
	strHandleCount = 0;
	strPoolIndex = 0;
	menuCount = 0;
	openMenuCount = 0;
	UI_InitMemory();
	Item_SetupKeywordHash();
	Menu_SetupKeywordHash();
}

/*
=================
PC_SourceWarning
=================
*/
void PC_SourceWarning(int handle, char *format, ...) {
	int line;
	char filename[128];
	va_list argptr;
	static char string[4096];

	va_start (argptr, format);
	vsprintf (string, format, argptr);
	va_end (argptr);

	filename[0] = '\0';
	line = 0;
	trap_PC_SourceFileAndLine(handle, filename, &line);

	Com_Printf(S_COLOR_YELLOW "WARNING: %s, line %d: %s\n", filename, line, string);
}

/*
=================
PC_SourceError
=================
*/
void PC_SourceError(int handle, char *format, ...) {
	int line;
	char filename[128];
	va_list argptr;
	static char string[4096];

	va_start (argptr, format);
	vsprintf (string, format, argptr);
	va_end (argptr);

	filename[0] = '\0';
	line = 0;
	trap_PC_SourceFileAndLine(handle, filename, &line);

	Com_Printf(S_COLOR_RED "ERROR: %s, line %d: %s\n", filename, line, string);
}

/*
=================
LerpColor
=================
*/
void LerpColor(vec4_t a, vec4_t b, vec4_t c, float t)
{
	int i;

	// lerp and clamp each component
	for (i=0; i<4; i++)
	{
		c[i] = a[i] + t*(b[i]-a[i]);
		if (c[i] < 0)
			c[i] = 0;
		else if (c[i] > 1.0)
			c[i] = 1.0;
	}
}

/*
=================
Float_Parse
=================
*/
qboolean Float_Parse(char **p, float *f) {
	char	*token;
	token = COM_ParseExt((const char **)p, qfalse);
	if (token && token[0] != 0) {
		*f = atof(token);
		return qtrue;
	} else {
		return qfalse;
	}
}

/*
=================
PC_Float_Parse
=================
*/
qboolean PC_Float_Parse(int handle, float *f) {
	pc_token_t token;
	int negative = qfalse;

	if (!trap_PC_ReadToken(handle, &token))
		return qfalse;
	if (token.string[0] == '-') {
		if (!trap_PC_ReadToken(handle, &token))
			return qfalse;
		negative = qtrue;
	}
	if (token.type != TT_NUMBER) {
		PC_SourceError(handle, "expected float but found %s\n", token.string);
		return qfalse;
	}
	if (negative)
		*f = -token.floatvalue;
	else
		*f = token.floatvalue;
	return qtrue;
}

/*
=================
Color_Parse
=================
*/
qboolean Color_Parse(char **p, vec4_t *c) {
	int i;
	float f;

	for (i = 0; i < 4; i++) {
		if (!Float_Parse(p, &f)) {
			return qfalse;
		}
		(*c)[i] = f;
	}
	return qtrue;
}

/*
=================
PC_Color_Parse
=================
*/
qboolean PC_Color_Parse(int handle, vec4_t *c) {
	int i;
	float f;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		(*c)[i] = f;
	}
	return qtrue;
}

/*
=================
Int_Parse
=================
*/
qboolean Int_Parse(char **p, int *i) {
	char	*token;
	token = COM_ParseExt((const char **)p, qfalse);

	if (token && token[0] != 0) {
		*i = atoi(token);
		return qtrue;
	} else {
		return qfalse;
	}
}

/*
=================
PC_Int_Parse
=================
*/
qboolean PC_Int_Parse(int handle, int *i) {
	pc_token_t token;
	int negative = qfalse;

	if (!trap_PC_ReadToken(handle, &token))
		return qfalse;
	if (token.string[0] == '-') {
		if (!trap_PC_ReadToken(handle, &token))
			return qfalse;
		negative = qtrue;
	}
	if (token.type != TT_NUMBER) {
		PC_SourceError(handle, "expected integer but found %s\n", token.string);
		return qfalse;
	}
	*i = token.intvalue;
	if (negative)
		*i = - *i;
	return qtrue;
}

/*
=================
Rect_Parse
=================
*/
qboolean Rect_Parse(char **p, rectDef_t *r) {
	if (Float_Parse(p, &r->x)) {
		if (Float_Parse(p, &r->y)) {
			if (Float_Parse(p, &r->w)) {
				if (Float_Parse(p, &r->h)) {
					return qtrue;
				}
			}
		}
	}
	return qfalse;
}

/*
=================
PC_Rect_Parse
=================
*/
qboolean PC_Rect_Parse(int handle, rectDef_t *r) {
	if (PC_Float_Parse(handle, &r->x)) {
		if (PC_Float_Parse(handle, &r->y)) {
			if (PC_Float_Parse(handle, &r->w)) {
				if (PC_Float_Parse(handle, &r->h)) {
					return qtrue;
				}
			}
		}
	}
	return qfalse;
}

/*
=================
String_Parse
=================
*/
qboolean String_Parse(char **p, const char **out) {
	char *token;

	token = COM_ParseExt((const char **)p, qfalse);
	if (token && token[0] != 0) {
		*(out) = String_Alloc(token);
		return *(out)!=NULL;
	}
	return qfalse;
}

/*
=================
PC_String_Parse
=================
*/
qboolean PC_String_Parse(int handle, const char **out) 
{
	static char*	squiggy = "}";
	pc_token_t		token;

	if (!trap_PC_ReadToken(handle, &token))
	{
		return qfalse;
	}

	// Save some memory by not return the end squiggy as an allocated string
	if ( !Q_stricmp ( token.string, "}" ) )
	{
		*(out) = squiggy;
	}
	else
	{
		*(out) = String_Alloc(token.string);
	}
	return qtrue;
}

/*
=================
PC_Script_Parse
=================
*/
qboolean PC_Script_Parse(int handle, const char **out) {
	char script[2048];
	pc_token_t token;

	script[0] = 0;
	// scripts start with { and have ; separated command lists.. commands are command, arg.. 
	// basically we want everything between the { } as it will be interpreted at run time
  
	if (!trap_PC_ReadToken(handle, &token))
		return qfalse;
	if (Q_stricmp(token.string, "{") != 0) {
	    return qfalse;
	}

	while ( 1 ) {
		if (!trap_PC_ReadToken(handle, &token))
			return qfalse;

		if (Q_stricmp(token.string, "}") == 0) {
			*out = String_Alloc(script);
			return qtrue;
		}

		if (token.string[1] != '\0') {
			Q_strcat(script, 2048, va("\"%s\"", token.string));
		} else {
			Q_strcat(script, 2048, token.string);
		}
		Q_strcat(script, 2048, " ");
	}
	return qfalse; 	// bk001105 - LCC   missing return value
}

// display, window, menu, item code
// 

/*
==================
Init_Display

Initializes the display with a structure to all the drawing routines
 ==================
*/
void Init_Display(displayContextDef_t *dc) {
	DC = dc;
}



// type and style painting 

void GradientBar_Paint(rectDef_t *rect, vec4_t color) {
	// gradient bar takes two paints
	DC->setColor( color );
//	DC->drawHandlePic(rect->x, rect->y, rect->w, rect->h, DC->Assets.gradientBar);
	DC->setColor( NULL );
}


/*
==================
Window_Init

Initializes a window structure ( windowDef_t ) with defaults
 
==================
*/
void Window_Init(Window *w) {
	memset(w, 0, sizeof(windowDef_t));
	w->borderSize = 1;
	w->foreColor[0] = w->foreColor[1] = w->foreColor[2] = w->foreColor[3] = 1.0;
//	w->cinematic = -1;
}

void Fade(int *flags, float *f, float clamp, int *nextTime, int offsetTime, qboolean bFlags, float fadeAmount) {
  if (*flags & (WINDOW_FADINGOUT | WINDOW_FADINGIN)) {
    if (DC->realTime > *nextTime) {
      *nextTime = DC->realTime + offsetTime;
      if (*flags & WINDOW_FADINGOUT) {
        *f -= fadeAmount;
        if (bFlags && *f <= 0.0) {
          *flags &= ~(WINDOW_FADINGOUT | WINDOW_VISIBLE);
        }
      } else {
        *f += fadeAmount;
        if (*f >= clamp) {
          *f = clamp;
          if (bFlags) {
            *flags &= ~WINDOW_FADINGIN;
          }
        }
      }
    }
  }
}



void Window_Paint(Window *w, float fadeAmount, float fadeClamp, float fadeCycle) 
{
	//float bordersize = 0;
	vec4_t color;
	rectDef_t fillRect = w->rect;


	if (debugMode) 
	{
		color[0] = color[1] = color[2] = color[3] = 1;
		DC->drawRect(w->rect.x, w->rect.y, w->rect.w, w->rect.h, 1, color);
	}

	if (w == NULL || (w->style == 0 && w->border == 0)) 
	{
		return;
	}

	if (w->border != 0) 
	{
		fillRect.x += w->borderSize;
		fillRect.y += w->borderSize;
		fillRect.w -= w->borderSize + 1;
		fillRect.h -= w->borderSize + 1;
	}

	if (w->style == WINDOW_STYLE_FILLED) 
	{
		// box, but possible a shader that needs filled
		if (w->background) 
		{
			Fade(&w->flags, &w->backColor[3], fadeClamp, &w->nextTime, fadeCycle, qtrue, fadeAmount);
			DC->setColor(w->backColor);
			DC->drawHandlePic(fillRect.x, fillRect.y, fillRect.w, fillRect.h, w->background);
			DC->setColor(NULL);
		} 
		else 
		{
			DC->fillRect(fillRect.x, fillRect.y, fillRect.w, fillRect.h, w->backColor);
		}
	} 
	else if (w->style == WINDOW_STYLE_GRADIENT) 
	{
		GradientBar_Paint(&fillRect, w->backColor);
	// gradient bar
	} 
	else if (w->style == WINDOW_STYLE_SHADER) 
	{
#ifndef CGAME
		if (w->flags & WINDOW_PLAYERCOLOR) 
		{
			vec4_t	color;
			color[0] = ui_char_color_red.integer/255.0f;
			color[1] = ui_char_color_green.integer/255.0f;
			color[2] = ui_char_color_blue.integer/255.0f;
			color[3] = 1;
			DC->setColor(color);
		}
#endif // 

		if (w->flags & WINDOW_FORECOLORSET) 
		{
			DC->setColor(w->foreColor);
		}
		DC->drawHandlePic(fillRect.x, fillRect.y, fillRect.w, fillRect.h, w->background);
		DC->setColor(NULL);
	} 
	else if (w->style == WINDOW_STYLE_TEAMCOLOR) 
	{
		if (DC->getTeamColor) 
		{
			DC->getTeamColor(&color);
			DC->fillRect(fillRect.x, fillRect.y, fillRect.w, fillRect.h, color);
		}
	} 
/*
	else if (w->style == WINDOW_STYLE_CINEMATIC) 
	{
		if (w->cinematic == -1) 
		{
			w->cinematic = DC->playCinematic(w->cinematicName, fillRect.x, fillRect.y, fillRect.w, fillRect.h);
			if (w->cinematic == -1) 
			{
				w->cinematic = -2;
			}
		} 
		if (w->cinematic >= 0) 
		{
			DC->runCinematicFrame(w->cinematic);
			DC->drawCinematic(w->cinematic, fillRect.x, fillRect.y, fillRect.w, fillRect.h);
		}
	}
*/
	if (w->border == WINDOW_BORDER_FULL) 
	{
		// full
		// HACK HACK HACK
		if (w->style == WINDOW_STYLE_TEAMCOLOR) 
		{
			if (color[0] > 0) 
			{				
				// red
				color[0] = 1;
				color[1] = color[2] = .5;
			} 
			else 
			{
				color[2] = 1;
				color[0] = color[1] = .5;
			}
			color[3] = 1;
			DC->drawRect(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize, color);
		} 
		else 
		{
			DC->drawRect(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize, w->borderColor);
		}
	} 
	else if (w->border == WINDOW_BORDER_HORZ) 
	{
		// top/bottom
		DC->setColor(w->borderColor);
		DC->drawTopBottom(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize);
		DC->setColor( NULL );
	} 
	else if (w->border == WINDOW_BORDER_VERT) 
	{
		// left right
		DC->setColor(w->borderColor);
		DC->drawSides(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize);
		DC->setColor( NULL );
	} 
	else if (w->border == WINDOW_BORDER_KCGRADIENT) 
	{
		// this is just two gradient bars along each horz edge
		rectDef_t r = w->rect;
		r.h = w->borderSize;
		GradientBar_Paint(&r, w->borderColor);
		r.y = w->rect.y + w->rect.h - 1;
		GradientBar_Paint(&r, w->borderColor);
	}
}


void Item_SetScreenCoords(itemDef_t *item, float x, float y) 
{ 

	if (item == NULL) 
	{
		return;
	}

	if (item->window.border != 0) 
	{
		x += item->window.borderSize;
		y += item->window.borderSize;
	}

	item->window.rect.x = x + item->window.rectClient.x;
	item->window.rect.y = y + item->window.rectClient.y;
	item->window.rect.w = item->window.rectClient.w;
	item->window.rect.h = item->window.rectClient.h;

	// force the text rects to recompute
	item->textRect.w = 0;
	item->textRect.h = 0;

	switch ( item->type)
	{
		case ITEM_TYPE_TEXTSCROLL:
		{
			textScrollDef_t *scrollPtr = (textScrollDef_t*)item->typeData;
			if ( scrollPtr )
			{
				scrollPtr->startPos = 0;
				scrollPtr->endPos = 0;
			}

			Item_TextScroll_BuildLines ( item );

			break;
		}
	}
}

// FIXME: consolidate this with nearby stuff
void Item_UpdatePosition(itemDef_t *item) 
{
	float x, y;
	menuDef_t *menu;
  
	if (item == NULL || item->parent == NULL) 
	{
		return;
	}

	menu = (menuDef_t *) item->parent;

	x = menu->window.rect.x;
	y = menu->window.rect.y;
  
	if (menu->window.border != 0) 
	{
		x += menu->window.borderSize;
		y += menu->window.borderSize;
	}

	Item_SetScreenCoords(item, x, y);

}

// menus
void Menu_UpdatePosition(menuDef_t *menu) {
  int i;
  float x, y;

  if (menu == NULL) {
    return;
  }
  
  x = menu->window.rect.x;
  y = menu->window.rect.y;
  if (menu->window.border != 0) {
    x += menu->window.borderSize;
    y += menu->window.borderSize;
  }

  for (i = 0; i < menu->itemCount; i++) {
    Item_SetScreenCoords(menu->items[i], x, y);
  }
}

void Menu_PostParse(menuDef_t *menu) {
	if (menu == NULL) {
		return;
	}
	if (menu->fullScreen) {
		menu->window.rect.x = 0;
		menu->window.rect.y = 0;
#ifdef _XBOX
		if(cg->widescreen)
			menu->window.rect.w = 720;
		else
#endif
		menu->window.rect.w = 640;
		menu->window.rect.h = 480;
	}
	Menu_UpdatePosition(menu);
}

itemDef_t *Menu_ClearFocus(menuDef_t *menu) {
  int i;
  itemDef_t *ret = NULL;

  if (menu == NULL) {
    return NULL;
  }

  for (i = 0; i < menu->itemCount; i++) {
    if (menu->items[i]->window.flags & WINDOW_HASFOCUS) {
		ret = menu->items[i];
//JLF } 
		menu->items[i]->window.flags &= ~WINDOW_HASFOCUS;
		if (menu->items[i]->leaveFocus) {
			Item_RunScript(menu->items[i], menu->items[i]->leaveFocus);
		}
//JLF
	}
  }
 
  return ret;
}

qboolean IsVisible(int flags) {
  return (flags & WINDOW_VISIBLE && !(flags & WINDOW_FADINGOUT));
}

qboolean Rect_ContainsPoint(rectDef_t *rect, float x, float y) {
  if (rect) {
    if (x > rect->x && x < rect->x + rect->w && y > rect->y && y < rect->y + rect->h) {
      return qtrue;
    }
  }
  return qfalse;
}

int Menu_ItemsMatchingGroup(menuDef_t *menu, const char *name) 
{
	int i;
	int count = 0;

	for (i = 0; i < menu->itemCount; i++) 
	{
		if ((!menu->items[i]->window.name) && (!menu->items[i]->window.group))
		{
			Com_Printf(S_COLOR_YELLOW"WARNING: item has neither name or group\n");
			continue;
		}

		if (Q_stricmp(menu->items[i]->window.name, name) == 0 || 
			(menu->items[i]->window.group && Q_stricmp(menu->items[i]->window.group, name) == 0)) 
		{
			count++;
		} 
	}

	return count;
}

itemDef_t *Menu_GetMatchingItemByNumber(menuDef_t *menu, int index, const char *name) {
  int i;
  int count = 0;
  for (i = 0; i < menu->itemCount; i++) {
    if (Q_stricmp(menu->items[i]->window.name, name) == 0 || (menu->items[i]->window.group && Q_stricmp(menu->items[i]->window.group, name) == 0)) {
      if (count == index) {
        return menu->items[i];
      }
      count++;
    } 
  }
  return NULL;
}

qboolean Script_SetColor ( itemDef_t *item, char **args ) 
{
	const char *name;
	int i;
	float f;
	vec4_t *out;
	
	// expecting type of color to set and 4 args for the color
	if (String_Parse(args, &name)) 
	{
		out = NULL;
		if (Q_stricmp(name, "backcolor") == 0) 
		{
			out = &item->window.backColor;
			item->window.flags |= WINDOW_BACKCOLORSET;
		} 
		else if (Q_stricmp(name, "forecolor") == 0) 
		{
			out = &item->window.foreColor;
			item->window.flags |= WINDOW_FORECOLORSET;
		} 
		else if (Q_stricmp(name, "bordercolor") == 0) 
		{
			out = &item->window.borderColor;
		}

		if (out) 
		{
			for (i = 0; i < 4; i++) 
			{
				if (!Float_Parse(args, &f)) 
				{
					return qtrue;
				}
			(*out)[i] = f;
			}
		}
	}

	return qtrue;
}

qboolean Script_SetAsset(itemDef_t *item, char **args) 
{
	const char *name;
	// expecting name to set asset to
	if (String_Parse(args, &name)) 
	{
		// check for a model 
		if (item->type == ITEM_TYPE_MODEL) 
		{
		}
	}
	return qtrue;
}

qboolean Script_SetBackground(itemDef_t *item, char **args) 
{
	const char *name;
	// expecting name to set asset to
	if (String_Parse(args, &name)) 
	{
		item->window.background = DC->registerShaderNoMip(name);
	}
	return qtrue;
}

qboolean Script_SetItemRectCvar(itemDef_t *item, char **args) 
{
	const char	*itemName;
	const char	*cvarName;
	char		cvarBuf[1024];
	const char	*holdVal;
	char		*holdBuf;
	itemDef_t	*item2=0;
	menuDef_t	*menu;	

	// expecting item group and cvar to get value from
	if (String_Parse(args, &itemName) && String_Parse(args, &cvarName)) 
	{
		item2 = Menu_FindItemByName((menuDef_t *) item->parent, itemName);

		if (item2)
		{
			// get cvar data
			DC->getCVarString(cvarName, cvarBuf, sizeof(cvarBuf));
			
			holdBuf = cvarBuf;
			if (String_Parse(&holdBuf,&holdVal))
			{
				menu = (menuDef_t *) item->parent;

				item2->window.rectClient.x = atof(holdVal) + menu->window.rect.x;
				if (String_Parse(&holdBuf,&holdVal))
				{
					item2->window.rectClient.y = atof(holdVal) + menu->window.rect.y;
					if (String_Parse(&holdBuf,&holdVal))
					{
						item2->window.rectClient.w = atof(holdVal);
						if (String_Parse(&holdBuf,&holdVal))
						{
							item2->window.rectClient.h = atof(holdVal);

							item2->window.rect.x = item2->window.rectClient.x;
							item2->window.rect.y = item2->window.rectClient.y;
							item2->window.rect.w = item2->window.rectClient.w;
							item2->window.rect.h = item2->window.rectClient.h;

							return qtrue;
						}
					}
				}
			}
		}
	}

	// Default values in case things screw up
	if (item2)
	{
		item2->window.rectClient.x = 0;
		item2->window.rectClient.y = 0;
		item2->window.rectClient.w = 0;
		item2->window.rectClient.h = 0;
	}

//	Com_Printf(S_COLOR_YELLOW"WARNING: SetItemRectCvar: problems. Set cvar to 0's\n" );

	return qtrue;
}

qboolean Script_SetItemBackground(itemDef_t *item, char **args) 
{
	const char *itemName;
	const char *name;

	// expecting name of shader
	if (String_Parse(args, &itemName) && String_Parse(args, &name)) 
	{
		Menu_SetItemBackground((menuDef_t *) item->parent, itemName, name);
	}
	return qtrue;
}

qboolean Script_SetItemText(itemDef_t *item, char **args) 
{
	const char *itemName;
	const char *text;

	// expecting text
	if (String_Parse(args, &itemName) && String_Parse(args, &text)) 
	{
		Menu_SetItemText((menuDef_t *) item->parent, itemName, text);
	}
	return qtrue;
}


itemDef_t *Menu_FindItemByName(menuDef_t *menu, const char *p) {
  int i;
  if (menu == NULL || p == NULL) {
    return NULL;
  }

  for (i = 0; i < menu->itemCount; i++) {
    if (Q_stricmp(p, menu->items[i]->window.name) == 0) {
      return menu->items[i];
    }
  }

  return NULL;
}

#ifdef _XBOX
qboolean Item_EnableShowViaCvar( itemDef_t *item, int flag );
itemDef_t *Menu_FindVisibleItemByName(menuDef_t *menu, const char *p) 
{
	int i;
	if (menu == NULL || p == NULL) 
	{
		return NULL;
	}

	for (i = 0; i < menu->itemCount; i++) 
	{
		if (Q_stricmp(p, menu->items[i]->window.name) == 0) 
		{
			if (menu->items[i]->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(menu->items[i], CVAR_ENABLE)) 
			{
				continue;
			}

			if (menu->items[i]->cvarFlags & (CVAR_SHOW | CVAR_HIDE) && !Item_EnableShowViaCvar(menu->items[i], CVAR_SHOW)) 
			{
				continue;
			}

			if ( !(menu->items[i]->window.flags & WINDOW_VISIBLE) )
			{
				continue;
			}

			return menu->items[i];
		}
	}

	return NULL;
}
#endif

qboolean Script_SetTeamColor(itemDef_t *item, char **args) 
{
	if (DC->getTeamColor) 
	{
		int i;
		vec4_t color;
		DC->getTeamColor(&color);
		for (i = 0; i < 4; i++) 
		{
			item->window.backColor[i] = color[i];
		}
	}
	return qtrue;
}

qboolean Script_SetItemColor(itemDef_t *item, char **args) 
{
	const char *itemname;
	const char *name;
	vec4_t color;
	int i;
	vec4_t *out;

	// expecting type of color to set and 4 args for the color
	if (String_Parse(args, &itemname) && String_Parse(args, &name)) 
	{
		itemDef_t	*item2;
		int			j,count;
		char buff[1024];

		// Is is specifying a cvar to get the item name from?
		if (itemname[0] == '*')
		{
			itemname += 1;
		    DC->getCVarString(itemname, buff, sizeof(buff));
			itemname = buff;
		}

		count = Menu_ItemsMatchingGroup((menuDef_t *) item->parent, itemname);

		if (!Color_Parse(args, &color)) 
		{
			return qtrue;
		}

		for (j = 0; j < count; j++) 
		{
			item2 = Menu_GetMatchingItemByNumber((menuDef_t *) item->parent, j, itemname);
			if (item2 != NULL) 
			{
				out = NULL;
				if (Q_stricmp(name, "backcolor") == 0) 
				{
					out = &item2->window.backColor;
				} 
				else if (Q_stricmp(name, "forecolor") == 0) 
				{
					out = &item2->window.foreColor;
					item2->window.flags |= WINDOW_FORECOLORSET;
				} 
				else if (Q_stricmp(name, "bordercolor") == 0) 
				{
					out = &item2->window.borderColor;
				}

				if (out) 
				{
					for (i = 0; i < 4; i++) 
					{
						(*out)[i] = color[i];
					}
				}
			}
		}
	}

	return qtrue;
}

qboolean Script_SetFocusColor(itemDef_t *item, char **args) 
{
	menuDef_t *menu = (menuDef_t *) item->parent;

	if (!Color_Parse(args, &menu->focusColor)) 
	{
		return qtrue;
	}

	return qtrue;
}

qboolean Script_SetItemColorCvar(itemDef_t *item, char **args) 
{
	const char *itemname;
	char	*colorCvarName,*holdBuf,*holdVal;
	char cvarBuf[1024];
	const char *name;
	vec4_t color;
	int i;
	vec4_t *out;

	// expecting type of color to set and 4 args for the color
	if (String_Parse(args, &itemname) && String_Parse(args, &name)) 
	{
		itemDef_t	*item2;
		int			j,count;
		char buff[1024];

		// Is is specifying a cvar to get the item name from?
		if (itemname[0] == '*')
		{
			itemname += 1;
		    DC->getCVarString(itemname, buff, sizeof(buff));
			itemname = buff;
		}

		count = Menu_ItemsMatchingGroup((menuDef_t *) item->parent, itemname);

		// Get the cvar with the color
		if (!String_Parse(args,(const char **) &colorCvarName))
		{
			return qtrue;
		}
		else
		{
			DC->getCVarString(colorCvarName, cvarBuf, sizeof(cvarBuf));
			
			holdBuf = cvarBuf;
			if (String_Parse(&holdBuf,(const char **) &holdVal))
			{
				color[0] = atof(holdVal);
				if (String_Parse(&holdBuf,(const char **) &holdVal))
				{
					color[1] = atof(holdVal);
					if (String_Parse(&holdBuf,(const char **) &holdVal))
					{
						color[2] = atof(holdVal);
						if (String_Parse(&holdBuf,(const char **) &holdVal))
						{
							color[3] = atof(holdVal);
						}
					}
				}
			}
		}

		for (j = 0; j < count; j++) 
		{
			item2 = Menu_GetMatchingItemByNumber((menuDef_t *) item->parent, j, itemname);
			if (item2 != NULL) 
			{
				out = NULL;
				if (Q_stricmp(name, "backcolor") == 0) 
				{
					out = &item2->window.backColor;
				} 
				else if (Q_stricmp(name, "forecolor") == 0) 
				{
					out = &item2->window.foreColor;
					item2->window.flags |= WINDOW_FORECOLORSET;
				} 
				else if (Q_stricmp(name, "bordercolor") == 0) 
				{
					out = &item2->window.borderColor;
				}

				if (out) 
				{
					for (i = 0; i < 4; i++) 
					{
						(*out)[i] = color[i];
					}
				}
			}
		}
	}

	return qtrue;
}

qboolean Script_SetItemRect(itemDef_t *item, char **args) 
{
	const char *itemname;
	rectDef_t *out;
	rectDef_t rect;
	menuDef_t	*menu;	

	// expecting type of color to set and 4 args for the color
	if (String_Parse(args, &itemname)) 
	{
		itemDef_t *item2;
		int j;
		int count = Menu_ItemsMatchingGroup((menuDef_t *) item->parent, itemname);

		if (!Rect_Parse(args, &rect)) 
		{
			return qtrue;
		}

		menu = (menuDef_t *) item->parent;

		for (j = 0; j < count; j++) 
		{
			item2 = Menu_GetMatchingItemByNumber(menu, j, itemname);
			if (item2 != NULL) 
			{
				out = &item2->window.rect;

				if (out) 
				{
					item2->window.rect.x = rect.x  + menu->window.rect.x;
					item2->window.rect.y = rect.y  + menu->window.rect.y;
					item2->window.rect.w = rect.w;
					item2->window.rect.h = rect.h;					
				}
			}
		}
	}
	return qtrue;
}

void Menu_ShowGroup (menuDef_t *menu, char *groupName, qboolean showFlag)
{
	itemDef_t *item;
	int count,j;

	count = Menu_ItemsMatchingGroup( menu, groupName);
	for (j = 0; j < count; j++) 
	{
		item = Menu_GetMatchingItemByNumber( menu, j, groupName);
		if (item != NULL) 
		{
			if (showFlag) 
			{
				item->window.flags |= WINDOW_VISIBLE;
			} 
			else 
			{
				item->window.flags &= ~(WINDOW_VISIBLE | WINDOW_HASFOCUS);
			}
		}
	}
}

/*
==========
Menu_SetItemDecorationByName
==========
*/
void Menu_SetItemDecorationByName(menuDef_t* menu, const char *p, qboolean bShow)
{
	itemDef_t *item;
	int i;
	int count;

	count = Menu_ItemsMatchingGroup(menu, p);

	if (!count)
	{
		Com_Printf(S_COLOR_YELLOW"WARNING: Menu_SetItemDecorationByName - unable to locate any items named :%s\n",p);
	}

	for (i = 0; i < count; i++) 
	{
		item = Menu_GetMatchingItemByNumber(menu, i, p);
		if (item != NULL) 
		{
			if (bShow) 
			{
				item->window.flags |= WINDOW_DECORATION;
			} 
			else 
			{
				item->window.flags &= ~WINDOW_DECORATION;
			}
		}
	}
}

void Menu_ShowItemByName(menuDef_t *menu, const char *p, qboolean bShow) {
	itemDef_t *item;
	int i;
	int count = Menu_ItemsMatchingGroup(menu, p);
	for (i = 0; i < count; i++) {
		item = Menu_GetMatchingItemByNumber(menu, i, p);
		if (item != NULL) {
			if (bShow) {
				item->window.flags |= WINDOW_VISIBLE;
			} else {
				item->window.flags &= ~WINDOW_VISIBLE;
				// stop cinematics playing in the window
/*
				if (item->window.cinematic >= 0) {
					DC->stopCinematic(item->window.cinematic);
					item->window.cinematic = -1;
				}
*/
			}
		}
	}
}

void Menu_FadeItemByName(menuDef_t *menu, const char *p, qboolean fadeOut) {
  itemDef_t *item;
  int i;
  int count = Menu_ItemsMatchingGroup(menu, p);
  for (i = 0; i < count; i++) {
    item = Menu_GetMatchingItemByNumber(menu, i, p);
    if (item != NULL) {
      if (fadeOut) {
        item->window.flags |= (WINDOW_FADINGOUT | WINDOW_VISIBLE);
        item->window.flags &= ~WINDOW_FADINGIN;
      } else {
        item->window.flags |= (WINDOW_VISIBLE | WINDOW_FADINGIN);
        item->window.flags &= ~WINDOW_FADINGOUT;
      }
    }
  }
}

menuDef_t *Menus_FindByName(const char *p) {
  int i;
  for (i = 0; i < menuCount; i++) {
    if (Q_stricmp(Menus[i].window.name, p) == 0) {
      return &Menus[i];
    } 
  }
  return NULL;
}

void Menus_ShowByName(const char *p) {
	menuDef_t *menu = Menus_FindByName(p);
	if (menu) {
		Menus_Activate(menu);
	}
}

void Menus_OpenByName(const char *p) {
  Menus_ActivateByName(p);
}

static void Menu_RunCloseScript(menuDef_t *menu) {
	if (menu && menu->window.flags & WINDOW_VISIBLE && menu->onClose) {
		itemDef_t item;
    item.parent = menu;
    Item_RunScript(&item, menu->onClose);
	}
}

void Menus_CloseByName ( const char *p )
{
	menuDef_t *menu = Menus_FindByName(p);
	
	// If the menu wasnt found just exit
	if (menu == NULL) 
	{
		return;
	}

	// Run the close script for the menu
	Menu_RunCloseScript(menu);

	// If this window had the focus then take it away
	if ( menu->window.flags & WINDOW_HASFOCUS )
	{	
		// If there is something still in the open menu list then
		// set it to have focus now
		if ( openMenuCount )
		{
			// Subtract one from the open menu count to prepare to
			// remove the top menu from the list
			openMenuCount -= 1;

			// Set the top menu to have focus now
			menuStack[openMenuCount]->window.flags |= WINDOW_HASFOCUS;

			// Remove the top menu from the list
			menuStack[openMenuCount] = NULL;
		}
	}

	// Window is now invisible and doenst have focus
	menu->window.flags &= ~(WINDOW_VISIBLE | WINDOW_HASFOCUS);
}

int FPMessageTime = 0;

void Menus_CloseAll() 
{
	int i;
		
	for (i = 0; i < menuCount; i++) 
	{
		// check for the noController pop-up ( this cannot be closed here )
		if(!strcmp(Menus[i].window.name, "noController"))
			continue;

		Menu_RunCloseScript ( &Menus[i] );
		Menus[i].window.flags &= ~(WINDOW_HASFOCUS | WINDOW_VISIBLE);
	}

	// Clear the menu stack
	openMenuCount = 0;

	FPMessageTime = 0;
}

qboolean Script_Show(itemDef_t *item, char **args) 
{
	const char *name;
	if (String_Parse(args, &name)) 
	{
		Menu_ShowItemByName((menuDef_t *) item->parent, name, qtrue);
	}
	return qtrue;
}

/*
==========
Script_SetDecoration
==========
*/
qboolean Script_SetDecoration(itemDef_t* item, char **args)
{
	const char *name;
	int val;
	if (String_Parse(args, &name))
	{
		if(!strcmp("vstr", name))
		{
			if(String_Parse(args, &name))
			{
				if(Int_Parse( args, &val))
				{
					Menu_SetItemDecorationByName((menuDef_t*) item->parent, Cvar_VariableString(name), val );
					return qtrue;
				}
			}
		}
		else
		{
			if(Int_Parse( args, &val))
			{
				Menu_SetItemDecorationByName((menuDef_t*) item->parent, name, val );
				return qtrue;
			}
		}
	}
	return qfalse;
}

qboolean Script_Hide(itemDef_t *item, char **args) 
{
	const char *name;
	if (String_Parse(args, &name)) 
	{
		Menu_ShowItemByName((menuDef_t *) item->parent, name, qfalse);
	}
	return qtrue;
}

qboolean Script_FadeIn(itemDef_t *item, char **args) 
{
	const char *name;
	if (String_Parse(args, &name)) 
	{
		Menu_FadeItemByName((menuDef_t *) item->parent, name, qfalse);
	}

	return qtrue;
}

qboolean Script_FadeOut(itemDef_t *item, char **args) 
{
	const char *name;
	if (String_Parse(args, &name)) 
	{
		Menu_FadeItemByName((menuDef_t *) item->parent, name, qtrue);
	}
	return qtrue;
}

qboolean Script_Open(itemDef_t *item, char **args) 
{
	const char *name;
	if (String_Parse(args, &name)) 
	{
		if(!strcmp("vstr",name))
		{
			if(String_Parse(args,&name))
			{
				Menus_OpenByName(Cvar_VariableString(name));
			}
		}
		else
		{
			Menus_OpenByName(name);
		}
	}
	return qtrue;
}

qboolean Script_Close(itemDef_t *item, char **args) 
{
	const char *name;
	if (String_Parse(args, &name)) 
	{
		if (Q_stricmp(name, "all") == 0)
		{
			Menus_CloseAll();
		}
		else
		{
			Menus_CloseByName(name);
		}
	}
	return qtrue;
}

//void Menu_TransitionItemByName(menuDef_t *menu, const char *p, rectDef_t rectFrom, rectDef_t rectTo, int time, float amt) 
void Menu_TransitionItemByName(menuDef_t *menu, const char *p, const rectDef_t *rectFrom, const rectDef_t *rectTo, int time, float amt) 
{
	itemDef_t *item;
	int i;
	int count = Menu_ItemsMatchingGroup(menu, p);
	for (i = 0; i < count; i++) 
	{
		item = Menu_GetMatchingItemByNumber(menu, i, p);
		if (item != NULL) 
		{
			if (!rectFrom)
			{
				rectFrom = &item->window.rect;	//if there are more than one of these with the same name, they'll all use the FIRST one's FROM.
			}
			item->window.flags |= (WINDOW_INTRANSITION | WINDOW_VISIBLE);
			item->window.offsetTime = time;
			memcpy(&item->window.rectClient, rectFrom, sizeof(rectDef_t));
			memcpy(&item->window.rectEffects, rectTo, sizeof(rectDef_t));
			item->window.rectEffects2.x = abs(rectTo->x - rectFrom->x) / amt;
			item->window.rectEffects2.y = abs(rectTo->y - rectFrom->y) / amt;
			item->window.rectEffects2.w = abs(rectTo->w - rectFrom->w) / amt;
			item->window.rectEffects2.h = abs(rectTo->h - rectFrom->h) / amt;

			Item_UpdatePosition(item);
		}
	}
}

/*
=================
Menu_Transition3ItemByName
=================
*/
//JLF
#define _TRANS3
#ifdef _TRANS3
void Menu_Transition3ItemByName(menuDef_t *menu, const char *p, const float minx, const float miny, const float minz,
								const float maxx, const float maxy, const float maxz, const float fovtx, const float fovty,
								const int time, const float amt)
{
	itemDef_t *item;
	int i;
	int count = Menu_ItemsMatchingGroup(menu, p);
	modelDef_t  * modelptr;
	for (i = 0; i < count; i++) 
	{
		item = Menu_GetMatchingItemByNumber(menu, i, p);
		if (item != NULL) 
		{
			if ( item->type == ITEM_TYPE_MODEL)
			{
				modelptr = (modelDef_t*)item->typeData;

				item->window.flags |= (WINDOW_INTRANSITIONMODEL | WINDOW_VISIBLE);
				item->window.offsetTime = time;
				modelptr->fov_x2 = fovtx;
				modelptr->fov_y2 = fovty;
				VectorSet(modelptr->g2maxs2, maxx, maxy, maxz);
				VectorSet(modelptr->g2mins2, minx, miny, minz);

//				//modelptr->g2maxs2.x= maxx;
//				modelptr->g2maxs2.y= maxy;
//				modelptr->g2maxs2.z= maxz;
//				modelptr->g2mins2.x= minx;
//				modelptr->g2mins2.y= miny;
//				modelptr->g2mins2.z= minz;

//				VectorSet(modelptr->g2maxs2, maxx, maxy, maxz);

				modelptr->g2maxsEffect[0] = abs(modelptr->g2maxs2[0] - modelptr->g2maxs[0]) / amt;
				modelptr->g2maxsEffect[1] = abs(modelptr->g2maxs2[1] - modelptr->g2maxs[1]) / amt;
				modelptr->g2maxsEffect[2] = abs(modelptr->g2maxs2[2] - modelptr->g2maxs[2]) / amt;

				modelptr->g2minsEffect[0] = abs(modelptr->g2mins2[0] - modelptr->g2mins[0]) / amt;
				modelptr->g2minsEffect[1] = abs(modelptr->g2mins2[1] - modelptr->g2mins[1]) / amt;
				modelptr->g2minsEffect[2] = abs(modelptr->g2mins2[2] - modelptr->g2mins[2]) / amt;
				

				modelptr->fov_Effectx = abs(modelptr->fov_x2 - modelptr->fov_x) / amt;
				modelptr->fov_Effecty = abs(modelptr->fov_y2 - modelptr->fov_y) / amt;
			}
				
		}
	}
}

#endif





/*
=================
Script_CvarIfEqual

Egads. OK. Script command should be:
cvarIfEqual	<cvarname> <value> <cvarTrueScript> <cvarFalseScript>

If the contents of cvarname are equal to value, then the script in
cvarTrueScript will be executed, otherwise the script in cvarFalseScript
=================
*/
qboolean Script_CvarIfEqual ( itemDef_t* item, char **args )
{
	const char *cvarName;
	const char *testVal;
	const char *cvarTrueScript;
	const char *cvarFalseScript;

	if (!String_Parse(args, &cvarName) || !String_Parse(args, &testVal) ||
		!String_Parse(args, &cvarTrueScript) || !String_Parse(args, &cvarFalseScript))
	{
		Com_Printf("Error parsing cvarIfEqual\n");
		return qfalse;
	}

	bool testResult = (Q_stricmp(Cvar_VariableString(cvarName), testVal) == 0);

	if( testResult )
		Item_RunScript( item, Cvar_VariableString( cvarTrueScript ) );
	else
		Item_RunScript( item, Cvar_VariableString( cvarFalseScript ) );

	return qtrue;
}

#define MAX_DEFERRED_SCRIPT		2048

char		ui_deferredScript [ MAX_DEFERRED_SCRIPT ];
itemDef_t*	ui_deferredScriptItem = NULL;

/*
=================
Script_Defer

Defers the rest of the script based on the defer condition.  The deferred
portion of the script can later be run with the "rundeferred"
=================
*/
qboolean Script_Defer ( itemDef_t* item, char **args )
{
	// Should the script be deferred?
	if ( DC->deferScript ( (char**)args ) )
	{
		// Need the item the script was being run on
		ui_deferredScriptItem = item;

		// Save the rest of the script
		Q_strncpyz ( ui_deferredScript, *args, MAX_DEFERRED_SCRIPT );

		// No more running
		return qfalse;
	}

	// Keep running the script, its ok
	return qtrue;
}

/*
=================
Script_RunDeferred

Runs the last deferred script, there can only be one script deferred at a 
time so be careful of recursion
=================
*/
qboolean Script_RunDeferred ( itemDef_t* item, char **args )
{
	// Make sure there is something to run.
	if ( !ui_deferredScript[0] || !ui_deferredScriptItem )
	{
		return qtrue;
	}

	// Run the deferred script now
	Item_RunScript ( ui_deferredScriptItem, ui_deferredScript );

	return qtrue;
}

/*
=================
Script_Delay

Delays the rest of the script for the specified amount of time.
SP stores a pointer to the script, which is easy. All parsing is
done on stack mem here, so we have to make a copy:
=================
*/

char		ui_delayedScript [ MAX_DEFERRED_SCRIPT ];

qboolean Script_Delay ( itemDef_t* item, char **args )
{
	int time;

	if (Int_Parse(args, &time))
	{
		item->window.flags |= WINDOW_SCRIPTWAITING;
		item->window.delayTime = DC->realTime + time;	// When to resume execution

		// Save the rest of the script
		Q_strncpyz ( ui_delayedScript, *args, MAX_DEFERRED_SCRIPT );
	}
	else
	{
		Com_Printf(S_COLOR_YELLOW"WARNING: Script_Delay: error parsing\n" );
	}

	// Stop running
	return qfalse;
}

qboolean Script_Transition(itemDef_t *item, char **args) 
{
	const char *name;
	rectDef_t rectFrom, rectTo;
	int time;
	float amt;

	if (String_Parse(args, &name)) 
	{
	    if ( Rect_Parse(args, &rectFrom) && Rect_Parse(args, &rectTo) && Int_Parse(args, &time) && Float_Parse(args, &amt)) 
		{
			Menu_TransitionItemByName((menuDef_t *) item->parent, name, &rectFrom, &rectTo, time, amt);
		}
	}

	return qtrue;
}

void Menu_OrbitItemByName(menuDef_t *menu, const char *p, float x, float y, float cx, float cy, int time) 
{
  itemDef_t *item;
  int i;
  int count = Menu_ItemsMatchingGroup(menu, p);
  for (i = 0; i < count; i++) {
    item = Menu_GetMatchingItemByNumber(menu, i, p);
    if (item != NULL) {
      item->window.flags |= (WINDOW_ORBITING | WINDOW_VISIBLE);
      item->window.offsetTime = time;
      item->window.rectEffects.x = cx;
      item->window.rectEffects.y = cy;
      item->window.rectClient.x = x;
      item->window.rectClient.y = y;
      Item_UpdatePosition(item);
    }
  }
}

void Menu_ItemDisable(menuDef_t *menu, char *name,int disableFlag)
{
	int	j,count;
	itemDef_t *itemFound;

	count = Menu_ItemsMatchingGroup(menu, name);
	// Loop through all items that have this name
	for (j = 0; j < count; j++) 
	{
		itemFound = Menu_GetMatchingItemByNumber( menu, j, name);
		if (itemFound != NULL) 
		{
			itemFound->disabled = disableFlag;
			// Just in case it had focus
			itemFound->window.flags &= ~WINDOW_MOUSEOVER;
		}
	}
}

// Set item disable flags
qboolean Script_Disable(itemDef_t *item, char **args) 
{
	char *name;
	int	value;
	menuDef_t *menu;

	if (String_Parse(args, (const char **)&name)) 
	{
		char buff[1024];

		// Is is specifying a cvar to get the item name from?
		if (name[0] == '*')
		{
			name += 1;
		    DC->getCVarString(name, buff, sizeof(buff));
			name = buff;
		}

		if ( Int_Parse(args, &value)) 
		{
			menu = Menu_GetFocused();
			Menu_ItemDisable(menu, name,value);
		}
	}

	return qtrue;
}


// Scale the given item instantly.
qboolean Script_Scale(itemDef_t *item, char **args) 
{
	const char *name;
	float scale;
	int	j,count;
	itemDef_t *itemFound;
	rectDef_t rectTo;

	if (String_Parse(args, &name)) 
	{
		char buff[1024];

		// Is is specifying a cvar to get the item name from?
		if (name[0] == '*')
		{
			name += 1;
		    DC->getCVarString(name, buff, sizeof(buff));
			name = buff;
		}

		count = Menu_ItemsMatchingGroup((menuDef_t *) item->parent, name);

		if ( Float_Parse(args, &scale)) 
		{
			for (j = 0; j < count; j++) 
			{
				itemFound = Menu_GetMatchingItemByNumber( (menuDef_t *) item->parent, j, name);
				if (itemFound != NULL) 
				{
					rectTo.h = itemFound->window.rect.h * scale;
					rectTo.w = itemFound->window.rect.w * scale;

					rectTo.x = itemFound->window.rect.x + ((itemFound->window.rect.h - rectTo.h)/2);
					rectTo.y = itemFound->window.rect.y + ((itemFound->window.rect.w - rectTo.w)/2);

					Menu_TransitionItemByName((menuDef_t *) item->parent, name, 0, &rectTo, 1, 1);
				}
			}
		}
	}

	return qtrue;
}

qboolean Script_Orbit(itemDef_t *item, char **args) 
{
	const char *name;
	float cx, cy, x, y;
	int time;

	if (String_Parse(args, &name)) 
	{
		if ( Float_Parse(args, &x) && Float_Parse(args, &y) && Float_Parse(args, &cx) && Float_Parse(args, &cy) && Int_Parse(args, &time) ) 
		{
			Menu_OrbitItemByName((menuDef_t *) item->parent, name, x, y, cx, cy, time);
		}
	}

	return qtrue;
}

qboolean Script_SetFocus(itemDef_t *item, char **args) 
{
  const char *name;
  const char *hardfocus;
  itemDef_t *focusItem;
  qboolean b_HardFocus= qfalse;

	if (String_Parse(args, &name)) {
	{
/*
if (strcmp(name,"hardfocus")== 0)
	  {
			b_HardFocus = qtrue;
			if (String_Parse(args, &name)) {
				focusItem = Menu_FindVisibleItemByName((menuDef_t *) item->parent, name);
			}
	  }
	  else
*/
		  focusItem = Menu_FindVisibleItemByName((menuDef_t *) item->parent, name);
	}

//	if (focusItem && !(focusItem->window.flags & WINDOW_DECORATION) && (!(focusItem->window.flags & WINDOW_HASFOCUS)||(b_HardFocus))) {

//	if (focusItem && !(focusItem->window.flags & WINDOW_DECORATION) && !(focusItem->window.flags & WINDOW_HASFOCUS)) {
	if (focusItem && !(focusItem->window.flags & WINDOW_DECORATION)) {

      Menu_ClearFocus((menuDef_t *) item->parent);
      Item_SetFocus(focusItem, 0,0); 

      if (focusItem->onFocus) {
        Item_RunScript(focusItem, focusItem->onFocus);
      }
      if (DC->Assets.itemFocusSound) {
        DC->startLocalSound( DC->Assets.itemFocusSound, CHAN_LOCAL_SOUND );
      }
    }
  }

	return qtrue;
}

qboolean Script_SetPlayerModel(itemDef_t *item, char **args) 
{
	const char *name;
	if (String_Parse(args, &name)) 
	{
		DC->setCVar("model", name);
	}

	return qtrue;
}

/*
=================
ParseRect
=================
*/
qboolean ParseRect(const char **p, rectDef_t *r) 
{
	if (!COM_ParseFloat(p, &r->x)) 
	{
		if (!COM_ParseFloat(p, &r->y)) 
		{
			if (!COM_ParseFloat(p, &r->w)) 
			{
				if (!COM_ParseFloat(p, &r->h)) 
				{
					return qtrue;
				}
			}
		}
	}
	return qfalse;
}
/*
=================
Script_Transition2
uses current origin instead of specifing a starting origin

transition2		lfvscr		25 0 202 264  20 25
=================
*/
qboolean Script_Transition2(itemDef_t *item, char **args) 
{
	const char *name;
	rectDef_t rectTo;
	int time;
	float amt;

	if (String_Parse(args, &name)) 
	{
		if ( ParseRect((const char **) args, &rectTo) && Int_Parse(args, &time) && !COM_ParseFloat((const char **) args, &amt)) 
		{
			Menu_TransitionItemByName((menuDef_t *) item->parent, name, 0, &rectTo, time, amt);
		}
		else
		{
			Com_Printf(S_COLOR_YELLOW"WARNING: Script_Transition2: error parsing '%s'\n", name );
		}
	}

	return qtrue;
}


#ifdef _TRANS3
/*
JLF 
=================
Script_Transition3

used exclusively with model views
uses current origin instead of specifing a starting origin


transition3		lfvscr		(min extent) (max extent) (fovx,y)  20 25
=================
*/
qboolean Script_Transition3(itemDef_t *item, char **args) 
{
	const char *name;
	const char *value;
	float minx, miny, minz, maxx, maxy, maxz, fovtx, fovty;
	int time;
	float amt;

	if (String_Parse(args, &name)) 
	{	
		if (String_Parse( args, &value))
		{
			if (*value == '-')
			{
				String_Parse(args,&value);
				minx = -(atof(value));
			}
			else
				minx = atof(value);
			if (String_Parse( args, &value))
			{
				if (*value == '-')
				{
					String_Parse(args,&value);
					miny = -(atof(value));
				}
				else
					miny = atof(value);
				if (String_Parse( args, &value))
				{
					if (*value == '-')
					{
						String_Parse(args,&value);
						minz = -(atof(value));
					}
					else
						minz = atof(value);
					if (String_Parse( args, &value))
					{	
						if (*value == '-')
						{
							String_Parse(args,&value);
							maxx = -(atof(value));
						}
						else
							maxx = atof(value);
						if (String_Parse( args, &value))
						{
							if (*value == '-')
							{
								String_Parse(args,&value);
								maxy = -(atof(value));
							}
							else
								maxy = atof(value);
							if (String_Parse( args, &value))
							{
								if (*value == '-')
								{
									String_Parse(args,&value);
									maxz = -(atof(value));
								}
								else
									maxz = atof(value);
								if (String_Parse( args, &value))
								{
									if (*value == '-')
									{
										String_Parse(args,&value);
										fovtx = -(atof(value));
									}
									else
										fovtx = atof(value);
									if (String_Parse( args, &value))
									{
										if (*value == '-')
										{
											String_Parse(args,&value);
											fovty = -(atof(value));
										}
										else
											fovty = atof(value);
										
										if (String_Parse( args, &value))
										{	
											time = atoi(value);
											if (String_Parse( args, &value))
											{	
												amt = atof(value);
												//set up the variables
												Menu_Transition3ItemByName((menuDef_t *) item->parent, 
														name,
														minx, miny, minz,
														maxx, maxy, maxz, 
														fovtx, fovty,
														time, amt);
												
												return qtrue;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	Com_Printf(S_COLOR_YELLOW"WARNING: Script_Transition2: error parsing '%s'\n", name );
	return qtrue;
}
#endif

#ifdef _XBOX


//only works on some feeders
int GetCurrentFeederIndex(itemDef_t * item)
{
#ifndef CGAME
	float feederID = item->special;
	char * name;
	int i, max;
	
	if (feederID == FEEDER_PLAYER_SPECIES) 
	{
		return uiInfo.playerSpeciesIndex;
	} 
	if (feederID == FEEDER_PLAYER_SKIN_HEAD) 
	{
		name = Cvar_VariableString("ui_char_skin_head");
		max = uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinHeadCount;
		for ( i = 0; i < max ; i++)
		{
			if (!Q_stricmp(name, uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinHeadNames[i]))
			{
				return i;
			}

		//	Cvar_Set("ui_char_skin_head", uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinHeadNames[index]);
		}
		return -1;
	} 
	else if (feederID == FEEDER_PLAYER_SKIN_TORSO) 
	{
		name = Cvar_VariableString("ui_char_skin_torso");
		max = uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinTorsoCount;
		for ( i = 0; i < max ; i++)
		{
			if (!Q_stricmp(name, uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinTorsoNames[i]))
			{
				return i;
			}
		//	Cvar_Set("ui_char_skin_head", uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinHeadNames[index]);
		}
		return -1;
		
	} 
	else if (feederID == FEEDER_PLAYER_SKIN_LEGS) 
	{
		name = Cvar_VariableString("ui_char_skin_legs");
		max = uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinLegCount;
		for ( i = 0; i < max ; i++)
		{
			if (!Q_stricmp(name, uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinLegNames[i]))
			{
				return i;
			}
		//	Cvar_Set("ui_char_skin_head", uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinHeadNames[index]);
		}
		return -1;


	//	if (index >= 0 && index < uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinLegCount)
	//	{
	//		Cvar_Set("ui_char_skin_legs", uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinLegNames[index]);
	//	}
	} 

	
	else if (feederID == FEEDER_COLORCHOICES)
	{
		extern void	Item_RunScript(itemDef_t *item, const char *s);		//from ui_shared;
	
		int currR, currG, currB, newR, newG, newB;
		currR = Cvar_VariableIntegerValue( "ui_char_color_red");
		currG = Cvar_VariableIntegerValue( "ui_char_color_green");
		currB = Cvar_VariableIntegerValue( "ui_char_color_blue");
		
		max = uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].ColorCount;
		for ( i = 0; i < max ; i++)
		{
			Item_RunScript(item, uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].ColorActionText[i]);
			newR = Cvar_VariableIntegerValue( "ui_char_color_red");
			newG = Cvar_VariableIntegerValue( "ui_char_color_green");
			newB = Cvar_VariableIntegerValue( "ui_char_color_blue");
			if ( currR == newR && currG == newG && currB == newB)
				return i;
		}
		return -1;

		

//JLF junk copied code
		/*
extern void	Item_RunScript(itemDef_t *item, const char *s);		//from ui_shared;
		name = Cvar_VariableString("ui_char_skin_legs");
		max = uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].ColorCount;
		for ( i = 0; i < max ; i++)
		if (!qstrcmp(name, uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinLegNames[i]))
		{
			return i;
		//	Cvar_Set("ui_char_skin_head", uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].SkinHeadNames[index]);
		}
		return -1;



		if (index >= 0 && index < uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].ColorCount)
		{
			Item_RunScript(item, uiInfo.playerSpecies[uiInfo.playerSpeciesIndex].ColorActionText[index]);
		}
	*/
	}
#endif
	return -1;

}


qboolean Script_IncrementFeeder(itemDef_t * item,  char ** args)
{
#ifndef CGAME
	int feedercount = uiInfo.uiDC.feederCount(item->special);
	int value = GetCurrentFeederIndex(item);
	value++;
	if ( value >= feedercount)
		value = 0;
	DC->feederSelection(item->special, value, item);
#endif
	return qtrue;
}

qboolean Script_DecrementFeeder(itemDef_t * item,  char ** args)
{
#ifndef CGAME
	int feedercount = uiInfo.uiDC.feederCount(item->special);
	int value = GetCurrentFeederIndex(item);
	value--;
	if ( value < 0)
		value = feedercount-1;
	DC->feederSelection(item->special, value, item);
#endif
	return qtrue;
}  



#endif




qboolean Script_SetCvar(itemDef_t *item, char **args) 
{
	const char *cvar, *val;
	if (String_Parse(args, &cvar) && String_Parse(args, &val)) 
	{
		DC->setCVar(cvar, val);
	}
	return qtrue;
}

qboolean Script_SetCvarToCvar(itemDef_t *item, char **args) {
	const char *cvar, *val;
	if (String_Parse(args, &cvar) && String_Parse(args, &val)) {
		char cvarBuf[1024];
		DC->getCVarString(val, cvarBuf, sizeof(cvarBuf));
		DC->setCVar(cvar, cvarBuf);
	}
	return qtrue;
}

qboolean Script_Exec(itemDef_t *item, char **args) {
	const char *val;
	if (String_Parse(args, &val)) {
		DC->executeText(EXEC_APPEND, va("%s ; ", val));
	}
	return qtrue;
}

qboolean Script_Play(itemDef_t *item, char **args) {
	const char *val;
	if (String_Parse(args, &val)) {
		DC->startLocalSound(DC->registerSound(val), CHAN_AUTO);
	}
	return qtrue;
}

qboolean Script_playLooped(itemDef_t *item, char **args) {
	const char *val;
	if (String_Parse(args, &val)) {
		DC->stopBackgroundTrack();
		DC->startBackgroundTrack(val, val, qfalse);
	}
	return qtrue;
}


commandDef_t commandList[] =
{
  {"fadein", &Script_FadeIn},                   // group/name
  {"fadeout", &Script_FadeOut},                 // group/name
  {"show", &Script_Show},                       // group/name
  {"hide", &Script_Hide},                       // group/name
  {"setcolor", &Script_SetColor},               // works on this
  {"open", &Script_Open},                       // nenu
  {"close", &Script_Close},                     // menu
  {"setasset", &Script_SetAsset},               // works on this
  {"setbackground", &Script_SetBackground},     // works on this
  {"setitemrectcvar", &Script_SetItemRectCvar},			// group/name
  {"setitembackground", &Script_SetItemBackground},// group/name
  {"setitemtext", &Script_SetItemText},			// group/name
  {"setitemcolor", &Script_SetItemColor},       // group/name
  {"setfocuscolor", &Script_SetFocusColor},       // affects whole menu
  {"setitemcolorcvar", &Script_SetItemColorCvar},// group/name
  {"setitemrect", &Script_SetItemRect},			// group/name
  {"setteamcolor", &Script_SetTeamColor},       // sets this background color to team color
  {"setfocus", &Script_SetFocus},               // sets focus
  {"setplayermodel", &Script_SetPlayerModel},   // sets model
  {"transition", &Script_Transition},           // group/name
  {"setcvar", &Script_SetCvar},					// name
  {"setcvartocvar", &Script_SetCvarToCvar},     // name
  {"exec", &Script_Exec},						// group/name
  {"play", &Script_Play},						// group/name
  {"playlooped", &Script_playLooped},           // group/name
  {"orbit", &Script_Orbit},                     // group/name
  {"scale",			&Script_Scale},				// group/name
  {"disable",		&Script_Disable},			// group/name
  {"defer",			&Script_Defer},				// 
  {"rundeferred",	&Script_RunDeferred},		//
  {"delay",			&Script_Delay},				// works on this (script)
  {"transition2",	&Script_Transition2},		// group/name
  {"setdecoration", &Script_SetDecoration},		// changes the decoration flag of an object
#ifdef _XBOX
  {"transition3",   &Script_Transition3},			// model exclusive transition
  {"incrementfeeder", &Script_IncrementFeeder},
  {"decrementfeeder", &Script_DecrementFeeder},
#endif
  {"cvarifequal",		&Script_CvarIfEqual},
};

// Set all the items within a given menu, with the given itemName, to the given shader
void Menu_SetItemBackground(const menuDef_t *menu,const char *itemName, const char *background) 
{
	itemDef_t	*item;
	int			j, count;

	if (!menu)	// No menu???
	{
		return;
	}

	count = Menu_ItemsMatchingGroup( (menuDef_t *) menu, itemName);

	for (j = 0; j < count; j++) 
	{
		item = Menu_GetMatchingItemByNumber( (menuDef_t *) menu, j, itemName);
		if (item != NULL) 
		{
			item->window.background = DC->registerShaderNoMip(background);
		}
	}
}

// Set all the items within a given menu, with the given itemName, to the given text
void Menu_SetItemText(const menuDef_t *menu,const char *itemName, const char *text) 
{
	itemDef_t	*item;
	int			j, count;

	if (!menu)	// No menu???
	{
		return;
	}

	count = Menu_ItemsMatchingGroup( (menuDef_t *) menu, itemName);

	for (j = 0; j < count; j++) 
	{
		item = Menu_GetMatchingItemByNumber( (menuDef_t *) menu, j, itemName);
		if (item != NULL) 
		{
			if (text[0] == '*')
			{
				// Gah. Need to handle TEXTSCROLL fields that get set to cvars.
				if( item->type == ITEM_TYPE_TEXTSCROLL )
				{
					item->cvar = text+1;

					textScrollDef_t *scrollPtr = (textScrollDef_t*)item->typeData;
					if ( scrollPtr )
					{
						scrollPtr->startPos = 0;
						scrollPtr->endPos = 0;
					}

//					Item_TextScroll_BuildLines ( item );
				}
				else
				{
					item->text = NULL;		// Null this out because this would take presidence over cvar text.
					item->cvar = text+1;
					// Just copying what was in ItemParse_cvar()
					if ( item->typeData)
					{
						editFieldDef_t *editPtr;
						editPtr = (editFieldDef_t*)item->typeData;
						editPtr->minVal = -1;
						editPtr->maxVal = -1;
						editPtr->defVal = -1;
					}
				}
			}
			else
			{
				item->text = text;
				if ( item->type == ITEM_TYPE_TEXTSCROLL )
				{
					textScrollDef_t *scrollPtr = (textScrollDef_t*)item->typeData;
					if ( scrollPtr )
					{
						scrollPtr->startPos = 0;
						scrollPtr->endPos = 0;
					}

					Item_TextScroll_BuildLines ( item );
				}
			}
		}
	}
}

int scriptCommandCount = sizeof(commandList) / sizeof(commandDef_t);

void Item_RunScript(itemDef_t *item, const char *s) 
{
	char script[2048], *p;
	int i;
	qboolean bRan;

	script[0] = 0;
	
	if (item && s && s[0]) 
	{
		Q_strcat(script, 2048, s);
		p = script;
		
		while (1) 
		{
			const char *command;

			// expect command then arguments, ; ends command, NULL ends script
			if (!String_Parse(&p, &command)) 
			{
				return;
			}

			if (command[0] == ';' && command[1] == '\0') 
			{
				continue;
			}

			bRan = qfalse;
			for (i = 0; i < scriptCommandCount; i++) 
			{
				if (Q_stricmp(command, commandList[i].name) == 0) 
				{
					// Allow a script command to stop processing the script
					if ( !commandList[i].handler(item, &p) )
					{
						return;
					}

					bRan = qtrue;
					break;
				}
			}

			// not in our auto list, pass to handler
			if (!bRan) 
			{
				DC->runScript(&p);
			}
		}
	}
}


void AddDeferedCommand(char * command)
{
	Q_strcat(ClientManager::ActiveClient().cmd_defer_text_buf ,2048," ; ");
	Q_strcat(ClientManager::ActiveClient().cmd_defer_text_buf,2048,command);
}

qboolean Item_EnableShowViaCvar(itemDef_t *item, int flag) {
  char script[2048], *p;
  if (item && item->enableCvar && *item->enableCvar && item->cvarTest && *item->cvarTest) {
		char buff[2048];
	  DC->getCVarString(item->cvarTest, buff, sizeof(buff));

    Q_strncpyz(script, item->enableCvar, 2048);
    p = script;
    while (1) {
      const char *val;
      // expect value then ; or NULL, NULL ends list
      if (!String_Parse(&p, &val)) {
				return (item->cvarFlags & flag) ? qfalse : qtrue;
      }

      if (val[0] == ';' && val[1] == '\0') {
        continue;
      }

			// enable it if any of the values are true
			if (item->cvarFlags & flag) {
        if (Q_stricmp(buff, val) == 0) {
					return qtrue;
				}
			} else {
				// disable it if any of the values are true
        if (Q_stricmp(buff, val) == 0) {
					return qfalse;
				}
			}

    }
		return (item->cvarFlags & flag) ? qfalse : qtrue;
  }
	return qtrue;
}


// will optionaly set focus to this item 
qboolean Item_SetFocus(itemDef_t *item, float x, float y) {
	int i;
	itemDef_t *oldFocus;
	sfxHandle_t *sfx = &DC->Assets.itemFocusSound;
	qboolean playSound = qfalse;
	menuDef_t *parent; // bk001206: = (menuDef_t*)item->parent;
	// sanity check, non-null, not a decoration and does not already have the focus
	if (item == NULL || item->window.flags & WINDOW_DECORATION || item->window.flags & WINDOW_HASFOCUS || !(item->window.flags & WINDOW_VISIBLE)) {
		return qfalse;
	}

	// bk001206 - this can be NULL.
	parent = (menuDef_t*)item->parent; 

	// items can be enabled and disabled 
	if (item->disabled) 
	{
		return qfalse;
	}
      
	// items can be enabled and disabled based on cvars
	if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) {
		return qfalse;
	}

	if (item->cvarFlags & (CVAR_SHOW | CVAR_HIDE) && !Item_EnableShowViaCvar(item, CVAR_SHOW)) {
		return qfalse;
	}

	oldFocus = Menu_ClearFocus((menuDef_t *) item->parent);

	if (item->type == ITEM_TYPE_TEXT) {
		rectDef_t r;
		r = item->textRect;
		r.y -= r.h;

		item->window.flags |= WINDOW_HASFOCUS;
/*
		if (item->focusSound) {
			sfx = &item->focusSound;
		}
*/
		playSound = qtrue;
	} else {
		if (item->type == ITEM_TYPE_LISTBOX) 
		{
			DC->feederSelection(item->special, item->cursorPos, item);
		}

	    item->window.flags |= WINDOW_HASFOCUS;
		if (item->onFocus) {
			Item_RunScript(item, item->onFocus);
		}
/*
		if (item->focusSound) {
			sfx = &item->focusSound;
		}
*/
		playSound = qtrue;
	}

	if (playSound && sfx) {
		DC->startLocalSound( *sfx, CHAN_LOCAL_SOUND );
	}

	for (i = 0; i < parent->itemCount; i++) {
		if (parent->items[i] == item) {
			parent->cursorItem = i;
			break;
		}
	}

	return qtrue;
}

int Item_TextScroll_MaxScroll ( itemDef_t *item ) 
{
	textScrollDef_t *scrollPtr = (textScrollDef_t*)item->typeData;
	
	int count = scrollPtr->iLineCount;
	int max   = count - (int)(item->window.rect.h / scrollPtr->lineHeight) + 1;

	if (max < 0) 
	{
		return 0;
	}

	return max;
}

int Item_TextScroll_ThumbPosition ( itemDef_t *item ) 
{
	float max, pos, size;
	textScrollDef_t *scrollPtr = (textScrollDef_t*)item->typeData;

	max  = Item_TextScroll_MaxScroll ( item );
	size = item->window.rect.h - (SCROLLBAR_SIZE * 2) - 2;

	if (max > 0) 
	{
		pos = (size-SCROLLBAR_SIZE) / (float) max;
	} 
	else 
	{
		pos = 0;
	}
	
	pos *= scrollPtr->startPos;
	
	return item->window.rect.y + 1 + SCROLLBAR_SIZE + pos;
}

int Item_TextScroll_ThumbDrawPosition ( itemDef_t *item ) 
{
	return Item_TextScroll_ThumbPosition(item);
}

qboolean Item_TextScroll_HandleKey ( itemDef_t *item, int key, qboolean down, qboolean force) 
{
	textScrollDef_t *scrollPtr = (textScrollDef_t*)item->typeData;
	int				max;
	int				viewmax;

	if (force || (Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory) && item->window.flags & WINDOW_HASFOCUS)) 
	{
		max = Item_TextScroll_MaxScroll(item);

		viewmax = (item->window.rect.h / scrollPtr->lineHeight);
		if ( key == A_CURSOR_UP || key == A_KP_8 ) 
		{
			scrollPtr->startPos--;
			if (scrollPtr->startPos < 0)
			{
				scrollPtr->startPos = 0;
			}
			return qtrue;
		}

		if ( key == A_CURSOR_DOWN || key == A_KP_2 ) 
		{
			scrollPtr->startPos++;
			if (scrollPtr->startPos > max)
			{
				scrollPtr->startPos = max;
			}

			return qtrue;
		}

		// mouse hit
		if (key == A_MOUSE1 || key == A_MOUSE2) 
		{
			if (item->window.flags & WINDOW_LB_LEFTARROW) 
			{
				scrollPtr->startPos--;
				if (scrollPtr->startPos < 0) 
				{
					scrollPtr->startPos = 0;
				}
			} 
			else if (item->window.flags & WINDOW_LB_RIGHTARROW) 
			{
				// one down
				scrollPtr->startPos++;
				if (scrollPtr->startPos > max) 
				{
					scrollPtr->startPos = max;
				}
			} 
			else if (item->window.flags & WINDOW_LB_PGUP) 
			{
				// page up
				scrollPtr->startPos -= viewmax;
				if (scrollPtr->startPos < 0) 
				{
					scrollPtr->startPos = 0;
				}
			} 
			else if (item->window.flags & WINDOW_LB_PGDN) 
			{
				// page down
				scrollPtr->startPos += viewmax;
				if (scrollPtr->startPos > max) 
				{
					scrollPtr->startPos = max;
				}
			} 
			else if (item->window.flags & WINDOW_LB_THUMB) 
			{
				// Display_SetCaptureItem(item);
			} 

			return qtrue;
		}

		if ( key == A_HOME || key == A_KP_7) 
		{
			// home
			scrollPtr->startPos = 0;
			return qtrue;
		}
		if ( key == A_END || key == A_KP_1) 
		{
			// end
			scrollPtr->startPos = max;
			return qtrue;
		}
		if (key == A_PAGE_UP || key == A_KP_9 ) 
		{
			scrollPtr->startPos -= viewmax;
			if (scrollPtr->startPos < 0) 
			{
					scrollPtr->startPos = 0;
			}

			return qtrue;
		}
		if ( key == A_PAGE_DOWN || key == A_KP_3 ) 
		{
			scrollPtr->startPos += viewmax;
			if (scrollPtr->startPos > max) 
			{
				scrollPtr->startPos = max;
			}
			return qtrue;
		}
	}

	return qfalse;
}

int Item_ListBox_MaxScroll(itemDef_t *item) {
	listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
	int count = DC->feederCount(item->special);
	int max;

	if (item->window.flags & WINDOW_HORIZONTAL) {
		max = count - (item->window.rect.w / listPtr->elementWidth) + 1;
	}
	else {
		max = count - (item->window.rect.h / listPtr->elementHeight) + 1;
	}
	if (max < 0) {
		return 0;
	}
	return max;
}

int Item_ListBox_ThumbPosition(itemDef_t *item) {
	float max, pos, size;
	listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;

	max = Item_ListBox_MaxScroll(item);
	if (item->window.flags & WINDOW_HORIZONTAL) {
		size = item->window.rect.w - (SCROLLBAR_SIZE * 2) - 2;
		if (max > 0) {
			pos = (size-SCROLLBAR_SIZE) / (float) max;
		} else {
			pos = 0;
		}
		pos *= listPtr->startPos;
		return item->window.rect.x + 1 + SCROLLBAR_SIZE + pos;
	}
	else {
		size = item->window.rect.h - (SCROLLBAR_SIZE * 2) - 2;
		if (max > 0) {
			pos = (size-SCROLLBAR_SIZE) / (float) max;
		} else {
			pos = 0;
		}
		pos *= listPtr->startPos;
		return item->window.rect.y + 1 + SCROLLBAR_SIZE + pos;
	}
}

int Item_ListBox_ThumbDrawPosition(itemDef_t *item) 
{
	return Item_ListBox_ThumbPosition(item);
}

// New version of this - always returns a number between 0.0f and 1.0f:
float Item_Slider_ThumbPosition(itemDef_t *item) {
	float value, range;
	editFieldDef_t *editDef = (editFieldDef_t *) item->typeData;

	if (!editDef || !item->cvar)
		return 0.0f;

	value = DC->getCVarValue(item->cvar);

	if (value < editDef->minVal) {
		value = editDef->minVal;
	} else if (value > editDef->maxVal) {
		value = editDef->maxVal;
	}

	range = editDef->maxVal - editDef->minVal;
	value -= editDef->minVal;
	return value / range;
}


qboolean Item_OwnerDraw_HandleKey(itemDef_t *item, int key) {
	if (item && DC->ownerDrawHandleKey)
	{
	
		// yep this is an ugly hack
		if( key == A_MOUSE1 || key == A_MOUSE2 )
		{
			switch( item->window.ownerDraw )
			{
				case UI_FORCE_SIDE:
				case UI_FORCE_RANK_HEAL:
				case UI_FORCE_RANK_LEVITATION:
				case UI_FORCE_RANK_SPEED:
				case UI_FORCE_RANK_PUSH:
				case UI_FORCE_RANK_PULL:
				case UI_FORCE_RANK_TELEPATHY:
				case UI_FORCE_RANK_GRIP:
				case UI_FORCE_RANK_LIGHTNING:
				case UI_FORCE_RANK_RAGE:
				case UI_FORCE_RANK_PROTECT:
				case UI_FORCE_RANK_ABSORB:
				case UI_FORCE_RANK_TEAM_HEAL:
				case UI_FORCE_RANK_TEAM_FORCE:
				case UI_FORCE_RANK_DRAIN:
				case UI_FORCE_RANK_SEE:
				case UI_FORCE_RANK_SABERATTACK:
				case UI_FORCE_RANK_SABERDEFEND:
				case UI_FORCE_RANK_SABERTHROW:
				if(!Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory) )
				{
					return qfalse;
				}
				break;
			}
		}
	  

		return DC->ownerDrawHandleKey(item->window.ownerDraw, item->window.ownerDrawFlags, &item->special, key);
	}
	return qfalse;
}


qboolean Item_Button_HandleKey(itemDef_t *item, int key) 
{
	if (key == A_MOUSE1)
	{
		if (Item_HandleAction(item))
		{
			return qtrue;
		}
	}
	else if (key == A_CURSOR_RIGHT)
	{
		if (Item_HandleSelectionNext(item))
		{
			//Item processed it 
			return qtrue;
		}
	}
	else if (key ==  A_CURSOR_LEFT)
	{
		if (Item_HandleSelectionPrev(item))
		{
			//Item processed it 
			return qtrue;
		}
	}
	return qfalse;
}

/*
=================
Item_Text_HandleKey
=================
*/
qboolean Item_Text_HandleKey(itemDef_t *item, int key) 
{
	if (key == A_MOUSE1)
	{
		if (Item_HandleAction(item))
		{
			return qtrue;
		}
	}
	else if (key == A_CURSOR_RIGHT)
	{
		if (Item_HandleSelectionNext(item))
		{
			//Item processed it 
			return qtrue;
		}
	}
	else if (key ==  A_CURSOR_LEFT)
	{
		if (Item_HandleSelectionPrev(item))
		{
			//Item processed it 
			return qtrue;
		}
	}
	return qfalse;
}


qboolean Item_ListBox_HandleKey(itemDef_t *item, int key, qboolean down, qboolean force) {
	listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
	int count = DC->feederCount(item->special);
	int max, viewmax, viewmaxh, viewmaxw;
	int gridcount;
	int i ;

	viewmaxw = (item->window.rect.w / listPtr->elementWidth);
	viewmaxh = (item->window.rect.h / listPtr->elementHeight);
	gridcount = viewmaxw * viewmaxh;

	if (force || item->window.flags & WINDOW_HASFOCUS)
	{
		max = Item_ListBox_MaxScroll(item);
		if (item->window.flags & WINDOW_HORIZONTAL) {
			viewmax = viewmaxw; //(item->window.rect.w / listPtr->elementWidth);
			if ( key == A_CURSOR_LEFT || key == A_KP_4 ) 
			{
				if (!listPtr->notselectable) {
					listPtr->cursorPos--;

					if (listPtr->cursorPos < 0) {
						listPtr->cursorPos = 0;
#ifdef _XBOX
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, item->cursorPos, item);
						return qfalse;
#endif
					}
					if (listPtr->cursorPos < listPtr->startPos) {
						listPtr->startPos = listPtr->cursorPos;
//JLF
#ifndef _XBOX
						return qfalse;
#endif
					}
					if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
						listPtr->startPos = listPtr->cursorPos - viewmax + 1;
					}
					item->cursorPos = listPtr->cursorPos;
					DC->feederSelection(item->special, item->cursorPos, NULL);
				}
				else {
					listPtr->startPos--;
					if (listPtr->startPos < 0)
						listPtr->startPos = 0;
				}
				return qtrue;
			}
			if ( key == A_CURSOR_RIGHT || key == A_KP_6 ) 
			{
				if (!listPtr->notselectable) {
					listPtr->cursorPos++;
					if (listPtr->cursorPos < listPtr->startPos) {
						listPtr->startPos = listPtr->cursorPos;

					}
					if (listPtr->cursorPos >= count) {
						listPtr->cursorPos = count-1;
#ifdef _XBOX
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, item->cursorPos, item);
						return qfalse;

#endif

					}
					if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
						listPtr->startPos = listPtr->cursorPos - viewmax + 1;
					}
					item->cursorPos = listPtr->cursorPos;
					DC->feederSelection(item->special, item->cursorPos, NULL);
				}
				else {
					listPtr->startPos++;
					if (listPtr->startPos >= count)
						listPtr->startPos = count-1;
				}
				return qtrue;
			}
		}
		// Vertical scroll
		else {
			
			// Multiple rows and columns (since it's more than twice as wide as an element)
			if (( item->window.rect.w > (listPtr->elementWidth*2)) &&  (listPtr->elementStyle == LISTBOX_IMAGE))
			{
				viewmax = (item->window.rect.w / listPtr->elementWidth);
			}
			else 
			{
				viewmax = (item->window.rect.h / listPtr->elementHeight);
			}

			if ( key == A_CURSOR_UP || key == A_KP_8 ) 
			{
				if (!listPtr->notselectable) {
					if (listPtr->elementStyle == LISTBOX_IMAGE) 
					{	listPtr->cursorPos-= viewmaxw;
						if (listPtr->cursorPos < 0)
						{
							listPtr->cursorPos+=viewmaxw;
							return qfalse;
						}
					}
					else
					{
						listPtr->cursorPos--;
					}
					if (listPtr->cursorPos < 0) {
						listPtr->cursorPos = 0;
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, item->cursorPos, item);
						return qfalse;
					}
					if (listPtr->cursorPos < listPtr->startPos) {
						if (listPtr->elementStyle == LISTBOX_IMAGE)
							listPtr->startPos -= viewmaxw;
						else
							listPtr->startPos = listPtr->cursorPos;
					}

					if (listPtr->elementStyle == LISTBOX_IMAGE)
					{
						if (listPtr->cursorPos >= listPtr->startPos + gridcount) 
							listPtr->startPos += gridcount;
					}
					else
					{
						if (listPtr->cursorPos >= listPtr->startPos + viewmaxh) 
							listPtr->startPos= listPtr->cursorPos-viewmaxh + 1;
					}


//					if (listPtr->cursorPos >= listPtr->startPos + gridcount) {//was + viewmax
//					//JLF	listPtr->startPos = listPtr->cursorPos - viewmax + 1;
//						listPtr->startPos += viewmaxw;
//					}
					item->cursorPos = listPtr->cursorPos;
					DC->feederSelection(item->special, item->cursorPos, NULL);
				}
				else {
					listPtr->startPos--;
					listPtr->cursorPos = listPtr->startPos;
					if (listPtr->cursorPos >= 0)
					{
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, listPtr->cursorPos, item);
					}
					else
						listPtr->cursorPos = 0;
					
					if (listPtr->startPos < 0)
					{	
						listPtr->startPos = 0;
						return qfalse;
					}
				}
				return qtrue;
			}
			if ( key == A_CURSOR_DOWN || key == A_KP_2 ) 
			{
				if (!listPtr->notselectable) {
					if (listPtr->elementStyle == LISTBOX_IMAGE) 
					{	
						listPtr->cursorPos+= viewmaxw;
						if (listPtr->cursorPos >=count)
						{
							listPtr->cursorPos-= viewmaxw;
							if ( count-1 == listPtr->cursorPos)
								listPtr->cursorPos++;
							else
								listPtr->cursorPos = count-1;
						}
					}
					else
						listPtr->cursorPos++;
					if (listPtr->cursorPos < listPtr->startPos) {
						listPtr->startPos = listPtr->cursorPos;
					}
					if (listPtr->cursorPos >= count) {
						listPtr->cursorPos = count-1;
#ifdef _XBOX
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, item->cursorPos, item);
						return qfalse;

#endif
					}
					if (listPtr->elementStyle == LISTBOX_IMAGE) 
					{
						if (listPtr->cursorPos >= listPtr->startPos + gridcount) {
#ifdef _XBOX
							listPtr->startPos += viewmaxw;
#else	
							listPtr->startPos = listPtr->cursorPos - viewmax + 1;
#endif
						}
					}
					else
					{
						if (listPtr->cursorPos >= listPtr->startPos + viewmaxh)
							listPtr->startPos++;
					}
					item->cursorPos = listPtr->cursorPos;
					DC->feederSelection(item->special, item->cursorPos, NULL);
				}
				else {
					listPtr->startPos++;

//JLF
#ifdef _XBOX // MPMOVED
					listPtr->cursorPos++;
					if (listPtr->cursorPos < count)
					{
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, listPtr->cursorPos, item);
					}
					else
						listPtr->cursorPos = count -1;

#endif

				
//JLFMOUSE
#ifdef _XBOX // MPMOVED
					if (listPtr->startPos > count-1)
					{
						listPtr->startPos = count-1;
						return false;
					}
#else

					if (listPtr->startPos > max)
					{
						listPtr->startPos = max;
					}
#endif
				}
				return qtrue;
			}
		
//JLF newstuff
			if ( key == A_CURSOR_LEFT && listPtr->elementStyle == LISTBOX_IMAGE  ) 
			{
				if (viewmaxw <=1)
					return qfalse;
				if (!listPtr->notselectable) 
				{
					listPtr->cursorPos--;

					if (listPtr->cursorPos < 0) 
					{
						listPtr->cursorPos = 0;
#ifdef _XBOX
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, item->cursorPos, item);
						return qfalse;

#endif
					}

					if (listPtr->cursorPos < listPtr->startPos) 
					{
//JLF newgrid
#ifdef _XBOX
						listPtr->startPos -= viewmaxw;
#else
						listPtr->startPos = listPtr->cursorPos;
#endif
					
					}
			//		if (listPtr->cursorPos >= listPtr->startPos + viewmaxh) 
			//		{
			//			listPtr->startPos = listPtr->cursorPos - viewmaxh + 1;
			//		}
					item->cursorPos = listPtr->cursorPos;

					DC->feederSelection(item->special, item->cursorPos, item);
				}
				else 
				{
					listPtr->startPos--;
//JLF
#ifdef _XBOX // MPMOVED
					listPtr->cursorPos--;
					if (listPtr->cursorPos >= 0)
						DC->feederSelection(item->special, listPtr->cursorPos, item);
					else
						listPtr->cursorPos = 0;
#endif

					if (listPtr->startPos < 0)
					{
						listPtr->startPos = 0;
//JLFMOUSE
#ifdef _XBOX // MPMOVED
						return false;
#endif

					}
				}
				return qtrue;
			}


			if ( key == A_CURSOR_RIGHT && listPtr->elementStyle == LISTBOX_IMAGE   ) 
			{
				if (viewmaxw <=1)
					return qfalse;
				if (!listPtr->notselectable) 
				{
					listPtr->cursorPos++;

					if (listPtr->cursorPos >= count) 
					{
						listPtr->cursorPos = count-1;
#ifdef _XBOX
						item->cursorPos = listPtr->cursorPos;
						DC->feederSelection(item->special, item->cursorPos, item);
						return qfalse;

#endif
					}
					if (listPtr->cursorPos < listPtr->startPos) 
					{
						listPtr->startPos = listPtr->cursorPos;
					}
					if (listPtr->cursorPos >= listPtr->startPos + gridcount) 
					{
//JLF newgrid	
						//listPtr->startPos = listPtr->cursorPos - viewmaxh + 1;

						listPtr->startPos += viewmaxw;
					}
					item->cursorPos = listPtr->cursorPos;

					DC->feederSelection(item->special, item->cursorPos, item);
				}
				else 
				{
					listPtr->startPos++;
//JLF
#ifdef _XBOX // MPMOVED
					listPtr->cursorPos++;
					if (listPtr->cursorPos < count)
						DC->feederSelection(item->special, listPtr->cursorPos, item);
					else
						listPtr->cursorPos = count-1;
#endif

					if (listPtr->startPos <count)
					{
						listPtr->startPos = count-1;
//JLFMOUSE
#ifdef _XBOX // MPMOVED
						return false;
#endif

					}
				}
				return qtrue;
			}

		}

		// mouse hit
		if (key == A_MOUSE1 || key == A_MOUSE2)
		{
			if (listPtr->doubleClick)
			{
				Item_RunScript(item, listPtr->doubleClick);
			}
			else
			{
				return Item_HandleAction(item);
			}
		}
	}

	return qfalse;
}

qboolean Item_YesNo_HandleKey(itemDef_t *item, int key) {
	if (item->window.flags & WINDOW_HASFOCUS && item->cvar) 
	{
		if ( key == A_CURSOR_RIGHT || key == A_CURSOR_LEFT)
		{
			DC->setCVar(item->cvar, va("%i", !DC->getCVarValue(item->cvar)));
			return qtrue;
		}
	}
	return qfalse;
}

int Item_Multi_CountSettings(itemDef_t *item) {
	multiDef_t *multiPtr = (multiDef_t*)item->typeData;
	if (multiPtr == NULL) {
		return 0;
	}
	return multiPtr->count;
}

int Item_Multi_FindCvarByValue(itemDef_t *item) {
	char buff[2048];
	float value = 0;
	int i;
	multiDef_t *multiPtr = (multiDef_t*)item->typeData;
	if (multiPtr) {
		if (multiPtr->strDef) {
	    DC->getCVarString(item->cvar, buff, sizeof(buff));
		} else {
			value = DC->getCVarValue(item->cvar);
		}
		for (i = 0; i < multiPtr->count; i++) {
			if (multiPtr->strDef) {
				if (Q_stricmp(buff, multiPtr->cvarStr[i]) == 0) {
					return i;
				}
			} else {
 				if (multiPtr->cvarValue[i] == value) {
 					return i;
 				}
 			}
 		}
	}
	return 0;
}

const char *Item_Multi_Setting(itemDef_t *item) {
	char buff[2048];
	float value = 0;
	int i;
	multiDef_t *multiPtr = (multiDef_t*)item->typeData;
	if (multiPtr) 
	{
		if (multiPtr->strDef) 
		{
			if (item->cvar)
			{
			    DC->getCVarString(item->cvar, buff, sizeof(buff));
			}
		} 
		else 
		{
			if (item->cvar)	// Was a cvar given?
			{
				value = DC->getCVarValue(item->cvar);
			}
		}

		for (i = 0; i < multiPtr->count; i++) 
		{
			if (multiPtr->strDef) 
			{
				if (Q_stricmp(buff, multiPtr->cvarStr[i]) == 0) 
				{
					return multiPtr->cvarList[i];
				}
			} 
			else 
			{
 				if (multiPtr->cvarValue[i] == value) 
				{
					return multiPtr->cvarList[i];
 				}
 			}
 		}
	}
	return "";
}

qboolean Item_Multi_HandleKey(itemDef_t *item, int key) 
{
	multiDef_t *multiPtr = (multiDef_t*)item->typeData;
	if (multiPtr) 
	{
		if (item->window.flags & WINDOW_HASFOCUS)// JLF* && item->cvar)
		{
			if ( key == A_CURSOR_RIGHT || key == A_CURSOR_LEFT)
			{
				int current = Item_Multi_FindCvarByValue(item);
				int max = Item_Multi_CountSettings(item);

				if (key == A_CURSOR_LEFT)	// Xbox uses CURSOR_LEFT
				{
					current--;
					if ( current < 0 ) 
					{
						current = max-1;
					}
				}
				else
				{
					current++;
					if ( current >= max ) 
					{
						current = 0;
					}
				}

				if (multiPtr->strDef) 
				{
					DC->setCVar(item->cvar, multiPtr->cvarStr[current]);
				} 
				else 
				{
					float value = multiPtr->cvarValue[current];
					if (((float)((int) value)) == value) 
					{
						DC->setCVar(item->cvar, va("%i", (int) value ));
					}
					else 
					{
						DC->setCVar(item->cvar, va("%f", value ));
					}
				}
				if (item->special)
				{//its a feeder?
					DC->feederSelection(item->special, current, item);
				}

				if (key == A_CURSOR_RIGHT)
				{
					Item_HandleSelectionNext(item);
				}
				else if (key ==  A_CURSOR_LEFT)
				{
					Item_HandleSelectionPrev(item);
				}
				
				return qtrue;
			}
		}
	}
  return qfalse;
}

qboolean Item_Slider_HandleKey(itemDef_t *item, int key, qboolean down) {
	float x, value, width, work;

	if (item->window.flags & WINDOW_HASFOCUS && item->cvar)
	{
		if (key == A_CURSOR_LEFT)
		{
			editFieldDef_t *editDef = (editFieldDef_s *) item->typeData;
			if (editDef) 
			{
				value = DC->getCVarValue(item->cvar);
				value -= (editDef->maxVal-editDef->minVal)/TICK_COUNT;
				if ( value < editDef->minVal)
					value = editDef->minVal;
				DC->setCVar(item->cvar, va("%f", value));
				return qtrue;
			}
		}
		if (key == A_CURSOR_RIGHT)
		{
			editFieldDef_t *editDef = (editFieldDef_s *) item->typeData;
			if (editDef) 
			{
				value = DC->getCVarValue(item->cvar);
				value += (editDef->maxVal-editDef->minVal)/TICK_COUNT;
				if ( value > editDef->maxVal)
					value = editDef->maxVal;
				DC->setCVar(item->cvar, va("%f", value));
				return qtrue;
			}
		}
	}

	return qfalse;
}


qboolean Item_HandleKey(itemDef_t *item, int key, qboolean down) {

	if (!down) {
		return qfalse;
	}

  switch (item->type) {
    case ITEM_TYPE_BUTTON:
	  return Item_Button_HandleKey(item, key);
      break;
    case ITEM_TYPE_RADIOBUTTON:
      return qfalse;
      break;
    case ITEM_TYPE_CHECKBOX:
      return qfalse;
      break;
    case ITEM_TYPE_COMBO:
      return qfalse;
      break;
    case ITEM_TYPE_LISTBOX:
      return Item_ListBox_HandleKey(item, key, down, qfalse);
      break;
	case ITEM_TYPE_TEXTSCROLL:
      return Item_TextScroll_HandleKey(item, key, down, qfalse);
      break;
    case ITEM_TYPE_YESNO:
      return Item_YesNo_HandleKey(item, key);
      break;
    case ITEM_TYPE_MULTI:
      return Item_Multi_HandleKey(item, key);
      break;
    case ITEM_TYPE_OWNERDRAW:
      return Item_OwnerDraw_HandleKey(item, key);
      break;
    case ITEM_TYPE_SLIDER:
      return Item_Slider_HandleKey(item, key, down);
      break;
	case ITEM_TYPE_TEXT:
	  return Item_Text_HandleKey(item, key);
	  break;
    default:
      return qfalse;
      break;
  }

  //return qfalse;
}


/*
-----------------------------------------
Item_HandleAction
	If Item has an action script, run it.
-------------------------------------------
*/
qboolean Item_HandleAction(itemDef_t * item)
{
	if (!item)
		return qfalse;
	if (item->action)
	{
		Item_RunScript(item, item->action);
		return qtrue;
	}
	return qfalse;
}

/*
-----------------------------------------
Item_HandleSelectionNext
	If Item has an selectionNext script, run it.
-------------------------------------------
*/
qboolean Item_HandleSelectionNext(itemDef_t * item)
{
	if (item->selectionNext)
	{
		Item_RunScript(item, item->selectionNext);
		return qtrue;
	}
	return qfalse;
}

/*
-----------------------------------------
Item_HandleSelectionPrev
	If Item has an selectionPrev script, run it.
-------------------------------------------
*/
qboolean Item_HandleSelectionPrev(itemDef_t * item)
{
	if (item->selectionPrev)
	{
		Item_RunScript(item, item->selectionPrev);
		return qtrue;
	}
	return qfalse;
}


itemDef_t *Menu_SetPrevCursorItem(menuDef_t *menu) {
	qboolean wrapped = qfalse;
	int oldCursor = menu->cursorItem;

	if (menu->cursorItem < 0) {
		menu->cursorItem = menu->itemCount-1;
		wrapped = qtrue;
	} 

	while (menu->cursorItem > -1) 
	{
		menu->cursorItem--;
		if (menu->cursorItem < 0) {
			if (wrapped)
			{
				break;
			}
			wrapped = qtrue;
			menu->cursorItem = menu->itemCount -1;
		}

		if (Item_SetFocus(menu->items[menu->cursorItem], DC->cursorx, DC->cursory)) {
			Menu_HandleMouseMove(menu, menu->items[menu->cursorItem]->window.rect.x + 1, menu->items[menu->cursorItem]->window.rect.y + 1);
			return menu->items[menu->cursorItem];
		}
	}
	menu->cursorItem = oldCursor;
	return NULL;

}

itemDef_t *Menu_SetNextCursorItem(menuDef_t *menu) {

  qboolean wrapped = qfalse;
	int oldCursor = menu->cursorItem;


  if (menu->cursorItem == -1) {
    menu->cursorItem = 0;
    wrapped = qtrue;
  }

  while (menu->cursorItem < menu->itemCount) {

    menu->cursorItem++;
    if (menu->cursorItem >= menu->itemCount && !wrapped) {
      wrapped = qtrue;
      menu->cursorItem = 0;
    }
		if (Item_SetFocus(menu->items[menu->cursorItem], DC->cursorx, DC->cursory)) {
			Menu_HandleMouseMove(menu, menu->items[menu->cursorItem]->window.rect.x + 1, menu->items[menu->cursorItem]->window.rect.y + 1);
      return menu->items[menu->cursorItem];
    }
    
  }

	menu->cursorItem = oldCursor;
	return NULL;
}

void  Menus_Activate(menuDef_t *menu)
{
	if (!(menu->window.flags & WINDOW_POPUP)&& menu->fullScreen )
	{
		DC->setCVar("ui_showWmove", "0");

		DC->setCVar("ui_showXrls", "0");
		DC->setCVar("ui_showXctrl", "0");
		DC->setCVar("ui_showXNew", "0");
		DC->setCVar("ui_showXforce", "0");
		DC->setCVar("ui_showXdfrc", "0");
		DC->setCVar("ui_showXcstm", "0");
		DC->setCVar("ui_showXhost", "0");
		DC->setCVar("ui_showXchk", "0");

		DC->setCVar("ui_showYref", "0");
		DC->setCVar("ui_showYdel", "0");
		DC->setCVar("ui_showYsaber", "0");
		DC->setCVar("ui_showYwpn", "0");

		DC->setCVar("ui_showAcallout", "0");
		DC->setCVar("ui_showBcallout", "0");
	}

#ifndef CGAME
	uiInfo.moveAnimTime = 0;
#endif

	menu->window.flags |= (WINDOW_HASFOCUS | WINDOW_VISIBLE);
	if (menu->onOpen) {
		itemDef_t item;
		item.parent = menu;
		item.window.flags = 0;	//err, item is fake here, but we want a valid flag before calling runscript
		Item_RunScript(&item, menu->onOpen);

		if (item.window.flags & WINDOW_SCRIPTWAITING)	//in case runscript set waiting, copy it up to the menu
		{
			menu->window.flags |= WINDOW_SCRIPTWAITING;
			menu->window.delayTime = item.window.delayTime;
		}
	}

	if (menu->soundName && *menu->soundName) {
//		DC->stopBackgroundTrack();					// you don't want to do this since it will reset s_rawend
		DC->startBackgroundTrack(menu->soundName, menu->soundName, qfalse);
	}

	menu->appearanceTime = 0;
}

int Display_VisibleMenuCount() {
	int i, count;
	count = 0;
	for (i = 0; i < menuCount; i++) {
		if (Menus[i].window.flags & (WINDOW_FORCED | WINDOW_VISIBLE)) {
			count++;
		}
	}
	return count;
}

qboolean inHandler = qfalse;

void Menu_HandleKey(menuDef_t *menu, int key, qboolean down) {
	int i;
	itemDef_t *item = NULL;
//	static qboolean inHandler = qfalse;

	if (inHandler) {
		return;
	}

	inHandler = qtrue;

	if (menu == NULL) {
		inHandler = qfalse;
		return;
	}

	// get the item with focus
	for (i = 0; i < menu->itemCount; i++) {
		if (menu->items[i]->window.flags & WINDOW_HASFOCUS) {
			item = menu->items[i];
		}
	}

	// Ignore if disabled
	if (item && item->disabled) 
	{
		return;
	}


	if (!down) {
		inHandler = qfalse;
		return;
	}

	if (item != NULL)
	{
		//Handlekey implies that this control was able to interpret and use this input
		if (Item_HandleKey(item, key, down))
		{
			if (((item->type == ITEM_TYPE_MULTI) || (item->type == ITEM_TYPE_SLIDER) || (item->type == ITEM_TYPE_YESNO)) &&
				((key == A_CURSOR_RIGHT) || (key == A_CURSOR_LEFT)))
			{
				Item_HandleAction(item);
			}
//			if ((item->type != ITEM_TYPE_LISTBOX) && (key != A_CURSOR_RIGHT && key != A_CURSOR_LEFT) || (item->type == ITEM_TYPE_MULTI))
//				Item_HandleAction(item);
			inHandler = qfalse;
			return;
		}
	}

	// default handling
	switch ( key ) {

		case A_CURSOR_UP:
			Menu_SetPrevCursorItem(menu);
			break;

		case A_ESCAPE:
			if (menu->onESC) {
				itemDef_t it;
				it.parent = menu;
				Item_RunScript(&it, menu->onESC);
			}
			break;

		case A_CURSOR_DOWN:
			Menu_SetNextCursorItem(menu);
			break;

		case A_DELETE:
			if (menu->xScript) {
				itemDef_t it;
				it.parent = menu;
				Item_RunScript(&it, menu->xScript);
			}
			break;

		case A_BACKSPACE:
			if (menu->yScript) {
				itemDef_t it;
				it.parent = menu;
				Item_RunScript(&it, menu->yScript);
			}
			break;

		case A_HOME:
			if (menu->whiteScript) {
				itemDef_t it;
				it.parent = menu;
				Item_RunScript(&it, menu->whiteScript);
			}
			break;

		case A_MOUSE1:
			if (menu->onAccept) 
			{
				itemDef_t it;
				it.parent = menu;
				it.window.flags = 0;	//err, item is fake here, but we want a valid flag before calling runscript
				Item_RunScript(&it, menu->onAccept);

				if (it.window.flags & WINDOW_SCRIPTWAITING)	//in case runscript set waiting, copy it up to the menu
				{
					menu->window.flags |= WINDOW_SCRIPTWAITING;
					menu->window.delayTime = it.window.delayTime;
				}
			}
			break;

		case A_JOY0:
		case A_JOY1:
		case A_JOY2:
		case A_JOY3:
		case A_JOY4:
		case A_AUX0:
		case A_AUX1:
		case A_AUX2:
		case A_AUX3:
		case A_AUX4:
		case A_AUX5:
		case A_AUX6:
		case A_AUX7:
		case A_AUX8:
		case A_AUX9:
		case A_AUX10:
		case A_AUX11:
		case A_AUX12:
		case A_AUX13:
		case A_AUX14:
		case A_AUX15:
		case A_AUX16:
			break;
		case A_KP_ENTER:
		case A_ENTER:
			if (item)
			{
				Item_HandleAction(item);
			}
			break;
	}
	inHandler = qfalse;
}

void ToWindowCoords(float *x, float *y, windowDef_t *window) {
	if (window->border != 0) {
		*x += window->borderSize;
		*y += window->borderSize;
	} 
	*x += window->rect.x;
	*y += window->rect.y;
}

void Rect_ToWindowCoords(rectDef_t *rect, windowDef_t *window) {
	ToWindowCoords(&rect->x, &rect->y, window);
}

void Item_SetTextExtents(itemDef_t *item, int *width, int *height, const char *text) {
	const char *textPtr = (text) ? text : item->text;

	if (textPtr == NULL ) {
		return;
	}

	*width = item->textRect.w;
	*height = item->textRect.h;

	// keeps us from computing the widths and heights more than once
	if (*width == 0 || (item->type == ITEM_TYPE_OWNERDRAW && item->textalignment == ITEM_ALIGN_CENTER)
#ifndef CGAME
		|| (item->text && item->text[0]=='@' && item->asset != se_language.modificationCount )	//string package language changed
#endif
		)
	{
		int originalWidth = DC->textWidth(textPtr, item->textscale, item->iMenuFont);

		if (item->type == ITEM_TYPE_OWNERDRAW && (item->textalignment == ITEM_ALIGN_CENTER || item->textalignment == ITEM_ALIGN_RIGHT)) 
		{
			originalWidth += DC->ownerDrawWidth(item->window.ownerDraw, item->textscale);
		} 
		else if (item->type == ITEM_TYPE_EDITFIELD && item->textalignment == ITEM_ALIGN_CENTER && item->cvar) 
		{
			char buff[256];
			DC->getCVarString(item->cvar, buff, 256);
			originalWidth += DC->textWidth(buff, item->textscale, item->iMenuFont);
		}

		*width = DC->textWidth(textPtr, item->textscale, item->iMenuFont);
		*height = DC->textHeight(textPtr, item->textscale, item->iMenuFont);
		
		item->textRect.w = *width;
		item->textRect.h = *height;
		item->textRect.x = item->textalignx;
		item->textRect.y = item->textaligny;
		if (item->textalignment == ITEM_ALIGN_RIGHT) {
			item->textRect.x = item->textalignx - originalWidth;
		} else if (item->textalignment == ITEM_ALIGN_CENTER) {
			item->textRect.x = item->textalignx - originalWidth / 2;
		}

		ToWindowCoords(&item->textRect.x, &item->textRect.y, &item->window);
#ifndef CGAME
		if (item->text && item->text[0]=='@' )//string package
		{//mark language
			item->asset = se_language.modificationCount;
		}
#endif
	}
}

void Item_TextColor(itemDef_t *item, vec4_t *newColor) {
	vec4_t lowLight;
	menuDef_t *parent = (menuDef_t*)item->parent;

	Fade(&item->window.flags, &item->window.foreColor[3], parent->fadeClamp, &item->window.nextTime, parent->fadeCycle, qtrue, parent->fadeAmount);

	if (item->window.flags & WINDOW_HASFOCUS) {
		memcpy(newColor, &parent->focusColor, sizeof(vec4_t));
/*
		lowLight[0] = 0.8 * parent->focusColor[0]; 
		lowLight[1] = 0.8 * parent->focusColor[1]; 
		lowLight[2] = 0.8 * parent->focusColor[2]; 
		lowLight[3] = 0.8 * parent->focusColor[3]; 
		LerpColor(parent->focusColor,lowLight,*newColor,0.5+0.5*sin((float)(DC->realTime / PULSE_DIVISOR)));
*/
	} else if (item->textStyle == ITEM_TEXTSTYLE_BLINK && !((DC->realTime/BLINK_DIVISOR) & 1)) {
		lowLight[0] = 0.8 * item->window.foreColor[0]; 
		lowLight[1] = 0.8 * item->window.foreColor[1]; 
		lowLight[2] = 0.8 * item->window.foreColor[2]; 
		lowLight[3] = 0.8 * item->window.foreColor[3]; 
		LerpColor(item->window.foreColor,lowLight,*newColor,0.5+0.5*sin((float)(DC->realTime / PULSE_DIVISOR)));
	} else {
		memcpy(newColor, &item->window.foreColor, sizeof(vec4_t));
		// items can be enabled and disabled based on cvars
	}

	if (item->disabled) 
	{
		memcpy(newColor, &parent->disableColor, sizeof(vec4_t));
	}

	if (item->enableCvar && *item->enableCvar && item->cvarTest && *item->cvarTest) {
		if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) {
			memcpy(newColor, &parent->disableColor, sizeof(vec4_t));
		}
	}
}

void Item_Text_AutoWrapped_Paint(itemDef_t *item) {
	char text[2048];
	const char *p, *textPtr, *newLinePtr;
	char buff[2048];
	int height, len, textWidth, newLine, newLineWidth;	//int width;
	float y;
	vec4_t color;

	textWidth = 0;
	newLinePtr = NULL;

	if (item->text == NULL) {
		if (item->cvar == NULL) {
			return;
		}
		else {
			DC->getCVarString(item->cvar, text, sizeof(text));
			textPtr = text;
		}
	}
	else {
		textPtr = item->text;
	}
	if (*textPtr == '@')	// string reference
	{
		trap_SP_GetStringTextString( &textPtr[1], text, sizeof(text));
		textPtr = text;
	}
	if (*textPtr == '\0') {
		return;
	}
	Item_TextColor(item, &color);
	//Item_SetTextExtents(item, &width, &height, textPtr);
	//if (item->value == 0)
	//{
	//	item->value = (int)(0.5 + (float)DC->textWidth(textPtr, item->textscale, item->font) / item->window.rect.w);
	//}
	height = DC->textHeight(textPtr, item->textscale, item->iMenuFont);

	y = item->textaligny;
	len = 0;
	buff[0] = '\0';
	newLine = 0;
	newLineWidth = 0;
	p = textPtr;
	while (p) {	//findmeste (this will break widechar languages)!
		if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\0') {
			newLine = len;
			newLinePtr = p+1;
			newLineWidth = textWidth;
		}
		textWidth = DC->textWidth(buff, item->textscale, 0);
		if ( (newLine && textWidth > item->window.rect.w) || *p == '\n' || *p == '\0') {
			if (len) {
				if (item->textalignment == ITEM_ALIGN_LEFT) {
					item->textRect.x = item->textalignx;
				} else if (item->textalignment == ITEM_ALIGN_RIGHT) {
					item->textRect.x = item->textalignx - newLineWidth;
				} else if (item->textalignment == ITEM_ALIGN_CENTER) {
					item->textRect.x = item->textalignx - newLineWidth / 2;
				}
				item->textRect.y = y;
				ToWindowCoords(&item->textRect.x, &item->textRect.y, &item->window);
				//
				buff[newLine] = '\0';
				DC->drawText(item->textRect.x, item->textRect.y, item->textscale, color, buff, 0, 0, item->textStyle, item->iMenuFont);
			}
			if (*p == '\0') {
				break;
			}
			//
			y += height + 5;
			p = newLinePtr;
			len = 0;
			newLine = 0;
			newLineWidth = 0;
			continue;
		}
		buff[len++] = *p++;
		buff[len] = '\0';
	}
}

void Item_Text_Wrapped_Paint(itemDef_t *item) {
	char text[1024];
	const char *p, *start, *textPtr;
	char buff[1024];
	int width, height;
	float x, y;
	vec4_t color;

	// now paint the text and/or any optional images
	// default to left

	if (item->text == NULL) {
		if (item->cvar == NULL) {
			return;
		}
		else {
			DC->getCVarString(item->cvar, text, sizeof(text));
			textPtr = text;
		}
	}
	else {
		textPtr = item->text;
	}
	if (*textPtr == '@')	// string reference
	{
		trap_SP_GetStringTextString( &textPtr[1], text, sizeof(text));
		textPtr = text;
	}
	if (*textPtr == '\0') {
		return;
	}

	Item_TextColor(item, &color);
	Item_SetTextExtents(item, &width, &height, textPtr);

	x = item->textRect.x;
	y = item->textRect.y;
	start = textPtr;
	p = strchr(textPtr, '\r');
	while (p && *p) {
		strncpy(buff, start, p-start+1);
		buff[p-start] = '\0';
		DC->drawText(x, y, item->textscale, color, buff, 0, 0, item->textStyle, item->iMenuFont);
		y += height + 2;
		start += p - start + 1;
		p = strchr(p+1, '\r');
	}
	DC->drawText(x, y, item->textscale, color, start, 0, 0, item->textStyle, item->iMenuFont);
}

void Item_Text_Paint(itemDef_t *item) {
	char text[1024];
	const char *textPtr;
	int height, width;
	vec4_t color;

	if (item->window.flags & WINDOW_WRAPPED) {
		Item_Text_Wrapped_Paint(item);
		return;
	}
	if (item->window.flags & WINDOW_AUTOWRAPPED) {
		Item_Text_AutoWrapped_Paint(item);
		return;
	}

	if (item->text == NULL) {
		if (item->cvar == NULL) {
			return;
		}
		else {
			DC->getCVarString(item->cvar, text, sizeof(text));
			textPtr = text;
		}
	}
	else {
		textPtr = item->text;
	}
	if (*textPtr == '@')	// string reference
	{
		trap_SP_GetStringTextString( &textPtr[1], text, sizeof(text));
		textPtr = text;
	}

	// this needs to go here as it sets extents for cvar types as well
	Item_SetTextExtents(item, &width, &height, textPtr);

	if (*textPtr == '\0') {
		return;
	}


	Item_TextColor(item, &color);

	DC->drawText(item->textRect.x, item->textRect.y, item->textscale, color, textPtr, 0, 0, item->textStyle, item->iMenuFont);

/*
	if (item->text2)	// Is there a second line of text?
	{
		textPtr = item->text2;
		if (*textPtr == '@')	// string reference
		{
			trap_SP_GetStringTextString( &textPtr[1], text, sizeof(text));
			textPtr = text;
		}
		Item_TextColor(item, &color);
		DC->drawText(item->textRect.x + item->text2alignx, item->textRect.y + item->text2aligny, item->textscale, color, textPtr, 0, 0, item->textStyle,item->iMenuFont);
	}
*/
}

void Item_YesNo_Paint(itemDef_t *item) {
	char	sYES[20];
	char	sNO[20];
	vec4_t newColor, lowLight;
	float value;
	menuDef_t *parent = (menuDef_t*)item->parent;
	const char *yesnovalue;
	
	value = (item->cvar) ? DC->getCVarValue(item->cvar) : 0;

	if (item->window.flags & WINDOW_HASFOCUS) {
		memcpy(&newColor, &parent->focusColor, sizeof(vec4_t));
/*
		lowLight[0] = 0.8 * parent->focusColor[0]; 
		lowLight[1] = 0.8 * parent->focusColor[1]; 
		lowLight[2] = 0.8 * parent->focusColor[2]; 
		lowLight[3] = 0.8 * parent->focusColor[3]; 
		LerpColor(parent->focusColor,lowLight,newColor,0.5+0.5*sin((float)(DC->realTime / PULSE_DIVISOR)));
*/
	} else {
		memcpy(&newColor, &item->window.foreColor, sizeof(vec4_t));
	}


	trap_SP_GetStringTextString("MENUS_YES",sYES, sizeof(sYES));
	trap_SP_GetStringTextString("MENUS_NO", sNO,  sizeof(sNO));

	if (item->invertYesNo)
		yesnovalue = (value == 0) ? sYES : sNO;
	else
		yesnovalue = (value != 0) ? sYES : sNO;
	
	if (item->text) 
	{
		Item_Text_Paint(item);
		if (item->xoffset)
		{
			// Want the multi right justified at rect+xoffset, shift by xoffset minus text width
			int textxOffset = item->xoffset - DC->textWidth(yesnovalue, item->textscale, item->iMenuFont);
			DC->drawText(item->textRect.x + textxOffset, item->textRect.y, item->textscale, newColor, yesnovalue, 0,0, item->textStyle, item->iMenuFont);
		}
		else
			DC->drawText(item->textRect.x + item->textRect.w + 8, item->textRect.y, item->textscale, newColor, yesnovalue, 0, 0, item->textStyle, item->iMenuFont);

	} 
	else 
	{
		if (item->xoffset)
		{
			// Want the multi right justified at rect+xoffset, shift by xoffset minus text width
			int textxOffset = item->xoffset - DC->textWidth(yesnovalue, item->textscale, item->iMenuFont);
			DC->drawText(item->textRect.x+textxOffset, item->textRect.y, item->textscale, newColor, yesnovalue, 0, 0, item->textStyle,item->iMenuFont);
		}
		else
			DC->drawText(item->window.rect.x, item->window.rect.y, item->textscale, newColor, yesnovalue, 0, 0, item->textStyle,item->iMenuFont);
	}		
}

void Item_Multi_Paint(itemDef_t *item)
{
	vec4_t newColor;
	const char *text = "";
	menuDef_t *parent = (menuDef_t*)item->parent;
	char	temp[MAX_STRING_CHARS];

	if (item->window.flags & WINDOW_HASFOCUS)
	{
		memcpy(&newColor, &parent->focusColor, sizeof(vec4_t));
	}
	else
	{
		memcpy(&newColor, &item->window.foreColor, sizeof(vec4_t));
	}

	text = Item_Multi_Setting(item);
	if (*text == '@')
	{
		// String reference
		trap_SP_GetStringTextString( &text[1]  , temp, sizeof(temp));
		text = temp;
	}
	else if (*text == '*')
	{
		// Name of a cvar to get the value from
		DC->getCVarString(&text[1], temp, sizeof(temp));
		text = temp;
	}

	// How big is the string:
	int textWidth = DC->textWidth( text, item->textscale, item->iMenuFont );

	if (item->text)
	{
		// Draw the item's label:
		Item_Text_Paint(item);

		int x = item->textRect.x;	// Start at left edge of rectangle
		x += item->xoffset;			// Add xoffset
		x -= textWidth;				// Minus width (to get right-justified)
		x -= 20;					// Leave 16 pixels (and slack) for arrow

		// Draw the text:
		DC->drawText( x, item->textRect.y, item->textscale, newColor, text, 0, 0, item->textStyle, item->iMenuFont );

		// If this item has focus, draw the change arrows two pixels out on either end:
		if (item->window.flags & WINDOW_HASFOCUS)
		{
			qhandle_t arrowShader = trap_R_RegisterShaderNoMip( "gfx/menus/newFront/left_arrow" );
			DC->setColor( NULL );
			int textHeight = DC->textHeight( "", item->textscale, item->iMenuFont );
			DC->drawStretchPic( x - 20, item->textRect.y + (textHeight/2) - 3, 16, 16, 0, 0, 1, 1, arrowShader );
			DC->drawStretchPic( x + textWidth + 4, item->textRect.y + (textHeight/2) - 3, 16, 16, 1, 1, 0, 0, arrowShader );
		}
	}
	else
	{
		int x = item->window.rect.x;	// Start at left edge of window
		x += item->xoffset;				// Add xoffset

		if( item->textalignment == ITEM_ALIGN_RIGHT )
		{
			x -= 20;					// Leave 16 pixels (and slack) for arrow
			x -= textWidth;				// Minus width (for right justified)
		}
		else if( item->textalignment == ITEM_ALIGN_CENTER )
		{
			x -= textWidth / 2;			// Minus half-width (for centered)
		}

		// Draw the text:
		DC->drawText( x, item->window.rect.y, item->textscale, newColor, text, 0, 0, item->textStyle, item->iMenuFont );

		// If this item has focus, draw the change arrows two pixels out on either end:
		if (item->window.flags & WINDOW_HASFOCUS)
		{
			qhandle_t arrowShader = trap_R_RegisterShaderNoMip( "gfx/menus/newFront/left_arrow" );
			DC->setColor( NULL );
			int textHeight = DC->textHeight( "", item->textscale, item->iMenuFont );
			DC->drawStretchPic( x - 20, item->window.rect.y + (textHeight/2) - 3, 16, 16, 0, 0, 1, 1, arrowShader );
			DC->drawStretchPic( x + textWidth + 4, item->window.rect.y + (textHeight/2) - 3, 16, 16, 1, 1, 0, 0, arrowShader );
		}
	}
}


void Item_Slider_Paint(itemDef_t *item)
{
	float x, y, value;

	if (item->text) {
		Item_Text_Paint(item);
	}

	// Offset is to right of the (full) slider:
	x = (item->window.rect.x + item->xoffset) - (SLIDER_WIDTH);
	// And if we drew text, we need to shift vertically to be centered as well:
	y = item->window.rect.y;
	if (item->text)
		y += (DC->textHeight("", item->textscale, item->iMenuFont) / 2) - 2;

	value = Item_Slider_ThumbPosition(item);

	DC->setColor( NULL );
	DC->drawStretchPic( x, y, SLIDER_WIDTH * value, SLIDER_HEIGHT, 0.0f, 0.0f, value, 1.0f, DC->Assets.sliderBar );
}

void UI_ScaleModelAxis(refEntity_t	*ent)

{		// scale the model should we need to
		if (ent->modelScale[0] && ent->modelScale[0] != 1.0f)
		{
			VectorScale( ent->axis[0], ent->modelScale[0] , ent->axis[0] );
			ent->nonNormalizedAxes = qtrue;
		}
		if (ent->modelScale[1] && ent->modelScale[1] != 1.0f)
		{
			VectorScale( ent->axis[1], ent->modelScale[1] , ent->axis[1] );
			ent->nonNormalizedAxes = qtrue;
		}
		if (ent->modelScale[2] && ent->modelScale[2] != 1.0f)
		{
			VectorScale( ent->axis[2], ent->modelScale[2] , ent->axis[2] );
			ent->nonNormalizedAxes = qtrue;
		}
}

#ifndef CGAME
#include "../namespace_end.h"	// Yes, these are inverted. The whole file is in the namespace.
extern void UI_SaberAttachToChar( itemDef_t *item );
#include "../namespace_begin.h"
#endif

void Item_Model_Paint(itemDef_t *item) 
{
	float x, y, w, h;
	refdef_t refdef;
	refEntity_t		ent;
	vec3_t			mins, maxs, origin;
	vec3_t			angles;
	modelDef_t *modelPtr = (modelDef_t*)item->typeData;

	if (modelPtr == NULL) 
	{
		return;
	}

	// a moves datapad anim is playing
#ifndef CGAME
	if (uiInfo.moveAnimTime && (uiInfo.moveAnimTime < uiInfo.uiDC.realTime))
	{
		modelDef_t *modelPtr;
		modelPtr = (modelDef_t*)item->typeData;
		if (modelPtr)
		{
			char modelPath[MAX_QPATH];

			Com_sprintf( modelPath, sizeof( modelPath ), "models/players/%s/model.glm", UI_Cvar_VariableString ( "ui_char_model" ) );
			//HACKHACKHACK: check for any multi-part anim sequences, and play the next anim, if needbe
			switch( modelPtr->g2anim )
			{
			case BOTH_FORCEWALLREBOUND_FORWARD:
			case BOTH_FORCEJUMP1: 
				ItemParse_model_g2anim_go( item, animTable[BOTH_FORCEINAIR1].name );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				if ( !uiInfo.moveAnimTime )
				{
					uiInfo.moveAnimTime = 500;
				}
				uiInfo.moveAnimTime += uiInfo.uiDC.realTime;
				break;
			case BOTH_FORCEINAIR1:
				ItemParse_model_g2anim_go( item, animTable[BOTH_FORCELAND1].name );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				uiInfo.moveAnimTime += uiInfo.uiDC.realTime;
				break;
			case BOTH_FORCEWALLRUNFLIP_START:
				ItemParse_model_g2anim_go( item, animTable[BOTH_FORCEWALLRUNFLIP_END].name );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				uiInfo.moveAnimTime += uiInfo.uiDC.realTime;
				break;
			case BOTH_FORCELONGLEAP_START:
				ItemParse_model_g2anim_go( item, animTable[BOTH_FORCELONGLEAP_LAND].name );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				uiInfo.moveAnimTime += uiInfo.uiDC.realTime;
				break;
			case BOTH_KNOCKDOWN3://on front - into force getup
				trap_S_StartLocalSound( uiInfo.uiDC.Assets.moveJumpSound, CHAN_LOCAL );
				ItemParse_model_g2anim_go( item, animTable[BOTH_FORCE_GETUP_F1].name );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				uiInfo.moveAnimTime += uiInfo.uiDC.realTime;
				break;
			case BOTH_KNOCKDOWN2://on back - kick forward getup
				trap_S_StartLocalSound( uiInfo.uiDC.Assets.moveJumpSound, CHAN_LOCAL );
				ItemParse_model_g2anim_go( item, animTable[BOTH_GETUP_BROLL_F].name );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				uiInfo.moveAnimTime += uiInfo.uiDC.realTime;
				break;
			case BOTH_KNOCKDOWN1://on back - roll-away
				trap_S_StartLocalSound( uiInfo.uiDC.Assets.moveRollSound, CHAN_LOCAL );
				ItemParse_model_g2anim_go( item, animTable[BOTH_GETUP_BROLL_R].name );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				uiInfo.moveAnimTime += uiInfo.uiDC.realTime;
				break;
			default:
				ItemParse_model_g2anim_go( item,  uiInfo.movesBaseAnim );
				ItemParse_asset_model_go( item, modelPath, &uiInfo.moveAnimTime );
				uiInfo.moveAnimTime = 0;
				break;
			}

			UI_UpdateCharacterSkin();

			//update saber models
			UI_SaberAttachToChar( item );
		}
	}
#endif

	// setup the refdef
	memset( &refdef, 0, sizeof( refdef ) );

	refdef.rdflags = RDF_NOWORLDMODEL;
	AxisClear( refdef.viewaxis );
	x = item->window.rect.x+1;
	y = item->window.rect.y+1;
	w = item->window.rect.w-2;
	h = item->window.rect.h-2;

	refdef.x = x * DC->xscale;
	refdef.y = y * DC->yscale;
	refdef.width = w * DC->xscale;
	refdef.height = h * DC->yscale;

	if (item->ghoul2)
	{ //ghoul2 models don't have bounds, so we have to parse them.
		VectorCopy(modelPtr->g2mins, mins);
		VectorCopy(modelPtr->g2maxs, maxs);

		if (!mins[0] && !mins[1] && !mins[2] &&
			!maxs[0] && !maxs[1] && !maxs[2])
		{ //we'll use defaults then I suppose.
			VectorSet(mins, -16, -16, -24);
			VectorSet(maxs, 16, 16, 32);
		}
	}
	else
	{
		DC->modelBounds( item->asset, mins, maxs );
	}

	origin[2] = -0.5 * ( mins[2] + maxs[2] );
	origin[1] = 0.5 * ( mins[1] + maxs[1] );

	// calculate distance so the model nearly fills the box
	if (qtrue) 
	{
		float len = 0.5 * ( maxs[2] - mins[2] );		
		origin[0] = len / 0.268;	// len / tan( fov/2 )
		//origin[0] = len / tan(w/2);
	} 
	else 
	{
		origin[0] = item->textscale;
	}
#ifdef _XBOX
	if(cg->widescreen)
		refdef.fov_x = (modelPtr->fov_x) ? modelPtr->fov_x : (int)((float)refdef.width / 720.0f * 90.0f);
	else
#endif
	refdef.fov_x = (modelPtr->fov_x) ? modelPtr->fov_x : (int)((float)refdef.width / 640.0f * 90.0f);
	refdef.fov_y = (modelPtr->fov_y) ? modelPtr->fov_y : atan2( refdef.height, refdef.width / tan( refdef.fov_x / 360 * M_PI ) ) * ( 360 / M_PI );
	
	//refdef.fov_x = (int)((float)refdef.width / 640.0f * 90.0f);
	//refdef.fov_y = atan2( refdef.height, refdef.width / tan( refdef.fov_x / 360 * M_PI ) ) * ( 360 / M_PI );

	DC->clearScene();

	refdef.time = DC->realTime;

	// add the model

	memset( &ent, 0, sizeof(ent) );

	// use item storage to track
	float curYaw = modelPtr->angle;
	if (modelPtr->rotationSpeed) 
	{
		curYaw += (float)refdef.time/modelPtr->rotationSpeed;
	}
//	if ( item->flags&ITF_ISANYSABER && !(item->flags&ITF_ISCHARACTER) )
//	{//hack to put saber on it's side
//		VectorSet( angles, curYaw, 0, 90 );
//	}
//	else
	if (item->flags&ITF_G2VALID)
	{
		VectorSet( angles, 0, curYaw, 0 );
	}
	else
	{
		// Hack to put the spinning saber logo thing on its side
		VectorSet( angles, curYaw, 0, 90 );
	}

//	if ( (item->flags&ITF_ISANYSABER) && !(item->flags&ITF_ISCHARACTER) )
//	{//hack to put saber on it's side
//		VectorSet( angles, modelPtr->angle, 0, 90 );
//	}

	AnglesToAxis( angles, ent.axis );

	if (item->ghoul2)
	{
		ent.ghoul2 = item->ghoul2;
		ent.radius = 1000;
		ent.customSkin = modelPtr->g2skin;

		VectorCopy(modelPtr->g2scale, ent.modelScale);
		UI_ScaleModelAxis(&ent);
#ifndef CGAME
		if ( (item->flags&ITF_ISCHARACTER) )
		{
			ent.shaderRGBA[0] = ui_char_color_red.integer;
			ent.shaderRGBA[1] = ui_char_color_green.integer;
			ent.shaderRGBA[2] = ui_char_color_blue.integer;
			ent.shaderRGBA[3] = 255;
//			UI_TalkingHead(item);
		}
		if ( item->flags&ITF_ISANYSABER )
		{//UGH, draw the saber blade!
			UI_SaberDrawBlades( item, origin, angles );
		}
#endif
	}
	else
	{
		ent.hModel = item->asset;
	}
	VectorCopy( origin, ent.origin );
	VectorCopy( ent.origin, ent.oldorigin );

	// Set up lighting
	VectorCopy( origin, ent.lightingOrigin );
	ent.renderfx = RF_LIGHTING_ORIGIN | RF_NOSHADOW;

	DC->addRefEntityToScene( &ent );
	DC->renderScene( &refdef );

}

void Item_TextScroll_Paint(itemDef_t *item) 
{
	char cvartext[1024];
	float x, y, size, count, thumb;
	int	  i;
	textScrollDef_t *scrollPtr = (textScrollDef_t*)item->typeData;

	
	scrollPtr->endPos = scrollPtr->startPos;

	// draw scrollbar to right side of the window
/* -- Removed for XBOX
	x = item->window.rect.x + item->window.rect.w - SCROLLBAR_SIZE - 1;
	y = item->window.rect.y + 1;
	DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowUp);
	y += SCROLLBAR_SIZE - 1;

	scrollPtr->endPos = scrollPtr->startPos;
	size = item->window.rect.h - (SCROLLBAR_SIZE * 2);
	DC->drawHandlePic(x, y, SCROLLBAR_SIZE, size+1, DC->Assets.scrollBar);
	y += size - 1;
	DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowDown);
	
	// thumb
	thumb = Item_TextScroll_ThumbDrawPosition(item);
	if (thumb > y - SCROLLBAR_SIZE - 1) 
	{
		thumb = y - SCROLLBAR_SIZE - 1;
	}
	DC->drawHandlePic(x, thumb, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarThumb);
*/
	if (item->cvar) 
	{
		DC->getCVarString(item->cvar, cvartext, sizeof(cvartext));
		item->text = cvartext;
		Item_TextScroll_BuildLines ( item );
	}

	// adjust size for item painting
	size = item->window.rect.h - 2;
	x	 = item->window.rect.x + item->textalignx + 1;
	y	 = item->window.rect.y + item->textaligny + 1;

	// make count the number of lines
	for (i = scrollPtr->startPos; i < 256; i++)
	{
		if(!scrollPtr->pLines[i])
			break;
	}
	scrollPtr->iLineCount	= i;
	count = scrollPtr->iLineCount;

	for (i = scrollPtr->startPos; i < count; i++) 
	{
		const char *text;

		text = scrollPtr->pLines[i];
		if (!text) 
		{
			continue;
		}

		DC->drawText(x + 4, y, item->textscale, item->window.foreColor, text, 0, 0, item->textStyle, item->iMenuFont);

		size -= scrollPtr->lineHeight;
		if (size < scrollPtr->lineHeight) 
		{
			scrollPtr->drawPadding = scrollPtr->lineHeight - size;
			break;
		}

		scrollPtr->endPos++;
		y += scrollPtr->lineHeight;
	}
}

#define COLOR_MAX 255.0f

// Draw routine for list boxes
void Item_ListBox_Paint(itemDef_t *item) {
	float x, y, sizeWidth, count, i, i2,sizeHeight, thumb;
	int startPos;
	qhandle_t image;
	qhandle_t optionalImage1, optionalImage2, optionalImage3;
	listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
//JLF 
	int numlines;
	int endDisplayNum;
	vec4_t	*color;
	vec4_t  newColor;


	// the listbox is horizontal or vertical and has a fixed size scroll bar going either direction
	// elements are enumerated from the DC and either text or image handles are acquired from the DC as well
	// textscale is used to size the text, textalignx and textaligny are used to size image elements
	// there is no clipping available so only the last completely visible item is painted
	count = DC->feederCount(item->special);

//JLFLISTBOX  
#ifdef _XBOX
	if (listPtr->notselectable)
		listPtr->startPos = listPtr->cursorPos;
	//item->cursorPos = listPtr->startPos;
#endif
//JLFLISTBOX 
	if (listPtr->startPos > (count?count-1:count))
	{//probably changed feeders, so reset
		listPtr->startPos = 0;
	}
//JLF END
	if (item->cursorPos > (count?count-1:count))
	{//probably changed feeders, so reset
		listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
		item->cursorPos = listPtr->cursorPos = (count?count-1:count);
		// NOTE : might consider moving this to any spot in here we change the cursor position
		DC->feederSelection( item->special, item->cursorPos, NULL );
	}


	// default is vertical if horizontal flag is not here
	if (item->window.flags & WINDOW_HORIZONTAL) 
	{
#ifdef	_DEBUG
		const char *text;
#endif

//JLF new variable (code just indented) MPMOVED
		if (!listPtr->scrollhidden)
		{
		// draw scrollbar in bottom of the window
		// bar
			if (Item_ListBox_MaxScroll(item) > 0) 
			{
				x = item->window.rect.x + 1;
				y = item->window.rect.y + item->window.rect.h - SCROLLBAR_SIZE - 1;
				DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowLeft);
				x += SCROLLBAR_SIZE - 1;
				sizeWidth = item->window.rect.w - (SCROLLBAR_SIZE * 2);
				DC->drawHandlePic(x, y, sizeWidth+1, SCROLLBAR_SIZE, DC->Assets.scrollBar);
				x += sizeWidth - 1;
				DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowRight);
				// thumb
				thumb = Item_ListBox_ThumbDrawPosition(item);//Item_ListBox_ThumbPosition(item);
				if (thumb > x - SCROLLBAR_SIZE - 1) {
					thumb = x - SCROLLBAR_SIZE - 1;
				}
				DC->drawHandlePic(thumb, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarThumb);
			}
			else if (listPtr->startPos > 0)
			{
				listPtr->startPos = 0;
			}
		}
//JLF end
		//
		listPtr->endPos = listPtr->startPos;
		sizeWidth = item->window.rect.w - 2;
		// items
		// size contains max available space
		if (listPtr->elementStyle == LISTBOX_IMAGE) 
		{
			// fit = 0;
			x = item->window.rect.x + 1;
			y = item->window.rect.y + 1;
			for (i = listPtr->startPos; i < count; i++) 
			{
				// always draw at least one
				// which may overdraw the box if it is too small for the element
				image = DC->feederItemImage(item->special, i);
				if (image) 
				{
#ifndef CGAME
					if (item->window.flags & WINDOW_PLAYERCOLOR) 
					{
						vec4_t	color;

						color[0] = ui_char_color_red.integer/COLOR_MAX;
						color[1] = ui_char_color_green.integer/COLOR_MAX;
						color[2] = ui_char_color_blue.integer/COLOR_MAX;
						color[3] = 1.0f;
						DC->setColor(color);
					}
#endif
					DC->drawHandlePic(x+1, y+1, listPtr->elementWidth - 2, listPtr->elementHeight - 2, image);
				}

				if (i == item->cursorPos) 
				{
					DC->drawRect(x, y, listPtr->elementWidth-1, listPtr->elementHeight-1, item->window.borderSize, item->window.borderColor);
				}

				sizeWidth -= listPtr->elementWidth;
				if (sizeWidth < listPtr->elementWidth) 
				{
					listPtr->drawPadding = sizeWidth; //listPtr->elementWidth - size;
					break;
				}
				x += listPtr->elementWidth;
				listPtr->endPos++;
				// fit++;
			}
		} 
		else 
		{
			//
		}

#ifdef	_DEBUG
		// Show pic name
		text = DC->feederItemText(item->special, item->cursorPos, 0, &optionalImage1, &optionalImage2, &optionalImage3);
		if (text) 
		{
			DC->drawText(item->window.rect.x, item->window.rect.y+item->window.rect.h, item->textscale, item->window.foreColor, text, 0, 0, item->textStyle, item->iMenuFont);
		}
#endif

	} 
	// A vertical list box
	else 
	{
//JLF MPMOVED
		numlines = item->window.rect.h / listPtr->elementHeight;
//JLFEND

		//JLF new variable (code idented with if)
		listPtr->endPos = listPtr->startPos;
		if (!listPtr->scrollhidden)
		{

			// draw scrollbar to right side of the window
			x = item->window.rect.x + item->window.rect.w - SCROLLBAR_SIZE - 1;
			y = item->window.rect.y + 1;
			DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowUp);
			y += SCROLLBAR_SIZE - 1;

			//listPtr->endPos = listPtr->startPos;
			sizeHeight = item->window.rect.h - (SCROLLBAR_SIZE * 2);
			DC->drawHandlePic(x, y, SCROLLBAR_SIZE, sizeHeight+1, DC->Assets.scrollBar);
			y += sizeHeight - 1;
			DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowDown);
			// thumb
			thumb = Item_ListBox_ThumbDrawPosition(item);//Item_ListBox_ThumbPosition(item);
			if (thumb > y - SCROLLBAR_SIZE - 1) {
				thumb = y - SCROLLBAR_SIZE - 1;
			}
			DC->drawHandlePic(x, thumb, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarThumb);
		}
		else
		{
			listPtr->endPos = listPtr->startPos;
		}

//JLF end

		// adjust size for item painting
		sizeWidth = item->window.rect.w - 2;
		sizeHeight = item->window.rect.h - 2;

		if (listPtr->elementStyle == LISTBOX_IMAGE) 
		{
			// Multiple rows and columns (since it's more than twice as wide as an element)
			if ( item->window.rect.w > (listPtr->elementWidth*2) )
			{
				startPos = listPtr->startPos;
				x = item->window.rect.x + 1;
				y = item->window.rect.y + 1;
				// Next row
				for (i2 = startPos; i2 < count; i2++) 
				{
					x = item->window.rect.x + 1;
					sizeWidth = item->window.rect.w - 2;
					// print a row
					for (i = startPos; i < count; i++) 
					{
						// always draw at least one
						// which may overdraw the box if it is too small for the element
						image = DC->feederItemImage(item->special, i);
						if (image) 
						{
		#ifndef CGAME
							if (item->window.flags & WINDOW_PLAYERCOLOR) 
							{
								vec4_t	color;

								color[0] = ui_char_color_red.integer/COLOR_MAX;
								color[1] = ui_char_color_green.integer/COLOR_MAX;
								color[2] = ui_char_color_blue.integer/COLOR_MAX;
								color[3] = 1.0f;
								DC->setColor(color);
							}
		#endif
							DC->drawHandlePic(x+1, y+1, listPtr->elementWidth - 2, listPtr->elementHeight - 2, image);
						}

						if (i == item->cursorPos) 
						{
							DC->drawRect(x, y, listPtr->elementWidth-1, listPtr->elementHeight-1, item->window.borderSize, item->window.borderColor);
						}

						sizeWidth -= listPtr->elementWidth;
						if (sizeWidth < listPtr->elementWidth) 
						{
							listPtr->drawPadding = sizeWidth; //listPtr->elementWidth - size;
							break;
						}
						x += listPtr->elementWidth;
						listPtr->endPos++;
					}

					sizeHeight -= listPtr->elementHeight;
					if (sizeHeight < listPtr->elementHeight) 
					{
						listPtr->drawPadding = sizeHeight; //listPtr->elementWidth - size;
						break;
					}
					// NOTE : Is endPos supposed to be valid or not? It was being used as a valid entry but I changed those
					// few spots that were causing bugs
					listPtr->endPos++;
					startPos = listPtr->endPos;
					y += listPtr->elementHeight;

				}
			}
			// single column
			else
			{
				x = item->window.rect.x + 1;
				y = item->window.rect.y + 1;
				for (i = listPtr->startPos; i < count; i++) 
				{
					// always draw at least one
					// which may overdraw the box if it is too small for the element
					image = DC->feederItemImage(item->special, i);
					if (image) 
					{
						DC->drawHandlePic(x+1, y+1, listPtr->elementWidth - 2, listPtr->elementHeight - 2, image);
					}

					if (i == item->cursorPos) 
					{
						DC->drawRect(x, y, listPtr->elementWidth - 1, listPtr->elementHeight - 1, item->window.borderSize, item->window.borderColor);
					}

					listPtr->endPos++;
					sizeHeight -= listPtr->elementHeight;
					if (sizeHeight < listPtr->elementHeight) 
					{
						listPtr->drawPadding = listPtr->elementHeight - sizeHeight;
						break;
					}
					y += listPtr->elementHeight;
					// fit++;
				}
			}
		} 
		else 
		{
			x = item->window.rect.x + 1;
			y = item->window.rect.y + 1;
//JLF MPMOVED
			y = item->window.rect.y + 1 - listPtr->elementHeight;
#ifdef _XBOX
			if (listPtr->notselectable)
				i = listPtr->startPos - (numlines/2);
			else
				i = listPtr->startPos;
#else
			i = listPtr->startPos;
#endif

			endDisplayNum = i + numlines;
			for ( ; i< endDisplayNum; i++)
			//for (; i < count; i++) 
//JLF END
			{
#ifdef _XBOX	// Draw selection bar using custom shader - do it first, so fancy gfx are under text:
				if (i == item->cursorPos && listPtr->selectionShader)
				{
					int barWidth = item->window.rect.w - 4 - (listPtr->scrollhidden ? 0 : SCROLLBAR_SIZE);
					// Crazy math to match the text offset below. Ugh.
					int yOff = DC->textHeight("", item->textscale, item->iMenuFont);
					yOff = (-yOff) / 2 + (listPtr->elementHeight / 2);
					DC->drawHandlePic(x + 2, y + listPtr->elementHeight + 2 + yOff, barWidth, listPtr->elementHeight, listPtr->selectionShader);
				}
#endif

				const char *text;
				// always draw at least one
				// which may overdraw the box if it is too small for the element
				if (listPtr->numColumns > 0) {
					int j;//, subX = listPtr->elementHeight;

					for (j = 0; j < listPtr->numColumns; j++) 
					{
						char	temp[MAX_STRING_CHARS];
						int imageStartX = listPtr->columnInfo[j].pos;
						text = DC->feederItemText(item->special, i, j, &optionalImage1, &optionalImage2, &optionalImage3);

						if( !text )
						{
							continue;
						}

						if (text[0]=='@')
						{
							trap_SP_GetStringTextString( &text[1]  , temp, sizeof(temp));
							text = temp;
						}

						/*
						if (optionalImage >= 0) {
							DC->drawHandlePic(x + 4 + listPtr->columnInfo[j].pos, y - 1 + listPtr->elementHeight / 2, listPtr->columnInfo[j].width, listPtr->columnInfo[j].width, optionalImage);
						} 
						else 
						*/
						if ( text )
						{
							int textyOffset = 0;
							int textxOffset = 0;

//JLF MPMOVED
#ifdef _XBOX
							float fScaleA = item->textscale;

							textyOffset = DC->textHeight (text, fScaleA, item->iMenuFont);
							textyOffset *= -1;
							textyOffset /=2;
							textyOffset += listPtr->elementHeight/2;
							
							// First column, always left justified:
							if( j == 0 )
							{
								textxOffset = 0;
							}
							else if( (j > 0) && (j < listPtr->numColumns - 1) )
							{ // Middle columns in those with three or more - centered
								// Half the column width minus half the text width -> centered
								textxOffset = (listPtr->columnInfo[j].width / 2) - 
											  (DC->textWidth (text, fScaleA, item->iMenuFont) / 2);
								textxOffset -= 4;	// Cancel out the +4 from below
							}
							else if( j == listPtr->numColumns - 1 )
							{ // Right most column, right justified
								// Colum width, minus text width -> right aligned
								textxOffset = listPtr->columnInfo[j].width -
											  DC->textWidth (text, fScaleA, item->iMenuFont);
								textxOffset -= 8;	// Go 4 pixels from the other border (see below)
							}
#endif

//							DC->drawText(x + 4 + listPtr->columnInfo[j].pos, y + listPtr->elementHeight, item->textscale, item->window.foreColor, text, 0, listPtr->columnInfo[j].maxChars, item->textStyle);
	//WAS LAST						DC->drawText(x + 4 + listPtr->columnInfo[j].pos, y, item->textscale, item->window.foreColor, text, 0, listPtr->columnInfo[j].maxChars, item->textStyle, item->iMenuFont);
#ifdef _XBOX

							if (i == item->cursorPos) 
							{
								if (item->window.flags & WINDOW_HASFOCUS)
								{
									Item_TextColor(item,&newColor);
									color = &newColor;
								}
								else
									color = &item->window.outlineColor;
							}
							else
							{
								color = &item->window.foreColor;
							}

							DC->drawText(x + 4 + listPtr->columnInfo[j].pos + textxOffset, y + listPtr->elementHeight+ textyOffset + item->textaligny, item->textscale, *color, text, 0,listPtr->columnInfo[j].width, item->textStyle, item->iMenuFont);

							// We alter the text color the selected item, rather than drawing the box:
//							if ( i == item->cursorPos )
//								DC->drawText(x + 4 + listPtr->columnInfo[j].pos + textxOffset, y + listPtr->elementHeight+ textyOffset + item->textaligny, item->textscale, item->window.outlineColor, text, 0,listPtr->columnInfo[j].width, item->textStyle, item->iMenuFont);
//							else
//								DC->drawText(x + 4 + listPtr->columnInfo[j].pos + textxOffset, y + listPtr->elementHeight+ textyOffset + item->textaligny, item->textscale, item->window.foreColor, text, 0,listPtr->columnInfo[j].width, item->textStyle, item->iMenuFont);
#else
							DC->drawText(x + 4 + listPtr->columnInfo[j].pos + textxOffset, y + listPtr->elementHeight+ textyOffset + item->textaligny, item->textscale, item->window.foreColor, text, 0,listPtr->columnInfo[j].width, item->textStyle, item->iMenuFont);
#endif

//JLF END
						}
#ifdef _XBOX
						// I fail to see how the PC code here EVER does the right thing
						// We'll just use our own version:
						DC->setColor( NULL );
						if (optionalImage1 >= 0)
						{
							DC->drawHandlePic(x + listPtr->columnInfo[j].pos, y+listPtr->elementHeight+2, listPtr->elementHeight, listPtr->elementHeight, optionalImage1);
						}
#else // _XBOX
						if ( j < listPtr->numColumns - 1 )
						{
							imageStartX = listPtr->columnInfo[j+1].pos;
						}
						DC->setColor( NULL );
						if (optionalImage3 >= 0) 
						{
							DC->drawHandlePic(imageStartX - listPtr->elementHeight*3, y+listPtr->elementHeight+2, listPtr->elementHeight, listPtr->elementHeight, optionalImage3);
						} 
						if (optionalImage2 >= 0) 
						{
							DC->drawHandlePic(imageStartX - listPtr->elementHeight*2, y+listPtr->elementHeight+2, listPtr->elementHeight, listPtr->elementHeight, optionalImage2);
						} 
						if (optionalImage1 >= 0) 
						{
							DC->drawHandlePic(imageStartX - listPtr->elementHeight, y+listPtr->elementHeight+2, listPtr->elementHeight, listPtr->elementHeight, optionalImage1);
						} 
#endif // _XBOX
					}
				} 
				else 
				{
#ifdef _XBOX
					if (i >= 0)
					{
#endif
					text = DC->feederItemText(item->special, i, 0, &optionalImage1, &optionalImage2, &optionalImage3 );
					if ( optionalImage1 >= 0 || optionalImage2 >= 0 || optionalImage3 >= 0) 
					{
						//DC->drawHandlePic(x + 4 + listPtr->elementHeight, y, listPtr->columnInfo[j].width, listPtr->columnInfo[j].width, optionalImage);
					} 
					else if (text) 
					{
//						DC->drawText(x + 4, y + listPtr->elementHeight, item->textscale, item->window.foreColor, text, 0, 0, item->textStyle);
						DC->drawText(x + 4, y + item->textaligny, item->textscale, item->window.foreColor, text, 0, 0, item->textStyle, item->iMenuFont);
					}
#ifdef _XBOX
					}
#endif
				}

#ifndef _XBOX
				if (i == item->cursorPos) 
				{
					DC->fillRect(x + 2, y + listPtr->elementHeight + 2, item->window.rect.w - SCROLLBAR_SIZE - 4, listPtr->elementHeight, item->window.outlineColor);
				}
#endif

				sizeHeight -= listPtr->elementHeight;
				if (sizeHeight < listPtr->elementHeight) 
				{
					listPtr->drawPadding = listPtr->elementHeight - sizeHeight;
					break;
				}
				listPtr->endPos++;
				y += listPtr->elementHeight;
				// fit++;
			}
		}
	}
}


void Item_OwnerDraw_Paint(itemDef_t *item) {
  menuDef_t *parent;

	if (item == NULL) {
		return;
	}
  parent = (menuDef_t*)item->parent;

	if (DC->ownerDrawItem) {
		vec4_t color, lowLight;
		menuDef_t *parent = (menuDef_t*)item->parent;
		Fade(&item->window.flags, &item->window.foreColor[3], parent->fadeClamp, &item->window.nextTime, parent->fadeCycle, qtrue, parent->fadeAmount);
		memcpy(&color, &item->window.foreColor, sizeof(color));
/*
		if (item->numColors > 0 && DC->getValue) {
			// if the value is within one of the ranges then set color to that, otherwise leave at default
			int i;
			float f = DC->getValue(item->window.ownerDraw);
			for (i = 0; i < item->numColors; i++) {
				if (f >= item->colorRanges[i].low && f <= item->colorRanges[i].high) {
					memcpy(&color, &item->colorRanges[i].color, sizeof(color));
					break;
				}
			}
		}
*/

		if (item->window.flags & WINDOW_HASFOCUS) {
			memcpy(color, &parent->focusColor, sizeof(vec4_t));
/*
			lowLight[0] = 0.8 * parent->focusColor[0]; 
			lowLight[1] = 0.8 * parent->focusColor[1]; 
			lowLight[2] = 0.8 * parent->focusColor[2]; 
			lowLight[3] = 0.8 * parent->focusColor[3]; 
			LerpColor(parent->focusColor,lowLight,color,0.5+0.5*sin((float)(DC->realTime / PULSE_DIVISOR)));
*/
		} else if (item->textStyle == ITEM_TEXTSTYLE_BLINK && !((DC->realTime/BLINK_DIVISOR) & 1)) {
			lowLight[0] = 0.8 * item->window.foreColor[0]; 
			lowLight[1] = 0.8 * item->window.foreColor[1]; 
			lowLight[2] = 0.8 * item->window.foreColor[2]; 
			lowLight[3] = 0.8 * item->window.foreColor[3]; 
			LerpColor(item->window.foreColor,lowLight,color,0.5+0.5*sin((float)(DC->realTime / PULSE_DIVISOR)));
		}

		if (item->disabled) 
		{
			memcpy(color, parent->disableColor, sizeof(vec4_t)); // bk001207 - FIXME: Com_Memcpy
		}

		if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) {
		  memcpy(color, parent->disableColor, sizeof(vec4_t)); // bk001207 - FIXME: Com_Memcpy
		}
	
		if (item->text) {
			Item_Text_Paint(item);
				if (item->text[0]) {
					// +8 is an offset kludge to properly align owner draw items that have text combined with them
					DC->ownerDrawItem(item->textRect.x + item->textRect.w + 8, item->window.rect.y, item->window.rect.w, item->window.rect.h, 0, item->textaligny, item->window.ownerDraw, item->window.ownerDrawFlags, item->alignment, item->special, item->textscale, color, item->window.background, item->textStyle,item->iMenuFont);
				} else {
					DC->ownerDrawItem(item->textRect.x + item->textRect.w, item->window.rect.y, item->window.rect.w, item->window.rect.h, 0, item->textaligny, item->window.ownerDraw, item->window.ownerDrawFlags, item->alignment, item->special, item->textscale, color, item->window.background, item->textStyle,item->iMenuFont);
				}
			} else {
			DC->ownerDrawItem(item->window.rect.x, item->window.rect.y, item->window.rect.w, item->window.rect.h, item->textalignx, item->textaligny, item->window.ownerDraw, item->window.ownerDrawFlags, item->alignment, item->special, item->textscale, color, item->window.background, item->textStyle,item->iMenuFont);
		}
	}
}


void Item_Paint(itemDef_t *item) 
{
	vec4_t		red;
	menuDef_t *parent = (menuDef_t*)item->parent;
	int			xPos,textWidth;
	vec4_t		color = {1, 1, 1, 1};

	red[0] = red[3] = 1;
	red[1] = red[2] = 0;

	if (item == NULL) 
	{
		return;
	}

	if (item->window.flags & WINDOW_SCRIPTWAITING)
	{
		if (DC->realTime > item->window.delayTime)
		{ // Time has elapsed, resume running whatever script we saved
			item->window.flags &= ~WINDOW_SCRIPTWAITING;
			Item_RunScript(item, ui_delayedScript);
		}
	}

	if (item->window.flags & WINDOW_ORBITING) 
	{
		if (DC->realTime > item->window.nextTime) 
		{
			float rx, ry, a, c, s, w, h;
      
			item->window.nextTime = DC->realTime + item->window.offsetTime;
			// translate
			w = item->window.rectClient.w / 2;
			h = item->window.rectClient.h / 2;
			rx = item->window.rectClient.x + w - item->window.rectEffects.x;
			ry = item->window.rectClient.y + h - item->window.rectEffects.y;
			a = 3 * M_PI / 180;
			c = cos(a);
			s = sin(a);
			item->window.rectClient.x = (rx * c - ry * s) + item->window.rectEffects.x - w;
			item->window.rectClient.y = (rx * s + ry * c) + item->window.rectEffects.y - h;
			Item_UpdatePosition(item);
		}
	}


	if (item->window.flags & WINDOW_INTRANSITION) 
	{
		if (DC->realTime > item->window.nextTime) 
		{
			int done = 0;
			item->window.nextTime = DC->realTime + item->window.offsetTime;

			// transition the x,y
			if (item->window.rectClient.x == item->window.rectEffects.x) 
			{
				done++;
			} 
			else 
			{
				if (item->window.rectClient.x < item->window.rectEffects.x) 
				{
					item->window.rectClient.x += item->window.rectEffects2.x;
					if (item->window.rectClient.x > item->window.rectEffects.x) 
					{
						item->window.rectClient.x = item->window.rectEffects.x;
						done++;
					}
				} 
				else 
				{
					item->window.rectClient.x -= item->window.rectEffects2.x;
					if (item->window.rectClient.x < item->window.rectEffects.x) 
					{
						item->window.rectClient.x = item->window.rectEffects.x;
						done++;
					}
				}
			}

			if (item->window.rectClient.y == item->window.rectEffects.y) 
			{
				done++;
			} 
			else 
			{
				if (item->window.rectClient.y < item->window.rectEffects.y) 
				{
					item->window.rectClient.y += item->window.rectEffects2.y;
					if (item->window.rectClient.y > item->window.rectEffects.y) 
					{
						item->window.rectClient.y = item->window.rectEffects.y;
						done++;
					}
				} 
				else 
				{
					item->window.rectClient.y -= item->window.rectEffects2.y;
					if (item->window.rectClient.y < item->window.rectEffects.y) 
					{
						item->window.rectClient.y = item->window.rectEffects.y;
						done++;
					}
				}
			}

			if (item->window.rectClient.w == item->window.rectEffects.w) 
			{
				done++;
			} 
			else 
			{
				if (item->window.rectClient.w < item->window.rectEffects.w) 
				{
					item->window.rectClient.w += item->window.rectEffects2.w;
					if (item->window.rectClient.w > item->window.rectEffects.w) 
					{
						item->window.rectClient.w = item->window.rectEffects.w;
						done++;
					}
				} 
				else 
				{
					item->window.rectClient.w -= item->window.rectEffects2.w;
					if (item->window.rectClient.w < item->window.rectEffects.w) 
					{
						item->window.rectClient.w = item->window.rectEffects.w;
						done++;
					}
				}
			}

			if (item->window.rectClient.h == item->window.rectEffects.h) 
			{
				done++;
			} 
			else 
			{
				if (item->window.rectClient.h < item->window.rectEffects.h) 
				{
					item->window.rectClient.h += item->window.rectEffects2.h;
					if (item->window.rectClient.h > item->window.rectEffects.h) 
					{
						item->window.rectClient.h = item->window.rectEffects.h;
						done++;
					}
				} 
				else 
				{
					item->window.rectClient.h -= item->window.rectEffects2.h;
					if (item->window.rectClient.h < item->window.rectEffects.h) 
					{
						item->window.rectClient.h = item->window.rectEffects.h;
						done++;
					}
				}
			}

			Item_UpdatePosition(item);

			if (done == 4) 
			{
				item->window.flags &= ~WINDOW_INTRANSITION;
			}

		}
	}

#ifdef _TRANS3
	
//JLF begin model transition stuff
	if (item->window.flags & WINDOW_INTRANSITIONMODEL) 
	{
		if ( item->type == ITEM_TYPE_MODEL)
		{
//fields ing modelptr
//				vec3_t g2mins2, g2maxs2, g2minsEffect, g2maxsEffect;
//	float fov_x2, fov_y2, fov_Effectx, fov_Effecty;

			modelDef_t  * modelptr = (modelDef_t *)item->typeData;

			if (DC->realTime > item->window.nextTime) 
			{
				int done = 0;
				item->window.nextTime = DC->realTime + item->window.offsetTime;


// transition the x,y,z max
				if (modelptr->g2maxs[0] == modelptr->g2maxs2[0]) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->g2maxs[0] < modelptr->g2maxs2[0]) 
					{
						modelptr->g2maxs[0] += modelptr->g2maxsEffect[0];
						if (modelptr->g2maxs[0] > modelptr->g2maxs2[0]) 
						{
							modelptr->g2maxs[0] = modelptr->g2maxs2[0];
							done++;
						}
					} 
					else 
					{
						modelptr->g2maxs[0] -= modelptr->g2maxsEffect[0];
						if (modelptr->g2maxs[0] < modelptr->g2maxs2[0]) 
						{
							modelptr->g2maxs[0] = modelptr->g2maxs2[0];
							done++;
						}
					}
				}
//y
				if (modelptr->g2maxs[1] == modelptr->g2maxs2[1]) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->g2maxs[1] < modelptr->g2maxs2[1]) 
					{
						modelptr->g2maxs[1] += modelptr->g2maxsEffect[1];
						if (modelptr->g2maxs[1] > modelptr->g2maxs2[1]) 
						{
							modelptr->g2maxs[1] = modelptr->g2maxs2[1];
							done++;
						}
					} 
					else 
					{
						modelptr->g2maxs[1] -= modelptr->g2maxsEffect[1];
						if (modelptr->g2maxs[1] < modelptr->g2maxs2[1]) 
						{
							modelptr->g2maxs[1] = modelptr->g2maxs2[1];
							done++;
						}
					}
				}


//z

				if (modelptr->g2maxs[2] == modelptr->g2maxs2[2]) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->g2maxs[2] < modelptr->g2maxs2[2]) 
					{
						modelptr->g2maxs[2] += modelptr->g2maxsEffect[2];
						if (modelptr->g2maxs[2] > modelptr->g2maxs2[2]) 
						{
							modelptr->g2maxs[2] = modelptr->g2maxs2[2];
							done++;
						}
					} 
					else 
					{
						modelptr->g2maxs[2] -= modelptr->g2maxsEffect[2];
						if (modelptr->g2maxs[2] < modelptr->g2maxs2[2]) 
						{
							modelptr->g2maxs[2] = modelptr->g2maxs2[2];
							done++;
						}
					}
				}

// transition the x,y,z min
				if (modelptr->g2mins[0] == modelptr->g2mins2[0]) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->g2mins[0] < modelptr->g2mins2[0]) 
					{
						modelptr->g2mins[0] += modelptr->g2minsEffect[0];
						if (modelptr->g2mins[0] > modelptr->g2mins2[0]) 
						{
							modelptr->g2mins[0] = modelptr->g2mins2[0];
							done++;
						}
					} 
					else 
					{
						modelptr->g2mins[0] -= modelptr->g2minsEffect[0];
						if (modelptr->g2mins[0] < modelptr->g2mins2[0]) 
						{
							modelptr->g2mins[0] = modelptr->g2mins2[0];
							done++;
						}
					}
				}
//y
				if (modelptr->g2mins[1] == modelptr->g2mins2[1]) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->g2mins[1] < modelptr->g2mins2[1]) 
					{
						modelptr->g2mins[1] += modelptr->g2minsEffect[1];
						if (modelptr->g2mins[1] > modelptr->g2mins2[1]) 
						{
							modelptr->g2mins[1] = modelptr->g2mins2[1];
							done++;
						}
					} 
					else 
					{
						modelptr->g2mins[1] -= modelptr->g2minsEffect[1];
						if (modelptr->g2mins[1] < modelptr->g2mins2[1]) 
						{
							modelptr->g2mins[1] = modelptr->g2mins2[1];
							done++;
						}
					}
				}


//z

				if (modelptr->g2mins[2] == modelptr->g2mins2[2]) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->g2mins[2] < modelptr->g2mins2[2]) 
					{
						modelptr->g2mins[2] += modelptr->g2minsEffect[2];
						if (modelptr->g2mins[2] > modelptr->g2mins2[2]) 
						{
							modelptr->g2mins[2] = modelptr->g2mins2[2];
							done++;
						}
					} 
					else 
					{
						modelptr->g2mins[2] -= modelptr->g2minsEffect[2];
						if (modelptr->g2mins[2] < modelptr->g2mins2[2]) 
						{
							modelptr->g2mins[2] = modelptr->g2mins2[2];
							done++;
						}
					}
				}



//fovx
				if (modelptr->fov_x == modelptr->fov_x2) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->fov_x < modelptr->fov_x2) 
					{
						modelptr->fov_x += modelptr->fov_Effectx;
						if (modelptr->fov_x > modelptr->fov_x2) 
						{
							modelptr->fov_x = modelptr->fov_x2;
							done++;
						}
					} 
					else 
					{
						modelptr->fov_x -= modelptr->fov_Effectx;
						if (modelptr->fov_x < modelptr->fov_x2) 
						{
							modelptr->fov_x = modelptr->fov_x2;
							done++;
						}
					}
				}

//fovy
				if (modelptr->fov_y == modelptr->fov_y2) 
				{
					done++;
				} 
				else 
				{
					if (modelptr->fov_y < modelptr->fov_y2) 
					{
						modelptr->fov_y += modelptr->fov_Effecty;
						if (modelptr->fov_y > modelptr->fov_y2) 
						{
							modelptr->fov_y = modelptr->fov_y2;
							done++;
						}
					} 
					else 
					{
						modelptr->fov_y -= modelptr->fov_Effecty;
						if (modelptr->fov_y < modelptr->fov_y2) 
						{
							modelptr->fov_y = modelptr->fov_y2;
							done++;
						}
					}
				}

				if (done == 5) 
				{
					item->window.flags &= ~WINDOW_INTRANSITIONMODEL;
				}

			}
		}
	}
#endif
//JLF end transition stuff for models



	if (item->window.ownerDrawFlags && DC->ownerDrawVisible) {
		if (!DC->ownerDrawVisible(item->window.ownerDrawFlags)) {
			item->window.flags &= ~WINDOW_VISIBLE;
		} else {
			item->window.flags |= WINDOW_VISIBLE;
		}
	}

	if (item->cvarFlags & (CVAR_SHOW | CVAR_HIDE)) {
		if (!Item_EnableShowViaCvar(item, CVAR_SHOW)) {
			return;
		}
	}

  if (item->window.flags & WINDOW_TIMEDVISIBLE) {

	}

	if (!(item->window.flags & WINDOW_VISIBLE)) 
	{
		return;
	}


//JLFMOUSE
	if (item->window.flags & WINDOW_HASFOCUS)
	{
		if (item->descText)
		{
			const char *textPtr = item->descText;
			if (*textPtr == '@')	// string reference
			{
				char	temp[MAX_STRING_CHARS];
				trap_SP_GetStringTextString( &textPtr[1]  , temp, sizeof(temp));
				textPtr = temp;
			}

			Item_TextColor(item, &color);

			{// stupid C language
				float fDescScale = parent->descScale ? parent->descScale : 1;
				float fDescScaleCopy = fDescScale;
				int iYadj = 0;
				while (1)
				{
					textWidth = DC->textWidth(textPtr,fDescScale, FONT_SMALL2);

					if (parent->descAlignment == ITEM_ALIGN_RIGHT)
					{
						xPos = parent->descX - textWidth;	// Right justify
					}
					else if (parent->descAlignment == ITEM_ALIGN_CENTER)
					{
						xPos = parent->descX - (textWidth/2);	// Center justify
					}
					else										// Left justify	
					{
						xPos = parent->descX;
					}

					if (parent->descAlignment == ITEM_ALIGN_CENTER)
					{
						// only this one will auto-shrink the scale until we eventually fit...
						//
						int screenw = 640;
#ifdef _XBOX
						if(cg->widescreen)
							screenw = 720;
#endif
						if (xPos + textWidth > (screenw-4)) {
							fDescScale -= 0.001f;
							continue;
						}
					}

					// Try to adjust it's y placement if the scale has changed...
					//
					if (fDescScale != fDescScaleCopy)
					{
						int iOriginalTextHeight = DC->textHeight(textPtr, fDescScaleCopy, FONT_MEDIUM);
						iYadj = iOriginalTextHeight - DC->textHeight(textPtr, fDescScale, FONT_MEDIUM);
					}

					DC->drawText(xPos, parent->descY + iYadj, fDescScale, parent->descColor, textPtr, 0, 0, item->textStyle, FONT_SMALL2);
					break;
				}
			}
		}
	}

  // paint the rect first.. 
  Window_Paint(&item->window, parent->fadeAmount , parent->fadeClamp, parent->fadeCycle);

  // Draw box to show rectangle extents, in debug mode
  if (debugMode) 
  {
	vec4_t color;
    color[1] = color[3] = 1;
    color[0] = color[2] = 0;
	DC->drawRect(
		item->window.rect.x, 
		item->window.rect.y, 
		item->window.rect.w, 
		item->window.rect.h, 
		1, 
		color);
  }

  //DC->drawRect(item->window.rect.x, item->window.rect.y, item->window.rect.w, item->window.rect.h, 1, red);

  switch (item->type) {
    case ITEM_TYPE_OWNERDRAW:
      Item_OwnerDraw_Paint(item);
      break;
    case ITEM_TYPE_TEXT:
    case ITEM_TYPE_BUTTON:
      Item_Text_Paint(item);
      break;
    case ITEM_TYPE_RADIOBUTTON:
      break;
    case ITEM_TYPE_CHECKBOX:
      break;
    case ITEM_TYPE_COMBO:
      break;
    case ITEM_TYPE_LISTBOX:
      Item_ListBox_Paint(item);
      break;
	case ITEM_TYPE_TEXTSCROLL:
	  Item_TextScroll_Paint ( item );
	  break;
    case ITEM_TYPE_MODEL:
      Item_Model_Paint(item);
      break;
    case ITEM_TYPE_YESNO:
      Item_YesNo_Paint(item);
      break;
    case ITEM_TYPE_MULTI:
      Item_Multi_Paint(item);
      break;
    case ITEM_TYPE_SLIDER:
      Item_Slider_Paint(item);
      break;
    default:
      break;
  }

}

void Menu_Init(menuDef_t *menu) {
	memset(menu, 0, sizeof(menuDef_t));
	menu->cursorItem = -1;
	menu->fadeAmount = DC->Assets.fadeAmount;
	menu->fadeClamp = DC->Assets.fadeClamp;
	menu->fadeCycle = DC->Assets.fadeCycle;
	Window_Init(&menu->window);
}

itemDef_t *Menu_GetFocusedItem(menuDef_t *menu) {
  int i;
  if (menu) {
    for (i = 0; i < menu->itemCount; i++) {
      if (menu->items[i]->window.flags & WINDOW_HASFOCUS) {
        return menu->items[i];
      }
    }
  }
  return NULL;
}

menuDef_t *Menu_GetFocused() {
  int i;
  for (i = 0; i < menuCount; i++) {
    if (Menus[i].window.flags & WINDOW_HASFOCUS && Menus[i].window.flags & WINDOW_VISIBLE) {
      return &Menus[i];
    }
  }
  return NULL;
}

void Menu_ScrollFeeder(menuDef_t *menu, int feeder, qboolean down) {
	if (menu) {
		int i;
    for (i = 0; i < menu->itemCount; i++) {
			if (menu->items[i]->special == feeder) {
				Item_ListBox_HandleKey(menu->items[i], (down) ? A_CURSOR_DOWN : A_CURSOR_UP, qtrue, qtrue);
				return;
			}
		}
	}
}



void Menu_SetFeederSelection(menuDef_t *menu, int feeder, int index, const char *name) {
	if (menu == NULL) {
		if (name == NULL) {
			menu = Menu_GetFocused();
		} else {
			menu = Menus_FindByName(name);
		}
	}

	if (menu) {
		int i;
    for (i = 0; i < menu->itemCount; i++) {
			if (menu->items[i]->special == feeder) {
				if (index == 0) {
					listBoxDef_t *listPtr = (listBoxDef_t*)menu->items[i]->typeData;
					listPtr->cursorPos = 0;
					listPtr->startPos = 0;
				}
				menu->items[i]->cursorPos = index;

				DC->feederSelection(menu->items[i]->special, menu->items[i]->cursorPos, NULL);
				return;
			}
		}
	}
}

qboolean Menus_AnyFullScreenVisible() {
  int i;
  for (i = 0; i < menuCount; i++) {
    if (Menus[i].window.flags & WINDOW_VISIBLE && Menus[i].fullScreen) {
			return qtrue;
    }
  }
  return qfalse;
}

menuDef_t *Menus_ActivateByName(const char *p) {
  int i;
  menuDef_t *m = NULL;
	menuDef_t *focus = Menu_GetFocused();

		// get the controller disconntected menu
	menuDef_t*	disconnected	= Menus_FindByName("noController");
	bool		giveDisconnectedFocus	= false;

	if(disconnected && (disconnected->window.flags & (WINDOW_VISIBLE | WINDOW_HASFOCUS)))
	{
		// don't open any menus below the no controller
		giveDisconnectedFocus = true;
		return NULL;
	}

  for (i = 0; i < menuCount; i++) {
//JLF debugtest
 // OutputDebugString(Menus[i].window.name);
    if (Q_stricmp(Menus[i].window.name, p) == 0) {
	    m = &Menus[i];
			Menus_Activate(m);
			if (openMenuCount < MAX_OPEN_MENUS && focus != NULL) {
				menuStack[openMenuCount++] = focus;
			}
    } else {
      Menus[i].window.flags &= ~WINDOW_HASFOCUS;
    }
  }

	// Want to handle a mouse move on the new menu in case your already over an item
	Menu_HandleMouseMove ( m, DC->cursorx, DC->cursory );

	if(!m)
	{
		Com_Printf("WARNING: Could not open %s\n", p);
	}

	return m;
}


void Item_Init(itemDef_t *item) {
	memset(item, 0, sizeof(itemDef_t));
	item->textscale = 0.55f;
	Window_Init(&item->window);
}
#include "../namespace_end.h"
extern int gScrollDelta;
#include "../namespace_begin.h"

void Menu_HandleMouseMove(menuDef_t *menu, float x, float y) {
#ifdef _XBOX
	if (!menu)
		return;

	itemDef_t * item;
	int i, itemCount;
	itemCount = menu->itemCount;
	textScrollDef_t *scrollPtr;
	int scrollCount;
	if (menu->window.flags & WINDOW_HASFOCUS)
	{
		for( i = 0; i < itemCount ;i++)
		{
			item = menu->items[i];

			if (item->type != ITEM_TYPE_TEXTSCROLL)
				continue;

			if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) 
				continue;

			if (item->cvarFlags & (CVAR_SHOW | CVAR_HIDE) && !Item_EnableShowViaCvar(item, CVAR_SHOW)) 
				continue;

			if ( !(item->window.flags & WINDOW_VISIBLE) )
				continue;

			scrollPtr = (textScrollDef_t*)item->typeData;
			if (gScrollDelta >0)
			{
				if (scrollPtr->endPos < scrollPtr->iLineCount-1)
				{
					scrollPtr->startPos+= gScrollDelta;
				}
			}
			else if (gScrollDelta <0)
			{
				if(scrollPtr->startPos > 0)
				{
					scrollPtr->startPos+=gScrollDelta;
				}
			}

			if ((scrollPtr->endPos +1 >= scrollPtr->iLineCount) || (scrollPtr->endPos == 0))
			{
				trap_Cvar_Set("ui_downArrow","0");
			}
			else
			{
				trap_Cvar_Set("ui_downArrow","1");
			}
			if (scrollPtr->startPos <= 0)
			{
				trap_Cvar_Set("ui_upArrow","0");
			}
			else
			{
				trap_Cvar_Set("ui_upArrow","1");
			}
		}
	}
#endif
	return;
}

void Menu_Paint(menuDef_t *menu, qboolean forcePaint) {
	int i;

	if (menu == NULL) {
		return;
	}

	if (menu->window.flags & WINDOW_SCRIPTWAITING)
	{
		if (DC->realTime > menu->window.delayTime)
		{ // Time has elapsed, resume running whatever script we saved
			itemDef_t item;
			item.parent = menu;
			item.window.flags = 0;	//clear before calling RunScript
			menu->window.flags &= ~WINDOW_SCRIPTWAITING;
			Item_RunScript(&item, ui_delayedScript);

			// Could have hit another delay. Need to hoist from fake item
			if (item.window.flags & WINDOW_SCRIPTWAITING)
			{
				menu->window.flags |= WINDOW_SCRIPTWAITING;
				menu->window.delayTime = item.window.delayTime;
			}
		}
	}

	if (!(menu->window.flags & WINDOW_VISIBLE) &&  !forcePaint) {
		return;
	}

	if (menu->window.ownerDrawFlags && DC->ownerDrawVisible && !DC->ownerDrawVisible(menu->window.ownerDrawFlags)) {
		return;
	}
	
	if (forcePaint) {
		menu->window.flags |= WINDOW_FORCED;
	}

	// draw the background if necessary
	if (menu->fullScreen) {
		// implies a background shader
		// FIXME: make sure we have a default shader if fullscreen is set with no background
#ifdef _XBOX
		if(cg->widescreen)
			DC->drawHandlePic( 0, 0, 720, SCREEN_HEIGHT, menu->window.background );
		else
#endif
		DC->drawHandlePic( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, menu->window.background );
	} else if (menu->window.background) {
		// this allows a background shader without being full screen
		//UI_DrawHandlePic(menu->window.rect.x, menu->window.rect.y, menu->window.rect.w, menu->window.rect.h, menu->backgroundShader);
	}

	// paint the background and or border
	Window_Paint(&menu->window, menu->fadeAmount, menu->fadeClamp, menu->fadeCycle );

	// Loop through all items for the menu and paint them
	for (i = 0; i < menu->itemCount; i++) 
	{
		if (!menu->items[i]->appearanceSlot)
		{
			Item_Paint(menu->items[i]);
		}
		else // Timed order of appearance
		{
			if (menu->appearanceTime < DC->realTime)	// Time to show another item
			{
				menu->appearanceTime = DC->realTime + menu->appearanceIncrement;
				menu->appearanceCnt++;
			}

			if (menu->items[i]->appearanceSlot<=menu->appearanceCnt)
			{
				Item_Paint(menu->items[i]);
			}
		}
	}

	if (debugMode) {
		vec4_t color;
		color[0] = color[2] = color[3] = 1;
		color[1] = 0;
		DC->drawRect(menu->window.rect.x, menu->window.rect.y, menu->window.rect.w, menu->window.rect.h, 1, color);
	}
}

/*
===============
Item_ValidateTypeData
===============
*/
void Item_ValidateTypeData(itemDef_t *item) 
{
	if (item->typeData) 
	{
		return;
	}

	if (item->type == ITEM_TYPE_LISTBOX) 
	{
		item->typeData = UI_Alloc(sizeof(listBoxDef_t));
		memset(item->typeData, 0, sizeof(listBoxDef_t));
	}
	else if (item->type == ITEM_TYPE_EDITFIELD || item->type == ITEM_TYPE_NUMERICFIELD || item->type == ITEM_TYPE_YESNO || item->type == ITEM_TYPE_BIND || item->type == ITEM_TYPE_SLIDER || item->type == ITEM_TYPE_TEXT) 
	{
		item->typeData = UI_Alloc(sizeof(editFieldDef_t));
		memset(item->typeData, 0, sizeof(editFieldDef_t));
		if (item->type == ITEM_TYPE_EDITFIELD) 
		{
			if (!((editFieldDef_t *) item->typeData)->maxPaintChars) 
			{
				((editFieldDef_t *) item->typeData)->maxPaintChars = MAX_EDITFIELD;
			}
		}
	} 
	else if (item->type == ITEM_TYPE_MULTI) 
	{
		item->typeData = UI_Alloc(sizeof(multiDef_t));
		memset(item->typeData, 0, sizeof(multiDef_t));
	} 
	else if (item->type == ITEM_TYPE_MODEL) 
	{
		item->typeData = UI_Alloc(sizeof(modelDef_t));
		memset(item->typeData, 0, sizeof(modelDef_t));
	}
	else if (item->type == ITEM_TYPE_TEXTSCROLL )
	{
		item->typeData = UI_Alloc(sizeof(textScrollDef_t));
	}
}

/*
===============
Keyword Hash
===============
*/

#define KEYWORDHASH_SIZE	512

typedef struct keywordHash_s
{
	char *keyword;
	qboolean (*func)(itemDef_t *item, int handle);
	struct keywordHash_s *next;
} keywordHash_t;

int KeywordHash_Key(char *keyword) {
	int register hash, i;

	hash = 0;
	for (i = 0; keyword[i] != '\0'; i++) {
		if (keyword[i] >= 'A' && keyword[i] <= 'Z')
			hash += (keyword[i] + ('a' - 'A')) * (119 + i);
		else
			hash += keyword[i] * (119 + i);
	}
	hash = (hash ^ (hash >> 10) ^ (hash >> 20)) & (KEYWORDHASH_SIZE-1);
	return hash;
}

void KeywordHash_Add(keywordHash_t *table[], keywordHash_t *key) {
	int hash;

	hash = KeywordHash_Key(key->keyword);
/*
	if (table[hash]) {
		int collision = qtrue;
	}
*/
	key->next = table[hash];
	table[hash] = key;
}

keywordHash_t *KeywordHash_Find(keywordHash_t *table[], char *keyword)
{
	keywordHash_t *key;
	int hash;

	hash = KeywordHash_Key(keyword);
	for (key = table[hash]; key; key = key->next) {
		if (!Q_stricmp(key->keyword, keyword))
			return key;
	}
	return NULL;
}

/*
===============
Item Keyword Parse functions
===============
*/

// name <string>
qboolean ItemParse_name( itemDef_t *item, int handle ) {
	if (!PC_String_Parse(handle, &item->window.name)) {
		return qfalse;
	}
	return qtrue;
}

// name <string>
/*
qboolean ItemParse_focusSound( itemDef_t *item, int handle ) {
	pc_token_t token;
	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}
	item->focusSound = DC->registerSound(token.string);
	return qtrue;
}
*/


// text <string>
qboolean ItemParse_text( itemDef_t *item, int handle ) {
	if (!PC_String_Parse(handle, &item->text)) {
		return qfalse;
	}
	return qtrue;
}

/*
===============
ItemParse_descText 
	text <string>
===============
*/
qboolean ItemParse_descText( itemDef_t *item, int handle) 
{

	if (!PC_String_Parse(handle, &item->descText))
	{
		return qfalse;
	}

	return qtrue;

}


/*
===============
ItemParse_text 
	text <string>
===============
*/
/*
qboolean ItemParse_text2( itemDef_t *item, int handle) 
{

	if (!PC_String_Parse(handle, &item->text2))
	{
		return qfalse;
	}

	return qtrue;

}
*/

/*
===============
ItemParse_text2alignx 
===============
*/
/*
qboolean ItemParse_text2alignx( itemDef_t *item, int handle) 
{
	if (!PC_Float_Parse(handle, &item->text2alignx)) 
	{
		return qfalse;
	}
	return qtrue;
}
*/

/*
===============
ItemParse_text2aligny 
===============
*/
/*
qboolean ItemParse_text2aligny( itemDef_t *item, int handle) 
{
	if (!PC_Float_Parse(handle, &item->text2aligny)) 
	{
		return qfalse;
	}
	return qtrue;
}
*/

// group <string>
qboolean ItemParse_group( itemDef_t *item, int handle ) {
	if (!PC_String_Parse(handle, &item->window.group)) {
		return qfalse;
	}
	return qtrue;
}

typedef struct uiG2PtrTracker_s uiG2PtrTracker_t;

struct uiG2PtrTracker_s
{
	void *ghoul2;
	uiG2PtrTracker_t *next;
};

uiG2PtrTracker_t *ui_G2PtrTracker = NULL;

//rww - UI G2 shared management functions.

//Insert the pointer into our chain so we can keep track of it for freeing.
void UI_InsertG2Pointer(void *ghoul2)
{
	uiG2PtrTracker_t **nextFree = &ui_G2PtrTracker;

	while ((*nextFree) && (*nextFree)->ghoul2)
	{ //check if it has a ghoul2, if not we can reuse it.
		nextFree = &((*nextFree)->next);
	}

	if (!nextFree)
	{ //shouldn't happen
		assert(0);
		return;
	}

	if (!(*nextFree))
	{ //if we aren't reusing a chain then allocate space for it.
		(*nextFree) = (uiG2PtrTracker_t *)BG_Alloc(sizeof(uiG2PtrTracker_t));
		(*nextFree)->next = NULL;
	}

	(*nextFree)->ghoul2 = ghoul2;
}

//Remove a ghoul2 pointer from the chain if it's there.
void UI_ClearG2Pointer(void *ghoul2)
{
	uiG2PtrTracker_t *next = ui_G2PtrTracker;

	if (!ghoul2)
	{
		return;
	}

	while (next)
	{
		if (next->ghoul2 == ghoul2)
		{ //found it, set it to null so we can reuse this link.
			next->ghoul2 = NULL;
			break;
		}

		next = next->next;
	}
}

//Called on shutdown, cleans up all the ghoul2 instances laying around.
void UI_CleanupGhoul2(void)
{
	uiG2PtrTracker_t *next = ui_G2PtrTracker;

	while (next)
	{
		if (next->ghoul2 && trap_G2_HaveWeGhoul2Models(next->ghoul2))
		{ //found a g2 instance, clean it.
			trap_G2API_CleanGhoul2Models(&next->ghoul2);
		}

		next = next->next;
	}

#ifdef _XBOX
	ui_G2PtrTracker = NULL;
#endif
}

// asset_model <string>
int UI_ParseAnimationFile(const char *filename, animation_t *animset, qboolean isHumanoid);

/*
===============
ItemParse_asset_model 
	asset_model <string>
===============
*/
qboolean ItemParse_asset_model_go( itemDef_t *item, const char *name,int *runTimeLength ) 
{
#ifndef CGAME
	int g2Model;
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;
	*runTimeLength =0.0f;

	if (!Q_stricmp(&name[strlen(name) - 4], ".glm"))
	{ //it's a ghoul2 model then
		if ( item->ghoul2 )
		{
#ifdef _XBOX
			// Free up this model's memory slot, as the UI only has 1
			CGhoul2Info_v &ghoul2 = *(CGhoul2Info_v*)item->ghoul2;
//			ModelMem.FreeModelMemory(ghoul2[0].mFileName);
#endif
//#ifndef _XBOX
			UI_ClearG2Pointer(item->ghoul2);	//remove from tracking list
//#endif
			trap_G2API_CleanGhoul2Models(&item->ghoul2);	//remove ghoul info
			item->flags &= ~ITF_G2VALID;
		}

		g2Model = trap_G2API_InitGhoul2Model(&item->ghoul2, name, 0, modelPtr->g2skin, 0, 0, 0);
		if (g2Model >= 0)
		{
//#ifndef _XBOX
			UI_InsertG2Pointer(item->ghoul2); //remember it so we can free it when the ui shuts down.
//#endif
			item->flags |= ITF_G2VALID;

			if (modelPtr->g2anim)
			{ //does the menu request this model be playing an animation?
//					DC->g2hilev_SetAnim(&item->ghoul2[0], "model_root", modelPtr->g2anim);

				char GLAName[MAX_QPATH];	

				GLAName[0] = 0;
				trap_G2API_GetGLAName(item->ghoul2, 0, GLAName);

				if (GLAName[0])
				{
					int animIndex;
					char *slash;

					slash = Q_strrchr( GLAName, '/' );

					if ( slash )
					{ //If this isn't true the gla path must be messed up somehow.
						strcpy(slash, "/animation.cfg");
					
						animIndex = UI_ParseAnimationFile(GLAName, NULL, qfalse);
						if (animIndex != -1)
						{ //We parsed out the animation info for whatever model this is
							animation_t *anim = &bgAllAnims[animIndex].anims[modelPtr->g2anim];

							int sFrame = anim->firstFrame;
							int eFrame = anim->firstFrame + anim->numFrames;
							int flags = BONE_ANIM_OVERRIDE_FREEZE;
							int time = DC->realTime;
							float animSpeed = 50.0f / anim->frameLerp;
							int blendTime = 150;

							if (anim->loopFrames != -1)
							{
								flags |= BONE_ANIM_OVERRIDE_LOOP;
							}

							trap_G2API_SetBoneAnim(item->ghoul2, 0, "model_root", sFrame, eFrame, flags, animSpeed, time, -1, blendTime);
							*runTimeLength =((anim->frameLerp * (anim->numFrames-2)));					
						}
					}
				} 
			}

			if ( modelPtr->g2skin )
			{
//					DC->g2_SetSkin( &item->ghoul2[0], 0, modelPtr->g2skin );//this is going to set the surfs on/off matching the skin file
				//trap_G2API_InitGhoul2Model(&item->ghoul2, name, 0, modelPtr->g2skin, 0, 0, 0);
				//ahh, what are you doing?!
				trap_G2API_SetSkin(item->ghoul2, 0, modelPtr->g2skin, modelPtr->g2skin);
			}
		}
		/*
		else
		{
			Com_Error(ERR_FATAL, "%s does not exist.", name);
		}
		*/
	}
	else if(!(item->asset)) 
	{ //guess it's just an md3
		item->asset = DC->registerModel(name);
		item->flags &= ~ITF_G2VALID;
	}
#endif
	return qtrue;
}

qboolean ItemParse_asset_model( itemDef_t *item, int handle ) {
	const char *temp;
	modelDef_t *modelPtr;
	int animRunLength;
	pc_token_t token;

	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}
	temp = token.string;

#ifndef CGAME
	if (!stricmp(token.string,"ui_char_model") )
	{
		char modelPath[MAX_QPATH];
		char ui_char_model[MAX_QPATH];
		trap_Cvar_VariableStringBuffer("ui_char_model", ui_char_model, sizeof(ui_char_model) );
		Com_sprintf( modelPath, sizeof( modelPath ), "models/players/%s/model.glm", ui_char_model );
		temp = modelPath;

		// Remember that we did this substitution, so the UIC stuff can save it right
		modelPtr->g2IsPlayer = true;
	}
#endif
	return (ItemParse_asset_model_go( item, temp, &animRunLength ));
}

// asset_shader <string>
/*
qboolean ItemParse_asset_shader( itemDef_t *item, int handle ) {
	pc_token_t token;
	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}
	item->asset = DC->registerShaderNoMip(token.string);
	return qtrue;
}
*/

// model_origin <number> <number> <number>
qboolean ItemParse_model_origin( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (PC_Float_Parse(handle, &modelPtr->origin[0])) {
		if (PC_Float_Parse(handle, &modelPtr->origin[1])) {
			if (PC_Float_Parse(handle, &modelPtr->origin[2])) {
				return qtrue;
			}
		}
	}
	return qfalse;
}

// model_fovx <number>
qboolean ItemParse_model_fovx( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!PC_Float_Parse(handle, &modelPtr->fov_x)) {
		return qfalse;
	}
	return qtrue;
}

// model_fovy <number>
qboolean ItemParse_model_fovy( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!PC_Float_Parse(handle, &modelPtr->fov_y)) {
		return qfalse;
	}
	return qtrue;
}

// model_rotation <integer>
qboolean ItemParse_model_rotation( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!PC_Int_Parse(handle, &modelPtr->rotationSpeed)) {
		return qfalse;
	}
	return qtrue;
}

// model_angle <integer>
qboolean ItemParse_model_angle( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!PC_Int_Parse(handle, &modelPtr->angle)) {
		return qfalse;
	}
	return qtrue;
}

// model_g2mins <number> <number> <number>
qboolean ItemParse_model_g2mins( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (PC_Float_Parse(handle, &modelPtr->g2mins[0])) {
		if (PC_Float_Parse(handle, &modelPtr->g2mins[1])) {
			if (PC_Float_Parse(handle, &modelPtr->g2mins[2])) {
				return qtrue;
			}
		}
	}
	return qfalse;
}

// model_g2maxs <number> <number> <number>
qboolean ItemParse_model_g2maxs( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (PC_Float_Parse(handle, &modelPtr->g2maxs[0])) {
		if (PC_Float_Parse(handle, &modelPtr->g2maxs[1])) {
			if (PC_Float_Parse(handle, &modelPtr->g2maxs[2])) {
				return qtrue;
			}
		}
	}
	return qfalse;
}

// model_g2scale <number> <number> <number>
qboolean ItemParse_model_g2scale( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (PC_Float_Parse(handle, &modelPtr->g2scale[0])) {
		if (PC_Float_Parse(handle, &modelPtr->g2scale[1])) {
			if (PC_Float_Parse(handle, &modelPtr->g2scale[2])) {
				return qtrue;
			}
		}
	}
	return qfalse;
}

// model_g2skin <string>
qhandle_t trap_R_RegisterSkin( const char *name );

qboolean ItemParse_model_g2skin( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	pc_token_t token;

	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}

	if (!token.string[0])
	{ //it was parsed correctly so still return true.
		return qtrue;
	}

	modelPtr->g2skin = trap_R_RegisterSkin(token.string);

	return qtrue;
}

// model_g2anim <number>
qboolean ItemParse_model_g2anim( itemDef_t *item, int handle ) {
	modelDef_t *modelPtr;
	pc_token_t token;
	int i = 0;

	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}

	if ( !token.string[0])
	{ //it was parsed correctly so still return true.
		return qtrue;
	}

	while (i < MAX_ANIMATIONS)
	{
		if (!Q_stricmp(token.string, animTable[i].name))
		{ //found it
			modelPtr->g2anim = i;
			return qtrue;
		}
		i++;
	}

	Com_Printf("Could not find '%s' in the anim table\n", token.string);
	return qtrue;
}

// model_g2skin <string>
qboolean ItemParse_model_g2skin_go( itemDef_t *item, const char *skinName ) 
{

	modelDef_t *modelPtr;
	int defSkin;


	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!skinName || !skinName[0])
	{ //it was parsed correctly so still return true.
		modelPtr->g2skin = 0;
		trap_G2API_SetSkin(item->ghoul2, 0, 0, 0);

		return qtrue;
	}

	// set skin
	if ( item->ghoul2 )
	{
		defSkin = trap_R_RegisterSkin(skinName);
		trap_G2API_SetSkin(item->ghoul2, 0, defSkin, defSkin);
	}

	return qtrue;
}

// model_g2anim <number>
qboolean ItemParse_model_g2anim_go( itemDef_t *item, const char *animName ) 
{
	modelDef_t *modelPtr;
	int i = 0;

	Item_ValidateTypeData(item);
	modelPtr = (modelDef_t*)item->typeData;

	if (!animName || !animName[0])
	{ //it was parsed correctly so still return true.
		return qtrue;
	}

	while (i < MAX_ANIMATIONS)
	{
		if (!Q_stricmp(animName, animTable[i].name))
		{ //found it
			modelPtr->g2anim = animTable[i].id;
			return qtrue;
		}
		i++;
	}

	Com_Printf("Could not find '%s' in the anim table\n", animName);
	return qtrue;
}

// Get the cvar, get the values and stuff them in the rect structure.
qboolean ItemParse_rectcvar( itemDef_t *item, int handle ) 
{
	char	cvarBuf[1024];
	const char	*holdVal;
	char	*holdBuf;

	// get Cvar name
	pc_token_t token;
	if (!trap_PC_ReadToken(handle, &token))
	{
		return qfalse;
	}

	// get cvar data
	DC->getCVarString(token.string, cvarBuf, sizeof(cvarBuf));
	
	holdBuf = cvarBuf;
	if (String_Parse(&holdBuf,&holdVal))
	{
		item->window.rectClient.x = atof(holdVal);
		if (String_Parse(&holdBuf,&holdVal))
		{
			item->window.rectClient.y = atof(holdVal);
			if (String_Parse(&holdBuf,&holdVal))
			{
				item->window.rectClient.w = atof(holdVal);
				if (String_Parse(&holdBuf,&holdVal))
				{
					item->window.rectClient.h = atof(holdVal);
					return qtrue;
				}
			}
		}
	}

	// There may be no cvar built for this, and that's okay. . . I guess.
	return qtrue;
}

// rect <rectangle>
qboolean ItemParse_rect( itemDef_t *item, int handle ) {
	if (!PC_Rect_Parse(handle, &item->window.rectClient)) {
		return qfalse;
	}
	return qtrue;
}

/*
===============
ItemParse_flag
	style <integer>
===============
*/
qboolean ItemParse_flag( itemDef_t *item, int handle) 
{
	int		i;
	pc_token_t token;

	if (!trap_PC_ReadToken(handle, &token))
	{
		return qfalse;
	}

	i=0;
	while (styles[i])
	{
		if (Q_stricmp(token.string,itemFlags[i].string)==0)
		{
			item->window.flags |= itemFlags[i].value;
			break;
		}
		i++;
	}

	if (itemFlags[i].string == NULL)
	{
		Com_Printf(va( S_COLOR_YELLOW "Unknown item style value '%s'",token.string));
	}

	return qtrue;
}

/*
===============
ItemParse_style 
	style <integer>
===============
*/
qboolean ItemParse_style( itemDef_t *item, int handle) 
{
	if (!PC_Int_Parse(handle, &item->window.style))
	{
		Com_Printf(S_COLOR_YELLOW "Unknown item style value");
		return qfalse;
	}

	return qtrue;
}


// decoration
qboolean ItemParse_decoration( itemDef_t *item, int handle ) {
	item->window.flags |= WINDOW_DECORATION;
	return qtrue;
}

// notselectable
qboolean ItemParse_notselectable( itemDef_t *item, int handle ) {
	listBoxDef_t *listPtr;
	Item_ValidateTypeData(item);
	listPtr = (listBoxDef_t*)item->typeData;
	if (item->type == ITEM_TYPE_LISTBOX && listPtr) {
		listPtr->notselectable = qtrue;
	}
	return qtrue;
}

/*
===============
ItemParse_scrollhidden
	scrollhidden
===============
*/
qboolean ItemParse_scrollhidden( itemDef_t *item , int handle) 
{
	listBoxDef_t *listPtr;
	Item_ValidateTypeData(item);
	listPtr = (listBoxDef_t*)item->typeData;

	if (item->type == ITEM_TYPE_LISTBOX && listPtr) 
	{
		listPtr->scrollhidden = qtrue;
	}
	return qtrue;
}

/*
===============
ItemParse_selectionShader
	selectionShader
===============
*/
qboolean ItemParse_selectionShader( itemDef_t *item, int handle ) 
{
	pc_token_t token;

	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}

	listBoxDef_t *listPtr;
	Item_ValidateTypeData(item);
	listPtr = (listBoxDef_t*)item->typeData;

	if (item->type == ITEM_TYPE_LISTBOX && listPtr) 
	{
		listPtr->selectionShader = DC->registerShaderNoMip(token.string);
	}

	return qtrue;
}




// manually wrapped
qboolean ItemParse_wrapped( itemDef_t *item, int handle ) {
	item->window.flags |= WINDOW_WRAPPED;
	return qtrue;
}

// auto wrapped
qboolean ItemParse_autowrapped( itemDef_t *item, int handle ) {
	item->window.flags |= WINDOW_AUTOWRAPPED;
	return qtrue;
}


// horizontalscroll
qboolean ItemParse_horizontalscroll( itemDef_t *item, int handle ) {
	item->window.flags |= WINDOW_HORIZONTAL;
	return qtrue;
}

/*
===============
ItemParse_type 
	type <integer>
===============
*/
qboolean ItemParse_type( itemDef_t *item, int handle  ) 
{
//	int		i,holdInt;

	if (!PC_Int_Parse(handle, &item->type)) 
	{
		return qfalse;
	}
	Item_ValidateTypeData(item);
	return qtrue;
}

// elementwidth, used for listbox image elements
// uses textalignx for storage
qboolean ItemParse_elementwidth( itemDef_t *item, int handle ) {
	listBoxDef_t *listPtr;

	Item_ValidateTypeData(item);
	listPtr = (listBoxDef_t*)item->typeData;
	if (!PC_Float_Parse(handle, &listPtr->elementWidth)) {
		return qfalse;
	}
	return qtrue;
}

// elementheight, used for listbox image elements
// uses textaligny for storage
qboolean ItemParse_elementheight( itemDef_t *item, int handle ) {
	listBoxDef_t *listPtr;

	Item_ValidateTypeData(item);
	listPtr = (listBoxDef_t*)item->typeData;
	if (!PC_Float_Parse(handle, &listPtr->elementHeight)) {
		return qfalse;
	}
	return qtrue;
}

// feeder <float>
qboolean ItemParse_feeder( itemDef_t *item, int handle ) {
	if (!PC_Float_Parse(handle, &item->special)) {
		return qfalse;
	}
	return qtrue;
}

// elementtype, used to specify what type of elements a listbox contains
// uses textstyle for storage
qboolean ItemParse_elementtype( itemDef_t *item, int handle ) {
	listBoxDef_t *listPtr;

	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;
	listPtr = (listBoxDef_t*)item->typeData;
	if (!PC_Int_Parse(handle, &listPtr->elementStyle)) {
		return qfalse;
	}
	return qtrue;
}

// columns sets a number of columns and an x pos and width per.. 
qboolean ItemParse_columns( itemDef_t *item, int handle ) {
	int num, i;
	listBoxDef_t *listPtr;

	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;
	listPtr = (listBoxDef_t*)item->typeData;
	if (PC_Int_Parse(handle, &num)) {
		if (num > MAX_LB_COLUMNS) {
			num = MAX_LB_COLUMNS;
		}
		listPtr->numColumns = num;
		for (i = 0; i < num; i++) {
			int pos, width, maxChars;

			if (PC_Int_Parse(handle, &pos) && PC_Int_Parse(handle, &width) && PC_Int_Parse(handle, &maxChars)) {
				listPtr->columnInfo[i].pos = pos;
				listPtr->columnInfo[i].width = width;
				listPtr->columnInfo[i].maxChars = maxChars;
			} else {
				return qfalse;
			}
		}
	} else {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_border( itemDef_t *item, int handle ) {
	if (!PC_Int_Parse(handle, &item->window.border)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_bordersize( itemDef_t *item, int handle ) {
	if (!PC_Float_Parse(handle, &item->window.borderSize)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_visible( itemDef_t *item, int handle ) {
	int i;

	if (!PC_Int_Parse(handle, &i)) {
		return qfalse;
	}
	if (i) {
		item->window.flags |= WINDOW_VISIBLE;
	}
	return qtrue;
}

qboolean ItemParse_ownerdraw( itemDef_t *item, int handle ) {
	if (!PC_Int_Parse(handle, &item->window.ownerDraw)) {
		return qfalse;
	}
	item->type = ITEM_TYPE_OWNERDRAW;
	return qtrue;
}

qboolean ItemParse_align( itemDef_t *item, int handle ) {
	if (!PC_Int_Parse(handle, &item->alignment)) {
		return qfalse;
	}
	return qtrue;
}

/*
===============
ItemParse_isCharacter 

Sets item flag showing this is a character
===============
*/
qboolean ItemParse_isCharacter( itemDef_t *item, int handle  )
{
	int flag;

	if (PC_Int_Parse(handle, &flag)) 
	{
		if ( flag )
		{
			item->flags |= ITF_ISCHARACTER;
		}
		else
		{
			item->flags &= ~ITF_ISCHARACTER;
		}
		return qtrue;
	}
	return qfalse;
}


/*
===============
ItemParse_textalign 
===============
*/
qboolean ItemParse_textalign( itemDef_t *item, int handle ) 
{
	if (!PC_Int_Parse(handle, &item->textalignment)) 
	{
		Com_Printf(S_COLOR_YELLOW "Unknown text alignment value");
	
		return qfalse;
	}

	return qtrue;
}

qboolean ItemParse_textalignx( itemDef_t *item, int handle ) {
	if (!PC_Float_Parse(handle, &item->textalignx)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_textaligny( itemDef_t *item, int handle ) {
	if (!PC_Float_Parse(handle, &item->textaligny)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_textscale( itemDef_t *item, int handle ) {
	if (!PC_Float_Parse(handle, &item->textscale)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_textstyle( itemDef_t *item, int handle ) {
	if (!PC_Int_Parse(handle, &item->textStyle)) {
		return qfalse;
	}
	return qtrue;
}

/*
===============
ItemParse_invertyesno
===============
*/
//JLFYESNO MPMOVED
qboolean ItemParse_invertyesno( itemDef_t *item, int handle) 
{
	if (!PC_Int_Parse(handle, &item->invertYesNo)) 
	{
		return qfalse;
	}
	return qtrue;
}

/*
===============
ItemParse_xoffset (used for yes/no and multi)
===============
*/
qboolean ItemParse_xoffset( itemDef_t *item, int handle) 
{
	if (!PC_Int_Parse(handle, &item->xoffset)) 
	{
		return qfalse;
	}
	return qtrue;
}


qboolean ItemParse_backcolor( itemDef_t *item, int handle ) {
	int i;
	float f;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		item->window.backColor[i]  = f;
	}
	return qtrue;
}

qboolean ItemParse_forecolor( itemDef_t *item, int handle ) {
	int i;
	float f;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}

		if (f < 0)
		{	//special case for player color
			item->window.flags |= WINDOW_PLAYERCOLOR;
			return qtrue;
		}

		item->window.foreColor[i]  = f;
		item->window.flags |= WINDOW_FORECOLORSET;
	}
	return qtrue;
}

qboolean ItemParse_bordercolor( itemDef_t *item, int handle ) {
	int i;
	float f;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		item->window.borderColor[i]  = f;
	}
	return qtrue;
}

qboolean ItemParse_outlinecolor( itemDef_t *item, int handle ) {
	if (!PC_Color_Parse(handle, &item->window.outlineColor)){
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_background( itemDef_t *item, int handle ) {
	pc_token_t token;

	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}
	item->window.background = DC->registerShaderNoMip(token.string);
	return qtrue;
}

/*
qboolean ItemParse_cinematic( itemDef_t *item, int handle ) {
	if (!PC_String_Parse(handle, &item->window.cinematicName)) {
		return qfalse;
	}
	return qtrue;
}
*/

qboolean ItemParse_doubleClick( itemDef_t *item, int handle ) {
	listBoxDef_t *listPtr;

	Item_ValidateTypeData(item);
	if (!item->typeData) {
		return qfalse;
	}

	listPtr = (listBoxDef_t*)item->typeData;

	if (!PC_Script_Parse(handle, &listPtr->doubleClick)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_onFocus( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->onFocus)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_leaveFocus( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->leaveFocus)) {
		return qfalse;
	}
	return qtrue;
}

/*
qboolean ItemParse_mouseEnter( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->mouseEnter)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_mouseExit( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->mouseExit)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_mouseEnterText( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->mouseEnterText)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_mouseExitText( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->mouseExitText)) {
		return qfalse;
	}
	return qtrue;
}
*/

qboolean ItemParse_action( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->action)) {
		return qfalse;
	}
	return qtrue;
}

//JLF
qboolean ItemParse_selectionNext( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->selectionNext)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_selectionPrev( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->selectionPrev)) {
		return qfalse;
	}
	return qtrue;
}
/*
qboolean ItemParse_accept( itemDef_t *item, int handle ) {
	if (!PC_Script_Parse(handle, &item->accept)) {
		return qfalse;
	}
	return qtrue;
}
*/


qboolean ItemParse_special( itemDef_t *item, int handle ) {
	if (!PC_Float_Parse(handle, &item->special)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_cvarTest( itemDef_t *item, int handle ) {
	if (!PC_String_Parse(handle, &item->cvarTest)) {
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_cvar( itemDef_t *item, int handle ) 
{
	Item_ValidateTypeData(item);
	if (!PC_String_Parse(handle, &item->cvar)) 
	{
		return qfalse;
	}

	if ( item->typeData)
	{
		editFieldDef_t *editPtr;

		switch ( item->type )
		{
			case ITEM_TYPE_EDITFIELD:
			case ITEM_TYPE_NUMERICFIELD:
			case ITEM_TYPE_YESNO:
			case ITEM_TYPE_BIND:
			case ITEM_TYPE_SLIDER:
			case ITEM_TYPE_TEXT:
				editPtr = (editFieldDef_t*)item->typeData;
				editPtr->minVal = -1;
				editPtr->maxVal = -1;
				editPtr->defVal = -1;
				break;
		}
	}
	return qtrue;
}

qboolean ItemParse_font( itemDef_t *item, int handle ) 
{
	Item_ValidateTypeData(item);
	if (!PC_Int_Parse(handle, &item->iMenuFont)) 
	{
		return qfalse;
	}
	return qtrue;
}


qboolean ItemParse_maxChars( itemDef_t *item, int handle ) {
	editFieldDef_t *editPtr;
	int maxChars;

	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;

	if (!PC_Int_Parse(handle, &maxChars)) {
		return qfalse;
	}
	editPtr = (editFieldDef_t*)item->typeData;
	editPtr->maxChars = maxChars;
	return qtrue;
}

qboolean ItemParse_maxPaintChars( itemDef_t *item, int handle ) {
	editFieldDef_t *editPtr;
	int maxChars;

	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;

	if (!PC_Int_Parse(handle, &maxChars)) {
		return qfalse;
	}
	editPtr = (editFieldDef_t*)item->typeData;
	editPtr->maxPaintChars = maxChars;
	return qtrue;
}

qboolean ItemParse_maxLineChars( itemDef_t *item, int handle ) 
{
	textScrollDef_t *scrollPtr;
	int				maxChars;

	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;

	if (!PC_Int_Parse(handle, &maxChars)) 
	{
		return qfalse;
	}

	scrollPtr = (textScrollDef_t*)item->typeData;
	scrollPtr->maxLineChars = maxChars;

	return qtrue;
}

qboolean ItemParse_lineHeight( itemDef_t *item, int handle ) 
{
	textScrollDef_t *scrollPtr;
	int				height;

	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;

	if (!PC_Int_Parse(handle, &height)) 
	{
		return qfalse;
	}

	scrollPtr = (textScrollDef_t*)item->typeData;
	scrollPtr->lineHeight = height;

	return qtrue;
}

qboolean ItemParse_cvarFloat( itemDef_t *item, int handle ) {
	editFieldDef_t *editPtr;

	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;
	editPtr = (editFieldDef_t*)item->typeData;
	if (PC_String_Parse(handle, &item->cvar) &&
		PC_Float_Parse(handle, &editPtr->defVal) &&
		PC_Float_Parse(handle, &editPtr->minVal) &&
		PC_Float_Parse(handle, &editPtr->maxVal)) {
		return qtrue;
	}
	return qfalse;
}

char currLanguage[32][128];
static const char languageString[32] = "@MENUS_MYLANGUAGE";

qboolean ItemParse_cvarStrList( itemDef_t *item, int handle ) {
	pc_token_t token;
	multiDef_t *multiPtr;
	int pass;
	
	Item_ValidateTypeData(item);
	if (!item->typeData)
		return qfalse;
	multiPtr = (multiDef_t*)item->typeData;
	multiPtr->count = 0;
	multiPtr->strDef = qtrue;

	if (!trap_PC_ReadToken(handle, &token))
	{
		return qfalse;
	}

	if (!Q_stricmp(token.string,"feeder") && item->special == FEEDER_PLAYER_SPECIES) 
	{
#ifndef CGAME
		for (; multiPtr->count < uiInfo.playerSpeciesCount; multiPtr->count++)
		{
			multiPtr->cvarList[multiPtr->count] = String_Alloc(strupr(va("@MENUS_%s",uiInfo.playerSpecies[multiPtr->count].Name )));	//look up translation
			multiPtr->cvarStr[multiPtr->count] = uiInfo.playerSpecies[multiPtr->count].Name;	//value
		}
#endif
		return qtrue;
	}
	// languages
	if (!Q_stricmp(token.string,"feeder") && item->special == FEEDER_LANGUAGES) 
	{
#ifndef CGAME
		assert( 0 );
/*
		for (; multiPtr->count < uiInfo.languageCount; multiPtr->count++)
		{
			// The displayed text
			trap_GetLanguageName( (const int) multiPtr->count,(char *) currLanguage[multiPtr->count]  );	// eg "English"
			multiPtr->cvarList[multiPtr->count] = languageString;
			// The cvar value that goes into se_language
			trap_GetLanguageName( (const int) multiPtr->count,(char *) currLanguage[multiPtr->count] );
			multiPtr->cvarStr[multiPtr->count] = currLanguage[multiPtr->count];
		}
*/
#endif
		return qtrue;
	}

	if (*token.string != '{') {
		return qfalse;
	}

	pass = 0;
	while ( 1 ) {
		char* psString;

//		if (!trap_PC_ReadToken(handle, &token)) {
//			PC_SourceError(handle, "end of file inside menu item\n");
//			return qfalse;
//		}		   

		if (!PC_String_Parse(handle, (const char **)&psString)) 
		{
			PC_SourceError(handle, "end of file inside menu item\n");
			return qfalse;
		}

		//a normal StringAlloc ptr
		if ((int)psString > 0)	
		{
			if (*psString == '}') {
				return qtrue;
			}

			if (*psString == ',' || *psString == ';') {
				continue;
			}
		}

		if (pass == 0) {
			multiPtr->cvarList[multiPtr->count] = psString;
			pass = 1;
		} else {
			multiPtr->cvarStr[multiPtr->count] = psString;
			pass = 0;
			multiPtr->count++;
			if (multiPtr->count >= MAX_MULTI_CVARS) {
				return qfalse;
			}
		}

	}
	return qfalse; 	// bk001205 - LCC missing return value
}

qboolean ItemParse_cvarFloatList( itemDef_t *item, int handle ) 
{
	pc_token_t token;
	multiDef_t *multiPtr;
	
	Item_ValidateTypeData(item);
	if (!item->typeData)
	{
		return qfalse;
	}
	
	multiPtr = (multiDef_t*)item->typeData;
	multiPtr->count = 0;
	multiPtr->strDef = qfalse;

	if (!trap_PC_ReadToken(handle, &token))
	{
		return qfalse;
	}
	
	if (*token.string != '{') 
	{
		return qfalse;
	}

	while ( 1 ) 
	{
		char* string;

		if ( !PC_String_Parse ( handle, (const char **)&string ) )
		{
			PC_SourceError(handle, "end of file inside menu item\n");
			return qfalse;
		}
			
		//a normal StringAlloc ptr
		if ((int)string > 0)	
		{
			if (*string == '}') 
			{
				return qtrue;
			}

			if (*string == ',' || *string == ';') 
			{
				continue;
			}
		}

		multiPtr->cvarList[multiPtr->count] = string;
		if (!PC_Float_Parse(handle, &multiPtr->cvarValue[multiPtr->count])) 
		{
			return qfalse;
		}

		multiPtr->count++;
		if (multiPtr->count >= MAX_MULTI_CVARS) 
		{
			return qfalse;
		}

	}
	return qfalse; 	// bk001205 - LCC missing return value
}


/*
qboolean ItemParse_addColorRange( itemDef_t *item, int handle ) {
	colorRangeDef_t color;

	if (PC_Float_Parse(handle, &color.low) &&
		PC_Float_Parse(handle, &color.high) &&
		PC_Color_Parse(handle, &color.color) ) {
		if (item->numColors < MAX_COLOR_RANGES) {
			memcpy(&item->colorRanges[item->numColors], &color, sizeof(color));
			item->numColors++;
		}
		return qtrue;
	}
	return qfalse;
}
*/

qboolean ItemParse_ownerdrawFlag( itemDef_t *item, int handle ) {
	int i;
	if (!PC_Int_Parse(handle, &i)) {
		return qfalse;
	}
	item->window.ownerDrawFlags |= i;
	return qtrue;
}

qboolean ItemParse_enableCvar( itemDef_t *item, int handle ) {
	if (PC_Script_Parse(handle, &item->enableCvar)) {
		item->cvarFlags = CVAR_ENABLE;
		return qtrue;
	}
	return qfalse;
}

qboolean ItemParse_disableCvar( itemDef_t *item, int handle ) {
	if (PC_Script_Parse(handle, &item->enableCvar)) {
		item->cvarFlags = CVAR_DISABLE;
		return qtrue;
	}
	return qfalse;
}

qboolean ItemParse_showCvar( itemDef_t *item, int handle ) {
	if (PC_Script_Parse(handle, &item->enableCvar)) {
		item->cvarFlags = CVAR_SHOW;
		return qtrue;
	}
	return qfalse;
}

qboolean ItemParse_hideCvar( itemDef_t *item, int handle ) {
	if (PC_Script_Parse(handle, &item->enableCvar)) {
		item->cvarFlags = CVAR_HIDE;
		return qtrue;
	}
	return qfalse;
}

/*
===============
ItemParse_align 
===============
*/
qboolean ItemParse_Appearance_slot( itemDef_t *item, int handle ) 
{
	if (!PC_Int_Parse(handle, &item->appearanceSlot))
	{
		return qfalse;
	}
	return qtrue;
}

qboolean ItemParse_isSaber( itemDef_t *item, int handle  )
{
#ifndef CGAME

	int	i;

	if (PC_Int_Parse(handle, &i)) 
	{
		if ( i )
		{
			item->flags |= ITF_ISSABER;
			UI_CacheSaberGlowGraphics();
			if ( !ui_saber_parms_parsed )
			{
				UI_SaberLoadParms();
			}
			gDoNotMakeUic = true;
		}
		else
		{
			item->flags &= ~ITF_ISSABER;
		}
	
		return qtrue;
	}
#endif
	return qfalse;
}

//extern void UI_SaberLoadParms( void );
//extern qboolean ui_saber_parms_parsed;
//extern void UI_CacheSaberGlowGraphics( void );

qboolean ItemParse_isSaber2( itemDef_t *item, int handle  )
{
#ifndef CGAME
	int	i;
	if (PC_Int_Parse(handle, &i)) 
	{
		if ( i )
		{
			item->flags |= ITF_ISSABER2;
			UI_CacheSaberGlowGraphics();
			if ( !ui_saber_parms_parsed )
			{
				UI_SaberLoadParms();
			}
			gDoNotMakeUic = true;
		}
		else
		{
			item->flags &= ~ITF_ISSABER2;
		}

		return qtrue;
	}
#endif
	
	return qfalse;
}

keywordHash_t itemParseKeywords[] = {
	{"action",			ItemParse_action,			NULL	},
//	{"addColorRange",	ItemParse_addColorRange,	NULL	},
	{"align",			ItemParse_align,			NULL	},
	{"autowrapped",		ItemParse_autowrapped,		NULL	},
	{"appearance_slot",	ItemParse_Appearance_slot,	NULL	},
	{"asset_model",		ItemParse_asset_model,		NULL	},
//	{"asset_shader",	ItemParse_asset_shader,		NULL	},
	{"backcolor",		ItemParse_backcolor,		NULL	},
	{"background",		ItemParse_background,		NULL	},
	{"border",			ItemParse_border,			NULL	},
	{"bordercolor",		ItemParse_bordercolor,		NULL	},
	{"bordersize",		ItemParse_bordersize,		NULL	},
//	{"cinematic",		ItemParse_cinematic,		NULL	},
	{"columns",			ItemParse_columns,			NULL	},
	{"cvar",			ItemParse_cvar,				NULL	},
	{"cvarFloat",		ItemParse_cvarFloat,		NULL	},
	{"cvarFloatList",	ItemParse_cvarFloatList,	NULL	},
	{"cvarStrList",		ItemParse_cvarStrList,		NULL	},
	{"cvarTest",		ItemParse_cvarTest,			NULL	},
	{"desctext",		ItemParse_descText,			NULL	},
	{"decoration",		ItemParse_decoration,		NULL	},
	{"disableCvar",		ItemParse_disableCvar,		NULL	},
	{"doubleclick",		ItemParse_doubleClick,		NULL	},
	{"elementheight",	ItemParse_elementheight,	NULL	},
	{"elementtype",		ItemParse_elementtype,		NULL	},
	{"elementwidth",	ItemParse_elementwidth,		NULL	},
	{"enableCvar",		ItemParse_enableCvar,		NULL	},
	{"feeder",			ItemParse_feeder,			NULL	},
	{"flag",			ItemParse_flag,				NULL	},
//	{"focusSound",		ItemParse_focusSound,		NULL	},
	{"font",			ItemParse_font,				NULL	},
	{"forecolor",		ItemParse_forecolor,		NULL	},
	{"group",			ItemParse_group,			NULL	},
	{"hideCvar",		ItemParse_hideCvar,			NULL	},
	{"horizontalscroll", ItemParse_horizontalscroll, NULL	},
	{"isCharacter",		ItemParse_isCharacter,		NULL	},
	{"isSaber",			ItemParse_isSaber,			NULL	},
	{"isSaber2",		ItemParse_isSaber2,			NULL	},
	{"leaveFocus",		ItemParse_leaveFocus,		NULL	},
	{"maxChars",		ItemParse_maxChars,			NULL	},
	{"maxPaintChars",	ItemParse_maxPaintChars,	NULL	},
	{"model_angle",		ItemParse_model_angle,		NULL	},
	{"model_fovx",		ItemParse_model_fovx,		NULL	},
	{"model_fovy",		ItemParse_model_fovy,		NULL	},
	{"model_origin",	ItemParse_model_origin,		NULL	},
	{"model_rotation",	ItemParse_model_rotation,	NULL	},
	//rww - g2 begin
	{"model_g2mins",	ItemParse_model_g2mins,		NULL	},
	{"model_g2maxs",	ItemParse_model_g2maxs,		NULL	},
	{"model_g2scale",	ItemParse_model_g2scale,	NULL	},
	{"model_g2skin",	ItemParse_model_g2skin,		NULL	},
	{"model_g2anim",	ItemParse_model_g2anim,		NULL	},
	//rww - g2 end
//	{"mouseEnter",		ItemParse_mouseEnter,		NULL	},
//	{"mouseEnterText",	ItemParse_mouseEnterText,	NULL	},
//	{"mouseExit",		ItemParse_mouseExit,		NULL	},
//	{"mouseExitText",	ItemParse_mouseExitText,	NULL	},
	{"name",			ItemParse_name,				NULL	},
	{"notselectable",	ItemParse_notselectable,	NULL	},
	{"onFocus",			ItemParse_onFocus,			NULL	},
	{"outlinecolor",	ItemParse_outlinecolor,		NULL	},
	{"ownerdraw",		ItemParse_ownerdraw,		NULL	},
	{"ownerdrawFlag",	ItemParse_ownerdrawFlag,	NULL	},
	{"rect",			ItemParse_rect,				NULL	},
	{"rectcvar",		ItemParse_rectcvar,			NULL	},
	{"showCvar",		ItemParse_showCvar,			NULL	},
	{"special",			ItemParse_special,			NULL	},
	{"style",			ItemParse_style,			NULL	},
	{"text",			ItemParse_text,				NULL	},
	{"textalign",		ItemParse_textalign,		NULL	},
	{"textalignx",		ItemParse_textalignx,		NULL	},
	{"textaligny",		ItemParse_textaligny,		NULL	},
	{"textscale",		ItemParse_textscale,		NULL	},
	{"textstyle",		ItemParse_textstyle,		NULL	},
//	{"text2",			ItemParse_text2,			NULL	},
//	{"text2alignx",		ItemParse_text2alignx,		NULL	},
//	{"text2aligny",		ItemParse_text2aligny,		NULL	},
	{"type",			ItemParse_type,				NULL	},
	{"visible",			ItemParse_visible,			NULL	},
	{"wrapped",			ItemParse_wrapped,			NULL	},

	// Text scroll specific
	{"maxLineChars",	ItemParse_maxLineChars,		NULL	},
	{"lineHeight",		ItemParse_lineHeight,		NULL	},
	{"invertyesno",		ItemParse_invertyesno,		NULL	},
	//JLF 
	{"scrollhidden",	ItemParse_scrollhidden,		NULL	},
	{"selectionshader",	ItemParse_selectionShader,	NULL	},
	{"xoffset",			ItemParse_xoffset,			NULL	},
	{"selectNext",		ItemParse_selectionNext,	},
	{"selectPrev",		ItemParse_selectionPrev,	},
//	{"accept",			ItemParse_accept,			},

	//JLF end



	{0,					0,							0		}
};

keywordHash_t *itemParseKeywordHash[KEYWORDHASH_SIZE];

/*
===============
Item_SetupKeywordHash
===============
*/
void Item_SetupKeywordHash(void) {
	int i;

	memset(itemParseKeywordHash, 0, sizeof(itemParseKeywordHash));
	for (i = 0; itemParseKeywords[i].keyword; i++) {
		KeywordHash_Add(itemParseKeywordHash, &itemParseKeywords[i]);
	}
}

/*
===============
Item_Parse
===============
*/
qboolean Item_Parse(int handle, itemDef_t *item) {
	pc_token_t token;
	keywordHash_t *key;


	if (!trap_PC_ReadToken(handle, &token))
		return qfalse;
	if (*token.string != '{') {
		return qfalse;
	}
	while ( 1 ) {
		if (!trap_PC_ReadToken(handle, &token)) {
			PC_SourceError(handle, "end of file inside menu item\n");
			return qfalse;
		}

		if (*token.string == '}') {
			return qtrue;
		}

		key = KeywordHash_Find(itemParseKeywordHash, token.string);
		if (!key) {
			PC_SourceError(handle, "unknown menu item keyword %s", token.string);
			continue;
		}
		if ( !key->func(item, handle) ) {
			PC_SourceError(handle, "couldn't parse menu item keyword %s", token.string);
			return qfalse;
		}
	}
	return qfalse; 	// bk001205 - LCC missing return value
}

static void Item_TextScroll_BuildLines ( itemDef_t* item )
{
	char text[2048];

#if 1

	// new asian-aware line breaker...  (pasted from elsewhere late @ night, hence aliasing-vars ;-)
	//
	textScrollDef_t* scrollPtr = (textScrollDef_t*) item->typeData;
	const char *psText = item->text;	// for copy/paste ease
	int iBoxWidth = item->window.rect.w - SCROLLBAR_SIZE - 10;

	// this could probably be simplified now, but it was converted from something else I didn't originally write, 
	//	and it works anyway so wtf...
	//
	const char *psCurrentTextReadPos;
	const char *psReadPosAtLineStart;	
	const char *psBestLineBreakSrcPos;
	const char *psLastGood_s;	// needed if we get a full screen of chars with no punctuation or space (see usage notes)
	qboolean bIsTrailingPunctuation;
	unsigned int uiLetter;

	if (*psText == '@')	// string reference
	{
		trap_SP_GetStringTextString( &psText[1], text, sizeof(text));
		psText = text;
	}

	psCurrentTextReadPos = psText;
	psReadPosAtLineStart = psCurrentTextReadPos;
	psBestLineBreakSrcPos = psCurrentTextReadPos;

	scrollPtr->iLineCount = 0;
	memset((char*)scrollPtr->pLines,0,sizeof(scrollPtr->pLines));

	while (*psCurrentTextReadPos && (scrollPtr->iLineCount < MAX_TEXTSCROLL_LINES) )
	{
		char sLineForDisplay[2048];	// ott

		// construct a line...
		//
		psCurrentTextReadPos = psReadPosAtLineStart;
		sLineForDisplay[0] = '\0';
		while ( *psCurrentTextReadPos )
		{
			int iAdvanceCount;
			psLastGood_s = psCurrentTextReadPos;

			// read letter...
			//
			uiLetter = trap_AnyLanguage_ReadCharFromString(psCurrentTextReadPos, &iAdvanceCount, &bIsTrailingPunctuation);
			psCurrentTextReadPos += iAdvanceCount;

			// concat onto string so far...
			//
			if (uiLetter == 32 && sLineForDisplay[0] == '\0')
			{
				psReadPosAtLineStart++;
				continue;	// unless it's a space at the start of a line, in which case ignore it.
			}

			if (uiLetter > 255)
			{
				Q_strcat(sLineForDisplay, sizeof(sLineForDisplay),va("%c%c",uiLetter >> 8, uiLetter & 0xFF));
			}
			else
			{
				Q_strcat(sLineForDisplay, sizeof(sLineForDisplay),va("%c",uiLetter & 0xFF));
			}

			if (uiLetter == '\n')
			{
				// explicit new line...
				//
				sLineForDisplay[ strlen(sLineForDisplay)-1 ] = '\0';	// kill the CR
				psReadPosAtLineStart = psCurrentTextReadPos;
				psBestLineBreakSrcPos = psCurrentTextReadPos;

				// hack it to fit in with this code...
				//
				scrollPtr->pLines[ scrollPtr->iLineCount ] = String_Alloc ( sLineForDisplay );
				break;	// print this line
			}
			else 
			if ( DC->textWidth( sLineForDisplay, item->textscale, item->iMenuFont ) >= iBoxWidth )
			{					
				// reached screen edge, so cap off string at bytepos after last good position...
				//
				if (uiLetter > 255 && bIsTrailingPunctuation && !trap_Language_UsesSpaces())
				{
					// Special case, don't consider line breaking if you're on an asian punctuation char of
					//	a language that doesn't use spaces...
					//
					uiLetter = uiLetter;	// breakpoint line only
				}
				else
				{
					if (psBestLineBreakSrcPos == psReadPosAtLineStart)
					{
						//  aarrrggh!!!!!   we'll only get here is someone has fed in a (probably) garbage string,
						//		since it doesn't have a single space or punctuation mark right the way across one line
						//		of the screen.  So far, this has only happened in testing when I hardwired a taiwanese 
						//		string into this function while the game was running in english (which should NEVER happen 
						//		normally).  On the other hand I suppose it's entirely possible that some taiwanese string 
						//		might have no punctuation at all, so...
						//
						psBestLineBreakSrcPos = psLastGood_s;	// force a break after last good letter
					}

					sLineForDisplay[ psBestLineBreakSrcPos - psReadPosAtLineStart ] = '\0';
					psReadPosAtLineStart = psCurrentTextReadPos = psBestLineBreakSrcPos;

					// hack it to fit in with this code...
					//
					scrollPtr->pLines[ scrollPtr->iLineCount ] = String_Alloc( sLineForDisplay );
					break;	// print this line
				}
			}

			// record last-good linebreak pos...  (ie if we've just concat'd a punctuation point (western or asian) or space)
			//
			if (bIsTrailingPunctuation || uiLetter == ' ' || (uiLetter > 255 && !trap_Language_UsesSpaces()))
			{
				psBestLineBreakSrcPos = psCurrentTextReadPos;
			}
		}

		/// arrgghh, this is gettng horrible now...
		//
		if (scrollPtr->pLines[ scrollPtr->iLineCount ] == NULL && strlen(sLineForDisplay))
		{
			// then this is the last line and we've just run out of text, no CR, no overflow etc...
			//
			scrollPtr->pLines[ scrollPtr->iLineCount ] = String_Alloc( sLineForDisplay );
		}
			
		scrollPtr->iLineCount++;
	}
	
#else
	// old version...
	//
	int				 width;
	char*			 lineStart;
	char*			 lineEnd;
	float			 w;
	float			 cw;

	textScrollDef_t* scrollPtr = (textScrollDef_t*) item->typeData;

	scrollPtr->iLineCount = 0;
	width = scrollPtr->maxLineChars;
	lineStart = (char*)item->text;
	lineEnd   = lineStart;
	w		  = 0;

	// Keep going as long as there are more lines
	while ( scrollPtr->iLineCount < MAX_TEXTSCROLL_LINES )
	{
		// End of the road
		if ( *lineEnd == '\0')
		{
			if ( lineStart < lineEnd )
			{
				scrollPtr->pLines[ scrollPtr->iLineCount++ ] = lineStart;
			}

			break;
		}

		// Force a line end if its a '\n'
		else if ( *lineEnd == '\n' )
		{
			*lineEnd = '\0';
			scrollPtr->pLines[ scrollPtr->iLineCount++ ] = lineStart;
			lineStart = lineEnd + 1;
			lineEnd   = lineStart;
			w = 0;
			continue;
		}

		// Get the current character width 
		cw = DC->textWidth ( va("%c", *lineEnd), item->textscale, item->iMenuFont );

		// Past the end of the boundary?
		if ( w + cw > (item->window.rect.w - SCROLLBAR_SIZE - 10) )
		{
			// Past the end so backtrack to the word boundary
			while ( *lineEnd != ' ' && *lineEnd != '\t' && lineEnd > lineStart )
			{
				lineEnd--;
			}					

			*lineEnd = '\0';
			scrollPtr->pLines[ scrollPtr->iLineCount++ ] = lineStart;

			// Skip any whitespaces
			lineEnd++;
			while ( (*lineEnd == ' ' || *lineEnd == '\t') && *lineEnd )
			{
				lineEnd++;
			}

			lineStart = lineEnd;
			w = 0;
		}
		else
		{
			w += cw;
			lineEnd++;
		}
	}
#endif
}

// Item_InitControls
// init's special control types
void Item_InitControls(itemDef_t *item) 
{
	if (item == NULL) 
	{
		return;
	}

	switch ( item->type )
	{
		case ITEM_TYPE_LISTBOX:
		{
			listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
			item->cursorPos = 0;
			if (listPtr) 
			{
				listPtr->cursorPos = 0;
				listPtr->startPos = 0;
				listPtr->endPos = 0;
				listPtr->cursorPos = 0;
			}

			break;
		}
	}
}

/*
===============
Menu Keyword Parse functions
===============
*/

qboolean MenuParse_font( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_String_Parse(handle, &menu->font)) {
		return qfalse;
	}
	if (!DC->Assets.fontRegistered) {
		//DC->registerFont(menu->font, 48, &DC->Assets.textFont);
		DC->Assets.qhMediumFont = DC->RegisterFont(menu->font);
		DC->Assets.fontRegistered = qtrue;
	}
	return qtrue;
}

qboolean MenuParse_name( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_String_Parse(handle, &menu->window.name)) {
		return qfalse;
	}
	if (Q_stricmp(menu->window.name, "main") == 0) {
		// default main as having focus
		//menu->window.flags |= WINDOW_HASFOCUS;
	}
	return qtrue;
}

qboolean MenuParse_fullscreen( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Int_Parse(handle, (int*) &menu->fullScreen)) { // bk001206 - cast qboolean
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_rect( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Rect_Parse(handle, &menu->window.rect)) {
		return qfalse;
	}
	return qtrue;
}

/*
=================
MenuParse_style
=================
*/
qboolean MenuParse_style( itemDef_t *item, int handle) 
{
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &menu->window.style))
	{
		Com_Printf(S_COLOR_YELLOW "Unknown menu style value");
		return qfalse;
	}

	return qtrue;
}

qboolean MenuParse_visible( itemDef_t *item, int handle ) {
	int i;
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &i)) {
		return qfalse;
	}
	if (i) {
		menu->window.flags |= WINDOW_VISIBLE;
	}
	return qtrue;
}

qboolean MenuParse_onOpen( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Script_Parse(handle, &menu->onOpen)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_onClose( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Script_Parse(handle, &menu->onClose)) {
		return qfalse;
	}
	return qtrue;
}

//JLFACCEPT MPMOVED
/*
=================
MenuParse_onAccept
=================
*/
qboolean MenuParse_onAccept( itemDef_t *item, int handle ) 
{
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Script_Parse(handle, &menu->onAccept)) 
	{
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_onESC( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Script_Parse(handle, &menu->onESC)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_xScript( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Script_Parse(handle, &menu->xScript)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_yScript( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Script_Parse(handle, &menu->yScript)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_whiteScript( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Script_Parse(handle, &menu->whiteScript)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_border( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Int_Parse(handle, &menu->window.border)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_borderSize( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Float_Parse(handle, &menu->window.borderSize)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_backcolor( itemDef_t *item, int handle ) {
	int i;
	float f;
	menuDef_t *menu = (menuDef_t*)item;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		menu->window.backColor[i]  = f;
	}
	return qtrue;
}

/*
=================
MenuParse_descAlignment
=================
*/
qboolean MenuParse_descAlignment( itemDef_t *item, int handle ) 
{
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &menu->descAlignment)) 
	{
		Com_Printf(S_COLOR_YELLOW "Unknown desc alignment value");
		return qfalse;
	}

	return qtrue;
}

/*
=================
MenuParse_descX
=================
*/
qboolean MenuParse_descX( itemDef_t *item, int handle ) 
{
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &menu->descX))
	{
		return qfalse;
	}
	return qtrue;
}

/*
=================
MenuParse_descY
=================
*/
qboolean MenuParse_descY( itemDef_t *item, int handle ) 
{
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &menu->descY))
	{
		return qfalse;
	}
	return qtrue;
}

/*
=================
MenuParse_descScale
=================
*/
qboolean MenuParse_descScale( itemDef_t *item, int handle) 
{
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Float_Parse(handle, &menu->descScale)) 
	{
		return qfalse;
	}
	return qtrue;
}

/*
=================
MenuParse_descColor
=================
*/
qboolean MenuParse_descColor( itemDef_t *item, int handle) 
{
	int i;
	float f;
	menuDef_t *menu = (menuDef_t*)item;

	for (i = 0; i < 4; i++) 
	{
		if (!PC_Float_Parse(handle, &f)) 
		{
			return qfalse;
		}
		menu->descColor[i]  = f;
	}
	return qtrue;
}

qboolean MenuParse_forecolor( itemDef_t *item, int handle ) {
	int i;
	float f;
	menuDef_t *menu = (menuDef_t*)item;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		if (f < 0)
		{	//special case for player color
			menu->window.flags |= WINDOW_PLAYERCOLOR;
			return qtrue;
		}
		menu->window.foreColor[i]  = f;
		menu->window.flags |= WINDOW_FORECOLORSET;
	}
	return qtrue;
}

qboolean MenuParse_bordercolor( itemDef_t *item, int handle ) {
	int i;
	float f;
	menuDef_t *menu = (menuDef_t*)item;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		menu->window.borderColor[i]  = f;
	}
	return qtrue;
}

qboolean MenuParse_focuscolor( itemDef_t *item, int handle ) {
	int i;
	float f;
	menuDef_t *menu = (menuDef_t*)item;

	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		menu->focusColor[i]  = f;
	}
	return qtrue;
}

qboolean MenuParse_disablecolor( itemDef_t *item, int handle ) {
	int i;
	float f;
	menuDef_t *menu = (menuDef_t*)item;
	for (i = 0; i < 4; i++) {
		if (!PC_Float_Parse(handle, &f)) {
			return qfalse;
		}
		menu->disableColor[i]  = f;
	}
	return qtrue;
}


qboolean MenuParse_outlinecolor( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (!PC_Color_Parse(handle, &menu->window.outlineColor)){
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_background( itemDef_t *item, int handle ) {
	pc_token_t token;
	menuDef_t *menu = (menuDef_t*)item;

	if (!trap_PC_ReadToken(handle, &token)) {
		return qfalse;
	}
	menu->window.background = DC->registerShaderNoMip(token.string);
	return qtrue;
}

/*
qboolean MenuParse_cinematic( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_String_Parse(handle, &menu->window.cinematicName)) {
		return qfalse;
	}
	return qtrue;
}
*/

qboolean MenuParse_ownerdrawFlag( itemDef_t *item, int handle ) {
	int i;
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &i)) {
		return qfalse;
	}
	menu->window.ownerDrawFlags |= i;
	return qtrue;
}

qboolean MenuParse_ownerdraw( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &menu->window.ownerDraw)) {
		return qfalse;
	}
	return qtrue;
}


// decoration
qboolean MenuParse_popup( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	menu->window.flags |= WINDOW_POPUP;
	return qtrue;
}


qboolean MenuParse_outOfBounds( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;

	menu->window.flags |= WINDOW_OOB_CLICK;
	return qtrue;
}

qboolean MenuParse_soundLoop( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_String_Parse(handle, &menu->soundName)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_fadeClamp( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Float_Parse(handle, &menu->fadeClamp)) {
		return qfalse;
	}
	return qtrue;
}

qboolean MenuParse_fadeAmount( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Float_Parse(handle, &menu->fadeAmount)) {
		return qfalse;
	}
	return qtrue;
}


qboolean MenuParse_fadeCycle( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Int_Parse(handle, &menu->fadeCycle)) {
		return qfalse;
	}
	return qtrue;
}


qboolean MenuParse_itemDef( itemDef_t *item, int handle ) {
	menuDef_t *menu = (menuDef_t*)item;
	if (menu->itemCount < MAX_MENUITEMS) {
		menu->items[menu->itemCount] = (itemDef_t *) UI_Alloc(sizeof(itemDef_t));
		Item_Init(menu->items[menu->itemCount]);
		if (!Item_Parse(handle, menu->items[menu->itemCount])) {
			return qfalse;
		}
		Item_InitControls(menu->items[menu->itemCount]);
		menu->items[menu->itemCount++]->parent = menu;
	}
	return qtrue;
}
/*
=================
MenuParse_focuscolor
=================
*/
qboolean MenuParse_appearanceIncrement( itemDef_t *item, int handle ) 
{
	menuDef_t *menu = (menuDef_t*)item;

	if (!PC_Float_Parse(handle, &menu->appearanceIncrement))
	{
		return qfalse;
	}
	return qtrue;
}

keywordHash_t menuParseKeywords[] = {
	{"appearanceIncrement",	MenuParse_appearanceIncrement,	NULL	},
	{"backcolor",			MenuParse_backcolor,	NULL	},
	{"background",			MenuParse_background,	NULL	},
	{"border",				MenuParse_border,		NULL	},
	{"bordercolor",			MenuParse_bordercolor,	NULL	},
	{"borderSize",			MenuParse_borderSize,	NULL	},
//	{"cinematic",			MenuParse_cinematic,	NULL	},
	{"descAlignment",		MenuParse_descAlignment,NULL	},
	{"desccolor",			MenuParse_descColor,	NULL	},
	{"descX",				MenuParse_descX,		NULL	},
	{"descY",				MenuParse_descY,		NULL	},
	{"descScale",			MenuParse_descScale,	NULL	},
	{"disablecolor",		MenuParse_disablecolor, NULL	},
	{"fadeAmount",			MenuParse_fadeAmount,	NULL	},
	{"fadeClamp",			MenuParse_fadeClamp,	NULL	},
	{"fadeCycle",			MenuParse_fadeCycle,	NULL	},
	{"focuscolor",			MenuParse_focuscolor,	NULL	},
	{"font",				MenuParse_font,			NULL	},
	{"forecolor",			MenuParse_forecolor,	NULL	},
	{"fullscreen",			MenuParse_fullscreen,	NULL	},
	{"itemDef",				MenuParse_itemDef,		NULL	},
	{"name",				MenuParse_name,			NULL	},
	{"onClose",				MenuParse_onClose,		NULL	},
	//JLFACCEPT MPMOVED
	{"onAccept",			MenuParse_onAccept,		NULL    },

	{"onESC",				MenuParse_onESC,		NULL	},
	{"xScript",				MenuParse_xScript,		NULL	},
	{"yScript",				MenuParse_yScript,		NULL	},
	{"whiteScript",			MenuParse_whiteScript,	NULL	},
	{"outOfBoundsClick",	MenuParse_outOfBounds,	NULL	},
	{"onOpen",				MenuParse_onOpen,		NULL	},
	{"outlinecolor",		MenuParse_outlinecolor, NULL	},
	{"ownerdraw",			MenuParse_ownerdraw,	NULL	},
	{"ownerdrawFlag",		MenuParse_ownerdrawFlag,NULL	},
	{"popup",				MenuParse_popup,		NULL	},
	{"rect",				MenuParse_rect,			NULL	},
	{"soundLoop",			MenuParse_soundLoop,	NULL	},
	{"style",				MenuParse_style,		NULL	},
	{"visible",				MenuParse_visible,		NULL	},
	{0,						0,						0		}
};

keywordHash_t *menuParseKeywordHash[KEYWORDHASH_SIZE];

/*
===============
Menu_SetupKeywordHash
===============
*/
void Menu_SetupKeywordHash(void) {
	int i;

	memset(menuParseKeywordHash, 0, sizeof(menuParseKeywordHash));
	for (i = 0; menuParseKeywords[i].keyword; i++) {
		KeywordHash_Add(menuParseKeywordHash, &menuParseKeywords[i]);
	}
}

/*
===============
Menu_Parse
===============
*/
qboolean Menu_Parse(int handle, menuDef_t *menu) {
	pc_token_t token;
	keywordHash_t *key;

	if (!trap_PC_ReadToken(handle, &token))
		return qfalse;
	if (*token.string != '{') {
		return qfalse;
	}
    
	while ( 1 ) {
		if (!trap_PC_ReadToken(handle, &token)) {
			PC_SourceError(handle, "end of file inside menu\n");
			return qfalse;
		}

		if (*token.string == '}') {
			return qtrue;
		}

		key = KeywordHash_Find(menuParseKeywordHash, token.string);
		if (!key) {
			PC_SourceError(handle, "unknown menu keyword %s", token.string);
			continue;
		}
		if ( !key->func((itemDef_t*)menu, handle) ) {
			PC_SourceError(handle, "couldn't parse menu keyword %s", token.string);
			return qfalse;
		}
	}
	return qfalse; 	// bk001205 - LCC missing return value
}

/*
===============
Menu_New
===============
*/
void Menu_New(int handle) {
	menuDef_t *menu = &Menus[menuCount];

	if (menuCount < MAX_MENUS) {
		Menu_Init(menu);
		if (Menu_Parse(handle, menu)) {
			Menu_PostParse(menu);
			menuCount++;
		}
	}
}

int Menu_Count() {
	return menuCount;
}

void Menu_PaintAll() {
	int i;
	if (captureFunc) {
		captureFunc(captureData);
	}

	bool fullScreenUI = false;

	for (i = 0; i < menuCount; i++) 
	{
		if (!(Menus[i].window.flags & WINDOW_POPUP || !Menus[i].fullScreen))
		{
			Menu_Paint(&Menus[i], qfalse);
			fullScreenUI = true;
		}
	}

	// Draw the notification icons when in the UI:
	if( fullScreenUI )
	{
		qhandle_t h;

		if( XBL_F_GameNotice() )
		{
			trap_R_SetColor( NULL );
			h = trap_R_RegisterShaderNoMip( "gfx/mp/game_received_icon" );
			DC->drawHandlePic( 515, 40, 28, 28, h );
		}

		if( XBL_F_FriendNotice() )
		{
			trap_R_SetColor( NULL );
			h = trap_R_RegisterShaderNoMip( "gfx/mp/friend_received_icon" );
			DC->drawHandlePic( 545, 42, 25, 25, h );
		}
	}

	for (i = 0; i < menuCount; i++) 
	{
		if ((Menus[i].window.flags & WINDOW_POPUP || !Menus[i].fullScreen)) 
			Menu_Paint(&Menus[i], qfalse);
	}


	if (debugMode) {
		vec4_t v = {1, 1, 1, 1};
		DC->drawText(5, 25, .75, v, va("fps: %f", DC->FPS), 0, 0, 0, 0);
		DC->drawText(5, 45, .75, v, va("x: %d  y:%d", DC->cursorx,DC->cursory), 0, 0, 0, 0);
	}
}

void Menu_Reset() {
	menuCount = 0;
}

displayContextDef_t *Display_GetContext() {
	return DC;
}
 
void *Display_CaptureItem(int x, int y) {
	int i;

	for (i = 0; i < menuCount; i++) {
		// turn off focus each item
		// menu->items[i].window.flags &= ~WINDOW_HASFOCUS;
		if (Rect_ContainsPoint(&Menus[i].window.rect, x, y)) {
			return &Menus[i];
		}
	}
	return NULL;
}


// FIXME: 
qboolean Display_MouseMove(void *p, int x, int y) {

//JLFMOUSE  AGAIN I THINK THIS SHOULD BE MOOT
//#ifdef _XBOX
	//look for all textscroll items in this menu
	

//	return qtrue;
//#endif
	//END JLF
	int i;
	menuDef_t *menu = (menuDef_t *) p;

	if (menu == NULL) {
    menu = Menu_GetFocused();
		if (menu) {
			if (menu->window.flags & WINDOW_POPUP) {
				Menu_HandleMouseMove(menu, x, y);
				return qtrue;
			}
		}
		for (i = 0; i < menuCount; i++) {
			Menu_HandleMouseMove(&Menus[i], x, y);
		}
	} else {
		menu->window.rect.x += x;
		menu->window.rect.y += y;
		Menu_UpdatePosition(menu);
	}
 	return qtrue;

}

int Display_CursorType(int x, int y) {
	int i;
	for (i = 0; i < menuCount; i++) {
		rectDef_t r2;
		r2.x = Menus[i].window.rect.x - 3;
		r2.y = Menus[i].window.rect.y - 3;
		r2.w = r2.h = 7;
		if (Rect_ContainsPoint(&r2, x, y)) {
			return CURSOR_SIZER;
		}
	}
	return CURSOR_ARROW;
}


void Display_HandleKey(int key, qboolean down, int x, int y) {
	menuDef_t *menu = (menuDef_t *) Display_CaptureItem(x, y);
	if (menu == NULL) {  
		menu = Menu_GetFocused();
	}
	if (menu) {
		Menu_HandleKey(menu, key, down );
	}
}

static void Window_CacheContents(windowDef_t *window) {
/*
	if (window) {
		if (window->cinematicName) {
			int cin = DC->playCinematic(window->cinematicName, 0, 0, 0, 0);
			DC->stopCinematic(cin);
		}
	}
*/
}


static void Item_CacheContents(itemDef_t *item) {
	if (item) {
		Window_CacheContents(&item->window);
	}

}

static void Menu_CacheContents(menuDef_t *menu) {
	if (menu) {
		int i;
		Window_CacheContents(&menu->window);
		for (i = 0; i < menu->itemCount; i++) {
			Item_CacheContents(menu->items[i]);
		}

		if (menu->soundName && *menu->soundName) {
			DC->registerSound(menu->soundName);
		}
	}

}

void Display_CacheAll() {
	int i;
	for (i = 0; i < menuCount; i++) {
		Menu_CacheContents(&Menus[i]);
	}
}

/*
	Support routines for the new, heinous, UI Cache shit.

	We save out fully parsed menu files, such that loading them takes a
	minute fraction of the original time.
*/
typedef const char *uic_string;
typedef void *uic_void;

void UIC_SaveString( FILE *f, uic_string s )
{
	if( !s )
		return;
	int len = strlen( s );
	fwrite( &len, sizeof(len), 1, f );
	fwrite( s, len, 1, f );
}

void UIC_SaveShader( FILE *f, qhandle_t s )
{
	if( !s )
		return;
	UIC_SaveString( f, RE_ShaderNameFromIndex( s ) );
}

void UIC_SaveListBox( FILE *f, listBoxDef_t *l )
{
	fwrite( l, sizeof(listBoxDef_t), 1, f );
	UIC_SaveString( f, l->doubleClick );
	UIC_SaveShader( f, l->selectionShader );
}

void UIC_SaveEditField( FILE *f, editFieldDef_t *e )
{
	fwrite( e, sizeof(editFieldDef_t), 1, f );
}

void UIC_SaveMulti( FILE *f, multiDef_t *m )
{
	fwrite( m, sizeof(multiDef_t), 1, f );
	for( int i = 0; i < MAX_MULTI_CVARS; ++i )
	{
		UIC_SaveString( f, m->cvarList[i] );
		UIC_SaveString( f, m->cvarStr[i] );
	}
}

void UIC_SaveModel( FILE *f, modelDef_t *m )
{
	fwrite( m, sizeof(modelDef_t), 1, f );
}

void UIC_SaveTextScroll( FILE *f, textScrollDef_t *t )
{
	fwrite( t, sizeof(textScrollDef_t), 1, f );
	for( int i = 0; i < MAX_TEXTSCROLL_LINES; ++i )
		UIC_SaveString( f, t->pLines[i] );
}

void UIC_SaveGhoul2( FILE *f, itemDef_t *item )
{
	if( !item->ghoul2 )
		return;

	modelDef_t *modelPtr = (modelDef_t *)item->typeData;

	// Will be restored from "ui_char_model" on load
	if( modelPtr->g2IsPlayer )
	{
		return;
	}
	else
	{
		const char *glmName = G2API_GetGLMName(item->ghoul2, 0);
		UIC_SaveString( f, glmName );
	}
}

void UIC_SaveItem( FILE *f, itemDef_t *item )
{
	fwrite( item, sizeof(itemDef_t), 1, f );

	UIC_SaveString( f, item->text );
//	UIC_SaveString( f, item->text2 );
//	UIC_SaveString( f, item->mouseEnterText );
//	UIC_SaveString( f, item->mouseExitText );
//	UIC_SaveString( f, item->mouseEnter );
//	UIC_SaveString( f, item->mouseExit );
	UIC_SaveString( f, item->action );
//	UIC_SaveString( f, item->accept );
	UIC_SaveString( f, item->selectionNext );
	UIC_SaveString( f, item->selectionPrev );
	UIC_SaveString( f, item->onFocus );
	UIC_SaveString( f, item->leaveFocus );
	UIC_SaveString( f, item->cvar );
	UIC_SaveString( f, item->cvarTest );
	UIC_SaveString( f, item->enableCvar );
	UIC_SaveString( f, item->descText );

	UIC_SaveString( f, item->window.name );
	UIC_SaveString( f, item->window.group );
//	UIC_SaveString( f, item->window.cinematicName );

	UIC_SaveShader( f, item->window.background );

	if( item->typeData )
	{
		switch( item->type )
		{
			case ITEM_TYPE_LISTBOX:
				UIC_SaveListBox( f, (listBoxDef_t *)item->typeData );
				break;

			case ITEM_TYPE_EDITFIELD:
			case ITEM_TYPE_NUMERICFIELD:
			case ITEM_TYPE_YESNO:
			case ITEM_TYPE_BIND:
			case ITEM_TYPE_SLIDER:
			case ITEM_TYPE_TEXT:
				UIC_SaveEditField( f, (editFieldDef_t *)item->typeData );
				break;

			case ITEM_TYPE_MULTI:
				UIC_SaveMulti( f, (multiDef_t *)item->typeData );
				break;

			case ITEM_TYPE_MODEL:
				UIC_SaveModel( f, (modelDef_t *)item->typeData );
				break;

			case ITEM_TYPE_TEXTSCROLL:
				UIC_SaveTextScroll( f, (textScrollDef_t *)item->typeData );
				break;

			default:
				assert( 0 );
		}
	}

	UIC_SaveGhoul2( f, item );
}

void UIC_SaveMenu( FILE *f, menuDef_t *menu )
{
	fwrite( menu, sizeof(menuDef_t), 1, f );

	UIC_SaveString( f, menu->font );
	UIC_SaveString( f, menu->onOpen );
	UIC_SaveString( f, menu->onClose );
	UIC_SaveString( f, menu->onAccept );
	UIC_SaveString( f, menu->onESC );
	UIC_SaveString( f, menu->xScript );
	UIC_SaveString( f, menu->yScript );
	UIC_SaveString( f, menu->whiteScript );
	UIC_SaveString( f, menu->soundName );

	UIC_SaveString( f, menu->window.name );
	UIC_SaveString( f, menu->window.group );
//	UIC_SaveString( f, menu->window.cinematicName );

	UIC_SaveShader( f, menu->window.background );

	for( int i = 0; i < menu->itemCount; ++i )
		UIC_SaveItem( f, menu->items[i] );
}


///////////////////////////////////////////////////////////////////////////////

// We load the entire UIC file at once, to avoid tons of small disk-reads
static void *uicBuffer = NULL;
static int uicBufferPos = 0;
static int uicBufferLen = 0;

void uic_read( void *dst, size_t size )
{
	assert( uicBufferPos + size <= uicBufferLen );
	memcpy( dst, ((byte *)uicBuffer) + uicBufferPos, size );
	uicBufferPos += size;
}

// Utility, because we store strings for shaders, and don't want to
// automatically call String_Alloc on those:
const char *UIC_LoadString_Internal( void )
{
	// UICFIXME: This function isn't length-safe!
	static char buf[4096];
	int len;

	uic_read( &len, sizeof(len) );
	assert( len < sizeof(buf) );
	uic_read( buf, len );
	buf[len] = 0;

	return buf;
}

void UIC_LoadString( uic_string &s )
{
	if( !s )
		return;
	s = String_Alloc( UIC_LoadString_Internal() );
}

void UIC_LoadShader( qhandle_t &s )
{
	if( !s )
		return;
	s = DC->registerShaderNoMip( UIC_LoadString_Internal() );
}

void UIC_LoadListBox( uic_void &l )
{
	l = UI_Alloc( sizeof(listBoxDef_t) );
	uic_read( l, sizeof(listBoxDef_t) );
	UIC_LoadString( ((listBoxDef_t *)l)->doubleClick );
	UIC_LoadShader( ((listBoxDef_t *)l)->selectionShader );
}

void UIC_LoadEditField( uic_void &e )
{
	e = UI_Alloc( sizeof(editFieldDef_t) );
	uic_read( e, sizeof(editFieldDef_t) );
}

void UIC_LoadMulti( uic_void &m )
{
	m = UI_Alloc( sizeof(multiDef_t) );
	uic_read( m, sizeof(multiDef_t) );

	for( int i = 0; i < MAX_MULTI_CVARS; ++i )
	{
		UIC_LoadString( ((multiDef_t *)m)->cvarList[i] );
		UIC_LoadString( ((multiDef_t *)m)->cvarStr[i] );
	}
}

void UIC_LoadModel( uic_void &m )
{
	m = UI_Alloc( sizeof(modelDef_t) );
	uic_read( m, sizeof(modelDef_t) );
}

void UIC_LoadTextScroll( uic_void &t )
{
	t = UI_Alloc( sizeof(textScrollDef_t) );
	uic_read( t, sizeof(textScrollDef_t) );

	for( int i = 0; i < MAX_TEXTSCROLL_LINES; ++i )
		UIC_LoadString( ((textScrollDef_t *)t)->pLines[i] );
}

void UIC_LoadGhoul2( itemDef_t *item )
{
	if( !item->ghoul2 )
		return;

	item->ghoul2 = NULL;
	modelDef_t *modelPtr = (modelDef_t *)item->typeData;
	const char *name;

	if( modelPtr->g2IsPlayer )
		name = va( "models/players/%s/model.glm", Cvar_VariableString( "ui_char_model" ) );
	else
		name = UIC_LoadString_Internal();

	int g2Model = trap_G2API_InitGhoul2Model( &item->ghoul2, name, 0, modelPtr->g2skin, 0, 0, 0);
	if (g2Model >= 0)
	{
		UI_InsertG2Pointer( item->ghoul2 );
		item->flags |= ITF_G2VALID;

		if ( modelPtr->g2anim )
		{
			char GLAName[MAX_QPATH];	

			GLAName[0] = 0;
			trap_G2API_GetGLAName(item->ghoul2, 0, GLAName);

			if ( GLAName[0] )
			{
				int animIndex;
				char *slash;

				slash = Q_strrchr( GLAName, '/' );

				if ( slash )
				{ //If this isn't true the gla path must be messed up somehow.
					strcpy(slash, "/animation.cfg");

					animIndex = UI_ParseAnimationFile(GLAName, NULL, qfalse);
					if (animIndex != -1)
					{ //We parsed out the animation info for whatever model this is
						animation_t *anim = &bgAllAnims[animIndex].anims[modelPtr->g2anim];

						int sFrame = anim->firstFrame;
						int eFrame = anim->firstFrame + anim->numFrames;
						int flags = BONE_ANIM_OVERRIDE_FREEZE;
						int time = DC->realTime;
						float animSpeed = 50.0f / anim->frameLerp;
						int blendTime = 150;

						if (anim->loopFrames != -1)
						{
							flags |= BONE_ANIM_OVERRIDE_LOOP;
						}

						trap_G2API_SetBoneAnim(item->ghoul2, 0, "model_root", sFrame, eFrame, flags, animSpeed, time, -1, blendTime);
//						*runTimeLength =((anim->frameLerp * (anim->numFrames-2)));					
					}
				}
			} 
		}

		if ( modelPtr->g2skin )
		{
			trap_G2API_SetSkin(item->ghoul2, 0, modelPtr->g2skin, modelPtr->g2skin);
		}
	}
}

void UIC_LoadItem( itemDef_t *&item )
{
	item = (itemDef_t *) UI_Alloc( sizeof(itemDef_t) );

	uic_read( item, sizeof(itemDef_t) );

	UIC_LoadString( item->text );
//	UIC_LoadString( item->text2 );
//	UIC_LoadString( item->mouseEnterText );
//	UIC_LoadString( item->mouseExitText );
//	UIC_LoadString( item->mouseEnter );
//	UIC_LoadString( item->mouseExit );
	UIC_LoadString( item->action );
//	UIC_LoadString( item->accept );
	UIC_LoadString( item->selectionNext );
	UIC_LoadString( item->selectionPrev );
	UIC_LoadString( item->onFocus );
	UIC_LoadString( item->leaveFocus );
	UIC_LoadString( item->cvar );
	UIC_LoadString( item->cvarTest );
	UIC_LoadString( item->enableCvar );
	UIC_LoadString( item->descText );

	UIC_LoadString( item->window.name );
	UIC_LoadString( item->window.group );
//	UIC_LoadString( item->window.cinematicName );

	UIC_LoadShader( item->window.background );

	if( item->typeData )
	{
		switch( item->type )
		{
			case ITEM_TYPE_LISTBOX:
				UIC_LoadListBox( item->typeData );
				break;

			case ITEM_TYPE_EDITFIELD:
			case ITEM_TYPE_NUMERICFIELD:
			case ITEM_TYPE_YESNO:
			case ITEM_TYPE_BIND:
			case ITEM_TYPE_SLIDER:
			case ITEM_TYPE_TEXT:
				UIC_LoadEditField( item->typeData );
				break;

			case ITEM_TYPE_MULTI:
				UIC_LoadMulti( item->typeData );
				break;

			case ITEM_TYPE_MODEL:
				UIC_LoadModel( item->typeData );
				break;

			case ITEM_TYPE_TEXTSCROLL:
				UIC_LoadTextScroll( item->typeData );
				break;

			default:
				assert( 0 );
		}
	}

	UIC_LoadGhoul2( item );
}

void UIC_LoadMenu( menuDef_t *menu )
{
	uic_read( menu, sizeof(menuDef_t) );

	UIC_LoadString( menu->font );
	UIC_LoadString( menu->onOpen );
	UIC_LoadString( menu->onClose );
	UIC_LoadString( menu->onAccept );
	UIC_LoadString( menu->onESC );
	UIC_LoadString( menu->xScript );
	UIC_LoadString( menu->yScript );
	UIC_LoadString( menu->whiteScript );
	UIC_LoadString( menu->soundName );

	UIC_LoadString( menu->window.name );
	UIC_LoadString( menu->window.group );
//	UIC_LoadString( menu->window.cinematicName );

	UIC_LoadShader( menu->window.background );

	for( int i = 0; i < menu->itemCount; ++i )
	{
		UIC_LoadItem( menu->items[i] );
		menu->items[i]->parent = menu;
	}
}

void UIC_SaveMenuFile( const char *filename, int menuLen )
{
	FILE *f = fopen( filename, "wb" );
	if( !f )
		return;
	fwrite( &menuLen, sizeof(int), 1, f );
	UIC_SaveMenu( f, &Menus[menuCount-1] );
	fclose( f );
}

bool UIC_LoadMenuFile( const char *filename, int menuLen )
{
	uicBufferLen = FS_ReadFile( filename, &uicBuffer );
	if( uicBufferLen > 0 )
	{
		uicBufferPos = 0;

		int storedLen;
		uic_read( &storedLen, sizeof(int) );

		// During development, check that the stored menu file size matches
#ifndef FINAL_BUILD
		if( menuLen != storedLen )
		{
			Com_Printf( "UIC file %s is old, re-generating.\n", filename );
			FS_FreeFile( uicBuffer );
			return false;
		}
#endif

		UIC_LoadMenu( &Menus[menuCount] );
		menuCount++;
		FS_FreeFile( uicBuffer );
		return true;
	}

	return false;
}

#include "../namespace_end.h"
