/**
 * return 数値
 */
function getVerticalScrollPosition() {
	//var obj = new Object();
	//obj.x = document.documentElement.scrollLeft || document.body.scrollLeft;
	//obj.y = document.documentElement.scrollTop || document.body.scrollTop;
	return (document.documentElement.scrollTop || document.body.scrollTop);
	//return obj;
}

function HeaderManager() {
	// ===== public fields =====
	var mElements = new Array();
	// ===== private fields =====
	var mIntervalID = null;
	var mTargetPosition = 0;
	var mCurrentPosition = 0;
	// ===== constructor =====
	mElements[0] = document.getElementById("header");
	mElements[1] = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "h1").item(0);
	//mElements[2] = document.getElementById("test");
	// ===== public methods =====
	this.setVerticalPosition = function(aPosition) {
		mTargetPosition = aPosition;
		window.setTimeout(checkState, 200, aPosition);
	};
	// ===== private methods =====
	var checkState = function(aPosition) {
		if( mIntervalID == null ) {
			if( mTargetPosition == aPosition ) {
				mIntervalID = window.setInterval(move, 70);
				//window.alert("start!!");
			}
		} else {
			if( mTargetPosition != aPosition ) {
				window.clearInterval(mIntervalID);
				mIntervalID = null;
			}
		}
	}
	var move = function() {
		var d = mTargetPosition - mCurrentPosition;
		if( d < -10 || 10 < d ) {
			d = d * 3 / 4;
		}
		mCurrentPosition += d;
		for( var i = 0; i < mElements.length; i++ ) {
			mElements[i].style.top = mCurrentPosition + "px";
		}
		if( mCurrentPosition == mTargetPosition ) {
			window.clearInterval(mIntervalID);
			mIntervalID = null;
			//window.alert("stop!!");
		}
	};
}

var hm = new HeaderManager();
//var header = document.getElementById("header");
//var h1 = document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "h1");
//var h1 = h1[0];
var el = function(evt) {
	//header.style.top = getVerticalScrollPosition() + "px";
	//h1.style.top     = getVerticalScrollPosition() + "px";
	//window.alert(h1);
	hm.setVerticalPosition( getVerticalScrollPosition() );
};
window.addEventListener("scroll", el, false);
