/* 
** timeouts, don't know why they have to go up here instead
** of inside analogButtons function with their callers. 
*/
var timeoutSpeedsetUp;
var timeoutSpeedsetClickUp;
var timeoutSpeedsetDown;
var timeoutSpeedsetClickDown;

function speedsetTimerUp() 
{
  timeoutSpeedsetUp = setTimeout("speedsetTimerUp()", 500);
  
  if(set_speed.speed < 45.0)
  {
    set_speed.speed += 0.5;
  } 
  else
  {
    set_speed.speed = 45.0;
  }
  set_speed.speed_initial();
}

function speedsetTimerDown() 
{
  timeoutSpeedsetDown = setTimeout("speedsetTimerDown()", 500);

  if(set_speed.speed > 5.0)
  {
    set_speed.speed -= 0.5;
  } 
  else
  {
    set_speed.speed = 5.0;
  }
  set_speed.speed_initial();
}

/*
** setup analog buttons
*/
function analogButtons()
{
  $('analog_btns').select('.home').each(function(element) {
    element.observe('click', function(event) {
      setScreen('home');
    });
  });
  
  $('analog_btns').select('.up')[0].observe('click', function(event) {
    switch(current_screen)
    {
      case "profiles":
        profileUpClicked();
        break;
      case "setup":
        setupUpClicked();
        break;
      default:
        //
    }  
  });
  
  $('analog_btns').select('.up')[0].observe('mousedown', function(event) {
    switch(current_screen)
    {
      case "speedset":
        speedsetUpMouseDown();
        break;
      default:
        //
    }  
  })
  
  $('analog_btns').select('.up')[0].observe('mouseup', function(event) {
    switch(current_screen)
    {
      case "speedset":
        speedsetUpMouseUp();
        break;
      default:
        //
    }
  });
  
  $('analog_btns').select('.down')[0].observe('click', function(event) {
    switch(current_screen)
    {
      case "profiles":
        profileDownClicked();
        break;
      case "setup":
        setupDownClicked();
        break;
      default:
        //
    }  
  });
  
  $('analog_btns').select('.down')[0].observe('mousedown', function(event) {
    switch(current_screen)
    {
      case "speedset":
        speedsetDownMouseDown();
        break;
      default:
        //
    }  
  }).observe('mouseup', function(event) {
    switch(current_screen)
    {
      case "speedset":
        speedsetDownMouseUp();
        break;
      default:
        //
    }
  });
  
  function speedsetUpMouseDown()
  {
    var run_once = 0; 
    if(speedset_status == "off")
    {
      speedsetActivation("on");
    }
    
    if(set_speed.speed < 44.9)
    {
      set_speed.speed += 0.2;
    }
    else
    {
      set_speed.speed = 45.0;
    }
    set_speed.speed_initial();
    
    timeoutSpeedsetClickUp = setTimeout("speedsetTimerUp()", 1000);
  }
  
  function speedsetDownMouseDown()
  { 
    if(speedset_status == "off") 
    {
      if(set_speed.speed > 5.0)
      {
        speedsetActivation("on");
      }
    }
    
    if(set_speed.speed > 5.1)
    {
      set_speed.speed -= 0.2;
    }
    else
    {
      set_speed.speed = 5.0;
    }
    set_speed.speed_initial();
    
    timeoutSpeedsetClickDown = setTimeout("speedsetTimerDown()", 1000);
  }
  
  function speedsetUpMouseUp()
  {
    clearTimeout(timeoutSpeedsetClickUp);
    clearTimeout(timeoutSpeedsetUp);
  }
  
  function speedsetDownMouseUp()
  {
    clearTimeout(timeoutSpeedsetClickDown);
    clearTimeout(timeoutSpeedsetDown);
  }
  
  function profileUpClicked()
  {
    var profiles_position = $("profiles").select('.profile')[0].getStyle('marginTop');
    profiles_position = Number(profiles_position.sub("px", ""));
    
    if(profiles_position < 0) 
    {
      $("profiles").select('.profile')[0].setStyle({
        marginTop: ""+ (profiles_position + 68) +"px"
      });
    }
  }
  
  function profileDownClicked()
  {
    var profiles_position = $("profiles").select('.profile')[0].getStyle('marginTop');
    profiles_position = Number(profiles_position.sub("px", ""));
    
    if(profiles_position > -272)
    {
      $("profiles").select('.profile')[0].setStyle({
        marginTop: ""+ (profiles_position - 68) +"px"
      });
    }
  }
  
  function setupUpClicked()
  {
    var setup_object = $('buttons').select('.highlight.setup')[0];
    var setup_position = setup_object.getStyle('marginTop');
    var increment_value = 20;
    setup_position = Number(setup_position.sub("px", ""));
    function increment()
    {
      $('buttons').select('.highlight.setup')[0].setStyle({
        marginTop: ""+ (setup_position - increment_value) +"px"
      });
    }
    switch(setup_position)
    {
      case 73:
        break;
      case 114:
        increment_value = 21;
        increment();
        break;
      case 155:
        increment_value = 21;
        increment();
        break;
      case 196:
        increment_value = 21;
        increment();
        break;
      case 237:
        increment_value = 21;
        increment();
        break;
      default:
        increment_value = 20;
        increment();
    }
  }
  
  function setupDownClicked() 
  {
    var setup_object = $('buttons').select('.highlight.setup')[0];
    var setup_position = setup_object.getStyle('marginTop');
    var increment_value = 20;
    setup_position = Number(setup_position.sub("px", ""));
    function increment()
    {
      $('buttons').select('.highlight.setup')[0].setStyle({
        marginTop: ""+ (setup_position + increment_value) +"px"
      });
    }
    switch(setup_position)
    {
      case 93:
        increment_value = 21;
        increment();
        break;
      case 134:
        increment_value = 21;
        increment();
        break;
      case 175:
        increment_value = 21;
        increment();
        break;
      case 216:
        increment_value = 21;
        increment();
        break;
      case 257:
        break;
      default:
        increment_value = 20;
        increment();
    }
  }
}

