/*
 *  Copyright (C) 2008 Nuke (wiinuke@gmail.com)
 *  Copyright (C) 2009 WiiPower
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// This menu code is pretty shit, i wrote it very quickly, and just for speed.
// Would really like for someone to help out to write a decent menu
// Progress bar isn't threaded etc


#include <malloc.h>
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <ogcsys.h>
#include <sys/unistd.h>
#include "geckomenu.h"
#include "font.h"
#include "apploader.h"
#include "patchcode.h"
#include "geckogreen.h"
#include "geckogrey.h"
#include "tools.h"
#include "identify.h"
#include "libcios.h"
#include "easywbfs.h"
#include "config.h"
#include "sdhc.h"


#define fontheight 24
#define innermenu_start 20
#define innermenu_end 306
#define screenwidth 320
#define progtotal 132

u32 barstate;
u32 skipvalue = 0;
u32 startypos = 125;

char textbuffer[255];

char languages[12][22] = 
{{"Console Default"},
{"Japanese"},
{"English"},
{"German"},
{"French"},
{"Spanish"},
{"Italian"},
{"Dutch"},
{"S. Chinese"},
{"T. Chinese"},
{"Korean"},
{"Auto Force"}};

char ocarina_options[4][17] = 
{{"No"},
{"Yes(default IOS)"},
{"Yes(IOS36)"},
{"Yes(IOS61)"}};

char hook_options[8][15] = 
{{"None"},
{"VBI"},
{"KPAD"},
{"Joypad"},
{"GXDraw"},
{"GXFlush"},
{"OSSleepThread"},
{"AXNextFrame"}};


char video_options[9][9] =
{{"Wii"},
{"Disc"},
{"NTSC480i"},
{"NTSC480p"},
{"PAL480i"},
{"PAL480p"},
{"PAL576i"},
{"MPAL480i"},
{"MPAL480p"}};

char videopatch_options[4][8] =
{{"No"},
{"Normal"},
{"More"},
{"All"}};

char wbfs_options[3][27] =
{{"Load Games from: <DVD>"},
{"Load Games from: <USB>"},
{"Load Games from: <SD/SDHC>"}};


char discisinserted_options[3][6] = 
{{"Auto"},
{"Yes"},
{"No"}};


char alternativedol_options[3][8] = 
{{"No"},
{"Storage"},
{"Disc"}};

char storage_options[2][6] = 
{{"SD"},
{"USB"}};


char bool_options[2][6] = 
{{"No"},
{"Yes"}};


extern u32 *xfb;
extern const unsigned char kenobiwii[];
extern const int kenobiwii_size;
extern s32 __SYS_LoadMenu();


u32 currentmenu = ROOTMENU;	// 0 ROOT
s32 rootmenu_item = 0;
u32 menufreeze = 0;

u32 mainmenu_item = 0;


static void _plotpixel(u32 *framebuffer, u16 x, u16 y, u32 color)
{
	framebuffer[320*y+x] = color;
}

void drawicon(u32 *framebuffer, u16 xscreen, u16 yscreen, u16 width, u16 height, u32 gicon)
{
	
	u16 x = 0;
	u16 y = 0;
	u32 i = 0;
	u32 colorgreen = 0;
	u32 colorgrey = 0;

	for(y = yscreen; y < yscreen+height; y++)
	{
		for(x = xscreen; x < xscreen+width; x++)
		{
			if(gicon){	// high green, grey low
				colorgreen = be32(geckogreen+i);
				_plotpixel(framebuffer, x, y, colorgreen);
			}
			else {
				colorgrey = be32(geckogrey+i);
				_plotpixel(framebuffer, x, y, colorgrey);
			}
			
			i += 4;
		}
	}
}


void clearscreen(u32 *framebuffer, u16 xscreen, u16 yscreen, u16 width, u16 height, u32 color)
{	
	u16 x;
	u16 y;

	for(y = yscreen; y < yscreen+height; y++)
	{
		for(x = xscreen; x < xscreen+width; x++)
		{
			_plotpixel(framebuffer, x, y, color);
		}
	}
}

void clearmenu()
{
	clearscreen(xfb, 0, 110, 319, 270, 0x00800080);
}

/* All wii remote joypad handling and menu processing */
void processwpad()
{
	Verify_Flags(); // Standby and Reset
	
	WPAD_ScanPads();
	u32 pressed = WPAD_ButtonsDown(0);

	PAD_ScanPads();
	u32 pressedGC = PAD_ButtonsDown(0);

	VIDEO_WaitVSync();
	
	// TODO: Get totally rid of the flickering
	if (pressed || pressedGC)
	{
		clearmenu();
	}
 
	/* Root menu actions */
	if((pressed & WPAD_BUTTON_A && currentmenu == ROOTMENU) || (pressedGC & PAD_BUTTON_A && currentmenu == ROOTMENU))
	{
		if (rootmenu_item == 0 && (wbfsdevice == 0 || getGameCount() > 0))
		{
			clearmenu();
			
			Save_Config();
			
			write_font(185, startypos + 1*24, "Launching Game");
	
			apploader();
			
			// Being here means there was an error
			sleep(6);
			clearmenu();
			pressed = 0;
			pressedGC = 0;
		}
		if ( (rootmenu_item == 0 || rootmenu_item == 1) && getGameCount() == 0 && wbfsdevice != 0)
		{
			u32 trynr = 1;
			s32 ret = -1;
			while (ret < 0  && trynr <= 30)
			{
				if (trynr == 7)
				{
					WPAD_Shutdown();
				}
				if (trynr >= 7)
				{
					IOS_ReloadIOS(CIOS_VERSION);
				}
				ret = Try_WBFS_Init();
				
				if (ret < 0)
				{
					Verify_Flags(); // Standby and Reset
				
					WPAD_ScanPads();
					u32 pressed = WPAD_ButtonsDown(0);

					PAD_ScanPads();
					u32 pressedGC = PAD_ButtonsDown(0);

					clearscreen(xfb, 0, startypos + 1*24, 319, 24, 0x00800080);
					if (wbfsdevice == 1)
					{
						sprintf(textbuffer,"Mounting USB Tries left: %u", 30-trynr);
					} else
					{
						sprintf(textbuffer,"Mounting SD/SDHC Tries left: %u", 30-trynr);
					}
					write_font(115, startypos + 1*24, textbuffer);
					if (pressed || pressedGC)
					{
						break;		
					}
					VIDEO_WaitVSync();
				}
				trynr++;
			}			
			if (trynr >= 7)
			{
				PAD_Init();
				WPAD_Init();
				WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
			}
			clearscreen(xfb, 0, startypos + 1*24, 319, 24, 0x00800080);
			if (ret >= 0)
			{
				ret = initGameList();
				SDHC_Close();
			}
		}	

		if (rootmenu_item == 3)
		{
			mainmenu_item = rootmenu_item;
			currentmenu = REGIONMENU;
			rootmenu_item = 0;
		}

		if (rootmenu_item == 4)
		{
			mainmenu_item = rootmenu_item;
			currentmenu = CONFIGMENU;
			rootmenu_item = 0;
		}

		if (rootmenu_item == 5)
		{
			mainmenu_item = rootmenu_item;
			currentmenu = REBOOTMENU;
			rootmenu_item = 0;
		}

		if (rootmenu_item == 6)
		{
			Save_Config();	
			boot_menu();
			clearmenu();
		}

		if (rootmenu_item == 7)
		{
			mainmenu_item = rootmenu_item;
			currentmenu = ABOUTMENU;
			rootmenu_item = about_itemcount -1;
			menufreeze = 1;
		}

		if (rootmenu_item == 9)
		{
			Save_Config();
			if (exitworks) exit(0);
			SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0);
		}
		
		// Do this because don't want button A be recognised by one of the sub menus, if it's not pressed there
		pressed = 0;
		pressedGC = 0;
	}
	
	// Root Left
	if (currentmenu == ROOTMENU && ((pressed & WPAD_BUTTON_LEFT) || (pressedGC & PAD_BUTTON_LEFT)))
	{	
		if(rootmenu_item == 0 && getGameCount() != 0)
		{
			if(gameselected == 0)
			{
				gameselected = getGameCount()-1;
			}
			else {
				gameselected -= 1;
			}	
		}

		if(rootmenu_item == 1)
		{
			gameselected = 0;
			if(wbfsdevice == 0)
			{
				wbfsdevice = 2;
			}
			else {
				wbfsdevice -= 1;
			}
		}
	}
	
	if (currentmenu == ROOTMENU && ((pressed & WPAD_BUTTON_MINUS) || (pressedGC & PAD_TRIGGER_L)))
	{
		if(rootmenu_item == 0)
		{
			if(gameselected >= 7)
			{
				gameselected -= 7;
			}
		}
	}
	
	// Root Right
	if (currentmenu == ROOTMENU && ((pressed & WPAD_BUTTON_RIGHT) || (pressedGC & PAD_BUTTON_RIGHT)))
	{	
		if(rootmenu_item == 0 && getGameCount() != 0)
		{
			if(gameselected == getGameCount()-1)
			{
				gameselected = 0;
			}
			else {
				gameselected += 1;
			}	
		}

		if(rootmenu_item == 1)
		{
			gameselected = 0;
			if(wbfsdevice == 2)
			{
				wbfsdevice = 0;
			}
			else 
			{
				wbfsdevice += 1;
			}
		}
	}
	
	if (currentmenu == ROOTMENU && ((pressed & WPAD_BUTTON_PLUS) || (pressedGC & PAD_TRIGGER_R)))
	{
		if(rootmenu_item == 0)
		{
			if(gameselected+7 <= getGameCount()-1)
			{
				gameselected += 7;
			}
		}
	}


	/* About menu actions */
	if((pressed & WPAD_BUTTON_A && currentmenu == ABOUTMENU) || (pressedGC & PAD_BUTTON_A && currentmenu == ABOUTMENU))
	{
		menufreeze = 0;
		rootmenu_item = mainmenu_item;
		currentmenu = ROOTMENU;			
	}

	/* Config menu actions */
	if((pressed & WPAD_BUTTON_A && currentmenu == CONFIGMENU) || (pressedGC & PAD_BUTTON_A && currentmenu == CONFIGMENU))
	{
		/* Return */
		if(rootmenu_item == config_itemcount - 1){
			rootmenu_item = mainmenu_item;
			currentmenu = ROOTMENU;
		}
		
	}

	/* Config Left*/
	if((pressed & WPAD_BUTTON_LEFT && currentmenu == CONFIGMENU) || (pressedGC & PAD_BUTTON_LEFT && currentmenu == CONFIGMENU))
	{
		if(rootmenu_item == 0){
			if(hookselect == 0){
				hookselect = 7;
			}
			else {
				hookselect -= 1;
			}	
		}

		if(rootmenu_item == 1){
			if(ocarinaselect == 0){
				ocarinaselect = 3;
			}
			else {
				ocarinaselect -= 1;
			}	
		}
		
		if(rootmenu_item == 2){
			if(debuggerselect == 0){
				debuggerselect = 1;
			}
			else {
				debuggerselect -= 1;
			}	
		}
		
		if(rootmenu_item == 3){
			if(alternativedolselect == 0){
				alternativedolselect = 2;
			}
			else {
				alternativedolselect -= 1;
			}	
		}		
		


		if(rootmenu_item == 4){
			if(patchselect == 0){
				patchselect = 1;
			}
			else {
				patchselect -= 1;
			}	
		}		

		if(rootmenu_item == 5){
			if(storageselect == 0){
				storageselect = 1;
			}
			else {
				storageselect -= 1;
			}	
		}		

		if(rootmenu_item == 6){
			if(configselect == 0){
				configselect = 1;
			}
			else {
				configselect -= 1;
			}	
		}		
	}

	/* Config Right*/
	if((pressed & WPAD_BUTTON_RIGHT && currentmenu == CONFIGMENU) || (pressedGC & PAD_BUTTON_RIGHT && currentmenu == CONFIGMENU))
	{
		/* Hooks */
		if(rootmenu_item == 0){
			if(hookselect == 7){
				hookselect = 0;
			}
			else {
				hookselect += 1;
			}	
		}
		/* Ocarina */
		if(rootmenu_item == 1){
			if(ocarinaselect == 3){
				ocarinaselect = 0;
			}
			else {
				ocarinaselect += 1;
			}	
		}

		if(rootmenu_item == 2)
		{
			if(debuggerselect == 1){
				debuggerselect = 0;
			}
			else {
				debuggerselect += 1;
			}	
		}
			
		if(rootmenu_item == 3){
			if(alternativedolselect == 2){
				alternativedolselect = 0;
			}
			else {
				alternativedolselect += 1;
			}	
		}		

		if(rootmenu_item == 4){
			if(patchselect == 1){
				patchselect = 0;
			}
			else {
				patchselect += 1;
			}	
		}		

		if(rootmenu_item == 5){
			if(storageselect == 1){
				storageselect = 0;
			}
			else {
				storageselect += 1;
			}	
		}		

		if(rootmenu_item == 6){
			if(configselect == 1){
				configselect = 0;
			}
			else {
				configselect += 1;
			}	
		}		
	}

	/* REBOOT menu actions */
	if((pressed & WPAD_BUTTON_A && currentmenu == REBOOTMENU) || (pressedGC & PAD_BUTTON_A && currentmenu == REBOOTMENU))
	{
	
		if(rootmenu_item == rebooter_itemcount - 1)
		{
			rootmenu_item = mainmenu_item;
			currentmenu = ROOTMENU;
			/* check if both ntsc and pal60 modes are forced */
		//	if((videoselect == 1 && videopatchselect == 1) || (free2select == 1 && videopatchselect == 1) || (free2select == 1 && videoselect == 1)){
		//		clearmenu();
		//		write_font(startypos + 0*24, startypos + 1*24, "Can't Force Two Video Modes");
		//		sleep(1);
		//		currentmenu = CONFIGMENU;
		//	}
		}
	}
	
	/* REBOOT menu left */
	if((pressed & WPAD_BUTTON_LEFT && currentmenu == REBOOTMENU) || (pressedGC & PAD_BUTTON_LEFT && currentmenu == REBOOTMENU))
	{
		if(rootmenu_item == 0){
			if(recoveryselect == 0){
				recoveryselect = 1;
			}
			else {
				recoveryselect -= 1;
			}	
		}

		if(rootmenu_item == 1){
			if(regionfreeselect == 0){
				regionfreeselect = 1;
			}
			else {
				regionfreeselect -= 1;
			}	
		}

		if(rootmenu_item == 2){
			if(nocopyselect == 0){
				nocopyselect = 1;
			}
			else {
				nocopyselect -= 1;
			}	
		}

		if(rootmenu_item == 3){
			if(buttonskipselect == 0){
				buttonskipselect = 1;
			}
			else {
				buttonskipselect -= 1;
			}	
		}

		if(rootmenu_item == 4){
			if(skipupdateselect == 0){
				skipupdateselect = 1;
			}
			else {
				skipupdateselect -= 1;
			}	
		}
	}

	/* REBOOT menu right */
	if((pressed & WPAD_BUTTON_RIGHT && currentmenu == REBOOTMENU) || (pressedGC & PAD_BUTTON_RIGHT && currentmenu == REBOOTMENU))
	{
		if(rootmenu_item == 0){
			if(recoveryselect == 1){
				recoveryselect = 0;
			}
			else {
				recoveryselect += 1;
			}	
		}

		if(rootmenu_item == 1){
			if(regionfreeselect == 1){
				regionfreeselect = 0;
			}
			else {
				regionfreeselect += 1;
			}	
		}

		if(rootmenu_item == 2){
			if(nocopyselect == 1){
				nocopyselect = 0;
			}
			else {
				nocopyselect += 1;
			}	
		}

		if(rootmenu_item == 3){
			if(buttonskipselect == 1){
				buttonskipselect = 0;
			}
			else {
				buttonskipselect += 1;
			}	
		}
		
		if(rootmenu_item == 4){
			if(skipupdateselect == 1){
				skipupdateselect = 0;
			}
			else {
				skipupdateselect += 1;
			}	
		}		
	}

	/* NEOGAMMA menu actions */
	if((pressed & WPAD_BUTTON_A && currentmenu == REGIONMENU) || (pressedGC & PAD_BUTTON_A && currentmenu == REGIONMENU))
	{	
		if(rootmenu_item == region_itemcount-1)
		{
			rootmenu_item = mainmenu_item;
			currentmenu = ROOTMENU;
		}
	}
	
	// NEOGAMMA menu left
	if((pressed & WPAD_BUTTON_LEFT && currentmenu == REGIONMENU) || (pressedGC & PAD_BUTTON_LEFT && currentmenu == REGIONMENU))
	{
		if(rootmenu_item == 0){
			if(langselect == 0){
				langselect = 11;
			}
			else {
				langselect -= 1;
			}	
		}

		if(rootmenu_item == 1){
			if(videoselect == 0){
				videoselect = 8;
			}
			else {
				videoselect -= 1;
			}	
		}

		/* force ntsc patch */
		if(rootmenu_item == 2){
			if(videopatchselect == 0){
				videopatchselect = 3;
			}
			else {
				videopatchselect -= 1;
			}	
		}

		if(rootmenu_item == 3){
			if(viselect == 0){
				viselect = 1;
			}
			else {
				viselect -= 1;
			}	
		}

		if(rootmenu_item == 4){
			if(countrystringselect == 0){
				countrystringselect = 1;
			}
			else {
				countrystringselect -= 1;
			}	
		}
	}

	// NEOGAMMA menu right
	if((pressed & WPAD_BUTTON_RIGHT && currentmenu == REGIONMENU) || (pressedGC & PAD_BUTTON_RIGHT && currentmenu == REGIONMENU))
	{
		/* language */
		if(rootmenu_item == 0){
			if(langselect == 11){
				langselect = 0;
			}
			else {
				langselect += 1;
			}	
		}
		/* pal60 */
		if(rootmenu_item == 1){
			if(videoselect == 8){
				videoselect = 0;
			}
			else {
				videoselect += 1;
			}	
		}

		/* force ntsc patch */
		if(rootmenu_item == 2){
			if(videopatchselect == 3){
				videopatchselect = 0;
			}
			else {
				videopatchselect += 1;
			}	
		}

		/* VI patch */
		if(rootmenu_item == 3){
			if(viselect == 1){
				viselect = 0;
			}
			else {
				viselect += 1;
			}	
		}

		if(rootmenu_item == 4){
			if(countrystringselect == 1){
				countrystringselect = 0;
			}
			else {
				countrystringselect += 1;
			}	
		}
	}

	/* Wiimote Pad UP */
	if((pressed & WPAD_BUTTON_UP && menufreeze == 0) || (pressedGC & PAD_BUTTON_UP && menufreeze == 0))
	{
		rootmenu_item -= 1;
		if (rootmenu_item < 0)
		{
			switch (currentmenu)
			{
				case ROOTMENU:
					rootmenu_item = root_itemcount-1;
					break;
				case CONFIGMENU:
					rootmenu_item = config_itemcount-1;
					break;
				case REBOOTMENU:
					rootmenu_item = rebooter_itemcount-1;
					break;
				case REGIONMENU:
					rootmenu_item = region_itemcount-1;
					break;			
			}		
		}
		
		if (currentmenu == ROOTMENU && (rootmenu_item == 2 || rootmenu_item == root_itemcount - 2))
		{
			rootmenu_item -= 1;
		}

		if (currentmenu == CONFIGMENU && (rootmenu_item == config_itemcount - 2))
		{
			rootmenu_item -= 1;
		}

		if (currentmenu == REBOOTMENU && rootmenu_item == rebooter_itemcount - 2)
		{
			rootmenu_item -= 1;
		}

		if (currentmenu == REGIONMENU && rootmenu_item == region_itemcount - 2)
		{
			rootmenu_item -= 1;
		}
	}

	/* Wiimote Pad Down */
	if((pressed & WPAD_BUTTON_DOWN && menufreeze == 0) || (pressedGC & PAD_BUTTON_DOWN && menufreeze == 0))
	{
		rootmenu_item += 1;
		if (	(currentmenu == ROOTMENU && rootmenu_item >= root_itemcount)
		||	 	(currentmenu == CONFIGMENU && rootmenu_item >= config_itemcount)
		||	 	(currentmenu == REBOOTMENU && rootmenu_item >= rebooter_itemcount)
		||	 	(currentmenu == REGIONMENU && rootmenu_item >= region_itemcount) )
		{
			rootmenu_item = 0;
		}
		
		if (currentmenu == ROOTMENU && (rootmenu_item == 2 || rootmenu_item == root_itemcount - 2))
		{
			rootmenu_item += 1;
		}

		if (currentmenu == CONFIGMENU && (rootmenu_item == config_itemcount - 2))
		{
			rootmenu_item += 1;
		}

		if (currentmenu == REBOOTMENU && rootmenu_item == rebooter_itemcount - 2)
		{
			rootmenu_item += 1;
		}

		if (currentmenu == REGIONMENU && rootmenu_item == region_itemcount - 2)
		{
			rootmenu_item += 1;
		}
	}
