
$(document).ready(
  function ()
  {
    var prefixname = 'images/framework/layerbutton-ig-156';
    var leftBorderHover = 4;
    var rightBorderHover = 4;
    var topBorderHover = 4;
    var bottomBorderHover = 4;

    $('#liveoptimization .button').LayerButton(
      {
        'groupID':        'liveoptimization',
        'amountTiles':    1,
        'normal':
        {
          'imageSource':  { 'prefix': prefixname + '_', 'suffix': '.png' },
          'imageBorders': { 'right': rightBorderHover, 'left': leftBorderHover, 'top': topBorderHover, 'bottom': bottomBorderHover }
        },
        'hover':
        {
          'imageSource':  { 'prefix': prefixname + '-hover_', 'suffix': '.png' },
          'imageBorders': { 'right': rightBorderHover, 'left': leftBorderHover, 'top': topBorderHover, 'bottom': bottomBorderHover }
        }
      }
    );

    $('#liveoptimization .button').click(
      function ()
      {
        if (debug)
        {
          updateSidebar('liveoptimization', $('#liveoptimization .status span').hasClass('off'));
        }
      }
    );

    $('#liveoptimization .icon').hover(
      function ()
      {
        $(this).addClass('hover');
      },
      function ()
      {
        $(this).removeClass('hover');
      }
    );

    $('#liveoptimization .icon, #liveoptimization .status').click(
      function ()
      {
        $('#liveoptimization .button').click();
      }
    );

    activateTooltip($('#liveoptimization .button'));

    if (debug)
    {
      setTimeout(
        function ()
        {
          updateSidebar('liveoptimization', true);
          updatePillar('maintenance', 'good', 'Manuell', 'Die <a onclick="call(\'#asdf\');">automatische Wartung</a> ist deaktiviert. Führen Sie die 1-Klick-Wartung regelmäßig manuell durch.', new Array('<a onclick="call(\'#launch:test.exe\')">Automatische Wartung konfigurieren</a>', '<a onclick="call(\'#launch:test2.exe\')">1-Klick-Wartung starten</a>'));
          updatePillar('performance', 'warning', 'Manuell', 'Die <a>automatische Wartung</a> ist deaktiviert. Führen Sie die 1-Klick-Wartung regelmäßig manuell durch.', new Array('<a onclick=\'call(\"#asdf\");\'>Automatische Wartung konfigurieren</a>', '<a onclick="call(\'#launch:test3.exe\')">1-Klick-Wartung starten</a>'));
          updatePillar("health", "good", "Optimiert", "<div>Es wurden <span class=dashboard-good>keine Probleme</span> gefunden.</div>", new Array("<a onclick=\'call(\"#dashboard:health\");\'>Details anzeigen</a>"));
          updatePillar("health", "good", "Optimiert", "<div>Es wurden <span class=dashboard-good>keine Probleme</span> gefunden.</div>", new Array("<a onclick=\'call(\"#dashboard:health\");\'>Details anzeigen</a>"));
          updatePillar("health", "critical", "Optimiert", "<div>Es wurden <span class=dashboard-good>keine Probleme</span> gefunden.</div>", new Array("<a onclick=\'call(\"#dashboard:health\");\'>Details anzeigen</a>"));
        },
        1000
      );
    }
  }
);