function speedsetActivation(status) 
{
  speedset_status = status;
  if(speedset_status == "off") {
    if(current_screen == "speedset")
    {
      $('screen').select('.status.speedset')[0].update("Speed Off");
      $('buttons').select('.button.speedset.set')[0].setStyle({
        backgroundPosition: "0px 0px"
      });
    }
  }
  else
  {
    if(current_screen == "speedset")
    {
      $('screen').select('.status.speedset')[0].update("Speed On");
      $('buttons').select('.button.speedset.set')[0].setStyle({
        backgroundPosition: "-94px 0px"
      });
    }
  }
}

function standardButtons()
{
  $('buttons').insert("<div id='standard_buttons_top'></div>");
  $('buttons').insert("<div id='standard_buttons_bottom'></div>");
  $('standard_buttons_top').insert("<div class='button speedset'></div>");
  $('standard_buttons_bottom').insert("<div class='button audio'></div>");
  
  $('standard_buttons_bottom').select('.audio')[0].observe('click', function(event) {
    setScreen('audio');
  });
  
  $('standard_buttons_top').select('.speedset')[0].observe('click', function(event) {
    setScreen('speedset');
  });
}

function speedsetButtons()
{
  $('buttons').select('.button.speedset.set')[0].observe('click', function(event) {
    if(speedset_status == "on") 
    {
      speedset_speed = set_speed.speed;
      speedset_activated = true;
      
      if(speedset_speed > standard_speed.speed) 
      {
        acceleration = "up";
        speedCheck();
      }
      else
      {
        acceleration = "down";
        speedCheck();
      }
      
      speedsetActivation("off");
      set_speed.speed = 5.0;
      set_speed.speed_initial();
    }
  });
  
  $('buttons').select('.button.speedset.save_as_profile')[0].observe('click', function(event) {
    setScreen('profiles');
  });
}

function homeButtons()
{
  $('buttons').insert("<div class='button home gauge'></div><div class='button home depth'></div><div class='button home profiles'></div><div class='button home lights'></div><div class='button home switches'></div>");
  
  $('buttons').select('.gauge').each(function(element) {
    element.observe('click', function(event) {
      setScreen('gauges');
    });
  });
  $('buttons').select('.depth').each(function(element) {
    element.observe('click', function(event) {
      setScreen('depth');
    });
  });
  $('buttons').select('.profiles').each(function(element) {
    element.observe('click', function(event) {
      setScreen('profiles');
    });
  });
  $('buttons').select('.lights').each(function(element) {
    element.observe('click', function(event) {
      setScreen('lights');
    });
  });
  $('buttons').select('.switches').each(function(element) {
    element.observe('click', function(event) {
      setScreen('switches');
    });
  });
}

