User:ValkyrieWiki/common.js

function do_countdown(widget) { // UTC+9 is JP time var tzdiff = (new Date.getTimezoneOffset / 60) + 9; var tzdiff_ms = tzdiff * 60 * 60 * 1000; var now = new Date; var time = now.getTime; var jptime = new Date(time + tzdiff_ms); var target = widget.attr("data-target"); var target_date; var is_maintenance_countdown = (target === undefined); var widget_head = widget.find(".head").first; var widget_date = widget.find(".date").first; var widget_body = widget.find(".body").first; if(is_maintenance_countdown) { widget_head.html("Time until weekly maintenance:"); var maintenance_day = 4; var maintenance_hour = 10; var maintenance_stamp = new Date(jptime.getFullYear,			jptime.getMonth,			jptime.getDate + (7 + maintenance_day - jptime.getDay) % 7,			maintenance_hour); target_date = maintenance_stamp; } else { widget_head.html(widget.attr("data-title") || "Time remaining in event:"); target_date = Date.parse(target); }	var target_stamp = new Date(target_date + tzdiff_ms); widget_date.html(target_stamp.toLocaleString({ timeZone: "Asia/Tokyo", hour12: false })); var ms_til_target = target_stamp - jptime; var minutes_til_target = ms_til_target / 1000 / 60; var diff_days = Math.floor(minutes_til_target / 60 / 24); var diff_hours = Math.floor(minutes_til_target / 60) % 24; var diff_minutes = Math.floor(minutes_til_target % 60); if(ms_til_target < 0) { widget_body.html("Event finished"); } else { widget_body.empty .append("Days:") .append($(" ", { class: "days", html: diff_days })) .append(" Hours:") .append($(" ", { class: "hours", html: diff_hours })) .append(" Minutes:") .append($(" ", { class: "minutes", html: diff_minutes })); } } function initialize_time_widget { $(".timewidget").each(function(index, widget) {		$(widget).empty			.append($(" ", { class: "head" }))			.append($(" ", { class: "date" }))			.append($(" ", { class: "body" }));	}); var update = function { $(".timewidget").each(function(index, widget) {			do_countdown($(widget));		}); };	if($(".timewidget").length > 0) { setInterval(update, 1000); }	update; } $(document).ready(initialize_time_widget);