
function menu_item(
  name,
  href
) 
{
  var name = name;
  var href = href;
  
  this.print = function(
    html_obj
  )
  {
    html_obj.innerHTML += "<a href=\"" + href + "\" class=\"button\">" + 
      name + "</a>";          
  }  
}

function meteo_page(  
)
{
  var menu = null;
  var battery = null;
  var supply = null;
  var measurements = ["temperature", "humidity", "pressure",
                    "wind_spd", "wind_dir", "rain"];
  
  var menu_items = [
    new menu_item("Home", "index.html"),
    new menu_item("O Projekte", "about.html"),
  ];
  
  this.init = function(
  )
  {
    var item;
    
    menu = document.getElementById("menu");
    battery = document.getElementById("battery");;
    supply = document.getElementById("supply");
    
    for(item = 0; item < menu_items.length; item ++)
    {
      menu_items[item].print(menu);    
    }    
    this.set_power_info(0, "--", 0);
  }
  
  this.set_power_info = function(
    chrg,
    level,    
    ext_pwr
  )
  {
    var icon = 3;
    if(chrg == 0)
    {
      icon = Math.ceil(level/33) - 1;
      if(icon > 2)
      {
        icon = 2;
      }
    }    
     battery.innerHTML = level + " %";        
     battery.style.backgroundPosition = "0px " + (-40 * icon) + "px";
     supply.style.backgroundPosition =
      "0px " + (ext_pwr ? -160 : 40) + "px";    
  }
  
  this.set_measurement = function(
    measurement,
    value
  )
  {        
    document.getElementById(measurements[measurement]).
      getElementsByTagName('div')[0].innerHTML = value;
  }
  
  this.update_all = function()
  {
    var wind_dirs =  ["S", "SV", "V", "JV", "J", "JZ", "Z", "SZ", "Err"];
    var ajax = GetXmlHttp();
    if(ajax)
    {
      ajax.onreadystatechange = (function(_this)
      {
        return function()
        {
          if (ajax.readyState==4 && ajax.status==200)
          {
            var meas = JSON.parse(ajax.responseText);
            _this.set_measurement(0, meas["temp"]/10 + " °C");
            _this.set_measurement(1, meas["hum"] + " %");
            _this.set_measurement(2, meas["press"] + " hPa");
            _this.set_measurement(3, meas["wind_spd"]/100 + " m/s");
            _this.set_measurement(4, wind_dirs[meas["wind_dir"]]);
            _this.set_measurement(5, meas["rain"] + " mm");
            
            _this.set_power_info(
              meas["chrg"],
              meas["bat"],
              meas["ext_pwr"]);          
          }
        }
      })(this);
      ajax.open("GET", "dyn/current", true);
      ajax.send(null);            
    }                
  }
  
  this.update_supply = function()
  {    
    var ajax = GetXmlHttp();
    if(ajax)
    {
      ajax.onreadystatechange = (function(_this)
      {
        return function()
        {
          if (ajax.readyState==4 && ajax.status==200)
          {
            var meas = JSON.parse(ajax.responseText);
                        
            _this.set_power_info(
              meas["chrg"],
              meas["bat"],
              meas["ext_pwr"]);          
          }
        }
      })(this);
      ajax.open("GET", "dyn/supply", true);
      ajax.send(null);      
    }                
  }
  
  function GetXmlHttp()
  {
    var xmlhttp = false;
    if (window.XMLHttpRequest)
    {
      xmlhttp = new XMLHttpRequest()
    }
    else if (window.ActiveXObject)// code for IE
    {
      try
      {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
      } catch (e)
      {
        try
        {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
        } catch (E) 
        {
          xmlhttp=false
        }
      }
    }
    return xmlhttp;
  }   
}

var this_page = new meteo_page();



                    


