﻿/**
 *	@name			func.js
 *	@facility		値の取得・基本処理
 *	@created date	2008/05/02
 *	@copyright (c) 2008 COCONOE inc.
 *
 *	[更新履歴]
 *	@v1.0		:	2008/05/02	:	値の取得・基本処理用に独立させました
 *	@v1.1		:	2008/05/14	:	frameScrollをX軸,Y軸の両用に修正
 *	@v1.2		:	2008/05/15	:	frameScrollのバグ修正
 *	@v1.3		:	2008/05/15	:	さらにframeScrollのバグ修正
 *	@v1.4		:	2008/05/16	:	・getDocumentAreaの追加
 *									・frameScrollの修正
 *	@v1.5		:	2008/07/24	:	scrollWidth, scrollHeight, clientWidth, clientHeightの修正
 *	@v1.6		:	2008/07/28	:	getWinFromUrlの追加
 *	@v1.7		:	2008/11/14	:	addOnresizeEventの追加
**/
var nUserAgent = navigator.userAgent;
var nAppVersion	= navigator.appVersion;
var nAppName = navigator.appName;
var nMimeTypes = navigator.mimeTypes;
var dUrl = document.URL;

var IE = /*@cc_on!@*/false;
var OPERA = (nUserAgent.indexOf("Opera", 0) != -1);
var FIREFOX = (nUserAgent.indexOf("Firefox", 0) != -1);
var SAFARI = (nUserAgent.indexOf("Safari", 0) != -1);
var WIN = (nUserAgent.indexOf("Win", 0) != -1);
var MAC = (nUserAgent.indexOf("Mac", 0) != -1);

var scrollTimer;
var stageHeight;

/******************************
 *	hide div area
*******************************/
function hideArea(id)
{
	if(document.getElementById(id))
	{
		document.getElementById(id).style.display = "none";
		document.getElementById(id).style.visibility = "hidden";
	}
}

/******************************
 *	show div area
*******************************/
function showArea(id)
{
	if(document.getElementById(id))
	{
		document.getElementById(id).style.display = "block";
		document.getElementById(id).style.visibility = "visible";
	}
}

/******************************
 *	get document area
*******************************/
function getDocumentArea()
{
	return {
		w: getDocumentWidth(),
		h: getDocumentHeight()
	}
}

/******************************
 *	get width of document
*******************************/
function getDocumentWidth()
{
	val = [
		document.documentElement.scrollWidth ? document.documentElement.scrollWidth : 0,
		document.body.scrollWidth ? document.body.scrollWidth : 0
	]
	return Math.max(val[0], val[1]);
}

/******************************
 *	get height of document
*******************************/
function getDocumentHeight()
{
	val = [
		document.documentElement.scrollHeight ? document.documentElement.scrollHeight : 0,
		document.body.scrollHeight ? document.body.scrollHeight : 0
	]
	return Math.max(val[0], val[1]);
}

/******************************
 *	get viewport area
*******************************/
function getViewportArea()
{
	return {
		x: document.body.scrollLeft || document.documentElement.scrollLeft,
		y: document.body.scrollTop  || document.documentElement.scrollTop,
		w: getViewportWidth(),
		h: getViewportHeight()
	}
}

/******************************
 *	get width of browser's viewport
*******************************/
function getViewportWidth()
{
	val = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
	return val;
}

/******************************
 *	get height of browser's viewport
*******************************/
function getViewportHeight()
{
	val = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	return val;
}

/******************************
 *	move to Top
*******************************/
function toTop()
{
	frameScroll(0, 0, 0.3);
	return false;
}

/******************************
 *	smooth scroll (0 < a < 1)
*******************************/
var nowX = 0, nowY = 0;

function frameScroll(toX, toY, a)
{
	var view_area = getViewportArea();
	var doc_area = getDocumentArea();

	nowX = view_area.x;
	nowY = view_area.y;
	nowW = view_area.w;
	nowH = view_area.h;
	docW = doc_area.w;
	docH = doc_area.h;

	if(toX.isNaN) toX = nowX;
	if(toY.isNaN) toY = nowY;

	toX = (toX > (docW - nowW)) ? (docW - nowW) : toX;
	toY = (toY > (docH - nowH)) ? (docH - nowH) : toY;

	if(!toX || toX < 0) toX = 0;
	if(!toY || toY < 0) toY = 0;

	toX = Math.ceil(toX);
	toY = Math.ceil(toY);

	frameScrollEvent(toX, toY, a);
}

function stopFrameScroll()
{
	clearTimeout(scrollTimer);
}

function frameScrollEvent(toX, toY, a)
{
	var num = 0;
	if(scrollTimer) clearTimeout(scrollTimer);

	nowX += (toX - nowX) * a;
	nowY += (toY - nowY) * a;
	
	if((((toX - nowX) > 0) ? (toX - nowX) : (nowX - toX)) < 2)
	{
		nowX = toX;
		num = -~num;
	}

	if((((toY - nowY) > 0) ? (toY - nowY) : (nowY - toY)) < 2)
	{
		nowY = toY;
		num = -~num;
	}

	window.scroll(nowX, nowY);

	if(num == 2)
	{
		clearTimeout(scrollTimer);
	}
	else
	{
		scrollTimer = setTimeout("frameScrollEvent("+toX+", "+toY+", "+a+")", 10);
	}
}

/******************************
 *	add to window.onload
*******************************/
function addOnloadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

/******************************
 *	add to window.onresize
*******************************/
function addOnresizeEvent(func)
{
	var oldonresize = window.onresize;
	if (typeof window.onresize != 'function')
	{
    	window.onresize = func;
	}
	else
	{
		window.onresize = function()
		{
			oldonresize();
			func();
		}
	}
}

/******************************
 *	get Domain from URL
*******************************/
function getWinFromUrl(url)
{
	var domain = url;
	domain = domain.replace(/^(http|https):\/\/([^\/]+)/ig, '$2');
	domain = domain.replace(/\?[\S]+$/, '');
	domain = domain.replace(/\.|-|\//ig, '');
	return domain;
}
