/*********************************************************************
*	Option window definition
*	SOURCE FILE
*	Autor:	Michal Jirouš
*	Datum: 25.7.2008
*	Soubor: menu_optionwindow.cpp
*	Popis: Implementovane okno nastaveni umoznuje menit vlastnosti systemu
*			a hry. Lze menit nastaveni klavesnice, mysi, rozliseni obrazovky,
*			jakyk a dalsi obecne vlatnosti.
**********************************************************************/

#include "game_mainmenu.h"
#include "textLibrary.h"
#include "game_highscore.h"
#include <sstream>
#include "sys_console.h"
#include "ctrl_var_definition.h"
#include "ctrl_gamecontrol.h"
#include "sys_config.h"
#include "ctrl_keydatabase.h"

using namespace systemConsole;
using namespace vardef;
using namespace mainMenu;
using namespace std;

const float OPTION_KEYBOARD_LABEL_COMMAND_SIZE = 2.0f;
const float OPTION_KEYBOARD_LABEL_KEY_SIZE		= 1.0f;
const float OPTION_KEYBOARD_PANELS_PADDING = 0.1f;
const float OPTION_KEYBOARD_PANELS_HEIGHT = 32.0f;
const float OPTION_MOUSE_LABEL_HEIGHT = 100.0f;
const float OPTION_MOUSE_MAIN_PADDING = 10.0f;
const float OPTION_MOUSE_SLIDER_HEIGHT = 48;
const int	OPTION_MOUSE_SLIDER_ROUND_POSITIONS = 3;
const float OPTION_MOUSE_CHECKBOX_HEIGHT = 32;
const float OPTION_CONTAINER_PADDING = 0.1f;

const float OPTION_VIDEO_MAIN_PADDING = 10.0f;
const float OPTION_VIDEO_LISTBOX_PRIORITY = 1.0f;
const float OPTION_VIDEO_LEFT_LABEL_PRIORITY = 1.0f;
const float OPTION_VIDEO_RESOLUTION_LABEL_HEIGHT = 32.0f;
const float OPTION_VIDEO_WINDOWED_PART_HEIGHT = 32.0f;

const float OPTION_GENERAL_LABEL_HEIGHT = 48.0f;
const float OPTION_GENERAL_PART_HEIGHT = 32.0f;
const float OPTION_GENERAL_PART_SIZE = 1.0f;
static const char* OPTION_LABELS_FONT					= "SERIF";
/***************************************************************
***************	OPTION WINDOW CREATION AND FUNCTIONS *************
****************************************************************/

CSubWindow *MainMenu::createOptionWindow()
{
	float width = max(WINDOW_WIDTH * m_fWidth,WINDOW_MINIMUM_WIDTH);
	CSubWindow *window = new CSubWindow( m_fWidth / 2.0f - width / 2.0f + WINDOW_POSITION_DIFFERENCE,
		WINDOW_Y_POSITION * m_fHeight + WINDOW_POSITION_DIFFERENCE,
			width,
			WINDOW_HEIGHT * m_fHeight);
	window->setCaption( textLibrary::getText( TXT_OPTION ) );

	ContainerRelativeUpToDown *container = new ContainerRelativeUpToDown();
	window->setComponent( container );
	window->setResizable( true );
	Tabs *newTabs = new Tabs( 0,0 );
	container->addComponent( newTabs, container->getScissorBox().h - BUTTON_HEIGHT );	//vlozeni zalozek
	
	int id = newTabs->addPanel( textLibrary::getText( TXT_KEYBOARD ) );
	createKeyBoardTab( id, newTabs );
	
	id = newTabs->addPanel( textLibrary::getText( TXT_MOUSE ) );
	createMouseTab( id, newTabs );

	id = newTabs->addPanel( textLibrary::getText( TXT_VIDEO ) );
	createVideoTab( id, newTabs );

	id = newTabs->addPanel( textLibrary::getText( TXT_GENERAL ) );
	createGeneralTab( id, newTabs );

	id = newTabs->addPanel( textLibrary::getText( TXT_HUD ) );
	createHUDTab( id, newTabs );
	

	ContainerRelativeLeftToRight *containerBottomLR = new ContainerRelativeLeftToRight();
	container->addComponent( containerBottomLR, BUTTON_HEIGHT );

	Button *button = GFG_newButton( textLibrary::getText( TXT_SAVE ), OPTION_BUTTON_SAVE );
	containerBottomLR->setVerticalPadding( 1.0f - 3.0f * BUTTON_WIDTH / containerBottomLR->getScissorBox().w );
	containerBottomLR->addComponent( button );

	button = GFG_newButton( textLibrary::getText( TXT_STANDARD ), OPTION_BUTTON_DEFAULT );
	containerBottomLR->addComponent( button );

	button = GFG_newButton( textLibrary::getText( TXT_CLOSE ), OPTION_BUTTON_CANCEL );
	containerBottomLR->addComponent( button );

	return window;
}



