var mouseleaveTimeout;
function initTUAnalyzeResult()
{
  if (typeof(wAnalyzeResult) == "undefined") {
    try
    {
      wAnalyzeResult = new ActiveXObject("TUAnalyzeInfo.TUAnalyzeResult");
      if (parseInt(wAnalyzeResult.LoadState()) == 110)
      {
        $('#loaderMain').show();
        startAnalyze();
      }
      else
      {
        updateStatus();
      }
    }
    catch (e)
    {
      $('#loaderMain,#pillarName,#pillarNameOverlay').hide();
      $('.errormessage').show();
    }
  }
}

function toggleFlyoutMaintenance() 
{
  toggleFlyout('maintenance');
}

function toggleFlyoutSpeed() 
{
  toggleFlyout('speed');
}

function toggleFlyoutStatus() 
{
  toggleFlyout('status');
}

function toggleFlyoutPillarName()
{
  var flyoutId = lastId;
  if ((flyoutId == 'maintenance') ||
      (flyoutId == 'speed') ||
      (flyoutId == 'status')
  )
  {
    eval('toggleFlyout' + flyoutId.substr(0, 1).toUpperCase() + flyoutId.substr(1) + '();');
  }
}

var lastFlyout;
var intervalLoadFlyout;
function toggleFlyout(id)
{
  if (System.Gadget.Flyout.show &&
      lastFlyout == id)
  {
    System.Gadget.Flyout.show = false;
    // triggers hideFlyout
  }
  else
  {
    execApp('Integrator.exe', '/initstatuspage');
    //showFlyout(id);
  }
}

function showFlyout(id)
{
  $('.buttons .clicked').stop(false, true).fadeOut(150);
  $('#' + id + ' .clicked').stop(false, true).stop(false, true).fadeOut(150).fadeIn(150);
  setPillarName(id, 'clicked');
  clearInterval(intervalLoadFlyout);
  if (System.Gadget.Flyout.show)
  {
    intervalLoadFlyout = setInterval(
      function ()
      {
        if (System.Gadget.Flyout.document.parentWindow.loadContent(id))
        {
          clearInterval(intervalLoadFlyout);
        }
      },
      100
    );
  }
  else
  {
    System.Gadget.Flyout.file = 'flyout.html';
    System.Gadget.Flyout.show = true;
    intervalLoadFlyout = setInterval(
      function ()
      {
        if (System.Gadget.Flyout.document.parentWindow.loadContent(id))
        {
          clearInterval(intervalLoadFlyout);
        }
      },
      100
    );
  }
  lastFlyout = id;
  pillarIdFallback = id;
}

function hideFlyout()
{
  lastFlyout = false;
  $('.buttons .clicked:visible').stop(false, true).stop(false, true).fadeIn(150).fadeOut(150);
  $('.buttons .hover:visible').stop(false, true).stop(false, true);
  if ($('.buttons .hover:visible').size() > 0)
  {
    setPillarName($('.buttons .hover:visible').parent().parent().attr('id'), 'hover');
  }
  else
  {
    setPillarName();
  }
}

function getPillarName(id)
{
  switch (id)
  {
    case 'maintenance':
      return translate('<localized>System warten</localized>');
      break;
    case 'speed':
      return translate('<localized>Leistung steigern</localized>');
      break;
    case 'status':
      return translate('<localized>Probleme beheben</localized>');
      break;
  }
  return '';
}

function changeStatus(id, classname)
{
  $('#' + id).removeClass('good warning critical unknown').addClass(classname);
}

function updateStatus(updatetype)
{
  if (updatetype == 2)
  {
    //Speedoptimizer scan will start now after filling out questionaire
    setSpeedLoading();
  }
  else
  {
    $('#analyzeLink .loading').hide();
    $('#loaderMain.loading').hide();
    $('.buttons').show();
    $('.checkInfo').show();
    speedIsLoading = false;
    changeStatus('maintenance', maintenanceStatus());
    changeStatus('speed', speedStatus());
    changeStatus('status', statusStatus());

    var highspeedStatus = (wAnalyzeResult.IsTurboModeActive == 1);
    highspeedDone(highspeedStatus);
    gameoptimizerValue = highspeedStatus;
    $('#gameoptimizer').show();

    if (lastFlyout)
    {
      showFlyout(lastFlyout);
    }
  }
}

function closeGadget()
{
  System.Gadget.close();
}


function startAnalyze()
{
  $('#analyzeLink .loading').show();
  wAnalyzeResult.StartAnalyze();
}

