//-----------------------------------------------------------------------------
// Handle events of GUI controls of the options menu.
//-----------------------------------------------------------------------------

function OptionsMenuDlg::onWake(%this)
{
//   %pXml = loadMenuXML();
//   parseOptionsMenuXML(%pXml);
//   %pXml.delete();
   
   if($pref::Video::fullScreen $= "true" || $pref::Video::fullScreen $= 1)
   {
      YesTick.setVisible(true);
      NoTick.setVisible(false);
   }
   else
   {
      YesTick.setVisible(false);
      NoTick.setVisible(true);
   }
/*      
   if($pref::MeterPositionRight $= "true" || $pref::MeterPositionRight $= 1)
      DirectorMeterButtonText.text = $DirectorMeterButtonTextRight;
   else
      DirectorMeterButtonText.text = $DirectorMeterButtonTextLeft;
      
   if($pref::NShoppingListItems5 $= "true" || $pref::NShoppingListItems5 $= 1)
      ShoppingListButtonText.text = $NShoppingListItemsButtonText5;
   else
      ShoppingListButtonText.text = $NShoppingListItemsButtonText7;
      
   if($pref::CometEffectOn $= "true" || $pref::CometEffectOn $= 1)
      CometEffectButtonText.text = $CometEffectButtonTextOn;
   else
      CometEffectButtonText.text = $CometEffectButtonTextOff;
*/

   OPBackMenuButton.setVisible(true);
   OPOKButton.setVisible(false);

   %this.changeNow = false;

   SFXVolSlider.setValue($pref::SFX::channelVolume[$GuiAudioType]);
   MusicVolSlider.setValue($pref::SFX::channelVolume[$MusicAudioType]);
   
   %this.changeNow = true;   
   
   Canvas.setWidescreen(OptionsMenuDlgCenter, 100, false);
}

function OptionsMenuDlg::playButtonPressSound(%this)
{
   sfxPlay(AudioButtonPress);
}