/*********************************************************
***********	OPTION KEYBOARD TAB DEFINITION	   ***********
*********************************************************/
void MainMenu::createKeyBoardTab( int id, Tabs *tabs )
{
	if( !tabs )
		return;

	ContainerRelativeUpToDown *container = new ContainerRelativeUpToDown();
	tabs->setComponent( id, container );
	ContainerRelativeLeftToRight *containerLabels = new ContainerRelativeLeftToRight();
	containerLabels->setVerticalPadding( OPTION_KEYBOARD_PANELS_PADDING );
	container->addComponent( containerLabels, TOP_LABELS_HEIGHT );
	containerLabels->setPaddingRight( GFG_LABEL_SCROLLBAR_WIDTH + GFG_STANDARD_PADDING );

	LabelNoOutline *label = new LabelNoOutline(0,0);
	label->setAlign( FONT_ALIGN_CENTER );
	label->setFont( OPTION_LABELS_FONT );
	label->setCaption( textLibrary::getText( TXT_COMMAND ) );
	containerLabels->addComponent( label, OPTION_KEYBOARD_LABEL_COMMAND_SIZE );

	label = new LabelNoOutline(0,0);
	label->setAlign( FONT_ALIGN_CENTER );
	label->setFont( OPTION_LABELS_FONT );
	label->setCaption( textLibrary::getText( TXT_KEY ) );
	containerLabels->addComponent( label, OPTION_KEYBOARD_LABEL_KEY_SIZE );

	label = new LabelNoOutline(0,0);
	label->setAlign( FONT_ALIGN_CENTER );
	label->setFont( OPTION_LABELS_FONT );
	label->setCaption( textLibrary::getText( TXT_ALTERNATIVE ) );
	containerLabels->addComponent( label, OPTION_KEYBOARD_LABEL_KEY_SIZE );

	m_pKeyMapList = new ListBox(0,0);
	container->addComponent( m_pKeyMapList, container->getScissorBox().h - TOP_LABELS_HEIGHT );

	createKeyboardOptionListPanels();
}


void MainMenu::createKeyboardOptionListPanels()
{
	copyCurrentWatchedCommandSettings();
	list<string> *tmpList = binding::getRightOrderList();
	if( !tmpList )
		return;
	for( list<string>::iterator iter = tmpList->begin(); iter != tmpList->end(); iter++ )
	{
		map<string,binding::Keys>::iterator Witer = m_OptionKeyMap.find( (*iter) );
		if( Witer != m_OptionKeyMap.end() )
		{
			KeyOptionPanel *newPanel = new KeyOptionPanel();
			newPanel->create( Witer->first, &Witer->second );
			m_pKeyMapList->addPanel( newPanel );
		}
	}
}

//nastavi hodnoty menu na systemove
void MainMenu::copyCurrentWatchedCommandSettings()
{
	binding::copyMapTo( m_OptionKeyMap );
}

void MainMenu::newKeyboardSet( int key )
{
	m_bKeySetMode = false;
	KeyOptionPanel *newChangePanel = reinterpret_cast<KeyOptionPanel*>( m_pKeyMapList->getSelectedPanel() );
	if( !newChangePanel )
		return;

	list<CBasePanel*> *tmpList = m_pKeyMapList->getPanelList();
	if( !tmpList )
		return;

	KeyOptionPanel *lastCommandPanel = NULL;
	KeyOptionPanel *tmpPanel = NULL;
	string sCommand = newChangePanel->m_sCommand;
	string sLastCommand;

	

	list<CBasePanel*>::iterator lastOne;
	for( lastOne = tmpList->begin(); lastOne != tmpList->end(); lastOne++ )
	{
		tmpPanel = reinterpret_cast<KeyOptionPanel*>( (*lastOne) );
		if( tmpPanel && tmpPanel->m_Keys.key_one == key || tmpPanel->m_Keys.key_two == key )
		{
			lastCommandPanel = tmpPanel;
			sLastCommand = lastCommandPanel->m_sCommand;
		}
		
	}

	binding::updateWatchedCommands( sCommand, key, m_OptionKeyMap, sLastCommand, m_bAlternative );
		

	newChangePanel->refresh();
	if( lastCommandPanel )
		lastCommandPanel->refresh();

}

