var lastResultStatus;
var lastResultSpeed;

var startedSuccessfull = false;

function statusStatus()
{
  if (wAnalyzeResult)
  {
    lastResultStatus = parseInt(wAnalyzeResult.LoadState());
    switch (lastResultStatus)
    {
      case 0:
        if (wAnalyzeResult.CriticalCount)
        {
          return 'critical';
        }
        else if (wAnalyzeResult.WarningCount)
        {
          return 'warning';
        }
        else
        {
          return 'good';
        }
        break;
      case 30:
        return 'critical';
        break;
      default:
        return 'warning';
        break;
    }
  }
  else
  {
    return 'critical';
  }
}

function speedStatus()
{
  if (wAnalyzeResult)
  {
    lastResultSpeed = parseInt(wAnalyzeResult.LoadSpeedOptimizerState());
    switch (lastResultSpeed)
    {
      case 0:
        if (wAnalyzeResult.speedOptimizerQuestionnaire == 0)
        {
          return 'warning';
        }
        else if (wAnalyzeResult.SpeedOptimizerVisibleCount)
        {
          return 'warning';
        }
        else
        {
          return 'good';
        }
        break;
      case 30:
        return 'critical';
        break;
      default:
        return 'warning';
        break;
    }
  }
  else
  {
    return 'critical';
  }
}

function maintenanceStatus()
{
  if (wAnalyzeResult)
  {
    wAnalyzeResult.LoadOCMState();
    
    switch (wAnalyzeResult.OCMTaskWorking)
    {
      case 0:
          return 'good';
          break;
      case 1:
          return 'warning';
          break;
      case 2:
          return 'critical';
          break;
      default:
        return 'good';
    }
  }
  else
  {
    return 'critical';
  }
}

function TotalState()
{
  var SystemState = statusStatus();
  var SpeedState = speedStatus();
  var OCMState = maintenanceStatus();
  
  if ((SystemState == 'critical') || (SpeedState == 'critical') || (OCMState == 'critical'))
  {
    return 'critical';
  }
  else if ((SystemState == 'warning') || (SpeedState == 'warning') || (OCMState == 'warning'))
  {
    return 'warning';
  }
  else
  {
    return 'good';
  }
}

var BalloonInit = false;

function highspeedInit()
{
  SetChangeDescription(TurboButton);
  if(isInAction)
  {
    $('.turboballoon').stop(false, true).stop(false, true);
    $('.turboballoon').show();
  }
  else //triggered outside of gadget
  {
    isInAction = true;
    $('#tuning_standard').removeClass('inactive active unknown').addClass('inactive');
    $('#tuning_economy').removeClass('inactive active unknown').addClass('inactive');
    ActivePressedButton = $('#tuning_turbo'); 
    ActivePressedButton.find('.clicked').show();
  }
  BalloonInit = true;
}

function defaultInit()
{
  SetChangeDescription(NormalButton);
  if(isInAction)
  {
    $('.defaultballoon').stop(false, true).stop(false, true);
    $('.defaultballoon').show();
  }
  else //triggered outside of gadget
  {
    isInAction = true;
    $('#tuning_economy').removeClass('inactive active unknown').addClass('inactive');
    $('#tuning_turbo').removeClass('inactive active unknown').addClass('inactive');
    ActivePressedButton = $('#tuning_standard'); 
    ActivePressedButton.find('.clicked').show();
  }
  BalloonInit = true;
}

function ecomodeInit()
{
  SetChangeDescription(EcoButton);
  if(isInAction)
  {
    $('.ecoballoon').stop(false, true).stop(false, true);
    $('.ecoballoon').show();
  }
  else //triggered outside of gadget
  {
    isInAction = true;
    $('#tuning_standard').removeClass('inactive active unknown').addClass('inactive');
    $('#tuning_turbo').removeClass('inactive active unknown').addClass('inactive');
    ActivePressedButton = $('#tuning_economy'); 
    ActivePressedButton.find('.clicked').show();
  }
  BalloonInit = true;
}

function watchdogTimer()
{
  if(!startedSuccessfull)
  {
    if(wAnalyzeResult)
    {
      if(!(wAnalyzeResult.IsActionInProgress))
      {
        SetNewButtonState();
      }
    }
  }
}

