/*
** GLOBALS
*/
var acceleration = 'up';
var speedset_status = 'off';
var speedset_activated = false;
var speedset_speed = new Number(45.0);
var elevation = 'rise';
var min_depth;
var max_depth;
var timeoutSpeed;
var timeoutRpm;
var timeoutDepth;
var current_screen = "home";

/*
** STANDARD DATA
*/
var standard_speed = new Speed();
standard_speed.speed = new Number(0.0);
var rpm = new Rpm();
rpm.rpm = new Number(1000);
var depth = new Depth();
depth.depth = new Number(20);

/*
** GAUGES
*/
var gauge_speed = new Speed();
gauge_speed.speed = standard_speed.speed;
var gauge_depth = new Depth();
gauge_depth.depth = depth.depth;

/*
** PROFILES
*/
var prowak = new Profile();
prowak.profile_id = "prowak";
prowak.state = "active";
prowak.speed = new Number(24.0);
prowak.ballast = [1, 1, 1, 1];
var begwak = new Profile();
begwak.profile_id = "begwak";
begwak.state = "inactive";
begwak.speed = new Number(18.0);
begwak.ballast = [0, 0, 0, 0];
var surfrt = new Profile();
surfrt.profile_id = "surfrt";
surfrt.state = "inactive";
surfrt.speed = new Number(10.0);
surfrt.ballast = [0, 1, 0, 1];
var surflt = new Profile();
surflt.profile_id = "surflt";
surflt.state = "inactive";
surflt.speed = new Number(10.0);
surflt.ballast = [1, 0, 1, 0];
var home = new Profile();
home.profile_id = "home";
home.state = "inactive";
home.speed = new Number(30.0);
home.ballast = [0, 0, 0, 0];
var profiles_array = [prowak, begwak, surfrt, surflt, home];

/*
** LIGHTS
*/
var navigation_light = new Light();
navigation_light.state = "off";
var anchor_light = new Light();
anchor_light.state = "off";
var courtesy_light = new Light();
courtesy_light.state = "off";
var tower_light = new Light();
tower_light.state = "off";
var docking_light = new Light();
docking_light.state = "off";
var underwater_light = new Light();
underwater_light.state = "off";
var all_light = new Object();

/*
** SWITCHES
*/
var bilge_switch = new Switch();
bilge_switch.state = "off";
var blower_switch = new Switch();
blower_switch.state = "off";
var heater_switch = new Object();
heater_switch.state = "off";
var video_switch = new Switch();
video_switch.state = "off";
var ballast_switch = new Object();
ballast_switch.state = "off";
var all_switch = new Object();
var setup_switch = new Object();

/*
** SPEEDSET
*/
var set_speed = new SpeedsetSpeed();
set_speed.speed = new Number(5);
var actual_speed = new SpeedsetSpeed();
actual_speed.speed = new Number(standard_speed.speed);

/*
** JAVASCRIPT OBJECTS (CLASSES)
*/
function Speed()
{
  this.speed_initial = speed_initial
  
  function speed_initial()
  {
    this.type_object.update(""+ this.speed.toFixed(1) +"");
  }
}

function Rpm()
{
  this.rpm_initial = rpm_initial
  
  function rpm_initial()
  {
    this.type_object.update(""+ this.rpm +"");
  }
}

function Depth()
{
  this.depth_initial = depth_initial
  
  function depth_initial()
  {
    this.type_object.update(""+ this.depth +"");
  }
}