//out:
		
	drawmenu(currentmenu);
	drawselected(rootmenu_item);

	if(currentmenu == ROOTMENU)
	{
		if (wbfsdevice > 0)
		{
			sprintf(textbuffer, "%s", getGameTitle(gameselected));
			write_font_centered(42, 614, startypos + 0*24, textbuffer);
		} else
		{
			write_font_centered(42, 614, startypos + 0*24, "Launch Game on DVD");
		}
		sprintf(textbuffer, "%s", wbfs_options[wbfsdevice]);
		write_font_centered(42, 614, startypos + 1*24, textbuffer);
	}

	// Draw options maybe tidy this up and make a func if doing larger menus  
	if(currentmenu == CONFIGMENU)
	{
		write_font(375, startypos + 1*24, "%s", hook_options[hookselect]);
		write_font(375, startypos + 2*24, "%s", ocarina_options[ocarinaselect]);
		write_font(375, startypos + 3*24, "%s", bool_options[debuggerselect]);
		write_font(375, startypos + 4*24, "%s", alternativedol_options[alternativedolselect]);
		write_font(375, startypos + 5*24, "%s", bool_options[patchselect]);
		write_font(375, startypos + 6*24, "%s", storage_options[storageselect]);
		write_font(375, startypos + 7*24, "%s", bool_options[configselect]);
	}

	if(currentmenu == REBOOTMENU)
	{
		write_font(455, startypos + 1*24, "%s", bool_options[recoveryselect]);
		write_font(455, startypos + 2*24, "%s", bool_options[regionfreeselect]);
		write_font(455, startypos + 3*24, "%s", bool_options[nocopyselect]);
		write_font(455, startypos + 4*24, "%s", bool_options[buttonskipselect]);
		write_font(455, startypos + 5*24, "%s", bool_options[skipupdateselect]);
	}

	if(currentmenu == REGIONMENU)
	{
		write_font(410, startypos + 1*24, "%s", languages[langselect]);
		write_font(410, startypos + 2*24, "%s", video_options[videoselect]);
		write_font(410, startypos + 3*24, "%s", videopatch_options[videopatchselect]);
		write_font(410, startypos + 4*24, "%s", bool_options[viselect]);
		write_font(410, startypos + 5*24, "%s", bool_options[countrystringselect]);
	}
}