void MainMenu::refreshKeyMapListBoxPanels()
{
	list<CBasePanel*> *tmpList = m_pKeyMapList->getPanelList();
	for( list<CBasePanel*>::iterator iter = tmpList->begin(); iter != tmpList->end(); iter++ )
	{
		KeyOptionPanel *tmpPanel = reinterpret_cast<KeyOptionPanel*>( (*iter) );
		if( tmpPanel )
			tmpPanel->refresh();
	}
}

/*********************************************************
***********	OPTION MOUSE TAB DEFINITION	   ***************
*********************************************************/

void MainMenu::refreshMouseTabComponents()
{
	if( m_pMouseSensitivity )
	{
		m_pMouseSensitivity->setValueSize( SENSITIVITY_MIN, SENSITIVITY_MAX - SENSITIVITY_MIN );
		m_pMouseSensitivity->setValue( gamevarsLibrary::getfData( SENSITIVITY_NAME ) );
	}

	if( m_pReverseHorizontaly )
	{
		m_pReverseHorizontaly->setSwitched( gamevarsLibrary::getiData( REVERSE_MOUSE_H_NAME ) == TRUE );
	}
	if( m_pReverseVerticaly )
	{
		m_pReverseVerticaly->setSwitched( gamevarsLibrary::getiData( REVERSE_MOUSE_V_NAME ) == TRUE );
	}

}

void MainMenu::loadDefaultMouseTabComponentsSettings()
{
	if( m_pMouseSensitivity )
	{
		m_pMouseSensitivity->setValueSize( SENSITIVITY_MIN, SENSITIVITY_MAX - SENSITIVITY_MIN );
		m_pMouseSensitivity->setValue( SENSITIVITY_DEFAULT );
	}

	if( m_pReverseHorizontaly )
	{
		m_pReverseHorizontaly->setSwitched( REVERSE_MOUSE_H_DEFAULT == TRUE );
	}
	if( m_pReverseVerticaly )
	{
		m_pReverseVerticaly->setSwitched( REVERSE_MOUSE_V_DEFAULT == TRUE );
	}
}






void MainMenu::createMouseTab( int id, Tabs *tab )
{
	if( !tab )
		return;
	
	ContainerRelativeUpToDown *container = new ContainerRelativeUpToDown();
	tab->setComponent( id, container );
	container->setPadding( OPTION_MOUSE_MAIN_PADDING );
	container->setHorizontalPadding( OPTION_CONTAINER_PADDING );
	Label *label = new Label();
	const float LABEL_PREFERED_SIZE = (container->getScissorBox().h - 2.0f * OPTION_MOUSE_CHECKBOX_HEIGHT - OPTION_MOUSE_SLIDER_HEIGHT) / 2.0f;
	container->addComponent( label, LABEL_PREFERED_SIZE );
	label->setCaption( textLibrary::getText( TXT_MOUSE_SENSITIVITY ) );

	m_pMouseSensitivity = new Slider(0,0);
	container->addComponent( m_pMouseSensitivity, OPTION_MOUSE_SLIDER_HEIGHT );
	m_pMouseSensitivity->setRoundPositions( OPTION_MOUSE_SLIDER_ROUND_POSITIONS );

	label = new Label();
	label->setCaption( textLibrary::getText( TXT_HERE_REVERSE_MOUSE ) );
	container->addComponent( label, LABEL_PREFERED_SIZE );

	m_pReverseHorizontaly = new CheckButton();
	m_pReverseHorizontaly->init();
	m_pReverseHorizontaly->setCaption( textLibrary::getText( TXT_REVERSE_HORIZONTALY ) );
	container->addComponent( m_pReverseHorizontaly, OPTION_MOUSE_CHECKBOX_HEIGHT );

	m_pReverseVerticaly = new CheckButton();
	m_pReverseVerticaly->init();
	m_pReverseVerticaly->setCaption( textLibrary::getText( TXT_REVERSE_VERTICALY ) );
	container->addComponent( m_pReverseVerticaly, OPTION_MOUSE_CHECKBOX_HEIGHT );
	refreshMouseTabComponents();	//nacte aktualni nastaveni a upravi tak vlastnosti komponent
}