function OptionsMenuDlg::onResize(%this, %width, %height)
{
//   %fontSize = 36 * Canvas.scaleSize; if(%fontSize > 62) %fontSize = 62;
//   GuiLCAButtonProfile2.fontSize = %fontSize;
//   %fontSize = 40 * Canvas.scaleSize; if(%fontSize > 62) %fontSize = 62;   
//   GuiLCAButtonProfile.fontSize = %fontSize;
//   GuiLCATextProfile.fontSize = %fontSize;
           
//   FullScreenButton.setProfile(GuiDefaultProfile);
//   DirectorMeterButton.setProfile(GuiDefaultProfile);
//   CometEffectButton.setProfile(GuiDefaultProfile);
//   ShoppingListButtonText.setProfile(GuiDefaultProfile);
//   MainMenuOptButton.setProfile(GuiDefaultProfile);
//   SFXVolText.setProfile(GuiDefaultProfile);
//   MusicVolText.setProfile(GuiDefaultProfile);
   
//   FullScreenButton.setProfile(GuiLCAButtonProfile);
//   DirectorMeterButton.setProfile(GuiLCAButtonProfile);
//   CometEffectButton.setProfile(GuiLCAButtonProfile);
//   ShoppingListButtonText.setProfile(GuiLCAButtonProfile);
//   MainMenuOptButton.setProfile(GuiLCAButtonProfile2);   
//   SFXVolText.setProfile(GuiLCATextProfile);
//   MusicVolText.setProfile(GuiLCATextProfile);
   
   Canvas.setWidescreen(OptionsMenuDlgCenter, 2000, true);
}
/*
// Parse XML file looking for menu text tags
function parseOptionsMenuXML(%pXml)
{
   if(!isObject(%pXml))
      return;
      
   // Push the first child element named "options" onto the XML document stack.
   if(!%pXml.pushFirstChildElement("options"))
   {
      echo("Failed to load 'options' tag.");
      return;
   }
   
   if(%pXml.pushFirstChildElement("fullscreenyes"))
   {
      $FullScreenButtonTextYes = %pXml.attribute("text");
      %pXml.popElement();
   }

   if(%pXml.pushFirstChildElement("fullscreenno"))
   {
      $FullScreenButtonTextNo = %pXml.attribute("text");
      %pXml.popElement();
   }
  
   if(%pXml.pushFirstChildElement("directormeterleft"))
   {
      $DirectorMeterButtonTextLeft = %pXml.attribute("text");
      %pXml.popElement();
   }

   if(%pXml.pushFirstChildElement("directormeterright"))
   {
      $DirectorMeterButtonTextRight = %pXml.attribute("text");
      %pXml.popElement();
   }
   
   if(%pXml.pushFirstChildElement("cometeffecton"))
   {
      $CometEffectButtonTextOn = %pXml.attribute("text");
      %pXml.popElement();
   }
   
   if(%pXml.pushFirstChildElement("cometeffectoff"))
   {
      $CometEffectButtonTextOff = %pXml.attribute("text");
      %pXml.popElement();
   }
   
   if(%pXml.pushFirstChildElement("shoppinglist5"))
   {
      $NShoppingListItemsButtonText5 = %pXml.attribute("text");
      %pXml.popElement();
   }
   
   if(%pXml.pushFirstChildElement("shoppinglist7"))
   {
      $NShoppingListItemsButtonText7 = %pXml.attribute("text");
      %pXml.popElement();
   }

   if(%pXml.pushFirstChildElement("sfxvolume"))
   {
      SFXVolText.text = %pXml.attribute("text");
      %pXml.popElement();
   }

   if(%pXml.pushFirstChildElement("musicvolume"))
   {
      MusicVolText.text = %pXml.attribute("text");
      %pXml.popElement();
   }
   
   if(%pXml.pushFirstChildElement("back"))
   {
      MainMenuOptButtonText.text = %pXml.attribute("text");
      %pXml.popElement();
   }
}

// Stop grow/shrink animation of all menu UI elements
function StopMenuShrinkGrowOptionsMenu()
{
   StopShrinkGrowGUI(FullScreenButton); 
//   StopShrinkGrowGUI(DirectorMeterButton);    
//   StopShrinkGrowGUI(CometEffectButton);      
//   StopShrinkGrowGUI(MainMenuOptButton);   
}

function FullScreenButton::onMouseEnter(%this)
{
   FullScreenButtonText.color = "255 128 0 255";
   
   StopMenuShrinkGrowOptionsMenu();
   InitShrinkGrowGUI(%this);
}

function FullScreenButton::onMouseLeave(%this)
{
   FullScreenButtonText.color = "255 255 255 255";
}

function MainMenuOptButton::onMouseEnter(%this)
{
   MainMenuOptButtonText.color = "255 128 0 255";
   
   StopMenuShrinkGrowOptionsMenu();
   InitShrinkGrowGUI(%this);
}

function MainMenuOptButton::onMouseLeave(%this)
{
   MainMenuOptButtonText.color = "255 0 0 255";
}

function DirectorMeterButton::onMouseEnter(%this)
{
   DirectorMeterButtonText.color = "255 128 0 255";
}

function DirectorMeterButton::onMouseLeave(%this)
{
   DirectorMeterButtonText.color = "255 255 255 255";
}

function CometEffectButton::onMouseEnter(%this)
{
   CometEffectButtonText.color = "255 128 0 255";
}

function CometEffectButton::onMouseLeave(%this)
{
   CometEffectButtonText.color = "255 255 255 255";
}

function ShoppingListButton::onMouseEnter(%this)
{
   ShoppingListButtonText.color = "255 128 0 255";
}

function ShoppingListButton::onMouseLeave(%this)
{
   ShoppingListButtonText.color = "255 255 255 255";
}


function MusicVolSlider::onMouseEnter(%this)
{
   StopMenuShrinkGrowOptionsMenu();   
}

function SFXVolSlider::onMouseEnter(%this)
{
   StopMenuShrinkGrowOptionsMenu();   
}

function Options::AudioUpdateMasterVolume(%volume)
{
   if (%volume == $pref::SFX::masterVolume)
      return;
      
   sfxSetMasterVolume(%volume);
   $pref::SFX::masterVolume = %volume;
   
   if (!isObject($AudioTestHandle))
      $AudioTestHandle = sfxPlayOnce( ZeroChannel, expandFilename( "~/data/sound/testing.wav" ) );
}
*/
function Options::AudioUpdateSFXVolume(%volume)
{
   // Options were changed
   if(OptionsMenuDlg.changeNow)
   {   
      OPBackMenuButton.setVisible(false);
      OPOKButton.setVisible(true);
   }
   
   %channel1 = $GuiAudioType;   
   %channel2 = $SimAudioType;
   
   sfxSetChannelVolume(%channel1, %volume);
   sfxSetChannelVolume(%channel2, %volume);   
   $pref::SFX::channelVolume[%channel1] = %volume;
   $pref::SFX::channelVolume[%channel2] = %volume;
   
   if (!isObject($AudioTestHandle))
      $AudioTestHandle = sfxPlayOnce(AudioButtonPress);
}

