﻿
$(function(){
    bindHourData("/ashx/GetRealData.ashx", "stationName=乐清");
});

//异步加载,_type请求方式,_url数据源,_fn_success成功执行函数,_fn_error加载出错
function doAjax(_type, _url, _params, _fn_success, _fn_error) {
    $.ajax({
        type: _type,
        url: _url,
        data: _params,
        dataType: "json",
        success: function (data) { eval(_fn_success).call(window, data); },
        error: function () { eval(_fn_error).call(); }
    });
}

//小时站点数据
function doAjaxHourDataHandler(data) {
    if (data.Rows.length > 0) {
        if (data.Rows[0].StationPress != null && data.Rows[0].StationPress != "" && typeof (data.Rows[0].StationPress) == "number") {
            $("#qy").text(data.Rows[0].StationPress / 10 + "hPa");
        }
        else {
            $("#qy").text("暂无数据");
        }
        if (data.Rows[0].Visibility != null && data.Rows[0].Visibility != "" && typeof (data.Rows[0].Visibility) == "number") {
            $("#njd").text(data.Rows[0].Visibility/1000 + "km");
        }
        else {
            $("#njd").text("暂无数据");
        }
        if (data.Rows[0].Precipitation != null && data.Rows[0].Precipitation != "" && typeof (data.Rows[0].Precipitation) == "number") {
            $("#yl").text(data.Rows[0].Precipitation + "mm");
        }
        else {
            $("#ryl").text("暂无数据");
        }
        if (data.Rows[0].RelHumidity != null && data.Rows[0].RelHumidity != "" && typeof (data.Rows[0].RelHumidity) == "number") {
            $("#sd").text(data.Rows[0].RelHumidity + "%");
        }
        else {
            $("#sd").text("暂无数据");
        }

        //时间
        $("#obTempTime").text(GetOBTime(data.Rows[0].ObservTimes));

        //气温
        if (data.Rows[0].DryBulTemp != null && data.Rows[0].DryBulTemp != "" && typeof (data.Rows[0].DryBulTemp) == "number") {
            $("#temp_pic").attr("class", "temp_" + getTempClass(data.Rows[0].DryBulTemp));
            $(".temp_text").text(data.Rows[0].DryBulTemp / 10);
        }
        else {
            //默认给温度计一个样式 
            $("#temp_pic").addClass("temp_30");
            $(".temp_text").text("无数据");
        }

        //风速风向
        if (data.Rows[0].WindDirect != null && data.Rows[0].WindDirect != "" && typeof (data.Rows[0].WindDirect) == "number") {
            $("#wind_dire").attr("class", "wind_" + getfengxiang(data.Rows[0].WindVelocity)).text(data.Rows[0].WindDirect / 10 + "m/s");
            $("#wind_text").text(WindDire(data.Rows[0].WindVelocity));
            $("#wind_desc").text(WindLevel(data.Rows[0].WindDirect/10));
        }
        else {
            $("#wind_dire").addClass("wind_N");
        }
    }
}


//绑定站点小时数据
function bindHourData(_url, _params) {
    var fn = function (data) { doAjaxHourDataHandler(data) };
    doAjax("POST", _url, _params, fn, function () {
        alert("异步加载发生错误!");
    });
}

//根据温度值转换为相应的CSS类名
function getTempClass(tempCode) {
    var tempClass;
    if (parseInt(tempCode) < -150) tempClass = "-15-";
    else if (parseInt(tempCode) == -150) tempClass = "-15";
    else if (parseInt(tempCode) > -150 && parseInt(tempCode) < 0) tempClass = "-15-0";
    else if (parseInt(tempCode) == 0) tempClass = "0";
    else if (parseInt(tempCode) > 0 && parseInt(tempCode) < 150) tempClass = "0-15";
    else if (parseInt(tempCode) == 150) tempClass = "15";
    else if (parseInt(tempCode) > 150 && parseInt(tempCode) < 300) tempClass = "15-30";
    else if (parseInt(tempCode) == 300) tempClass = "30";
    else if (parseInt(tempCode) > 300 && parseInt(tempCode) < 450) tempClass = "30-45";
    else if (parseInt(tempCode) == 450) tempClass = "45";
    else if (parseInt(tempCode) > 450) tempClass = "45U";
    return tempClass;
}

