/*
 *	お天気ページ
 *	TOPページにアイコン表示
 *	@Hayato Chiba
 *
 */

 var ShowIcons = function(dataManager){
 	this.dataManager = dataManager;
 };
 /*
  * 天気アイコンをを表示
  */
 ShowIcons.prototype.showWeather = function(day){
  	
 // 	var map = new GMap2(document.getElementById("map"));
	
 	map.clearOverlays();
	map.setCenter(new GLatLng(35.674169, 136.862834), 4);
	for (var num = 0;num < ELEVEN_AREA_ID.length; num++){
		var imgName = this._getImgName(day, num);
		this._showWeatherIcon(day, num, imgName);
 	}
 	
 }
 /*
  * 気温アイコンを表示
  */
// ShowIcons.prototype.showTemp = function(day){
// 	
////	var map = new GMap2(document.getElementById("map"));
////	map.disableDragging();
//	map.setCenter(new GLatLng(35.674169, 136.862834), 4);
// 	
// 	var dir_name;
// 	switch (day){
// 		case TODAY:
// 			dir_name = "today";
// 		break;
// 		case TOMORROW:
// 			dir_name = "tomorrow";
// 		break;
// 		case AFTER_TOMORROW:
// 			dir_mame = "after_tomorrow";
// 	}
// 	
// 	for (var num = 0;num < ELEVEN_AREA_ID.length; num++){
//		var imgName = "/img/" + dir_name + "/kion-" + num + ".png";
//		
//		this._showIcon(map, day, num, imgName);
// 	}
// 	
// }

  //
 //	更新日時を表示
 //
 ShowIcons.prototype.showDateTime = function(){

 	var dateTime = this.dataManager.getRecordTime();
 	var y = dateTime.substr(0,4);
 	var m = dateTime.substr(4,2);
 	var d = dateTime.substr(6,2);
 	var h = dateTime.substr(8,2);
 	var mm = dateTime.substr(10,2);
 	
 	$("#dateTime").text(sprintf("%s年%s月%s日　%s:%s　",y,m,d,h,mm));
 	
 }
 
 /* private-------------------------------------------------- */

 //
 //	アイコンを表示する
 //
 ShowIcons.prototype._showIcon = function(day,num,imgName){
	var icon = new GIcon;
	icon.image = imgName;
	icon.iconSize = new GSize(ICON_TEMP_X, ICON_TEMP_Y);
	icon.shdowSize = new GSize(ICON_TEMP_X + 5,ICON_TEMP_Y + 5);
	icon.iconAnchor = new GPoint(ICON_TEMP_X /2, ICON_TEMP_Y / 2);
	icon.infoWindowAnchor = new GPoint(ICON_TEMP_X /2, ICON_TEMP_Y / 2);　　	
	var latlng = ELEVEN_AREA_LATLNG[ELEVEN_AREA_ID[num]];
	var marker = new GMarker(new GLatLng(latlng[0] / 100, latlng[1] /100), {icon: icon, draggable: false, dragCrossMove: false, title: "天気"});
			
	map.addOverlay(marker);
 	
 }
 //
 //	天気アイコンを表示する
 //
 ShowIcons.prototype._showWeatherIcon = function(day, num, imgName){

 	var icon = new GIcon;
	icon.image = imgName;
	icon.iconSize = new GSize(ICON_X, ICON_Y);
	icon.shdowSize = new GSize(ICON_X + 5,ICON_Y + 5);
	icon.iconAnchor = new GPoint(ICON_X /2, ICON_Y / 2);
	icon.infoWindowAnchor = new GPoint(ICON_X /2, ICON_Y / 2);　　	
	var latlng = ELEVEN_AREA_LATLNG[ELEVEN_AREA_ID[num]];
	var marker = new GMarker(new GLatLng(latlng[0] / 100, latlng[1] /100), {icon: icon, draggable: false, dragCrossMove: false, title: "天気"});
			
	map.addOverlay(marker);

	var message = this._makeMessage(day, num);
	GEvent.addListener(marker, "click", function(){
		marker.openInfoWindowHtml(message);
	}); 	
		 	
 }
 
 //
 //	天気アイコン画像名の生成
 //	
 ShowIcons.prototype._getImgName = function(day, num){

 	var weather = this.dataManager.getWeather(day, num);
 	
 	var word = {"晴":"hare",
 	            "雨":"ame",
 	            "雪":"yuki",
 	            "曇":"kumori",
 	            "霧":"kumori"
 	            };
  	var w_array = [];
  	var w_index = [];
  	var count = 0;
  	for (var key in word){
  		if (weather.indexOf(key) >= 0 && count < 2){
  			w_array[count] = word[key];
  			w_index[count] = weather.indexOf(key);
  			count++;
  		}
  	}
  	if (w_index[0] > w_index[1]){
  		var temp = w_array[0];
  		w_array[0] = w_array[1];
  		w_array[1] = temp;
  	}
  	
  	var con = "-";
  	if (weather.indexOf("時々") >= 0 ){
  		con = "-";
  	}else{
  		con = "_";
  	}
  	
 	var imgName = "/img/weather_icon/icon-";
 	if (count == 1){
 		imgName += w_array[0] + ".png";
 	}else{
 		imgName += w_array[0] + con + w_array[1] + ".png";
 	}	 	
 	return imgName;

 }
 //
 //	クリックしたときの表示メッセージ作成
 //
 ShowIcons.prototype._makeMessage = function(day, num){

 	var weather = this.dataManager.getWeather(day, num);
 	var temp = this.dataManager.getTemp(day, num);
 	var rain = this.dataManager.getRain(day, num);
 	var disp_day; 
 	switch (day) {
 		case TODAY:
 			disp_day = "きょうの";
 		break;
 		case TOMORROW:
 			disp_day = "あすの";
 		break;
 		case AFTER_TOMORROW:
 			disp_day = "あさっての";
 		break;	
 	}
 	var link = "/" + ELEVEN_LARGE_AREA[num] + "/" + ELEVEN_LARGE_AREA[num] + ELEVEN_AREA_ID[num] + ".html";
 	var msg = "<h4>" + disp_day + ELEVEN_AREA_NAME[num] + "の天気</h4>" +
 	          "<p>"  + weather  + "</p>" +
 	          "<p>"  + temp     + "</p>" +
 	          "<p>"  + rain     + "</p>" +
 	          '<p><a href="' + link + '">[詳細へ]</a></p><br />';
 	return msg;
 	
 }
