
//
// ============================
//  file name : common.js
//  author : staff015
// ============================
//

/////////////////////////////////////////////////////////////////////////////////////////

// smartScroll ver1.7
function smartScroll ()
{	
	var allLinks = document.getElementsByTagName('a');
	for (var i=0;i<allLinks.length;i++)
	{
		var lnk = allLinks[i];
		
		if ((lnk.href && lnk.href.indexOf('#') != -1) && ((lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname)))
		{
			var myHash = lnk.hash.replace(/#/g,"");
			if (!(myHash.length == 0)) //hrefが"#"だった場合は処理を中止
			{
				lnk.onclick = function ()
				{
					//移動先のid名を取得
					var hash = this.hash;
					var targetId = hash.replace(/#/g,"");
				
					if (!document.getElementById(targetId)) return;
					
					//移動先の位置（y座標）
					if (document.all)
					{
						var element = document.getElementById(targetId);
						var targetY = 0;
						do
						{
							targetY += element.offsetTop || 0;
						} while (element = element.offsetParent);
					} else {
						var targetY = document.getElementById(targetId).offsetTop;
					}
					
					var scrollTop = document.body.parentNode.scrollTop || window.pageYOffset || document.body.scrollTop; //現在の表示位置（y座標）
					
					//スクール量の調整			
					var windowHeight = window.innerHeight || document.documentElement.clientHeight; //ウィンドウの高さ	
					var bodyHeight = document.body.offsetHeight; //bodyの高さ
					var footHeight = bodyHeight - targetY;
					var adjust = windowHeight - footHeight;
						
					//移動距離を計算
					if (windowHeight > footHeight)
					{				
						var toY = targetY - scrollTop - adjust;
					} else
					{
						var toY = targetY - scrollTop;
					}
					
					function windowScroll ()
					{
						var moveY = Math.floor(toY*.2);					
						window.scrollBy(0,moveY);
						toY -= moveY; 
						myTimer = setTimeout(windowScroll,30);
						if (moveY == 0) clearTimeout(myTimer);
					}								
					windowScroll();	
					return false;
				};
			}
		}
	}	
}

/////////////////////////////////////////////////////////////////////////////////////////

// popupWindow ver1.2
function popupWindow (url, width, height, option, windowName)
{	
	if (!width) width = window.innerWidth || document.documentElement.clientWidth;
	if (!height) height = window.innerHeight || document.documentElement.clientHeight;
	if (!option) option = 'menubar=yes, toolbar=yes, location=yes, status=yes, scrollbars=yes, resizable=yes';
	if (!windowName) windowName = "popup";
	var x = (screen.availWidth - width)/2;
	var y = (screen.availHeight - height)/4;
	var o = option+', width='+width+', height='+height+', left='+x+', top='+y;
	var blockMessage = "ウィンドウがお使いのブラウザでポップアップブロックされました。\nポップアップブロックを解除してください。";
	var win = window.open(url, windowName, o);
	if (win)
	{
		win.focus();
	} else {
		alert(blockMessage);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

// init
function init()
{
	smartScroll();
}

// addLoadEvent
function addLoadEvent(func)
{
	if (typeof window.addEventListener == 'function')
	{
		window.addEventListener('load', func, false);
		return true;
	} else if (typeof window.attachEvent == 'object')
	{
		window.attachEvent('onload', func);
		return true;
	}
	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	} else {
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

addLoadEvent(init);

