/**
 * 气象设备类 属性：设备信息 数据信息 方法: 1、地图标注 2、更新地图标注 3、获取数据 4、定时读取数据（数据监控打开相信信息页面时执行）
 * 5、结束监控(关闭相信信息页面时执行)
 * 
 */
var WeatherClass = function() {
	var _this = this;

	/**
	 * 定时轮询时间，单位秒
	 */
	_this.interval = 60;

	/**
	 * 设备信息
	 */
	_this.equipmentInfo = {};
	/**
	 * 数据信息
	 */
	_this.dataInfo = {};

	/**
	 * z状态信息
	 * 
	 */
	_this.statesInfo = {};

	/**
	 * 定时器
	 */
	_this.TimesID = null;

	/**
	 * 地图上的mark图标对象
	 */
	_this.mark = {};

	/**
	 * 地图标注
	 */
	_this.markOnMap = function() {
		//alert(_this.equipmentInfo.status);
		//alert(_this.getWeatherInfopage());
		//_this.getData();
		//alert(_this.makeDataHtml());
		var showmap = new ShowInMap1();
		//showmap.title = "("+ _this.equipmentInfo.name + ")&nbsp;气象站";
		//showmap.title = _this.equipmentInfo.name + "&nbsp;(气象站)";
		showmap.title = _this.equipmentInfo.name; //20110516
		showmap.operationType = 'weather'+_this.equipmentInfo.status;
		showmap.sharpType = 'point';
		showmap.lonlatData = [{x : _this.equipmentInfo.longitude,y : _this.equipmentInfo.latitude}];
		//showmap.isPopup = false;
		showmap.width = 350;
		showmap.heigth = 300;
		showmap.ContentHTML = "<div id=\"" + _this.equipmentInfo.id + "_div\">"+ _this.getData()+"</div>";
		showmap.airTemperature=_this.dataInfo.airTemperature;
		showmap.windDirection=fengxiang(_this.dataInfo.windDirection);
		showmap.windSpeed=fengli(_this.dataInfo.windSpeed);
		showmap.yuliang=getYuliang(_this.dataInfo.precipitationSum);
		showmap.eventId = _this.equipmentInfo.id;
		//showmap.text = _this.equipmentInfo.name;
		//showmap.popOpen = _this.startMonitorData;
		//showmap.popClose = _this.stopMonitorData;
		//showmap.popClose =function(){alert('popClose')};
		_this.mark = showmap.show();
	}
	
	/**
	 * 更新地图标注
	 */
	_this.refMark = function() {

		// 删除图标
		mapObjSearch.removeOverlay(_this.mark);
		//添加新图标
		var showmap = new ShowInMap1();
		//showmap.title = "("+ _this.equipmentInfo.name + ")&nbsp;气象站";
		//showmap.title = _this.equipmentInfo.name + "&nbsp;(气象站)";
		showmap.title = _this.equipmentInfo.name; //20110516
		if (_this.statesInfo.status) {
			showmap.operationType = 'weather' + _this.statesInfo.status;
		} else {
			showmap.operationType = 'weather0';
		}

		showmap.sharpType = 'point';
		showmap.lonlatData = [{x : _this.equipmentInfo.longitude,y : _this.equipmentInfo.latitude}];
		showmap.ContentHTML = "<div id=\"" + _this.equipmentInfo.id + "_div\">"+ "数据加载中....</div>";
		//showmap.isPopup = false;
		showmap.width = 350;
		showmap.heigth = 300;
		showmap.eventId = _this.equipmentInfo.id;
		//showmap.text = _this.equipmentInfo.name;
		//showmap.popOpen = _this.startMonitorData;
		//showmap.popClose = _this.stopMonitorData;
		// showmap.popClose =function(){alert('popClose')};
		_this.mark = showmap.show();

	}
	
	/**
	 * 获取数据
	 */
	_this.getData = function() {
		var data;
		var id = _this.equipmentInfo.id;
		var URL = '/bjglwww/ws/wsi/WeatherService/getNewWeatherData/'+ _this.equipmentInfo.id ;
		var datadiv = document.getElementsByName(id + "_div");	
		
		var XMLHttpReq = false;
		function createXMLHttpRequest() {
		    if(window.XMLHttpRequest) { 
		        XMLHttpReq = new XMLHttpRequest();
		    }else if (window.ActiveXObject) {
				try {
			         XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
				}catch (e) {
			        try {
			              XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			        } catch (e) {	         
			        }
				}
		    }
		}
	  
	function sendRequestGetForecast() {
	    createXMLHttpRequest();
	    if(XMLHttpReq){
	        XMLHttpReq.open("get",  URL, false );
	        XMLHttpReq.onreadystatechange = processResponse;
	        XMLHttpReq.send(null);   
	    }    
	}
	
	function processResponse() {
		if (XMLHttpReq.readyState == 4) { 
	       if (XMLHttpReq.status == 200) {     	  	       
	      	   eval( "var   temp   =   "   +   XMLHttpReq.responseText);
	           } else {
	                 window.alert("气象数据信息读取失败");
	           }
	    }
	    return temp;
	}
		
	sendRequestGetForecast();
	
	try {
			var temp = processResponse();
			var WeatherData = temp.WeatherData;

			if (WeatherData) {
				_this.dataInfo = WeatherData;
				var temp2 = WeatherMap.get(WeatherData.id);
				if (temp2) {
					data = temp2.makeDataHtml();
				}
			}
		} catch (e) {
			alert("读取气象数据" + e.number + "\n" + e.description);
		}
	return data;	
}
	

	//------------------------------------------------------------------------
	
	
	/**
	 * 定时读取数据
	 */
	_this.startMonitorData = function() {
		//alert(id);
		//先调用一次，取得数据
		_this.getData();
		//如果已经开始侧先停止
		if (_this.TimesID) {
			clearInterval(_this.TimesID);
		}
		//定时执行
		_this.TimesID = setInterval(function() {_this.getData();}, _this.interval * 1000);
	}
	/**
	 * 结束监控
	 */
	_this.stopMonitorData = function() {
		if (_this.TimesID) {
			clearInterval(_this.TimesID);
			_this.TimesID = null;
		}

	}
	
	
	/**
	 * 生成数据html
	 * 
	 */
	_this.makeDataHtml = function() {
		var myvisibility = _this.dataInfo.visibility;
		if(parseInt(_this.dataInfo.visibility)==2000)
		{
			myvisibility="大于"+_this.dataInfo.visibility;
		}
		/**
		 *2011-05-20修改 迎检之后改回 
		 */
		var now= new Date();   
		var year=now.getYear();   
		var month=now.getMonth()+1;   
		var day=now.getDate();   
		var hour=now.getHours();   
		var minute=now.getMinutes();   
		//var second = now.getSeconds();
		if(minute>=5){
			minute = minute-5;
		}else{
			minute = minute-5+59;
			hour = hour-1;
		}
		if(minute<10)
			minute = "0"+minute;
		//alert(minute);
		var nowdate=year+"-"+month+"-"+day+" "+hour+":"+minute;
		
		/***/
		
		var html = "<table id=\"popup_table\">" 
				 + "<tr>" 
				 + "<td><table>"
				 + "<tr>" 
				 + "<td>设备编号<\/td>" 
				 + "<td>"
				 + _this.equipmentInfo.id.substring(2) 
				 + "<\/td>"
				 + "<\/tr>" 
				 + "<tr>" 
				 + "<td>日期时间<\/td>"
				 + "<td>" 
				 //+ _this.dataInfo.receiveTime 
				 +nowdate
				 + " <\/td>"
				 + "<\/tr>" 
				
				 + "<tr>"
				 + "<td>能见度<\/td>"
				 + "<td onmouseout=\" showVisibility(false,'Visibility_div',this)\" onmouseover=\"showVisibility(true,'Visibility_div',this)\">"
				 + myvisibility
				 + " 米&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<\/td>"
				 + "<\/tr>"
				
				 + "<tr>" 
				 + "<td>风力<\/td>"
				 + "<td>" 
				 + fengli(_this.dataInfo.windSpeed) 
				 + " 级<\/td>"
				 + "<\/tr>" 
				 + "<tr>" 
				 + "<td>风向<\/td>"
				 + "<td>" + fengxiang(_this.dataInfo.windDirection)
				 + "<\/td>" 
				 + "<\/tr>" 
				 + "<tr>"
				 + "<td>雨量<\/td>"
				 + "<td onmouseout=\" showVisibility(false,'yuliang_div',this)\" onmouseover=\"showVisibility(true,'yuliang_div',this)\">"
				 + getYuliang(_this.dataInfo.precipitationSum)
				 + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<\/td>"
				 + "<\/tr>"
				 + "<tr>"
				 + "<td>雪量<\/td>"
				 + "<td>"
				 + getXueLiang(_this.dataInfo.snowHeight)
				 + " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/td>"
				 + "<\/tr>"
				 + "<tr>" 
				 + "<td>温度<\/td>" 
				 + "<td>"
				 + _this.dataInfo.airTemperature 
				 + " 摄氏度<\/td>" 
				 + "<\/tr>"
				 + "<tr>" 
				 + "<td>相对湿度<\/td>" 
				 + "<td>"
				 + _this.dataInfo.humidity 
				 + " %<\/td>" 
				 + "<\/tr>"
				 + "<tr>" 
				 + "<td>地表温度<\/td>" 
				 + "<td>"
				 + _this.dataInfo.surfaceTemperature1 
				 + " 摄氏度<\/td>"
				 + "<\/tr>" 
				 + "<\/table><\/td>" 
				 + "<\/tr>"
				 + "<\/table>"

		return html;
	}

	
	/**
	 * 计算雨量
	 */
	function getYuliang(sspeed) {
		var yuliang = '无雨';
		// 无雨 0
		if (sspeed == 0) {
			yuliang = '无雨';
		}
		//小雨 0-10毫米
		if (sspeed > 0 && sspeed <= 10) {
			yuliang = '小雨';
		}
		//中雨 10-25毫米
		if (sspeed > 10 && sspeed <= 25) {
			yuliang = '中雨';
		}
		//大雨 25-50毫米
		if (sspeed > 25 && sspeed <= 50) {
			yuliang = '大雨';
		}
		//暴雨 50-100毫米
		if (sspeed > 50 && sspeed <= 100) {
			yuliang = '暴雨';
		}
		//大暴雨 100-200毫米
		if (sspeed > 100 && sspeed <= 200) {
			yuliang = '大暴雨';
		}
		//特大暴雨 大于200毫米
		if (sspeed > 200) {
			yuliang = '特大暴雨';
		}
		return yuliang;
	}
	
	
	/**
	 * 计算雪量
	 */
	function getXueLiang(snow){
		var snow = parseFloat(snow)*10;//厘米数转换成毫米
	    var xueliang = '无雪';
		// 无雪 0
		if (snow == 0) {
			xueliang = '无雪';
		}
		//小雪 0-2.4毫米
		if (snow > 0 && snow <= 2.4) {
			xueliang = '小雪';
		}
		//中雪 2.5-4.9毫米
		if (snow >= 2.5 && snow <= 4.9) {
			xueliang = '中雪';
		}
		//大雪 5.0-9.9毫米
		if (snow >= 5.0  && snow <= 9.9) {
			xueliang = '大雪';
		}
		//暴雪 >=10.0毫米
		if (snow >= 10 ) {
			xueliang = '暴雪';
		}
		return xueliang;
	
	}
	
};