var speedIsLoading = false;
function setSpeedLoading()
{
  speedIsLoading = true;
}

function initEvent()
{
  if (wAnalyzeResult)
  {
    wAnalyzeResult.RegisterOnClose(closeGadget);
    wAnalyzeResult.RegisterOnChange(updateStatus);
    wAnalyzeResult.RegisterOnChangeTurbo(updateTurboEvent);
  }
}

function cleanupEvent()
{
  if (wAnalyzeResult)
  {
    wAnalyzeResult.UnRegisterOnChange();
    wAnalyzeResult.UnRegisterOnClose();
    wAnalyzeResult.UnRegisterOnChangeTurbo();
  }
}

var lastState;
var lastId;
function setPillarName(id, status)
{
  lastId = id;
  if (id == 'fallback')
  {
    // search if something is clicked
    $('.buttons .clicked').stop(false, true).stop(false, true);
    var clickedVisible = $('.buttons .clicked:visible');
    if (clickedVisible.size() > 0)
    {
      id = clickedVisible.parent().parent().attr('id');
      lastId = id;
      var pillarName = getPillarName(id);
      status = 'clicked';
    }
    else
    {
      var pillarName = '';
    }
  }
  else if (id)
  {
    var pillarName = getPillarName(id);
  }
  else
  {
    var pillarName = '';
  }

  $('#pillarName, #pillarNameOverlay').css('cursor', (pillarName == '' ? 'none' : 'pointer'));

  $('#pillarNameOverlay').stop(false, true).stop(false, true);
  if ((pillarName != $('#pillarName .concrete').html()) ||
      (status != lastState)
  )
  {
    lastState = status;
    $('#pillarNameOverlay').removeClass('maintenance speed status hover clicked');
    $('#pillarNameOverlay').addClass(id);
    $('#pillarNameOverlay').addClass(status);
    $('#pillarNameOverlay').stop(false, true).stop(false, true).fadeIn(
      150,
      function ()
      {
        $('#pillarName').html('<div class="shadow">' + pillarName + '</div><div class="concrete">' + pillarName + '</div>');
        $('#pillarName .concrete').css('margin-top', '-' + ($('#pillarName .shadow').height() + 1) + 'px');
        $('#pillarName').removeClass('maintenance speed status hover clicked');
        $('#pillarName').addClass(id);
        $('#pillarName').addClass(status);
      }
    ).fadeOut(150);
  }
}

function initAnimations()
{
  try
  {
    System.Gadget.Flyout.onHide = hideFlyout;
  }
  catch (e)
  {
  }

  $('*').keydown(
    function (e)
    {
      switch (e.keyCode)
      {
        case 13:
          $(this).click();
          break;
      }
    }
  );

  $('.buttons .normal').mouseenter(
    function ()
    {
      $(this).find('.hover').stop(false, true).stop(false, true);
      if (lastId == $(this).parent().attr('id'))
      {
        $(this).find('.hover').show();
        clearTimeout(mouseleaveTimeout);
      }
      else
      {
        $(this).find('.hover').fadeOut(150).fadeIn(150);
        if ($(this).parent().attr('id') == 'analyzeLink')
        {
        }
        else
        {
          clearTimeout(mouseleaveTimeout);
          if ($(this).find('.clicked').is(':visible'))
          {
            setPillarName($(this).parent().attr('id'), 'clicked');
          }
          else
          {
            setPillarName($(this).parent().attr('id'), 'hover');
          }
        }
      }
    }
  );

  $('.buttons .normal').mouseleave(
    function ()
    {
      $(this).find('.hover').stop(false, true).stop(false, true).fadeIn(200).fadeOut(150);
      if ($(this).parent().attr('id') == 'analyzeLink')
      {
      }
      else
      {
        mouseleaveTimeout = setTimeout(
          function ()
          {
            setPillarName('fallback');
          },
          50
        );
      }
    }
  );
  
  $('#pillarName, #pillarNameOverlay').mouseenter(
    function ()
    {
      var hoverVisible = $('#' + lastId + ' .hover');
      hoverVisible.stop(false, true).stop(false, true).show();
      clearTimeout(mouseleaveTimeout);
    }
  );
  
  $('#pillarName, #pillarNameOverlay').mouseleave(
    function ()
    {
      clearTimeout(mouseleaveTimeout);
      var hoverVisible = $('.buttons .hover:visible');
      hoverVisible.parent().mouseleave();
    }
  );
}