var turbomodeStatus = false;
var mouseInMap = false;

$(document).ready(
  function ()
  {
    addHoverPressedEvents($('#turbomode .settings'));
    activateTooltip($('#turbomode .settings'));
    
    $('#turbomodeMap').click(
      function ()
      {
        if (turbomodeStatus)
        {
          call('#highspeed:0');
        }
        else
        {
          call('#highspeed:1');
        }
        if (debug)
        {
          for (var i=0; i<100; i++)
          {
            eval('setTimeout(function() {turbomodeProgress('+i+');}, i*50);');
          }
          setTimeout(function () {turbomodeDone(!turbomodeStatus);}, 5000);
        }
      }
    );
    $('#turbomodeMap').mouseenter(
      function ()
      {
        if (!$('#turbomodeStatus').hasClass('locked'))
        {
          $('#turbomodeStatus .hover').stop(false, true).show();
        }
        mouseInMap = true;
        call('#tooltip:1:highspeed-' + (turbomodeStatus ? '1' : '0'));
      }
    );
    $('#turbomodeMap').mouseleave(
      function ()
      {
        if (!$('#turbomodeStatus').hasClass('locked'))
        {
          $('#turbomodeStatus .hover').stop(false, true).hide();
        }
        mouseInMap = false;
        call('#tooltip:0:highspeed-' + (turbomodeStatus ? '1' : '0'));
      }
    );
    
    $('#turbomodeStatus').keydown(
      function (e)
      {
        if (e.keyCode == 13)
        {
          $(this).find('#turbomodeMap').click();
        }
      }
    );
  }
);

function turbomodeDone(newStatus)
{
  $('#turbomodeStatus').removeClass('locked');
  if (mouseInMap)
  {
    $('#turbomodeStatus .hover').stop(false, true).show();
  }
  turbomodeStatus = newStatus;
  if (newStatus)
  {
    if (!$('#turbomodeStatus').hasClass('on'))
    {
      $('#turbomodeStatus').addClass('on');
    }
    $('#allfunctions-turbo-on').hide();
    $('#allfunctions-turbo-on-progress').hide();
    $('#allfunctions-turbo-off-progress').hide();
    $('#allfunctions-turbo-off').show();
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '0');
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '1px');
  }
  else
  {
    $('#turbomodeStatus').removeClass('on');
    $('#allfunctions-turbo-off').hide();
    $('#allfunctions-turbo-on-progress').hide();
    $('#allfunctions-turbo-off-progress').hide();
    $('#allfunctions-turbo-on').show();
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '0');
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '1px');
  }
  $('#turbomodeStatus').css('background-position-y', 0);
  $('#turbomodeStatus').css('background-image', '');
  
  updateTuningIndexTurbo(tuningIndexTurbo);
}

function turbomodeProgress(percent)
{
  if (!$('#turbomodeStatus').hasClass('locked'))
  {
    $('#turbomodeStatus').addClass('locked');
    $('#turbomodeStatus .hover').stop(false, true).hide();
  }
  if (turbomodeStatus)
  {
    $('#turbomodeStatus').css('background-image', 'url(images/turbo_deactivate.png)');
    $('#allfunctions-turbo-on').hide();
    $('#allfunctions-turbo-off').hide();
    $('#allfunctions-turbo-on-progress').hide();
    $('#allfunctions-turbo-off-progress').show();
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '0');
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '1px');
  }
  else
  {
    $('#turbomodeStatus').css('background-image', 'url(images/turbo_activate.png)');
    $('#allfunctions-turbo-on').hide();
    $('#allfunctions-turbo-off').hide();
    $('#allfunctions-turbo-off-progress').hide();
    $('#allfunctions-turbo-on-progress').show();
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '0');
    $('#allfunctions-turbo-off').parent().parent().find('img').css('margin-top', '1px');
  }
  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 amountImages = availableImages.length;
  var percentalImage = amountImages*(percent/100);
  var showImage = availableImages[0];
  for (var x in availableImages)
  {
    if (percentalImage >= availableImages[x])
    {
      showImage = availableImages[x];
    }
    else
    {
      break;
    }
  }
  var position = showImage*(-66);
  $('#turbomodeStatus').css('background-position-y', position+'px');
}
