function fix2(num) {
return (num >= 10 ? '' : '0') + num;
}
function addMinute(timeStr) {
var arr = timeStr.split(':');
var hour = parseInt(arr[0]);
var min = parseInt(arr[1]);
min++;
if (min >= 60) {
hour++;
min -= 60;
}
if (hour >= 24)
hour -= 24;
return fix2(hour) + ":" + fix2(min);
}
var _isShow15day = false;
$(function () {
$(window).scroll(function () {
if($(".divCountryClimate").length > 0 && $.isOnScreen($(".divCountryClimate"))){
switchFC(".divCountryClimate");
}
if (($(".divMajorWeather").length > 0 && $.isOnScreen($(".divMajorWeather")) && $(".divMajorWeather").attr("isload") == "false")
|| ($(".divSingleWeather").length > 0 && $.isOnScreen($(".divSingleWeather")) && $(".divSingleWeather").attr("isload") == "false")
){
//set time every minute
setInterval(function () {
$(".current-time").each(function () {
$(this).text(addMinute($(this).text()));
});
}, 60000);
var _width = $(".divMajorWeather").width();
var days = parseInt($(".divMajorWeather").attr("days"));
if ($(".cityWeather" + days + "day").length > 0) {
$(".cityWeather" + days + "day").each(function (i) {
var city = $(this).attr("para");
var country = $(this).parents(".divMajorWeather").attr("para");
if (country.indexOf("|") > 0) {//multi country
country = country.split("|")[i];
}
//alert(city);
$.post("/inc/weather/WeatherHandler.ashx", { cmd: "getMajorCityWeatherData", para: city, country: country, days: days }, function (data) {
//alert(data);
$(".cityWeather" + days + "day[para='" + city + "']").append(data);
var columns = 3; //each line number of city
var space = 15; //each city margin (px)
if (_width <= 680) {
columns = 2;
space = 5;
}
if (days == 5) {// 5 days weather
columns = 2;
space = 28;
if (_width <= 480) {
columns = 1;
space = 5;
}
}
$(".cityWeather" + days + "day[para='" + city + "']").css({ 'width': Math.floor((_width - (columns - 1) * space) / columns) + 'px' });
var w = $(".cityWeather" + days + "day[para='" + city + "']").width();
//alert(w);
$(".w_" + days + "day .w_nextDay").css({ 'width': (w - (days - 1) * 3) / days + 'px' });
});
});
$(".divMajorWeather").prepend('
');
switchFC(".divMajorWeather");
}
if ($('.cityWeather7day').length > 0) {
var para = $('.cityWeather7day').attr("para");
//alert(para);
var ary = para.split("|");
if (ary.length > 0) {
//pageId 模板生成的
$.post("/inc/weather/WeatherHandler.ashx", { cmd: "getSingleCityWeatherData", country: ary[0], para: ary[1], pageId: pageId }, function (data) {
$('.cityWeather7day').append(data);
var w = $('.w_7day').width();
$('.w_7day .w_nextDay').css({ 'width': Math.floor((w - 5 * 1) / 6) + 'px' });
$(".btn15day").click(function () { show15Days(); });
$(".divSingleWeather").prepend('');
switchFC(".divSingleWeather");
//weather90Data:后台传入
if (weather90Data) {
render90weather(weather90Data);
}
});
}
}
switchFC(".divRegionalWeather");
//$('#btnGoogleMap').click(function () {
// initMap();
//});
if ($('#divMapWeather').length > 0) {
window.onload = loadScript;
}
//初始化左侧导航的天气预报
$(".side-weather-container").each(function () {
//nodata
if ($(this).attr("nodata") != undefined) {
console.error("侧边栏天气预报获取城市失败,请检查");
return true;//
}
var country = $(this).attr('country');
var city = $(this).attr('city');
var self = this;
if (!country || !city) {
return;
}
$.post("/inc/weather/WeatherHandler.ashx", {
cmd: "GetSide7DaysWeather",
country: country,
para: city,
pageId: pageId
}, function (res) {
if (!res) {
return;
}
$(self).html(res);
$(self).find(".side-weather__tool").click(function () {
if ($(this).hasClass('side-weather__tool--active')) {
$(this).removeClass('side-weather__tool--active');
$(self).find('.side-weather__more').removeClass('side-weather__more--active');
} else {
$(this).addClass('side-weather__tool--active');
$(self).find('.side-weather__more').addClass('side-weather__more--active');
}
});
$(self).find(".side-weather__change > div").click(function () {
if ($(this).hasClass("side-weather__change__item--active")) {
return;
}
$(self).find('.side-weather__change > div').removeClass("side-weather__change__item--active");
$(this).addClass("side-weather__change__item--active");
var isC = $(this).attr('tt') == 'c';
var unit = isC ? "℃" : "℉";
$(self).find(".side-weather [need-change]").each(function () {
var cmin = parseInt($(this).attr('cmin'));
var cmax = parseInt($(this).attr('cmax'));
cmin = isC ? cmin : parseInt((32 + cmin * 1.8));
cmax = isC ? cmax : Math.ceil((32 + cmax * 1.8));
$(this).text(cmin + unit + " - " + cmax + unit);
});
});
});
});
$(".divMajorWeather").attr("isload", "true");
$(".divSingleWeather").attr("isload", "true");
}
});
});
function switchFC(obj) {
$(obj + " #w_switchC").bind("click", function () { switchFC2(obj, 0); });
$(obj + " #w_switchF").bind("click", function () { switchFC2(obj, 1); });
};
function switchFC2(obj, b) {
if (b == 0) {
$(obj + " #w_switchC").addClass("w_switchCurrent").siblings("#w_switchF").removeClass("w_switchCurrent");
$(obj + " .temp_f").hide();
$(obj + " .temp_c").show();
} else {
$(obj + " #w_switchF").addClass("w_switchCurrent").siblings("#w_switchC").removeClass("w_switchCurrent");
$(obj + " .temp_c").hide();
$(obj + " .temp_f").show();
}
};
function show15Days() {
if (_isShow15day == false) {
$(".w_15day").show();
$(".btn15day").css({ 'background': '#5b5b5b url("https://data.agatetravel.com/images/weather/icon-arrow-up-white-11-10.png") 155px 10px no-repeat' });
_isShow15day = true;
} else {
$(".w_15day").hide();
$(".btn15day").css({ 'background': '#5b5b5b url("/images/weather/icon-arrow-down-white-11-10.png") 155px 10px no-repeat' });
_isShow15day = false;
}
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://maps.google.cn/maps/api/js?key=AIzaSyABggPs381gfIq-pEL6FEX8wx6DZp21ujk&callback=initMap";
document.body.appendChild(script);
}
function initMap() {
var country = $('#divMapWeather').attr("para");
$.post("/inc/weather/WeatherHandler.ashx", { cmd: "getCountryLatLngJson", country: country }, function (data) {
//alert(data);
eval('var o=' + data);
//alert(o.lat);
//alert(o.lng);
//alert(o.zoom);
//alert(o.cityList.length);
var centerLatlng = new google.maps.LatLng(parseFloat(o.lat), parseFloat(o.lng));
var map = new google.maps.Map(document.getElementById('divMapWeather'), { zoom: parseFloat(o.zoom), center: centerLatlng });
for (var i = 0; i < o.cityList.length; i++) {
(function (argMap) {
var cityObj = o.cityList[i];
var cityLatlng = new google.maps.LatLng(parseFloat(cityObj.lat), parseFloat(cityObj.lng));
var marker = new google.maps.Marker({
position: cityLatlng,
map: argMap,
icon: 'https://data.agatetravel.com/images/weather/dot-red-12-12.jpg'
});
if (cityObj.tempC != undefined) {//没有取到气温,就不显示
//alert(cityObj.city + ' undefined');
var infowindow = new google.maps.InfoWindow({
content: '' + cityObj.city + '
' + cityObj.img + '
' + cityObj.tempC + '
oC
' +
cityObj.tempF + '
oF
'
});
infowindow.open(argMap, marker);
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(argMap, marker);
});
}
})(map);
}
});
}
var wt90tem = "";
wt90tem += "";
wt90tem += "";
wt90tem += " ";
wt90tem += " ";
wt90tem += " Sun | ";
wt90tem += " Mon | ";
wt90tem += " Tue | ";
wt90tem += " Wed | ";
wt90tem += " Thu | ";
wt90tem += " Fri | ";
wt90tem += " Sat | ";
wt90tem += "
";
wt90tem += " ";
wt90tem += " ";
wt90tem += "
";
function render90weather(weather90Data) {
$(".wt90").html(wt90tem)
//var weather90Data = [{ "Week": 7, "Month": 3, "Day": 1, "HTemp": 27, "LTemp": 16, "Img": "05.png" }]
//获取某个月的天气数据
function getMonthData(month) {
var data = []
for (let i = 0; i < weather90Data.length; i++) {
var item = weather90Data[i];
if (item.Month == month) {
data.push(item)
}
}
return data;
}
//计算第一个月和最后一个月缺少的天数,补全日历
(function () {
var first = weather90Data[0];
for (var i = first.Day - 1; i > 0; i--) {
var w = parseInt((first.Week - (first.Day - i) + 7) % 7);
w = w == 0 ? 7 : w;
weather90Data.splice(0, 0, {
Month: first.Month,
Day: i,
Week: w,
IsEmpty: true
});
}
function getDaysOfMonth(month) {
var d = new Date();
d.setMonth(month);
d.setDate(0);
return d.getDate();
}
var last = weather90Data[weather90Data.length - 1];
var lessDayCount = getDaysOfMonth(last.Month) - last.Day;
for (let i = 0; i < lessDayCount; i++) {
weather90Data.push({
Month: last.Month,
Day: last.Day + i + 1,
IsEmpty: true
});
}
})()
//切换月 拼接数据并插入
$(".wt90-month").delegate("div", "click", function () {
$(".wt90-month > div").removeClass("active");
$(this).addClass("active");
var m = parseInt($(this).attr("m"));
var data = getMonthData(m);
var offset = (data[0].Week + 7) % 7;
var preRow = -1;
var html = "";
for (let i = 0; i < data.length; i++) {
var row = Math.floor((offset + i) / 7);
// var column = (data[i].Week + 7) % 7;
if (row != preRow) {
if (preRow != -1) {//除了第一行,换行加结尾标签
html += ""
}
html += ""
if (preRow == -1) {//补全开头的空白日期
for (let p = 0; p < offset; p++) {
html += " | ";
}
}
}
html += "";
var hasStr = data[i].IsEmpty ? "" : "has-data";
html += " ";
html += " ";
html += " " + data[i].Day;
html += " ";
if (!data[i].IsEmpty) {
html += " ";
html += " ";
html += " ";
html += " ";
html += " ";
html += " " + data[i].Desc; //根据图片读取信息
html += " ";
html += " ";
html += " ";
html += " " + data[i].LTemp + "℃";
html += " " + parseInt(32 + data[i].LTemp * 1.8) + "℉";
html += " - ";
html += " " + data[i].HTemp + "℃";
html += " " + Math.ceil(32 + data[i].HTemp * 1.8) + "℉";
html += " ";
}
html += " ";
html += " | "
preRow = row;
}
html += "
";
$(".wt90 table tbody").html(html);
//$(".wt90").height($(".wt90").height());
});
//初始化月份显示
var monthEnStr = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var hasMonths = []
for (let i = 0; i < weather90Data.length; i++) {
var item = weather90Data[i];
if (hasMonths.indexOf(item.Month) < 0) {
hasMonths.push(item.Month)
}
}
for (let i = 0; i < hasMonths.length; i++) {
$(".wt90-month").append("" + monthEnStr[hasMonths[i] - 1] + "
")
}
$(".wt90-month > div").eq(0).click();
}