function getfengxiang(data) {
			var angle = parseFloat(data);
			if (angle >= 348.76 || angle <= 11.25) {
				return "N";
			}
			if (angle >= 11.26 && angle <= 33.75) {
				return "NNE";
			}
			if (angle >= 33.76 && angle <= 56.25) {
				return "NE";
			}
			if (angle >= 56.26 && angle <= 78.75) {
				return "ENE";
			}
			if (angle >= 78.76 && angle <= 101.25) {
				return "E";
			}
			if (angle >= 101.26 && angle <= 123.75) {
				return "ESE";
			}
			if (angle >= 123.76 && angle <= 146.25) {
				return "SE";
			}
			if (angle >= 146.26 && angle <= 168.75) {
				return "SSE";
			}
			if (angle >= 168.76 && angle <= 191.25) {
				return "S";
			}
			if (angle >= 191.26 && angle <= 213.75) {
				return "SSW";
			}
			if (angle >= 213.76 && angle <= 236.25) {
				return "SW";
			}
			if (angle >= 236.26 && angle <= 258.75) {
				return "WSW";
			}
			if (angle >= 258.76 && angle <= 281.25) {
				return "W";
			}
			if (angle >= 281.26 && angle <= 303.75) {
				return "WNW";
			}
			if (angle >= 303.76 && angle <= 326.25) {
				return "NW";
			}
			if (angle >= 326.26 && angle <= 348.75) {
				return "NNW";
			}
			return "-";
		}
		
		function WindDire(data)
		{
		var angle = parseFloat(data);
		if (angle >= 348.76 || angle <= 11.25) {
				return "北";
			}
			if (angle >= 11.26 && angle <= 33.75) {
				return "北东北";
			}
			if (angle >= 33.76 && angle <= 56.25) {
				return "东北";
			}
			if (angle >= 56.26 && angle <= 78.75) {
				return "东东北";
			}
			if (angle >= 78.76 && angle <= 101.25) {
				return "东";
			}
			if (angle >= 101.26 && angle <= 123.75) {
				return "东东南";
			}
			if (angle >= 123.76 && angle <= 146.25) {
				return "东南";
			}
			if (angle >= 146.26 && angle <= 168.75) {
				return "南东南";
			}
			if (angle >= 168.76 && angle <= 191.25) {
				return "南";
			}
			if (angle >= 191.26 && angle <= 213.75) {
				return "南西南";
			}
			if (angle >= 213.76 && angle <= 236.25) {
				return "西南";
			}
			if (angle >= 236.26 && angle <= 258.75) {
				return "西西南";
			}
			if (angle >= 258.76 && angle <= 281.25) {
				return "西";
			}
			if (angle >= 281.26 && angle <= 303.75) {
				return "西";
			}
			if (angle >= 303.76 && angle <= 326.25) {
				return "西北";
			}
			if (angle >= 326.26 && angle <= 348.75) {
				return "北西北";
			}
			return "-";
		}
		
		function WindLevel(level) {
		    var windVelocity=parseFloat(level);
		        
				if (windVelocity <= 0.2) {
					return "0级(无风)";
				}
				if (windVelocity >= 0.3 && windVelocity <= 1.5) {
					return "1级(软风)";
				}
				if (windVelocity >= 1.6 && windVelocity <= 3.3) {
					return "2级(轻风)";
				}
				if (windVelocity >= 3.4 && windVelocity <= 5.4) {
					return "3级(微风)";
				}
				if (windVelocity >= 5.5 && windVelocity <= 7.9) {
					return "4级(和风)";
				}
				if (windVelocity >= 8.0 && windVelocity <= 10.7) {
					return "5级(劲风)";
				}
				if (windVelocity >= 10.8 && windVelocity <= 13.8) {
					return "6级(强风)";
				}
				if (windVelocity >= 13.9 && windVelocity <= 17.1) {
					return "7级(疾风)";
				}
				if (windVelocity >= 17.2 && windVelocity <= 20.7) {
					return "8级(大风)";
				}
				if (windVelocity >= 20.8 && windVelocity <= 24.4) {
					return "9级(烈风)";
				}
				if (windVelocity >= 24.5 && windVelocity <= 28.4) {
					return "10级(狂风)";
				}
				if (windVelocity >= 28.5 && windVelocity <= 32.6) {
					return "11级(暴风)";
				}
				if (windVelocity >= 32.7) {
					return "12级(飓风)";
				}
				else {
					return string.Empty;
				}				
		}