function profileButtons()
{
  $('buttons').insert("<div class='screen profiles'></div>");
  $('buttons').insert("<div id='profiles'></div>");
  $('buttons').insert("<div class='button profiles exit'></div><div class='button profiles select'></div><div class='button profiles save'></div>");
  
  $('buttons').select('.button.profiles.exit')[0].observe('click', function(event) {
    setScreen('home');
  });
  $('buttons').select('.button.profiles.select')[0].observe('click', function(event) {
    speedset_activated = true;
    profiles_array.each(function(object) {
      if(object.state == 'active')
      {
        set_speed.speed = object.speed;
      }
    });
    setScreen('speedset');
  });
  //$('buttons').select('.button.profiles.save')[0].observe('click', function(event) {
  //  
  //});
  
  prowak.profile_initial();
  prowak.profile_actions();
  begwak.profile_initial();
  begwak.profile_actions();
  surfrt.profile_initial();
  surfrt.profile_actions();
  surflt.profile_initial();
  surflt.profile_actions();
  home.profile_initial();
  home.profile_actions();
}

function lightsButtons()
{
  $('buttons').insert("<div class='button light navigation'></div><div class='button light anchor'></div><div class='button light courtesy'></div><div class='button light tower'></div><div class='button light docking'></div><div class='button light underwater'></div><div class='button light all'></div><div class='title light navigation'></div><div class='title light anchor'></div><div class='title light courtesy'></div><div class='title light tower'></div><div class='title light docking'></div><div class='title light underwater'></div>");
  
  // navigation light
  navigation_light.activator = $('buttons').select('.light.navigation')[0];
  navigation_light.light_initial();
  navigation_light.light_actions();
  // anchor light
  anchor_light.activator = $('buttons').select('.light.anchor')[0];
  anchor_light.light_initial();
  anchor_light.light_actions();
  // courtesy light
  courtesy_light.activator = $('buttons').select('.light.courtesy')[0];
  courtesy_light.light_initial();
  courtesy_light.light_actions();
  // tower light
  tower_light.activator = $('buttons').select('.light.tower')[0];
  tower_light.light_initial();
  tower_light.light_actions();
  // docking light
  docking_light.activator = $('buttons').select('.light.docking')[0];
  docking_light.light_initial();
  docking_light.light_actions();
  // underwater light
  underwater_light.activator = $('buttons').select('.light.underwater')[0];
  underwater_light.light_initial();
  underwater_light.light_actions();
  // all light
  all_light.activator = $('buttons').select('.light.all')[0];
  if(navigation_light.state == "off" && anchor_light.state == "off" && courtesy_light.state == "off" && tower_light.state == "off" && docking_light.state == "off" && underwater_light.state == "off")
  {
    all_light.state = "off";
  } else if(navigation_light.state == "on" && anchor_light.state == "on" && courtesy_light.state == "on" && tower_light.state == "on" && docking_light.state == "on" && underwater_light.state == "on")
  {
    all_light.state = "on";
  }
  if(all_light.state == "on")
  {
    all_light.activator.setStyle({
      backgroundPosition: "-96px 0"
    });
  }
  else if(all_light.state == "off")
  {
    all_light.activator.setStyle({
      backgroundPosition: "-1px 0"
    });
  }
  else
  {
    all_light.activator.setStyle({
      backgroundPosition: "-1px 0"
    });
  }
  all_light.activator.observe('click', function(event) {
    if(all_light.state == "off")
    {
      all_light.state = "on";
      all_light.activator.setStyle({
        backgroundPosition: "-96px 0"
      });
      allLightsOn();
    }
    else if(all_light.state == "on")
    {
      all_light.state = "off";
      all_light.activator.setStyle({
        backgroundPosition: "-1px 0"
      });
      allLightsOff();
    }
    else
    {
      all_light.state = "on";
      all_light.activator.setStyle({
        backgroundPosition: "-96px 0"
      });
    }
  });
  
  function allLightsOff()
  {
    navigation_light.state = "off";
    anchor_light.state = "off";
    courtesy_light.state = "off";
    tower_light.state = "off";
    docking_light.state = "off";
    underwater_light.state = "off";

    var light_activators_array = [navigation_light.activator, anchor_light.activator, courtesy_light.activator, tower_light.activator, docking_light.activator, underwater_light.activator];

    light_activators_array.each(function(element) {
      element.setStyle({
        backgroundPosition: "-1px 0"
      });
    });
  }

  function allLightsOn()
  {
    navigation_light.state = "on";
    anchor_light.state = "on";
    courtesy_light.state = "on";
    tower_light.state = "on";
    docking_light.state = "on";
    underwater_light.state = "on";

    var light_activators_array = [navigation_light.activator, anchor_light.activator, courtesy_light.activator, tower_light.activator, docking_light.activator, underwater_light.activator];

    light_activators_array.each(function(element) {
      element.setStyle({
        backgroundPosition: "-61px 0"
      });
    });
  }
}

