weatherview_artemis_rel = "";

function weatherview_load(artemis_rel, location)
{
  
  weatherview_artemis_rel = artemis_rel;
  var src =  'view/weatherview/weatherview_loader.php&location=' + location;
  var ajax = new Ajax.Request('ajax.php',
    {method: 'post', evalScripts: true, parameters: 's=' + src, onComplete: do_weatherview_load_complete }
  );    
}

function weatherview_condition_to_image(condition)
{
  condition = condition.toUpperCase();
  var image = "";
  switch(condition)
  {
    case "SCATTERED THUNDERSTORMS":
    case "THUNDERSTORM":
    case "CHANCE OF STORM":
    case "CHANCE OF TSTORM":
    case "STORM":
      image = "storm";
      break;
    case "SHOWERS":
    case "SCATTERED SHOWERS":
    case "FREEZING DRIZZLE":
    case "RAIN AND SNOW":
    case "CHANCE OF RAIN":
    case "RAIN":
    case "LIGHT RAIN":
      image = "rain";
      break;
    case "SLEET":
    case "SNOW":
    case "ICY":
    case "FLURRIES":
    case "LIGHT SNOW":
    case "CHANCE OF SNOW":
      image = "snow";
      break;
    case "OVERCAST":
    case "CLOUDY":
    case "MOSTLY CLOUDY":
      image = "cloudy";
      break;
    case "PARTLY CLOUDY":
    case "MOSTLY SUNNY":
    case "SMOKE":
    case "HAZE":
      image = "partly_cloudy";
      break;
    case "SUNNY":
    case "CLEAR":
    case "DUST":
    case "MIST":
    case "FOG":
    default:
      image = "sunny";
      break;
  }  
  return weatherview_artemis_rel + "/view/weatherview/icons/" + image + ".gif";
}

function do_weatherview_load_complete(xhr)
{
  

  var data; eval("data=" + xhr.responseText);
  var image = weatherview_condition_to_image(data.current.condition["@attributes"].data);
  $('weatherview_image0').src = image;
  $('weatherview_temp0').innerHTML = data.current.temp_c["@attributes"].data;
  var index = 1;
  data.forecast.each(function(forecast) {
    var low_f = forecast.low["@attributes"].data;
    var high_f = forecast.high["@attributes"].data;
    var low_c = Math.round((low_f-32)/1.8);
    var high_c = Math.round((high_f-32)/1.8);
    var celcius = Math.round((low_c + high_c)/2);
    $('weatherview_temp'+index).innerHTML = celcius.toString();
    var image = weatherview_condition_to_image(forecast.condition["@attributes"].data)
    $('weatherview_image'+index).src = image;
    index++;
  });
  $('weatherview_loading').hide();
  $('weatherview_data').show();
}
