﻿/**
 *	@name			base.js
 *	@facility		はなみち舎用 JavaScript
 *	@created date	2008/07/24
 *	@copyright (c) 2008 COCONOE inc.
 **/

var menuDiv, totopDiv;
var menuScrollTimer;
var totopScrollTimer;
var menuIniY = -180;
var nowMenuY = menuIniY;
var nowTotopY = 4000;
var flaLoaded = false;

function init()
{
	this.menuDiv = document.getElementById('menu');
	this.menuDiv.style.top = nowMenuY + "px";

	this.totopDiv = document.getElementById('totop');
	this.totopDiv.style.top = nowTotopY + "px";
	
	try
	{   
		window.addEventListener('scroll', onScrollEvent, false);
	}
	catch(e)
	{
		window.attachEvent('onscroll', onScrollEvent);
	}
	onScrollEvent();
	modAnchorTag();
}

function onScrollEvent()
{
	var viewarea = getViewportArea();
	menuScroll(viewarea.y + menuIniY);
	if(viewarea.y > 500)
	{
		totopScroll(viewarea.y + viewarea.h - 80);
	}
	else
	{
		totopScroll(4000);
	}
}

function menuScroll(toMenuY, a)
{
	if(toMenuY.isNaN) toMenuY = nowMenuY;
	//if(toMenuY < 0) toMenuY = 0;

	toMenuY = Math.ceil(toMenuY);

	menuScrollEvent(toMenuY, 0.06);
}

function totopScroll(toTotopY, a)
{
	if(toTotopY.isNaN) toTotopY = toTotopY;
	if(toTotopY < 0) toTotopY = 0;

	toTotopY = Math.ceil(toTotopY);

	totopScrollEvent(toTotopY, 0.03);
}

function menuScrollEvent(toMenuY, a)
{
	if(menuScrollTimer) clearTimeout(menuScrollTimer);

	nowMenuY += (toMenuY - nowMenuY) * a;

	if((((toMenuY - nowMenuY) > 0) ? (toMenuY - nowMenuY) : (nowMenuY - toMenuY)) < 1)
	{
		nowMenuY = toMenuY;
		clearTimeout(menuScrollTimer);
	}
	else
	{
		menuScrollTimer = setTimeout("menuScrollEvent("+toMenuY+", "+a+")", 10);
	}

	this.menuDiv.style.top = nowMenuY + "px";
}

function totopScrollEvent(toTotopY, a)
{
	if(totopScrollTimer) clearTimeout(totopScrollTimer);

	nowTotopY += (toTotopY - nowTotopY) * a;

	if((((toTotopY - nowTotopY) > 0) ? (toTotopY - nowTotopY) : (nowTotopY - toTotopY)) < 1)
	{
		nowTotopY = toTotopY;
		clearTimeout(totopScrollTimer);
	}
	else
	{
		totopScrollTimer = setTimeout("totopScrollEvent("+toTotopY+", "+a+")", 10);
	}

	this.totopDiv.style.top = nowTotopY + "px";
}

function modAnchorTag()
{
	if (!document.getElementsByTagName || !document.getElementById) return false;

	var ernal = document.getElementsByTagName('a');
	
	// for scroll anchor
	for (var i = 0; i < ernal.length; i++)
	{
		if (ernal[i].className == 'scroll')
		{
			ernal[i].onclick = function()
			{
				var hrf = this.href.substring(this.href.indexOf("#") + 1);
				scrollToAnchor(hrf);
				return false;
			}
		}
	}

	// for another window
	for (var i = 0; i < ernal.length; i++)
	{
		if (ernal[i].className == 'ext')
		{
			ernal[i].onclick = function()
			{
				var winName = getWinFromUrl(this.href);
				window.open(this.href, winName);
				return false;
			}
		}
	}
}

function openBlog(url)
{
	var winName = getWinFromUrl(url);
	window.open(url, winName);
}

function isSafari()
{
	return SAFARI;
}

// that's need "bytefx.js"
function scrollToAnchor(id)
{
	var objY = bytefx.$position(document.getElementById(id)).y;
	frameScroll(NaN, objY, 0.2);
}

addOnloadEvent(init);