function switchesButtons()
{
  $('buttons').insert("<div class='button switch bilge'></div><div class='button switch blower'></div><div class='button switch heater'></div><div class='button switch video'></div><div class='button switch ballast'></div><div class='button switch all'></div><div class='button switch setup'></div><div class='title switch bilge'></div><div class='title switch blower'></div><div class='title switch heater'></div><div class='title switch video'></div><div class='title switch ballast'></div><div class='title switch all'></div><div class='title switch setup'></div>");
  
  // bilge switch
  bilge_switch.activator = $('buttons').select('.switch.bilge')[0];
  bilge_switch.switch_initial();
  bilge_switch.switch_actions();
  // blower switch
  blower_switch.activator = $('buttons').select('.switch.blower')[0];
  blower_switch.switch_initial();
  blower_switch.switch_actions();
  // heater switch
  heater_switch.activator = $('buttons').select('.switch.heater')[0];
  heater_switch_initial();
  heater_switch.activator.observe('click', function(event) {
    if(heater_switch.state == "off")
    {
      heater_switch.state = "high";
      heater_switch.activator.setStyle({
        backgroundPosition: "-3px 0"
      });
    }
    else if(heater_switch.state == "high")
    {
      heater_switch.state = "low";
      heater_switch.activator.setStyle({
        backgroundPosition: "-66px 0"
      });
    }
    else if(heater_switch.state == "low")
    {
      heater_switch.state = "off";
      heater_switch.activator.setStyle({
        backgroundPosition: "-128px 0"
      });
    }
  });
  // video switch
  video_switch.activator = $('buttons').select('.switch.video')[0];
  video_switch.switch_initial();
  video_switch.switch_actions();
  // ballast switch
  ballast_switch.activator = $('buttons').select('.switch.ballast')[0];
  ballast_switch.activator.observe('click', function(event) {
    setScreen('ballast');
  });
  // all switch
  all_switch.activator = $('buttons').select('.switch.all')[0];
  all_switch.activator.observe('click', allSwitchesOff);
  // setup switch
  setup_switch.activator = $('buttons').select('.switch.setup')[0];
  setup_switch.activator.observe('click', function(event) {
    setScreen('setup');
  });
  
  function heater_switch_initial()
  {
    if(heater_switch.state == "high")
    {
      heater_switch.activator.setStyle({
        backgroundPosition: "-3px 0"
      });
    }
    else if(heater_switch.state == "low")
    {
      heater_switch.activator.setStyle({
        backgroundPosition: "-66px 0"
      });
    }
    else
    {
      heater_switch.activator.setStyle({
        backgroundPosition: "-128px 0"
      });
    }
  }
  
  function allSwitchesOff()
  {
    bilge_switch.state = "off";
    blower_switch.state = "off";
    heater_switch.state = "off";
    video_switch.state = "off";
    
    var switch_activators_array = [bilge_switch.activator, blower_switch.activator, video_switch.activator];
    
    switch_activators_array.each(function(element) {
      element.setStyle({
        backgroundPosition: "-1px 0"
      });
    });
    heater_switch.activator.setStyle({
      backgroundPosition: "-128px 0"
    });
  }
}

function ballastButtons()
{
  $('buttons').insert("<div class='screen ballast'></div>");
}

function setupButtons()
{
  $('buttons').insert("<div class='screen setup'></div><div class='highlight setup'></div>");
  $('buttons').select('.highlight.setup')[0].setStyle({
    marginTop: "73px"
  });
}
