﻿var className = 'weather';
var xml2JsonProxy = 'http://app.drk7.jp/xml2json/';
var webServiceURL = 'http://weather.livedoor.com/forecast/webservice/rest/v1';
var weather = {
  init : function() { 
		//weatherArea();
		//getLWWS(81); 
		}
	,
  onload : function( jSonData ){

		if (jSonData["forecastday"]=="today"){
			var datex=0;
		}else if(jSonData["forecastday"]=="tomorrow"){
			var datex=1;
		}else if(jSonData["forecastday"]=="dayaftertomorrow"){
			var datex=2;
		}

    $( "weather_result" + '_' + jSonData["forecastday"]  ).innerHTML = 
				'<font size="-1">'+edtDateStr(datex)+'</font><br />'+'<img src="' + jSonData[ "image" ][ "url" ] + '" width="40" height="20" />'
				+'<br /><font size="-1">' + jSonData[ "telop" ] +'</font>';
		if (datex==0){
    	$( "weather_result_description" ).innerHTML = '<font size="-1">' + jSonData["description"] + '</font>';
			$( "weather_copyright" ).innerHTML = '<br /><font size="-1">' + '  Powered by<a href="http://weather.livedoor.com/weather_hacks/" target="_blank">'+ "Weather Hacks" + '</a>' +'</font>';
		}

  }
}
function daymonth(year,month){
	var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(month==2 && leapyear(year)) return 29;
	return day[month-1];
}
function leapyear(year){
	return year%4==0 && (year%100!=0 || year%400==0);
}

function edtDateStr(dateflg){
// (i) dateflg 0:today 1:tomorrow 2:dayaftertomorrow

		var dateObj = new Date();
		var theMonth= dateObj.getMonth()+1;
		var theYear = dateObj.getYear();
		var theDatex= dateObj.getDate()+dateflg;

		var endDate=daymonth(theYear,theMonth);

		var theDate = (endDate < theDatex)?theDatex-endDate:theDatex;
		var theMonth =(endDate < theDatex)?theMonth+1:theMonth;

		return theMonth+'月'+theDate+'日';
}






function callLWWS(dayx,cityx){
  var script = document.createElement( 'script' );
  script.charset = 'UTF-8';
  script.src = 
    xml2JsonProxy + 
    'var=' + this.className + 
    '&' + 'url=' + 
    escape( webServiceURL + '?' + 'city=' + cityx + '&day=' + dayx);
//      'city='+ $F( "city" ) + '&' + 'day=' + $F("day")
//    );
  document.body.appendChild( script );

}
function weatherArea(){
  var htm ='<select name="weather_sel" onChange="selLocal(this)">\n';
	htm +='<option value="81" selected>大阪府</option>\n';
	htm +='<option value="79">京都府</option>\n';
	htm +='<option value="80">京都府・舞鶴</option>\n';
	htm +='<option value="82">兵庫県</option>\n';
	htm +='<option value="83">兵庫県・豊岡</option>\n';
	htm +='<option value="84">奈良県</option>\n';
	htm +='<option value="85">奈良県・風屋</option>\n';
	htm +='<option value="77">滋賀県・大津</option>\n';
	htm +='<option value="78">滋賀県・彦根</option>\n';
	htm +='<option value="86">和歌山県</option>\n';
	htm +='<option value="87">和歌山県・潮岬</option>\n';
	htm +='</select>';
	$('sel_weather_area').innerHTML=htm;
}

function getLWWS(city){
		//htmlSet();
		  callLWWS('today',city);
			callLWWS('tomorrow',city);
			callLWWS('dayaftertomorrow',city);
}

function selLocal(oj){
	var cno=81;
	if(oj.selectedIndex != 0){
       cno = oj.options[oj.selectedIndex].value;
	}
	//alert(cno);
	callLWWS('today',cno);
	callLWWS('tomorrow',cno);
	callLWWS('dayaftertomorrow',cno);

}

//window.onload = weather.init;
