﻿// アナログ時計

var aclock_base = "/my_home_page/files/clock.gif";   // 文字盤の画像
var aclock_hour = "/my_home_page/files/hour.gif";    // 時針の画像
var aclock_minute = "/my_home_page/files/minute.gif"; // 分針の画像
var aclock_second = "/my_home_page/files/second.gif";  // 秒針の画像

////////////////////////////////////////////////////////////////////

var aclock_hands = new Array();

aclock_init();

function aclock_init() {
	var ua = navigator.userAgent;
	var idx = ua.indexOf("MSIE ");
	if(!(idx > 0 && ua.indexOf("Windows") > 0)) return;
	var ver = parseFloat(ua.substr(idx + 5));
	if(ver < 6) return;
	
	document.write("<div id='imp_aclock'>"
		+ "<img src='" + aclock_base + "' style='border:none' /></div>");
	
	setTimeout("aclock_init2()", 500);
}

function aclock_init2() {
	var clock = document.getElementById("imp_aclock");
	if(!clock) {
		setTimeout("aclock_init2()", 500);
		return;
	}
	
	if(clock.parentNode.tagName == "A") {
		var pa = clock.parentNode;
		var papa = pa.parentNode;
		clock = pa.removeChild(clock);
		papa.removeChild(pa);
		papa.appendChild(clock);
	}
	
	var base = clock.firstChild;
	if(!base.complete) {
		setTimeout("aclock_init2()", 500);
		return;
	}
	
	clock.style.position = "relative";
	
	var i;
	for(i = 0; i < 3; i++) {
		var url;
		if(i == 0) url = aclock_second;
		else if(i == 1) url = aclock_minute;
		else if(i == 2) url = aclock_hour;
		
		var hand = document.createElement("DIV");
		aclock_hands[i] = hand;
		hand.style.visibility = "hidden";
		hand.style.position = "absolute";
		hand.style.width = clock.firstChild.offsetWidth + "px";
		
		var img = document.createElement("IMG");
		img.src = url;
		img.style.border = "none";
		hand.appendChild(img);
		
		clock.appendChild(hand);
		
		hand.style.cssText += "; filter:progid:DXImageTransform.Microsoft.Matrix(M11=-1,M12=0,M21=0,M22=1)";
		if(!hand.filters || !hand.filters.length) return false;
		
		hand.style.pixelLeft = 0;
		hand.style.pixelTop = 0;
		hand.style.pixelWidth  = clock.firstChild.offsetWidth;
		hand.style.pixelHeight = clock.firstChild.offsetHeight;
	}
	
	aclock_timer();
}

function aclock_timer() {
	var clock = document.getElementById("imp_aclock");
	
	var now = new Date();
	var hour = now.getHours();
	if(hour > 11) hour -= 12;
	var minute = now.getMinutes();
	var second = now.getSeconds();
	
	var i;
	for(i = 0; i < 3; i++)
	{
		var hand = aclock_hands[i];
		var img = hand.childNodes[0];
		if(!img.complete) continue;
		
		var n, rad;
		if(i == 0)
			rad = (second * 6 - 90) * (Math.PI * 2 / 360);
		else if(i == 1)
			rad = ((minute * 60 + second)/60 * 6 - 90)
				* (Math.PI * 2 / 360);
		else if(i == 2)
			rad = ((hour * 60 + minute)/60 * 30 - 90)
				* (Math.PI * 2 / 360);
		
		hand.filters[0].M11 = Math.cos(rad);
		hand.filters[0].M12 = -Math.sin(rad);
		hand.filters[0].M21 = Math.sin(rad);
		hand.filters[0].M22 = Math.cos(rad);
		hand.filters[0].Dx = clock.firstChild.offsetWidth/2
			+ img.height/2 * Math.sin(rad);
		hand.filters[0].Dy = clock.firstChild.offsetHeight/2
			- img.height/2 * Math.cos(rad);
		
		if(hand.style.visibility = "hidden")
			hand.style.visibility = "visible";
	}
	
	setTimeout("aclock_timer()", 1000 - now.getMilliseconds());
}