function highspeedToggle()
{
  startedSuccessfull = false;
  isInAction = true;
  if(wAnalyzeResult)
  {
    if (wAnalyzeResult.IsTrialOutdated == 1)
    {
      execApp('SettingCenter.exe', '/live');
    }
    else
    {
      if ((wAnalyzeResult.IsTurboModeActive == 0) ||
          (wAnalyzeResult.IsTurboModeActive == 1)
      )
      {
        wAnalyzeResult.SwitchTurboMode();
        setTimeout("watchdogTimer()", 5000); //nach 5 Sekunden sollte der Prozess laufen
        return;
      }
      else if (wAnalyzeResult.IsTurboModeActive == 2)
      {
        execApp('SettingCenter.exe', '-turbo -wizard');
      }
      
    }
  }
  
  SetNewButtonState();
}

function EcoModeToggle()
{
  startedSuccessfull = false;
  isInAction = true;
  if(wAnalyzeResult)
  {
    if (wAnalyzeResult.IsTrialOutdated == 1)
    {
      execApp('SettingCenter.exe', '/live');
    }
    else
    {
      if ((wAnalyzeResult.IsEcoModeActive == 0) ||
          (wAnalyzeResult.IsEcoModeActive == 1)
      )
      {
        wAnalyzeResult.SwitchEcoMode();
        setTimeout("watchdogTimer()", 5000); //nach 5 Sekunden sollte der Prozess laufen
        return;
      }
      else if (wAnalyzeResult.IsEcoModeActive == 2)
      {
        execApp('EnergyOptimizer.exe', 'StartEcoMode');
      }
      
    }
  }
  
  SetNewButtonState();
}

function SetNewButtonState()
{
  isInAction = false;
  
  if (ActivePressedButton)
  {
    ActivePressedButton.find('.clicked').stop(false, true).stop(false, true).fadeIn(200).fadeOut(100);
    ActivePressedButton = null;
  }
  
  if(isTurboActive)
  {
    $('#tuning_economy').removeClass('inactive active unknown').addClass('inactive');
    $('#tuning_standard').removeClass('inactive active unknown').addClass('inactive');
    $('#tuning_turbo').removeClass('inactive active unknown').addClass('active');
  }
  else if(isEcoModeActive)
  {
    $('#tuning_economy').removeClass('inactive active unknown').addClass('active');
    $('#tuning_standard').removeClass('inactive active unknown').addClass('inactive');
    $('#tuning_turbo').removeClass('inactive active unknown').addClass('inactive');
  }
  else
  {
    $('#tuning_economy').removeClass('inactive active unknown').addClass('inactive');
    $('#tuning_standard').removeClass('inactive active unknown').addClass('active');
    $('#tuning_turbo').removeClass('inactive active unknown').addClass('inactive');
  }
  
  SetTurboDescription();
}

function highspeedDone()
{
  if(wAnalyzeResult)
  {
    isTurboActive = (wAnalyzeResult.IsTurboModeActive == 1);
    isEcoModeActive = (wAnalyzeResult.IsEcoModeActive == 1);
  }
  
  SetNewButtonState();
  if($('.turboballoon').is(':visible'))
  {
    $('.turboballoon').stop(false, true).stop(false, true).fadeIn(200).fadeOut(150);
    $('.turboballoon').hide();
  }
  
  if($('.defaultballoon').is(':visible'))
  {
    $('.defaultballoon').stop(false, true).stop(false, true).fadeIn(200).fadeOut(150);
    $('.defaultballoon').hide();
  }

  if($('.ecoballoon').is(':visible'))
  {
    $('.ecoballoon').stop(false, true).stop(false, true).fadeIn(200).fadeOut(150);
    $('.ecoballoon').hide();
  }
  
  BalloonInit = false;
}

function highspeedProgress(percent)
{
  if((percent > 0) && (BalloonInit))
    BalloonInit = false; //flag not needed anymore
    
  var backgroundImageXPosition = "" + Math.round(89 * percent / 100.0) + "px 0px";
  if($('.ecoballoon').is(':visible'))
    $('.ecoballoon').find('.bar').css("background-position", backgroundImageXPosition);
  else if($('.defaultballoon').is(':visible'))
    $('.defaultballoon').find('.bar').css("background-position", backgroundImageXPosition);
  else if($('.turboballoon').is(':visible'))
    $('.turboballoon').find('.bar').css("background-position", backgroundImageXPosition);
}

function updateTurboEvent(percentOfProcess, bActivation)
{
  if (percentOfProcess == 0)
  {
    startedSuccessfull = true;
    if((bActivation == 1) && (!BalloonInit))
    {
      highspeedInit();
    }
    if((bActivation > 1) && (!BalloonInit))
    {
      ecomodeInit();
    }
    else if((bActivation == 0) && (!BalloonInit))
    {
      defaultInit();
    }
  }
  
  highspeedProgress(percentOfProcess);

  if (percentOfProcess == 100)
  {
    setTimeout("highspeedDone()", 500);
  }
}