u32 CvtRGB (u8 r1, u8 g1, u8 b1, u8 r2, u8 g2, u8 b2)
{
  int y1, cb1, cr1, y2, cb2, cr2, cb, cr;
 
  y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000;
  cb1 = (-16874 * r1 - 33126 * g1 + 50000 * b1 + 12800000) / 100000;
  cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000;
 
  y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000;
  cb2 = (-16874 * r2 - 33126 * g2 + 50000 * b2 + 12800000) / 100000;
  cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000;
 
  cb = (cb1 + cb2) >> 1;
  cr = (cr1 + cr2) >> 1;
 
  return (y1 << 24) | (cb << 16) | (y2 << 8) | cr;
}

/* highlight selected item */
void drawselected(u32 menuidpos)
{
	u32 x;
	u32 y;
	u32 startpos = startypos+1;
	if (currentmenu != ROOTMENU)
	{
		startpos = startpos + 24;
	} 
	u32 yscreen = startpos + (menuidpos * fontheight);

	
	for(y = yscreen; y < yscreen + fontheight; y++)
	{
		for(x = innermenu_start; x < innermenu_end; x++)
		{
			if(xfb[x + screenwidth * y] == 0x00800080)
			{
				xfb[x + screenwidth * y] = CvtRGB(44, 120, 8, 44, 120, 8);
			}
		}
	}
}

