var yp = new Array();
var idx = 0;

function weather_badge (nws_id, div_name) {

    var callback_obj = "yp[" + idx + "]";

    var url = "http://pipes.yahoo.com/pipes/pipe.run?" 
	+ "&_id=CI6HgSh43BGP8nmJouNLYQ" 
	+ "&textinput1=" + nws_id
	+ "&_run=1" 
	+ "&_render=json";
  
    yp[idx] = new YPipesWeather (url, div_name, callback_obj);
    yp[idx].requestJSON ();
    idx++;
}

// The YPipesWeather constructor

function YPipesWeather (ypipes_url, div_name, obj_name) {
  this.url = ypipes_url + "&_callback=" + obj_name + ".jsonHandler";
  this.div_name = div_name;
}

// The requestJSON method: it builds the script tag 
// that launches our request to the Yahoo! server.

YPipesWeather.prototype.requestJSON = function () {

  // Dynamically create a script tag.  This initiates 
  // a request to the Yahoo! server.

  var head = document.getElementsByTagName("head").item(0);
  var script = document.createElement ("script");
  script.src = this.url;
  head.appendChild (script);
}

// The jsonHandler method: this is our callback function.
// It's called when the JSON is returned to our browser
// from Yahoo!.

YPipesWeather.prototype.jsonHandler = function (json) {
  var div = document.getElementById (this.div_name);

  var weather = json.value.items[0];
  var viskilo = (weather.visibility_mi * 8)/5;

  var html = "";

  html += "<center>\n";

  html += "<b>" + "サンディエゴの天気" + "</b>" + "<br><br>" + "サンディエゴ国際空港" + "<br><br>\n";

//  html += weather.weather + "<br>";

  html += "<img border='0' src='" 
       + weather.icon_url_base 
       + weather.icon_url_name + "'><br><br>";

  html += "温度 : " + weather.temp_c + "<br>";

  html += "風 : " + weather.wind_kt + " 時速" + "<br>";
  html += "湿度 : " + weather.relative_humidity + "%<br>";
  html += "視程 : " + viskilo + " キロ<br>";
  
  html += "<br><span style='font-size: 0.8em; font-weight: bold;'>" 
  + "最終更新日" + "<br>" + weather.observation_time_rfc822 + "</span><br>";

//  html += "<br><span style='font-size: 0.8em; font-weight: bold;'>" 
//  + weather.observation_time + "</span><br>";

  html += "</center>\n";

  div.innerHTML = html;

}



