/*
** SPEED
*/
function speedCheck()
{
  timeoutSpeedCheck = setTimeout('speedCheck()', 222);
  
  if(acceleration == "up")
  {
    if(standard_speed.speed < speedset_speed) 
    {
      standard_speed.speed++;
      checkScreenRunSpeed();
    }
    else
    {
      standard_speed.speed = speedset_speed;
      checkScreenRunSpeed();
      clearTimeout(timeoutSpeedCheck);
    }
  }
  else if(acceleration == 'down')
  {
    if(standard_speed.speed > speedset_speed) 
    {
      standard_speed.speed--;
      checkScreenRunSpeed();
    }
    else
    {
      standard_speed.speed = speedset_speed;
      checkScreenRunSpeed();
      clearTimeout(timeoutSpeedCheck);
    }
  }
  
  function checkScreenRunSpeed()
  {
    if(current_screen == "speedset")
    {
      runActualSpeed();
    } 
    else if(current_screen == "home" || current_screen == "lights" || current_screen == "switches" || current_screen == "depth")
    {
      runStandardSpeed();
    }
    else if(current_screen == "gauges")
    {
      runGaugeSpeed();
    }
  }
}

function runStandardSpeed()
{
  standard_speed.speed = new Number(standard_speed.speed);
  standard_speed.speed_initial();
}

function runActualSpeed()
{
  actual_speed.speed = new Number(standard_speed.speed);
  actual_speed.speed_initial();
}

function runGaugeSpeed()
{
  gauge_speed.speed = standard_speed.speed;
  gauge_speed.speed_initial();
}

/*
** RPM
*/
function rpmCheck() 
{
  timeoutRpmCheck = setTimeout("rpmCheck()", 117);
  
  var rpm_target = 1000 + (standard_speed.speed * 111);
  
  if(acceleration == "up")
  { 
    if(rpm.rpm <= rpm_target)
    {
      rpm.rpm = rpm_target;
      checkRunRpm();
    }
    else
    {
      clearTimeout(timeoutRpmCheck);
    }
  }
  else
  {
    if(rpm.rpm >= rpm_target)
    {
      rpm.rpm = rpm_target;
      checkRunRpm();
    }
    else
    {
      clearTimeout(timeoutRpmCheck);
    }
  }
  
  function checkRunRpm() 
  {
    if(current_screen == "home" || current_screen == "lights" || current_screen == "switches" || current_screen == "depth")
    {
      runRpm();
    }
  }
}

function runRpm()
{
  rpm.rpm = new Number(rpm.rpm);
  rpm.rpm_initial();
}

/*
** DEPTH
*/
function depthCheck()
{ 
  timeoutDepthCheck = setTimeout("depthCheck()", (Math.floor(Math.random() * 5000) + 500));
  
  if(elevation == 'rise')
  {
    if(depth.depth < max_depth)
    {
      depth.depth++;
    } 
    else 
    {
      setDepthLimits();
      if(min_depth < depth.depth)
      {
        elevation = 'drop';
        depth.depth--;
      }
      else
      {
        depth.depth++;
      }
    }
  }
  else if(elevation == 'drop')
  {
    if(depth.depth > min_depth)
    {
      depth.depth--;
    }
    else
    {
      setDepthLimits();
      if(max_depth > depth.depth)
      {
        elevation = 'rise';
        depth.depth++;
      }
      else
      {
        depth.depth--;
      }
    }
  }
  
  if(current_screen == "home" || current_screen == "lights" || current_screen == "switches" || current_screen == "depth")
  {
    runDepth();
  } 
  else if(current_screen == "gauges")
  {
    runGaugeDepth();
  }
}

function runDepth()
{
  depth.depth = new Number(depth.depth);
  depth.depth_initial();
}

function runGaugeDepth()
{
  gauge_depth.depth = depth.depth;
  gauge_depth.depth_initial();
}

function setDepthLimits()
{ 
  min_depth = Math.floor((Math.random() * 4) + (Math.random() * 45));
  if(min_depth < 10) 
  {
    min_depth = 10;
  }
  max_depth = Math.floor((Math.random() * 4) + min_depth);
  if(max_depth > 50)
  {
    max_depth = 50;
  }
}

/*
** NUMBERS
*/
/*function numbers_output(numbers_element, numbers_array) 
{ 
  numbers_array.each(function(item) {
    item.each(function(item) {
      switch(item)
      {
        case '0':
          numbers_element.select('.numbers')[0].insert("<div class='zero'>"+ item +"</div>");
          break;
        case '1':
          numbers_element.select('.numbers')[0].insert("<div class='one'>"+ item +"</div>");
          break;
        case '2':
          numbers_element.select('.numbers')[0].insert("<div class='two'>"+ item +"</div>");
          break;
        case '3':
          numbers_element.select('.numbers')[0].insert("<div class='three'>"+ item +"</div>");
          break;
        case '4':
          numbers_element.select('.numbers')[0].insert("<div class='four'>"+ item +"</div>");
          break;
        case '5':
          numbers_element.select('.numbers')[0].insert("<div class='five'>"+ item +"</div>");
          break;
        case '6':
          numbers_element.select('.numbers')[0].insert("<div class='six'>"+ item +"</div>");
          break;
        case '7':
          numbers_element.select('.numbers')[0].insert("<div class='seven'>"+ item +"</div>");
          break;
        case '8':
          numbers_element.select('.numbers')[0].insert("<div class='eight'>"+ item +"</div>");
          break;
        case '9':
          numbers_element.select('.numbers')[0].insert("<div class='nine'>"+ item +"</div>");
          break;
        default:
          //alert(item);
      }
    });
  });
}*/

// gauge canvas
/*function gauge_canvas()
{
  $('screen').insert('<canvas></canvas>');
  var canvas = $('screen').select("canvas")[0];
  var ctx = canvas.getContext("2d");
  
  ctx.strokeStyle = "rgb(255, 0, 0)";
  ctx.beginPath();
  //ctx.arc(75,75,50,0,Math.PI*2,true); // Outer circle
  ctx.moveTo(110,75);
  ctx.arc(75, 75, 35, 0, Math.PI, false);   // Mouth (clockwise)
  //ctx.moveTo(65,65);
  //ctx.arc(60,65,5,0,Math.PI*2,true);  // Left eye
  //ctx.moveTo(95,65);
  //ctx.arc(90,65,5,0,Math.PI*2,true);  // Right eye
  ctx.stroke();
}*/
