//各レイヤー部に関するスタイルシート読み込み
if (document.getElementById) {
	document.write("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"css/sheetsLayers.css\">");
	window.onload = init;
}

//ブラウザ判別用フラグ
mac = (navigator.appVersion.indexOf("Mac")>-1) ? true : false;	//Mac
ie = (document.all)? true : false;	//IE
gecko = (navigator.userAgent.indexOf("Gecko/") > -1)? true : false;	//Gecko（Safari含まず）

spRate = 20;	//スライド移動時のフレームレート(ミリ秒)
sp = 40;		//スライド移動時の1フレームあたりの移動距離（ピクセル）
obj = null;		//現在表示中のレイヤー名

//ウインドウの読み込み完了時の初期設定
function init() {
	//bodyオブジェクト参照法の振り分け
	bodyObj = ((ie && !mac) || (mac && !gecko && !ie)) ? document.documentElement : document.body;
	
	//背面レイヤーのクリック防止のためのダミーレイヤーをbodyの末尾に追加
	document.body.appendChild(document.createElement('div')).setAttribute("id","dummy");
	if (ie && !mac) { document.getElementById("dummy").style.position = "absolute"; }
	
	//Windows IE用にレイヤーのpositionプロパティをabsoluteに変更しつつウインドウ表示エリア外に配置
	div = document.getElementsByTagName("div");
	for(i=0; i<div.length; i++) {
		with (div[i]) {
			if (className == "sheets") {
				if (ie && !mac) { style.position = "absolute"; }
				style.top = -(offsetHeight) + "px";
				style.visibility = "hidden";
			}
		}
	}
}

//ウインドウサイズを取得
function getWindowSize() {
	windowH = (ie) ? bodyObj.clientHeight : window.innerHeight;
	windowW = (ie) ? bodyObj.clientWidth : window.innerWidth;
	if (obj) { rePositioning(); }
}

//ウインドウのリサイズ・スクロール時の配置調整
function rePositioning() {
	//スクロール分含む基準座標
	offsetY = (ie && !mac) ? bodyObj.scrollTop : 0;
	offsetX = (ie && !mac) ? - document.body.offsetLeft: 0;
	
	//ダミーレイヤーを配置
	with (document.getElementById("dummy")) {
		style.top = offsetY + "px"; style.left = offsetX  + "px";
		style.width = windowW + "px"; style.height = windowH + "px";
	}
	
	//前面レイヤーを常にセンタリング
	with (document.getElementById(obj)) { style.left = ((windowW - offsetWidth)/2) + "px"; }
	scrollTo(0,savedY);
}

//レイヤーを表示（初期化）
function showLayer(n) {
	obj = n;
	window.onresize = window.onscroll = getWindowSize;
	savedY = bodyObj.scrollTop;

	//スクロールバーを消す（IEのみ）
	if (ie && !mac) { bodyObj.style.overflowX = bodyObj.style.overflowY = "hidden"; }
	else if (ie) { bodyObj.style.overflow = "hidden"; }

	getWindowSize();

	//レイヤーの配置を初期化
	with (document.getElementById(obj)) {
		style.top = offsetY - offsetHeight + "px";
		style.visibility = "visible";
	}
	
	document.getElementById("dummy").style.display = "block";
	loop = setInterval("showLayerLoop()",spRate);
}

//レイヤーを表示（ループ）
function showLayerLoop() {
	with (document.getElementById(obj)) {
		if ( offsetTop < offsetY ) { style.top = Math.min(offsetY,(offsetTop+sp))+"px"; }
		else {
			style.top = offsetY+"px";
			clearInterval(loop);
			if ((ie && mac) || (!gecko && mac)) { forceRePositioning = setInterval("getWindowSize()",500); }
			else { forceRePositioning = null; }
		}
	}
}

//レイヤーを非表示（初期化）
function hideLayer() {
	if (forceRePositioning) { clearInterval(forceRePositioning); }
	adjustY = (document.getElementById(obj).offsetTop-offsetY);
	loop = setInterval("hideLayerLoop()",spRate);
}

//レイヤーを非表示（ループ）
function hideLayerLoop() {
	with (document.getElementById(obj)) {
		if ((offsetTop - adjustY) > (offsetY-offsetHeight)) { style.top = Math.max((offsetY-offsetHeight),((offsetTop - adjustY)-sp))+"px"; }
		else {
			style.top = (offsetY - offsetHeight)+"px";
			style.visibility = "hidden";
			clearInterval(loop);
			document.getElementById("dummy").style.display = "none";
			
			//スクロールバーを再表示（IEのみ）
			if (ie && !mac) { bodyObj.style.overflowY = "scroll"; bodyObj.style.overflowX = "auto"; }
			else if (ie) { bodyObj.style.overflow = "auto"; }
			obj = null;
			window.onresize = window.onscroll = null;
		}
	}
}