var weather = {
  cached: null,
  currentGroup: null,
  groups: null,
  temperatureUnits: "C",
  baseURL: "http://www.tealit.com/weather",
  baseURL: "/weather",

  onTemperatureUnitChange: function() {
    if (document.getElementById('weather_c').checked) {
      this.setTemperatureUnits("C");
    } else {
      this.setTemperatureUnits("F");
    };
  },

  setTemperatureUnits: function(unit) {
    setCookie('temperature_units',unit,30);
    this.temperatureUnits = unit;
    this.show();
  },

  clear: function() {
    var tbody = document.getElementById('weather-cities').tBodies[0];
    while (tbody.rows.length > 0) {  
      tbody.deleteRow(0);
    };
  },

  show: function() {
    this.clear();

    var tbody = document.getElementById('weather-cities').tBodies[0];

    for (var i=0; i<this.cached.length; i++) {
      var city = this.cached[i];
      var row = tbody.insertRow(tbody.rows.length);

      var cellName = row.insertCell(0);
      cellName.className = 'city';
      cellName.innerHTML = city.getAttribute('name') + ":";

      var cellTemp = row.insertCell(1);
      cellTemp.className = 'temperature';
      cellTemp.innerHTML = weather.showTemperature(city.getAttribute('temperature_c')) + ",";
 
      var cellConditions = row.insertCell(2);
      cellConditions.className = 'conditions';
      cellConditions.innerHTML = city.getAttribute("conditions");
    };
  },

  showTemperature: function(t) {
    if (this.temperatureUnits == "C") {
      return t + " C";
    } else {
      return (Math.round(t*1.8 + 32)).toString() + " F";
    };
  },

  updateGroups: function() {
    request = createXMLHttpRequest();
    request.onsuccess = function() {
      var groups = request.xmlhttp.responseXML.getElementsByTagName('group');
      weather.groups = groups;

      weather.currentGroup = 0;
      document.getElementById('weather-next-group').innerHTML = groups[weather.getNextGroup()].getAttribute('name');
      weather.updateWeather(groups[0].getAttribute('id'));
    }
    request.open("GET",weather.baseURL+"/index.php?_a=listgroups",1);
    request.send("");
  },

  getNextGroup: function() {
    var group = this.currentGroup + 1;
    if (group >= this.groups.length) {
      group = 0;
    };
    return group;
  },

  nextGroup: function() { 
    this.currentGroup = this.getNextGroup();
                             
    document.getElementById('weather-next-group').innerHTML = this.groups[this.getNextGroup()].getAttribute('name');
    this.updateWeather(this.groups[this.currentGroup].getAttribute('id'));
  },

  updateWeather: function(group_id) {
    if (getCookie("temperature_units") == "F") { 
      this.temperatureUnits = "F";
    };

    request = createXMLHttpRequest();
    request.onsuccess = function() {
      var cities = request.xmlhttp.responseXML.getElementsByTagName('city');
      weather.cached = cities;
      weather.show();
    }
    //request.open("GET",weather.baseURL+"/index.php?a=_get&group_id="+group_id,1);
    request.open("GET",weather.baseURL+"/index.php?_a=get&group_id="+group_id,1);
    request.send("");
  }
}