static void __blitmenu (char items[][27], u32 maxitems, u32 shift)
{
	int i;
	int y = startypos + shift * 24;
	
	for(i=0;i<maxitems;i++)
	{
		write_font(160, y, (char *) items[i]);
		y += 24;
	}
}

void drawmenu(u32 menuid)
{
	char rootitems[root_itemcount][27] = {{""}, {""}, {""}, {"  Region Options"}, {"  Config Options"},{"  Rebooter Options"},{"  Use Rebooter"},{"  Credits"},{""}, {"  Exit NeoGamma"}};
	char aboutitems[about_itemcount][27] = {{"Gamma Loader - WiiGator"}, {"Modifications - WiiPower"},{"Graphics - Empyr69er"}, {""},{"Return to Menu"}};
	char configitems[config_itemcount][27] = {{"Hook Type:"},{"Ocarina On:"},{"Debugger:"}, {"Altern. .dol:"}, {"Search patches"}, {"Storage device"}, {"Save Config:"}, {""},{"Return to Menu"}};
	char rebooteritems[rebooter_itemcount][27] = {{"Recovery Mode:"},{"Region Free:"},{"Remove Copy Flags:"},{"Button Skip:"},{"Skip Updates:"}, {""},{"Return to Menu"}};
	char neogammaitems[region_itemcount][27] = {{"Boot Lang:"},{"Force Video:"},{"Patch Video:"},{"VIDTV Patch:"}, {"Patch Country Str.:"},{""},{"Return to Menu"}};

	switch(menuid)
	{
		case ROOTMENU:
			__blitmenu(&rootitems[0], root_itemcount, 0);
		break;

		case ABOUTMENU:
			__blitmenu(&aboutitems[0], about_itemcount, 1);
		break;

		case CONFIGMENU:
			__blitmenu(&configitems[0], config_itemcount, 1);
		break;

		case REBOOTMENU:
			__blitmenu(&rebooteritems[0], rebooter_itemcount, 1);
		break;

		case REGIONMENU:
			__blitmenu(&neogammaitems[0], region_itemcount, 1);
		break;
	}
}