/*********************************************************
***********	OPTION VIDEO TAB DEFINITION	   ***************
*********************************************************/

void MainMenu::createVideoTab( int id, Tabs *tab )
{
	if( !tab )
		return;
	
	ContainerRelativeLeftToRight *containerLR = new ContainerRelativeLeftToRight();
	tab->setComponent( id, containerLR );
	containerLR->setPadding( OPTION_VIDEO_MAIN_PADDING );

	
	//mainContainer->addComponent( containerLR, mainContainer->getScissorBox().h - OPTION_VIDEO_WINDOWED_PART_HEIGHT );
	
	containerLR->setVerticalPadding( OPTION_CONTAINER_PADDING );
	
	ContainerRelativeUpToDown *containerUD = new ContainerRelativeUpToDown();
	containerLR->addComponent( containerUD, OPTION_VIDEO_LISTBOX_PRIORITY );
	
	containerUD->setVerticalPadding( OPTION_CONTAINER_PADDING );
	containerUD->setHorizontalPadding( OPTION_CONTAINER_PADDING );
	
	SimpleLabel *label = new SimpleLabel(0,0);
	containerUD->addComponent( label, OPTION_VIDEO_RESOLUTION_LABEL_HEIGHT );
	label->setFont( "SERIF16" );
	label->setCaption( textLibrary::getText( TXT_RESOLUTION ) );

	m_pVideoModes = new ListBox(0,0);
	containerUD->addComponent( m_pVideoModes, containerUD->getScissorBox().h - OPTION_VIDEO_RESOLUTION_LABEL_HEIGHT - OPTION_VIDEO_WINDOWED_PART_HEIGHT );


	ScrollLabel *scrolllabel = new ScrollLabel(0,0);
	containerLR->addComponent( scrolllabel, OPTION_VIDEO_LEFT_LABEL_PRIORITY );
	scrolllabel->setCaption( textLibrary::getText( TXT_FOR_RESOLUTION_CHANGE ) );

	m_pWindowedBox = new CheckButton();
	m_pWindowedBox->init();
	m_pWindowedBox->setCaption( textLibrary::getText( TXT_RUN_IN_WINDOW ) );

	containerUD->addComponent( m_pWindowedBox, OPTION_VIDEO_WINDOWED_PART_HEIGHT );

	fillVideoModesList();
	refreshVideoSettings();
}

void MainMenu::fillVideoModesList()
{
	map<int,int> *videoModesMap = &systemconf::getVideoModes();
	for( map<int,int>::reverse_iterator iter = videoModesMap->rbegin(); iter != videoModesMap->rend(); iter++ )
	{
		VideoResolutionPanel *newPanel = new VideoResolutionPanel();
		newPanel->create( iter->first, iter->second );
		m_pVideoModes->addPanel( newPanel );
	
	}
}
void MainMenu::setActiveVideoModePanel( int w, int h )
{
	list<CBasePanel*> *tmpList = m_pVideoModes->getPanelList();
	VideoResolutionPanel *tmpVideoModePanel = NULL;
	for( list<CBasePanel*>::iterator iter = tmpList->begin(); iter != tmpList->end(); iter++ )
	{
		tmpVideoModePanel = reinterpret_cast<VideoResolutionPanel*>(*iter);
		if( tmpVideoModePanel && tmpVideoModePanel->m_iResolutionWidth == w && tmpVideoModePanel->m_iResolutionHeight == h)
		{
			m_pVideoModes->setSelectedPanel( iter );
			return;
		}
			
	}
}