function updatePillar(pillar, newState, rs_state, rs_description, links)
{
  if (pillar == "maintenance")
  {
    var selector = "#pillar1";
    var tabindex = 220;
  }
  else if (pillar == "performance")
  {
    var selector = "#pillar2";
    var tabindex = 240;
  }
  else if (pillar == "health")
  {
    var selector = "#pillar3";
    var tabindex = 270;
  }
  $('#allfunctions-' + pillar).attr('src', 'empty.gif');
  $('#allfunctions-' + pillar).css(
    {
      'width':  '16px',
      'height': '16px',
      'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src=\'images/panel6/' + pillar + '-' + newState + '.png\');'
     }
  );
  $(selector).removeClass('good warning critical').addClass(newState);
  $(selector + ' .pillarstatus .text').html('- ' + rs_state + ' -');
  $(selector + ' .pillartext').html(rs_description);
  $(selector + ' .pillartext a').attr('tabindex', (tabindex + 10));
  $(selector + ' .pillarlinks').html('');
  for (var i = 0; i < links.length; i++)
  {
    $(selector + ' .pillarlinks').append(
      links[i]
    );
    $(selector + ' .pillarlinks a:eq(' + i + ')').attr('id', pillar + '-' + i);
    if (i == 0)
    {
      $(selector + ' .pillarlinks a').html(
        '<table cellpadding="0" cellspacing="0">' +
          '<tr valign="middle">' +
            '<td>' +
              $(selector + ' .pillarlinks a').html() +
            '</td>' +
          '</tr>' +
        '</table>'
      );
    }

    $(selector + ' .pillarlinks a:last').attr('tabindex', tabindex++);
  }
  $(selector + ' .pillarlinks a:first').addClass('first');
  $(selector + ' .pillarlinks a:first td').attr('height', '32');
  $(selector + ' .pillarstatus .icon').unbind('mouseenter').mouseenter(
    function ()
    {
      $(this).addClass('hover');
    }
  );
  $(selector + ' .pillarstatus .icon').unbind('mouseleave').mouseleave(
    function ()
    {
      $(this).removeClass('hover');
    }
  );
  $(selector + ' .pillarstatus .icon, ' + selector + ' .pillarstatus .text').unbind('click').click(
    function ()
    {
      $(this).parent().parent().find('.pillarlinks a:first').click();
    }
  );

  $(selector + ' .pillarlinks a').unbind('mouseenter').unbind('mouseleave');

  activateTooltip($(selector + ' .pillarlinks a'));

  var prefixname = 'images/framework/layerbutton-ig-158';
  var leftBorderHover = 4;
  var rightBorderHover = 4;
  var topBorderHover = 4;
  var bottomBorderHover = 4;

  $(selector + ' .pillarlinks a:first').LayerButton(
    {
      'groupID':        selector.substr(1, selector.length - 1) + 'pillarlinks',
      'amountTiles':    1,
      'normal':
      {
        'imageSource':  { 'prefix': prefixname + '_', 'suffix': '.png' },
        'imageBorders': { 'right': rightBorderHover, 'left': leftBorderHover, 'top': topBorderHover, 'bottom': bottomBorderHover }
      },
      'hover':
      {
        'imageSource':  { 'prefix': prefixname + '-hover_', 'suffix': '.png' },
        'imageBorders': { 'right': rightBorderHover, 'left': leftBorderHover, 'top': topBorderHover, 'bottom': bottomBorderHover }
      }
    }
  );

  if ($(selector + ' .pillarlinks a').length > 1)
  {
    $(selector + ' .pillarlinks a:last').LayerButton(
      {
        'groupID':        selector.substr(1, selector.length - 1) + 'pillarlinks2',
        'amountTiles':    '3h',
        'normal':
        {
          'imageSource':  { 'prefix': prefixname + '_', 'suffix': '.png' },
          'imageBorders': { 'right': rightBorderHover, 'left': leftBorderHover, 'top': topBorderHover, 'bottom': bottomBorderHover }
        },
        'hover':
        {
          'imageSource':  { 'prefix': prefixname + '-hover_', 'suffix': '.png' },
          'imageBorders': { 'right': rightBorderHover, 'left': leftBorderHover, 'top': topBorderHover, 'bottom': bottomBorderHover }
        }
      }
    );
  }
  if (!$('#tab-1').hasClass('active') || $('#tab-6active').is(':visible'))
  {
    $(selector + ' .pillarlinks a').each(
      function ()
      {
        $('#' + $.LayerButton.getLayerID(this, 'normal', this.id)).hide();
      }
    );
  }

}

function updateSidebar(sidebar, newValue)
{
  $('#' + sidebar + ' .status').html(
    (newValue == 2) ?
    '<span class="on"><localized>- Aktiviert -</localized></span>' :
    (
      (newValue == 1) ?
      '<span class="on"><localized>- Teilweise aktiviert -</localized></span>' :
        '<span class="off"><localized>- Deaktiviert -</localized></span>'
     )
  );
}


function loadingPillar(pillar)
{
  if ($('#tab-1').hasClass('active')){
    if (pillar == "maintenance")
    {
      var selector = "#pillar1";
    }
    else if (pillar == "performance")
    {
      var selector = "#pillar2";
    }
    else if (pillar == "health")
    {
      var selector = "#pillar3";
    }
    $('#allfunctions-' + pillar).attr('src', 'images/panel6/loader.gif');
    $('#allfunctions-' + pillar).attr('style', '');
    $(selector + ' .text').html('- <localized>Status wird ermittelt</localized> -');
    $(selector).removeClass('good warning critical');
    $(selector + ' .pillartext').html('');
    $(selector + ' .pillarlinks a').each(
      function ()
      {
        $('#' + $.LayerButton.getLayerID(this, 'normal', this.id)).remove();
        $('#' + $.LayerButton.getLayerID(this, 'hover')).remove();
      }
    );
    $(selector + ' .pillarlinks').html('');
  }
}