// Copyright (C) ZEROBASE Inc. http://zerobase.jp/
// $Id: common.js,v 1.38 2006/03/27 03:28:55 hideto Exp $
// required: prototype.js http://prototype.conio.net/

window.onload = function () {
	/**
	 * calculate X-coord in a page
	 * @param elem element
	 * @returns {Number} X-coordinate
	 */
	function calculateXcoord(elem) {
		var xCoord = 0;
		while (elem) {
			xCoord += new Number(elem.offsetLeft);
			elem = elem.offsetParent;
		}
		if (navigator.userAgent.indexOf('Mac') != -1 &&
				typeof document.body.leftMargin != 'undefined') {
			xCoord += new Number(document.body.leftMargin);
		}
		return xCoord;
	}
	/**
	 * calculate Y-coord in a page
	 * @param elem element
	 * @returns {Number} Y-coordinate
	 */
	function calculateYcoord(elem) {
		var yCoord = 0;
		while (elem) {
			yCoord += new Number(elem.offsetTop);
			elem = elem.offsetParent;
		}
		if (navigator.userAgent.indexOf('Mac') != -1 &&
				typeof document.body.topMargin != 'undefined') {
			yCoord += new Number(document.body.topMargin);
		}
		return yCoord;
	}
	/**
	 * start smoothscroll action
	 * @param elem_dst element to jump
	 */
	function startScroll(elem_dst) {
		var destY = calculateYcoord(elem_dst);
		var ename = elem_dst.id ? elem_dst.id : elem_dst.name;
		var urlHash = (ename) ? ('#'+ename) : null;
		var interval = 30;  // millisecond
		var steps = 15;  // times
		var crnt = 0;  // counter
		var scrollTop = null;
		if (document.body.parentNode.scrollTop != null) {  //IE
			scrollTop = document.body.parentNode.scrollTop;
		} else if (window.scrollY != null) {  //NN5
			scrollTop = window.scrollY;
		} else if (window.pageYOffset != null) {  //NN4
			scrollTop = window.pageYOffset;
		}
		var diff = destY - scrollTop;
		var timerid = setInterval(function () {
			if (crnt++ < steps) {
				var t = crnt / steps;
				var f = t-1/Math.PI*Math.sin(Math.PI*t);
				var y = scrollTop + diff * f;
				window.scrollTo(0, y);
			} else {
				clearInterval(timerid);
				if (urlHash) {
					document.location.hash = urlHash;
				}
				if (navigator.userAgent.indexOf('Opera 8.01') != -1) {
					document.location.reload();  // Opera8.01 bug
				}
				return true;
			}
		}, interval);
	}
	/**
	 * closure that returns onclick handler for link
	 * @param anchor element to jump
	 * @returns event handler function
	 */
	function cloOnLinkClick(anchor) {
		return function(event) {
			startScroll(anchor);
			Event.stop(event);  // prevent browser default action(jump)
		};
	}
	
	// ページ内リンクでスムーススクロールするハンドラを全リンクに自動設定
	var myURL = document.location.href.replace(document.location.hash, ''); // remove hash
	var regex = '^'+myURL+'#';
	
	var anchorMap = new Object();  // hash -> anchor
	for (var i = 0; i < document.anchors.length; i++) {
		var a = document.anchors[i];
		var aname = a.id ? a.id : a.name;
		anchorMap['#'+aname] = a;
	}
	for (var i = 0; i < document.links.length; i++) {
		var link = document.links[i];
		if (!link.onclick && !link.onmousedown && link.href &&
				link.href.match(regex) && anchorMap[link.hash]) {
			// onclick,onmousedownハンドラ未定義　かつ　ページ内リンク
			var anchor = anchorMap[link.hash];
			Event.observe(link, 'click', cloOnLinkClick(anchor), false);
		}
	}
};

var DebugPrint = Class.create();
DebugPrint.prototype = {
initialize : function (active) {
	this.active = active ? true : false;
},
print : function (str, label) {
	if (!this.active) return;
	var pstr = '';
	if (label) {
		pstr += label + ':' + str;
	} else {
		pstr += str;
	}
	document.body.appendChild(document.createElement('p').appendChild(document.createTextNode('['+pstr+']')));
}
}
var _debugp = new DebugPrint(false);