void MainMenu::loadDefaultVideoSettings()
{
	int w = systemconf::DEFAULT_RESOLUTION_WIDTH;
	int h = systemconf::DEFAULT_RESOLUTION_HEIGHT;
	if( m_pWindowedBox )
		m_pWindowedBox->setSwitched( systemconf::DEFAULT_WINDOWEDMODE == TRUE );
	setActiveVideoModePanel( w, h );
}

void MainMenu::refreshVideoSettings()
{
	int w = systemconf::getSystemInfo().m_iResolutionWidth;
	int h = systemconf::getSystemInfo().m_iResolutionHeight;
	if( m_pWindowedBox )
		m_pWindowedBox->setSwitched( systemconf::getSystemInfo().m_iWindowedMode == TRUE );
	setActiveVideoModePanel( w, h );
}


/*********************************************************
***********	OPTION GENERAL TAB DEFINITION	**************
*********************************************************/

void MainMenu::createGeneralTab( int id, Tabs *tab )
{
	if( !tab )
		return;
	
	ContainerRelativeLeftToRight *containerLR = new ContainerRelativeLeftToRight();
	tab->setComponent( id, containerLR );
	containerLR->setPadding( OPTION_VIDEO_MAIN_PADDING );
	containerLR->setVerticalPadding( OPTION_CONTAINER_PADDING );

	ContainerRelativeUpToDown *containerUD = new ContainerRelativeUpToDown();
	containerUD->setHorizontalPadding( OPTION_CONTAINER_PADDING );
	containerLR->addComponent( containerUD, OPTION_GENERAL_PART_SIZE );
		
	SimpleLabel *simplelabel = new SimpleLabel(0,0);
	simplelabel->setFont( OPTION_LABELS_FONT );
	simplelabel->setCaption( textLibrary::getText(  TXT_LANGUAGE ) );
	containerUD->addComponent( simplelabel, OPTION_GENERAL_LABEL_HEIGHT );

	m_pLanguages = new ListBox(0,0);
	containerUD->addComponent( m_pLanguages, containerUD->getScissorBox().h - 2.0f * OPTION_GENERAL_PART_HEIGHT - OPTION_GENERAL_LABEL_HEIGHT );

	ScrollLabel *label = new ScrollLabel(0,0);
	//label->setFont( "SABIU" );
	label->setCaption( textLibrary::getText(  TXT_FOR_APLYING_LANGUAGE_CHANGES ) );
	containerLR->addComponent( label, OPTION_GENERAL_PART_SIZE );

	
	m_pSkipIntro = new CheckButton();
	m_pSkipIntro->init();
	m_pSkipIntro->setCaption( textLibrary::getText( TXT_SKIP_INTRO ) );
	containerUD->addComponent( m_pSkipIntro, OPTION_GENERAL_PART_HEIGHT );

	m_pDeveloperMode = new CheckButton();
	m_pDeveloperMode->init();
	m_pDeveloperMode->setCaption( textLibrary::getText( TXT_DEVELOPER_MODE ) );
	containerUD->addComponent( m_pDeveloperMode, OPTION_GENERAL_PART_HEIGHT );
	
	fillLanguages();
	refreshGeneralSettings();
}

void MainMenu::setActiveLanguage( std::string key )
{
	if( !m_pLanguages )
		return;

	list<CBasePanel*> *tmpList = m_pLanguages->getPanelList();
	LanguagePanel *tmplangPanel = NULL;
	for( list<CBasePanel*>::iterator iter = tmpList->begin(); iter != tmpList->end(); iter++ )
	{
		tmplangPanel = reinterpret_cast<LanguagePanel*>(*iter);
		if( tmplangPanel && tmplangPanel->m_sLanguage == key )
		{
			m_pLanguages->setSelectedPanel( iter );
			return;
		}
	}
}


void MainMenu::refreshGeneralSettings()
{
	setActiveLanguage( systemconf::getSystemInfo().m_sLanguage );
	if( m_pSkipIntro )
		m_pSkipIntro->setSwitched( systemconf::getSystemInfo().m_iSkipIntro == TRUE );

	if( m_pDeveloperMode )
		m_pDeveloperMode->setSwitched( systemconf::getSystemInfo().m_iDeveloper_mode == TRUE );
}

