var gameoptimizerValue;
var mouseInMap = false;
var gameoptimizerTimeout;
var gameoptimizerHasFocus;
var mousedownPositionX;
var mousedownPositionY;
var isInMovemode;

function initAnimationsHighspeed()
{
  $('#gameoptimizerMap').mouseenter(
    function ()
    {
      mouseInMap = true;
      $('#gameoptimizer .gameoptimizerstatehover').stop(false, true).fadeIn();
    }
  );

  $('#gameoptimizerMap').mousedown(
    function (e)
    {
      mousedownPositionX = e.pageX;
      mousedownPositionY = e.pageY;
      $('#gameoptimizer .gameoptimizerstatemousedown').stop(false, true).fadeIn();
    }
  );

  $('#gameoptimizerMap').mouseleave(
    function ()
    {
      mouseInMap = false;
      $('#gameoptimizer .gameoptimizerstatemousedown').stop(false, true).fadeOut();
      $('#gameoptimizer .gameoptimizerstatehover').stop(false, true).fadeOut();
    }
  );

  $('#gameoptimizerMap').mousemove(
    function (e)
    {
      if (mousedownPositionX && mousedownPositionY &&
          (
            (e.pageX >= (mousedownPositionX + 5)) ||
            (e.pageX <= (mousedownPositionX - 5)) ||
            (e.pageY >= (mousedownPositionY + 5)) ||
            (e.pageY <= (mousedownPositionY - 5))
          )
      )
      {
        isInMovemode = true;
      }
      else if (isInMovemode)
      {
        isInMovemode = false;
        $('#gameoptimizer .gameoptimizerstatemousedown').fadeOut();
        mousedownPositionX = mousedownPositionY = false;
      }
    }
  );

  $('#gameoptimizerMap').mouseup(
    function (e)
    {
      $('#gameoptimizer .gameoptimizerstatemousedown').fadeOut();
    }
  );

  $('#gameoptimizerMap').click(
    function()
    {
      highspeedToggle();
    }
  );

  $('#gameoptimizer .gameoptimizerstate').focus(
    function ()
    {
      gameoptimizerHasFocus = true;
    }
  );
  $('#gameoptimizer .gameoptimizerstate').blur(
    function ()
    {
      gameoptimizerHasFocus = false;
    }
  );

  $(document).keydown(
    function (e)
    {
      if ((gameoptimizerHasFocus == true) && (e.keyCode == 13))
      {
        highspeedToggle();
      }
    }
  );
}

function highspeedToggle()
{
  if (wAnalyzeResult.IsTrialOutdated == 1)
  {
    execApp('LiveOptimizer.exe', '/nag');
  }
  else
  {
    if ((wAnalyzeResult.IsTurboModeActive == 0) ||
        (wAnalyzeResult.IsTurboModeActive == 1)
    )
    {
      highspeedInit();
      wAnalyzeResult.SwitchTurboMode();
    }
    else if (wAnalyzeResult.IsTurboModeActive == 2)
    {
      execApp('TurboConfig.exe', '/turbofirststart');
    }
    else
    {
      hideFlyout();
      showFlyout('highspeed');
    }
  }
}

function highspeedInit()
{
  if (!highspeedInProgress)
  {
    gameoptimizerValue = !gameoptimizerValue;

    $('.gameoptimizerstatemotion').stop(false, true).animate({'opacity': 1}, 200, 'linear');
  }
}

function highspeedDone(endValue)
{
  gameoptimizerValue = endValue;
  $('#gameoptimizer .gameoptimizerstatemotion').css('filter', 'alpha(opacity=100)');
  $('#gameoptimizer .gameoptimizerstatemotion').animate({'opacity': 0}, 200, 'linear').stop(false, true);
  if (endValue)
  {
    $('#gameoptimizer .gameoptimizerstate').css('background-image', 'url(images/turbo_on.png)');
    $('#gameoptimizer .gameoptimizerstatemotion').css('background-position-y', '0px');
    $('#gameoptimizer .gameoptimizerstatemotion').css('background-image', 'url(images/turbo_deactivate.png)');
    $('#gameoptimizer .gameoptimizerstatehover').css('background-image', 'url(images/turbo_on_hover.png)');
    $('#gameoptimizer .gameoptimizerstatemousedown').css('background-image', 'url(images/turbo_on_onclick.png)');
  }
  else
  {
    $('#gameoptimizer .gameoptimizerstate').css('background-image', 'url(images/turbo_off.png)');
    $('#gameoptimizer .gameoptimizerstatemotion').css('background-position-y', '0px');
    $('#gameoptimizer .gameoptimizerstatemotion').css('background-image', 'url(images/turbo_activate.png)');
    $('#gameoptimizer .gameoptimizerstatehover').css('background-image', 'url(images/turbo_off_hover.png)');
    $('#gameoptimizer .gameoptimizerstatemousedown').css('background-image', 'url(images/turbo_off_onclick.png)');
  }
  highspeedInProgress = false;
}

function highspeedProgress(percent)
{
  var amountImages = 24;
  var percentalImage = (amountImages * (percent / 100));
  var availableImages = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23);
  var showImage = availableImages[0];
  for (var x in availableImages)
  {
    if (percentalImage >= availableImages[x])
    {
      showImage = availableImages[x];
    }
    else
    {
      break;
    }
  }
  var position = showImage*(-48);
  $('#gameoptimizer .gameoptimizerstatemotion').css('background-position-y', (position + 'px'));
}

var highspeedInProgress = false;
function updateTurboEvent(percentOfProcess, bActivation)
{
  if (!highspeedInProgress)
  {
    highspeedInit();
    highspeedInProgress = true;
  }

  highspeedProgress(percentOfProcess);

  if (percentOfProcess == 100)
  {
    highspeedDone(wAnalyzeResult.IsTurboModeActive == 1);
  }
}

function highspeedStatus()
{
  return 'highspeed';
}

function highspeedTextStatus()
{
  return translate('<localized>Der Turbo-Modus kann nicht gestartet werden, da der TuneUp Dienst für den Turbo-Modus deaktiviert ist.</localized>');
}

function highspeedLinkStatus()
{
  return '';
}