function Options::AudioUpdateMusicVolume(%volume)
{
   // Options were changed
   if(OptionsMenuDlg.changeNow)
   {   
      OPBackMenuButton.setVisible(false);
      OPOKButton.setVisible(true);
   }
   
   sfxSetChannelVolume($MusicAudioType, %volume);
   $pref::SFX::channelVolume[$MusicAudioType] = %volume;
}

function ApplyGraphics()
{
   // Options were changed
   if(OptionsMenuDlg.changeNow)
   {   
      OPBackMenuButton.setVisible(false);
      OPOKButton.setVisible(true);
   }
   
   %newFullScreen = $pref::Video::fullScreen;
   
   if(%newFullScreen $= "true" || %newFullScreen $= 1)
   {
      %deskRes    = getFullscreenResolution();      
      %deskResX   = getWord(%deskRes, $WORD::RES_X);
      %deskResY   = getWord(%deskRes, $WORD::RES_Y);
      %aspectRatio = %deskResX / %deskResY;

      if(%aspectRatio > 4/3)
         %newRes = %deskResX SPC %deskResY;
         //%newRes = $pref::Video::ResolutionW;
      else
	      %newRes = $pref::Video::Resolution;
   }
   else
	   %newRes = $pref::Video::windowedRes;
	   
	%newBpp        = $pref::Video::BitsPerPixel;
	%newRefresh    = $pref::Video::RefreshRate;
	%newFSAA       = $pref::Video::FSAALevel;
	%newDevice     = $pref::Video::displayDevice;
   $pref::Video::displayDevice = %newDevice;

   %newMode = %newRes SPC %newFullScreen SPC %newBpp SPC %newRefresh SPC %newFSAA;
   if(%newMode !$= $pref::Video::mode)
   {
      $pref::Video::mode = %newMode;
      configureCanvas();
   }
}

// Toggle between windowed mode and full screen mode
function Options::ToggleFullScreen(%this, %full)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   if(%full $= "")
      $pref::Video::fullScreen = !$pref::Video::fullScreen;
   else
      $pref::Video::fullScreen = %full;   

   if($pref::Video::fullScreen $= "true" || $pref::Video::fullScreen $= 1)
   {
      YesTick.setVisible(true);
      NoTick.setVisible(false);
   }
   else
   {
      YesTick.setVisible(false);
      NoTick.setVisible(true);      
   }
      
   ApplyGraphics();
   
   GuiCanvas::onResize(getWord(Canvas.Extent,0), getWord(Canvas.Extent,1));
}
/*
function Options::ToggleDirectorMeter(%this)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   $pref::MeterPositionRight = !$pref::MeterPositionRight;

   if($pref::MeterPositionRight $= "true" || $pref::MeterPositionRight $= 1)
      DirectorMeterButtonText.text = $DirectorMeterButtonTextRight;
   else
      DirectorMeterButtonText.text = $DirectorMeterButtonTextLeft;
}

function Options::ToggleShoppingList(%this)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   $pref::NShoppingListItems5 = !$pref::NShoppingListItems5;

   if($pref::NShoppingListItems5 $= "true" || $pref::NShoppingListItems5 $= 1)
      ShoppingListButtonText.text = $NShoppingListItemsButtonText5;
   else
      ShoppingListButtonText.text = $NShoppingListItemsButtonText7;
}

function Options::ToggleCometEffect(%this)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   $pref::CometEffectOn = !$pref::CometEffectOn;

   if($pref::CometEffectOn $= "true" || $pref::CometEffectOn $= 1)
      CometEffectButtonText.text = $CometEffectButtonTextOn;
   else
      CometEffectButtonText.text = $CometEffectButtonTextOff;
} */