/**
 *  google 地图 天气 工具
 * 
 *  打开: MapWeather.open(map);
 *  关闭: MapWeather.close();
 */

(function(){
	  
	var map = null;
	var mapPreChangeTime = 0;
	var isOpen = false;
	
	//打开地图显示天气功能
	function openWeather(map_){
		
		map = map_;	
		isOpen = true;
		
		map.addListener('zoom_changed', function() {	
			showWeather();
		});
		
		map.addListener('center_changed', function() {
			showWeather();
		});	

		showWeather();		
	}
	
	//关闭地图显示天气功能
	function closeWeather(){
		isOpen = false;
		clearWeatherMarker();
	}
	
	//显示天气标注
	function showWeather(){
		
		if(!isOpen) return;
		
		mapPreChangeTime = new Date().getTime();
		
		setTimeout(function(){
			
			if( new Date().getTime() - mapPreChangeTime > 300){
			
				var bound = map.getBounds();
				var northEast = bound.getNorthEast();
				var southWest = bound.getSouthWest();
				
				//console.info("northEast:<" + northEast.lat() + "," + northEast.lng()+ ">");
				//console.info("southWest:<" + southWest.lat() + "," + southWest.lng()+ ">");

				doJSONP("http://api.openweathermap.org/data/2.5/box/city?bbox=" + northEast.lng() + "," + northEast.lat() + "," + southWest.lng() + "," + southWest.lat() + "," + map.getZoom() + "&lang =zh_cn&cluster=false&appid=ab0c0257867b7907cc95179fbe757422&callback=renderWeatherMarker");
			}
			
		}, 300);
	}
	
	var weatherMarkerArr = [];
	
	window.renderWeatherMarker = function(data){
		
		if(!isOpen) return;
		
		clearWeatherMarker();
		
		for(var i = 0; i < data.cnt; i++){
		
			  var image = 'http://openweathermap.org/img/w/' + data.list[i].weather[0].icon + ".png";

			  var marker = new google.maps.Marker({
				position: {lat: data.list[i].coord.lat, lng: data.list[i].coord.lon},
				map: map,
				icon: image,
				title: data.list[i].name
			  });
			  
			  marker.tempLabel = new WeatherTempLable(map, data.list[i].main.temp + "°C", marker.getPosition());
			  
			  weatherMarkerArr.push(marker);
		}
	}

	function clearWeatherMarker(){
	
		for(var i = 0; i < weatherMarkerArr.length; i++){
		
			if(weatherMarkerArr[i].tempLabel) weatherMarkerArr[i].tempLabel.setMap(null);
			if(weatherMarkerArr[i]) weatherMarkerArr[i].setMap(null);
		}
	}
	
	function doJSONP(url){

		var script=document.createElement("script"); 	
		script.src=url; 
		document.getElementsByTagName("head")[0].appendChild(script);
	}
	
	function WeatherTempLable(map, text, latLng){
			 this.map_ = map;
			 this.text_ = '<div style="font-size:12px; color:#000000; font-weight:bold; white-space:nowrap; border:0px;">'+text+'</div>';
			 this.latLng_ = latLng;
			 this.div_ = null;
			 this.setMap(map);
			 
	}  
	// 继承自  google.maps.OverlayView
	WeatherTempLable.prototype = new google.maps.OverlayView();
	// 当准备将 悬浮层 添加到地图上时 调用
	WeatherTempLable.prototype.onAdd = function(){
		 var div = document.createElement('DIV');
		 div.style.border = 'none';
		 div.style.position='absolute';   
		 div.innerHTML = this.text_;   
		 this.div_ = div;   
		 var panes = this.getPanes();
		 panes.overlayLayer.appendChild(div);
	};
	  
	// 当第一次在地图上显示时 调用
	WeatherTempLable.prototype.draw = function(){
		 var overlayProjection = this.getProjection();
		 var latLng = overlayProjection.fromLatLngToDivPixel(this.latLng_);
		 // 设置层的大小 和 位置
		 var div = this.div_;
		 var size = new google.maps.Size(20, -40); // 修正坐标的值;
		 div.style.left = (latLng.x + size.width) + 'px';
		 div.style.top = (latLng.y + size.height) + 'px';
	};
	
	// 当设置 悬浮层的 setMap(null) 会自动调用 
	WeatherTempLable.prototype.onRemove = function(){
		 this.div_.parentNode.removeChild(this.div_);
		 this.div_ = null;
	};
	
	window.MapWeather = {
		open : openWeather,
		close : closeWeather
	}
	
})();