void MainMenu::loadDefaultGeneralSettings()
{
	setActiveLanguage( systemconf::DEFAULT_LANGUAGE );
	if( m_pSkipIntro )
		m_pSkipIntro->setSwitched( systemconf::DEFAULT_SKIPINTRO == TRUE );

	if( m_pDeveloperMode )
		m_pDeveloperMode->setSwitched( systemconf::DEFAULT_DEVELOPER == TRUE );
}

void MainMenu::fillLanguages()
{
	if( !m_pLanguages )
		return;

	map<string,textLibrary::Language> *languageMap = &textLibrary::getLanguageMap();
	for( map<string,textLibrary::Language>::iterator iter = languageMap->begin(); iter != languageMap->end(); iter++ )
	{
		LanguagePanel *newPanel = new LanguagePanel();
		newPanel->create( iter->first, iter->second.m_sLanguage );
		m_pLanguages->addPanel( newPanel );
	}
}


/*******************************/
/* IMPORTANT CONTROL FUNCTIONS */
/*******************************/

//ulozi aktualni nastaveni v okne Option
void MainMenu::saveOptionSettings()
{

	binding::setBindsFromWatchedCommandMap( &m_OptionKeyMap );
	binding::setMap( m_OptionKeyMap );

	if( m_pMouseSensitivity )
		gamevarsLibrary::setVariable( SENSITIVITY_NAME, m_pMouseSensitivity->getValue() );
	if( m_pReverseHorizontaly )
		gamevarsLibrary::setVariable( REVERSE_MOUSE_H_NAME, (int)m_pReverseHorizontaly->getSwitched() );
	if( m_pReverseVerticaly )
		gamevarsLibrary::setVariable( REVERSE_MOUSE_V_NAME, (int)m_pReverseVerticaly->getSwitched() );
	
	VideoResolutionPanel *targetResolution = reinterpret_cast<VideoResolutionPanel*>( m_pVideoModes->getSelectedPanel() );
	if( targetResolution )
	{
		systemconf::getSystemInfo().m_iResolutionWidth = targetResolution->m_iResolutionWidth;
		systemconf::getSystemInfo().m_iResolutionHeight = targetResolution->m_iResolutionHeight;
	}

	LanguagePanel *targetLanguage = reinterpret_cast<LanguagePanel*>( m_pLanguages->getSelectedPanel() );
	if( targetLanguage )
		systemconf::getSystemInfo().m_sLanguage = targetLanguage->m_sLanguage;
	
	if( m_pWindowedBox )
		systemconf::getSystemInfo().m_iWindowedMode = (int)m_pWindowedBox->getSwitched();

	if( m_pSkipIntro )
		systemconf::getSystemInfo().m_iSkipIntro = (int)m_pSkipIntro->getSwitched();

	if( m_pDeveloperMode )
		systemconf::getSystemInfo().m_iDeveloper_mode = (int)m_pDeveloperMode->getSwitched();
		
	systemconf::saveConfig( systemconf::SYSTEM_CONFIG_FILE );
	gameControl::game_control.saveConfig( gameControl::DEFAULT_CONFIG_FILE );


}

//funkce nacte standardni nastaveni systemu a hry
void MainMenu::loadDefaultOptionSettings()
{
	binding::loadDatabase( systemconf::WATCHED_COMMADS_FILE, &m_OptionKeyMap );
	refreshKeyMapListBoxPanels();
	loadDefaultMouseTabComponentsSettings();
	loadDefaultVideoSettings();
	loadDefaultGeneralSettings();
}


void MainMenu::createHUDTab( int id, Tabs *tab )
{
	if( !tab )
		return;
	
	ContainerRelativeUpToDown *container = new ContainerRelativeUpToDown();
	tab->setComponent( id, container );
	container->setPadding( OPTION_MOUSE_MAIN_PADDING );
	container->setHorizontalPadding( OPTION_CONTAINER_PADDING );
	Label *label = new Label();
	const float LABEL_PREFERED_SIZE = container->getScissorBox().h - BUTTON_HEIGHT;
	container->addComponent( label, LABEL_PREFERED_SIZE );
	label->setCaption( textLibrary::getText( TXT_HUD_DESCRIPTION ) );


	ContainerRelativeLeftToRight *containerLR = new ContainerRelativeLeftToRight();
	container->addComponent( containerLR, BUTTON_HEIGHT );
	containerLR->setVerticalPadding( (containerLR->getScissorBox().w - BUTTON_WIDTH) / containerLR->getScissorBox().w  );


	Button *newButton = GFG_newButton( textLibrary::getText( TXT_HUD_SETTINGS ), OPTION_BUTTON_HUDSET );
	containerLR->addComponent( newButton );
	
}






