
//
// ============================
//  file name : common.js
//  author : staff015
// ============================
//

/////////////////////////////////////////////////////////////////////////////////////////

// smartScroll 1.7
function smartScroll ()
{	
	var allLinks = new Array();
	var as = document.getElementsByTagName('a');
	var areas = document.getElementsByTagName('area');
	for (var i=0; i<as.length; i++){
		allLinks.push(as[i]);
	}
	for (var i=0; i<areas.length; i++){
		allLinks.push(areas[i]);
	}
	
	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;
				};
			}
		}
	}	
}

/////////////////////////////////////////////////////////////////////////////////////////

// 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 setEmail ()
{
	document.write('<a href="mailto:info&#64;watashistyle.com">info&#64;watashistyle.com</a>');
}


/////////////////////////////////////////////////////////////////////////////////////////

// initRollovers
function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++)
	{		
		if (aImages[i].className == 'hover')
		{
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_on'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			//alert(aImages[i].parentNode.nodeName);
			if (aImages[i].parentNode.nodeName == "A")	
			{
				aImages[i].onmouseover = function()
				{
					sTempSrc = this.getAttribute('src');
					this.setAttribute('src', this.getAttribute('hsrc'));
				}	
				
				aImages[i].onmouseout = function()
				{
					if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype);
					this.setAttribute('src', sTempSrc);
				}
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////


function checkAgree()
{
	if (!document.getElementById('checkAgree')) return;
	
	var checkAgree = document.getElementById('checkAgree');	
	var inquiryPageNaviSpan = document.getElementById('inquiryPageNavi').getElementsByTagName('span');
	var subNavigationSpan = document.getElementById('subNavigation').getElementsByTagName('span');

	var n;
	var timer = 40;
	var speed = 5;

	function setOpacityOn (inquiryPageNavi, subNavigation ,n)
	{
		function opacityOn ()
		{
			n += speed;
			inquiryPageNavi.style.MozOpacity = (n * 0.01);
			inquiryPageNavi.style.opacity = (n * 0.01);				
			if (document.all) inquiryPageNavi.style.filter = "alpha(opacity=" + n + ")";
			
			subNavigation.style.MozOpacity = (n * 0.01);
			subNavigation.style.opacity = (n * 0.01);				
			if (document.all) subNavigation.style.filter = "alpha(opacity=" + n + ")";
			
			inquiryPageNavi.style.display = 'block';
			subNavigation.style.display = 'block';
			
			var myTimer = setTimeout(opacityOn,timer);
			
			if (n >= 100) clearTimeout(myTimer);
		}
		opacityOn();
	}
			
	function setOpacityOff (inquiryPageNavi, subNavigation ,n)
	{ 
		function opacityOff ()
		{
			n -= speed;				
			inquiryPageNavi.style.MozOpacity = (n * 0.01);
			inquiryPageNavi.style.opacity = (n * 0.01);				
			if (document.all) inquiryPageNavi.style.filter = "alpha(opacity=" + n + ")";
			
			subNavigation.style.MozOpacity = (n * 0.01);
			subNavigation.style.opacity = (n * 0.01);				
			if (document.all) subNavigation.style.filter = "alpha(opacity=" + n + ")";
			
			var myTimer = setTimeout(opacityOff,timer);
			
			if (n <= 0)
			{
				clearTimeout(myTimer);
				inquiryPageNavi.style.display = 'none';
				subNavigation.style.display = 'none';
			}
		}
		opacityOff();
	}
	
	for (var i=0; i<=inquiryPageNaviSpan.length-1; i++)
	{
		if (checkAgree.checked)
		{			
			n = 100;			
			setOpacityOff (inquiryPageNaviSpan[i], subNavigationSpan[i], n);
		} else {
			n = 0;
			setOpacityOn (inquiryPageNaviSpan[i], subNavigationSpan[i], n);
		}
	}
}


/////////////////////////////////////////////////////////////////////////////////////////

function init()
{
	smartScroll();
	initRollovers();
	checkAgree();
}

//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);