function Profile()
{
  this.profile_initial = profile_initial;
  this.profile_actions = profile_actions;
  
  function profile_initial()
  {
    var the_profile = this;
    var profile_id_array = this.profile_id.toArray();
    if(profile_id_array.size() < 5)
    {
      var profile_id_string_offset = Math.floor((6 - profile_id_array.length)/2);
      var x = 0;
      while(x < profile_id_string_offset)
      {
        profile_id_array.unshift(" ");
        x++;
      }
    }
    var profile_id_string = "";
    profile_id_array.each(function(character) {
      profile_id_string += ""+ character +"<br />";
    });
    
    $('profiles').insert("<div id='"+ this.profile_id +"' class='profile'></div>");
    $(this.profile_id).insert("<div class='title WaukeganLDOExtendedBlack'>"+ profile_id_string +"</div>");
    $(this.profile_id).insert("<div class='button'></div>");
    $(this.profile_id).insert("<div class='data'></div>");
    $(this.profile_id).select('.data')[0].insert("<div class='ballast_meters'></div>")
    $(this.profile_id).select('.data .ballast_meters')[0].insert("<div class='ballast_meter'><div class='ballast_front_left'></div></div>");
    $(this.profile_id).select('.data .ballast_meters')[0].insert("<div class='ballast_meter'><div class='ballast_front_right'></div></div>");
    $(this.profile_id).select('.data .ballast_meters')[0].insert("<div class='ballast_meter'><div class='ballast_rear_left'></div></div>");
    $(this.profile_id).select('.data .ballast_meters')[0].insert("<div class='ballast_meter last'><div class='ballast_rear_right'></div></div>");
    
    this.ballast.each(function(item, id) {
      var fill = Math.round(20 * item);
      $(the_profile.profile_id).select('.data .ballast_meters .ballast_meter div')[id].setStyle({
        height: ""+ fill +"px"
      });
    });
    
    switch(this.profile_id) 
    {
      case 'prowak':
        $(this.profile_id).select('.data')[0].setStyle({
          backgroundImage: "url(../images/touchscreen/profiles/prowak_data.png)"
        });
        break;
      case 'begwak':
        $(this.profile_id).select('.data')[0].setStyle({
          backgroundImage: "url(../images/touchscreen/profiles/begwak_data.png)"
        });
        break;
      case 'surfrt':
        $(this.profile_id).select('.data')[0].setStyle({
          backgroundImage: "url(../images/touchscreen/profiles/surfrt_data.png)"
        });
        break;
      case 'surflt':
        $(this.profile_id).select('.data')[0].setStyle({
          backgroundImage: "url(../images/touchscreen/profiles/surfrt_data.png)"
        });
        break;
      case 'home':
        $(this.profile_id).select('.data')[0].setStyle({
          backgroundImage: "url(../images/touchscreen/profiles/home_data.png)"
        });
        break;
      default:
        $(this.profile_id).select('.data')[0].setStyle({
          backgroundImage: "url(../images/touchscreen/profiles/prowak_data.png)"
        });
    }
    
    this.activator = $(this.profile_id).select('.button')[0];
    
    if(this.state == "active")
    {
      this.activator.setStyle({
        backgroundPosition: "0 0"
      });
    } 
    else
    {
      this.activator.setStyle({
        backgroundPosition: "-55px 0"
      });
    }
  }
  
  function profile_actions()
  {
    var the_profile = this;
    the_profile.activator.observe('click', function(event) {
      if(the_profile.state == "active")
      {
        the_profile.state = "inactive";
        the_profile.activator.setStyle({
          backgroundPosition: "-55px 0"
        });
      } 
      else
      {
        deactivateProfiles();
        the_profile.state = "active";
        the_profile.activator.setStyle({
          backgroundPosition: "0 0"
        });
      }
    });
  }
  
  function deactivateProfiles()
  {
    profiles_array.each(function(element) {
      if(element.state = "active")
      {
        element.state = "inactive";
        element.activator.setStyle({
          backgroundPosition: "-55px 0"
        });
      }
    });
  }
}

function Light() 
{ 
  this.light_actions = light_actions;
  this.light_initial = light_initial;
  
  function light_initial()
  {
    if(this.state == "on")
    {
      this.activator.setStyle({
        backgroundPosition: "-61px 0"
      });
    }
    else
    {
      this.activator.setStyle({
        backgroundPosition: "-1px 0"
      });
    }
  }
  
  function light_actions()
  {
    var the_light = this;
    
    this.activator.observe('click', function(event) {
      if(the_light.state == "off")
      {
        the_light.state = "on";
        the_light.activator.setStyle({
          backgroundPosition: "-61px 0"
        });
        allLightsOnCheck();
      }
      else
      {
        the_light.state = "off";
        the_light.activator.setStyle({
          backgroundPosition: "-1px 0"
        });
        allLightsOffCheck();
      }
    });
  }
}

function allLightsOffCheck() 
{
  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";
    all_light.activator.setStyle({
      backgroundPosition: "-1px 0"
    });
  }
}

function allLightsOnCheck() 
{
  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";
    all_light.activator.setStyle({
      backgroundPosition: "-96px 0"
    });
  }
}

function Switch()
{
  this.switch_actions = switch_actions;
  this.switch_initial = switch_initial;
  
  function switch_initial()
  {
    if(this.state == "on")
    {
      this.activator.setStyle({
        backgroundPosition: "-61px 0"
      });
    }
    else
    {
      this.activator.setStyle({
        backgroundPosition: "-1px 0"
      });
    }
  }
  
  function switch_actions()
  {
    var the_switch = this;
    
    this.activator.observe('click', function(event) {
      if(the_switch.state == "off")
      {
        the_switch.state = "on";
        the_switch.activator.setStyle({
          backgroundPosition: "-61px 0"
        });
      }
      else
      {
        the_switch.state = "off";
        the_switch.activator.setStyle({
          backgroundPosition: "-1px 0"
        });
      }
    });
  }
}

function SpeedsetSpeed()
{
  this.speed_initial = speed_initial
  
  function speed_initial()
  {
    this.type_object.update(""+ this.speed.toFixed(1) +"");
  }
}