/************************************************************************
**************		KEY OPTION PANEL DEFINITION AND FUNCTIONS	*********
************************************************************************/

KeyOptionPanel::KeyOptionPanel()
{
	m_pMainButton = NULL;
	m_pAlternativeButton = NULL;
}

void KeyOptionPanel::create( std::string sCommand, binding::Keys *keys )
{
	if( keys == NULL )
		return;
	m_sCommand = sCommand;
	m_Keys.key_one = keys->key_one;
	m_Keys.key_two = keys->key_two;
	
	

	setHeight( OPTION_KEYBOARD_PANELS_HEIGHT );
	setPadding( 0 );
	ContainerRelativeLeftToRight *container = new ContainerRelativeLeftToRight();
	setComponent( container );
	//container->setPadding( 0 );
	container->setVerticalPadding( OPTION_KEYBOARD_PANELS_PADDING );
	SimpleLabel *label = new SimpleLabel(0,0);
	label->setFont( OPTION_LABELS_FONT );
	label->setPadding( 0.0f );
	label->setColorText( COLOR_PANEL_TEXT );
	label->setCaption( binding::getWatchedCommandName( m_sCommand ) );
	container->addComponent( label, OPTION_KEYBOARD_LABEL_COMMAND_SIZE );

	m_pMainButton = GFG_newButton( key_database::getKey( m_Keys.key_one ),OPTION_KEYBOARD_DEFINITION_MAIN );
	container->addComponent( m_pMainButton, OPTION_KEYBOARD_LABEL_KEY_SIZE );

	m_pAlternativeButton = GFG_newButton( key_database::getKey( m_Keys.key_two ),OPTION_KEYBOARD_DEFINITION_ALTERNATIVE );
	container->addComponent( m_pAlternativeButton, OPTION_KEYBOARD_LABEL_KEY_SIZE );
}

void KeyOptionPanel::refresh()
{
	binding::Keys *pKeys = main_menu.getKeysFromMap( m_sCommand );
		
	if( !pKeys )
		return;

	if( m_pMainButton && m_Keys.key_one != pKeys->key_one )
	{
		m_Keys.key_one = pKeys->key_one;
		m_pMainButton->setCaption( key_database::getKey( m_Keys.key_one ) );
	}

	if( m_pAlternativeButton && m_Keys.key_two != pKeys->key_two )
	{
		m_Keys.key_two = pKeys->key_two;
		m_pAlternativeButton->setCaption( key_database::getKey( m_Keys.key_two ) );
	}
}

/************************************************************************
**************		VIDEO OPTION PANEL DEFINITION AND FUNCTIONS	*********
************************************************************************/

VideoResolutionPanel::VideoResolutionPanel()
{
	m_iResolutionWidth = 0;
	m_iResolutionHeight = 0;
}

void VideoResolutionPanel::create( int width, int height )
{
	m_iResolutionWidth = width;
	m_iResolutionHeight = height;
	setHeight( MAINMENU_PANELS_HEIGHT );
	setPadding( 0 );
	SimpleLabel *label = new SimpleLabel(0,0);
	setComponent( label );
	label->setFont( OPTION_LABELS_FONT );
	label->setColorText( COLOR_PANEL_TEXT );
	ostringstream buf;
	buf << m_iResolutionWidth << "x" << m_iResolutionHeight;
	label->setCaption( buf.str() );
	buf.clear();
}

/************************************************************************
**************	LANGUAGE OPTION PANEL DEFINITION AND FUNCTIONS	*********
************************************************************************/

LanguagePanel::LanguagePanel()
{

}
		
void LanguagePanel::create( std::string key,  std::string lang )
{
	m_sLanguage = key;
	setHeight( MAINMENU_PANELS_HEIGHT );
	setPadding( 0 );
	SimpleLabel *label = new SimpleLabel(0,0);
	setComponent( label );
	label->setFont( OPTION_LABELS_FONT );
	label->setColorText( COLOR_PANEL_TEXT );
	label->setCaption( lang );
}



