
//
// ============================
//  file name : common.js
//  author : staff015
// ============================
//


/////////////////////////////////////////////////////////////////////////////////////////

// smartScroll 1.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
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)/2;
	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();
		return true;
	} else {
		alert(blockMessage);
		return false;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

/**
 * setMinSize
 */
function setMinSize(id,minW,minH) {
	var html = document.getElementsByTagName("html")[0];
	var body = document.body;
	var div = document.getElementById(id);
	var windowWidth, windowHeight;
	
	function onset() {
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight){
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		
		if (windowWidth < minW){
			div.style.width = minW + "px";
		} else {
			div.style.width = "100%";
		}
		
		if (windowHeight < minH){
			body.style.height = minH + "px";
		} else {
			body.style.height = "100%";
		}
		
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			html.style.overflow = "hidden";
				
			if (windowWidth < minW || windowHeight < minH){
				html.style.overflow = "scroll";
			} else {
				html.style.overflow = "hidden";
			}
		}
	}
	onset();
	window.onresize = onset;
};


/////////////////////////////////////////////////////////////////////////////////////////

function setSlideshow (idName,path,fileName,images,interval)
{
	if (!document.getElementById(idName)) return; 
	var targetId = document.getElementById(idName);
	var targetIdChild = targetId.getElementsByTagName('span')[0];
	var imgNumber = Math.floor(Math.random() *images) + 1;
	var imgUrl = 'url("'+ path + fileName + imgNumber + '.jpg")';
	var imgNumberCopy;
	targetId.style.backgroundImage = imgUrl;
	targetIdChild.style.backgroundImage = imgUrl;
	var timer = 20;
	var speed = 0.5;
	var flag = true;
	var n = 100;
	
	function fade ()
	{
		flag = !flag;

		if (flag)
		{
			function opacityOff ()
			{
				n -= speed;
				targetIdChild.style.MozOpacity = (n * 0.01);
				targetIdChild.style.opacity = (n * 0.01);				
				if (document.all) targetIdChild.style.filter = "alpha(opacity=" + n + ")";
				
				var myTimer = setTimeout(opacityOff,timer);
				
				if (n <= 0)
				{
					targetIdChild.style.backgroundImage = 'url("'+ path + fileName + imgNumber + '.jpg")';
					clearTimeout(myTimer);
					fade();
				}
			}
			setTimer = setTimeout(opacityOff,interval);
		} else {
			function opacityOn ()
			{
				n += speed;
				targetIdChild.style.MozOpacity = (n * 0.01);
				targetIdChild.style.opacity = (n * 0.01);				
				if (document.all) targetIdChild.style.filter = "alpha(opacity=" + n + ")";
				
				var myTimer = setTimeout(opacityOn,timer);
				
				if (n >= 100)
				{
					imgNumberCopy = imgNumber;
					while (imgNumber == imgNumberCopy)
					{						
						imgNumber = Math.floor(Math.random() *images) + 1;
					}					
					targetId.style.backgroundImage = 'url("'+ path + fileName + imgNumber + '.jpg")';				
					clearTimeout(myTimer);					
					fade();
				}
			}
			opacityOn();
		}
	}
	fade();
}

/////////////////////////////////////////////////////////////////////////////////////////

function init(){
	//setSlideshow();
	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);




