﻿/*  ================================================================================
 *
 *  JavaScript -Default version 3.0.6
 *  (c) 2004-2009 coliss.com
 *
 *  この作品は、クリエイティブ・コモンズの表示 2.1 日本ライセンスの下で
 *  ライセンスされています。
 *  この使用許諾条件を見るには、http://creativecommons.org/licenses/by/2.1/jp/を
 *  チェックするか、クリエイティブ･コモンズに郵便にてお問い合わせください。
 *  住所は：559 Nathan Abbott Way, Stanford, California 94305, USA です。
 *
================================================================================  */


/*  ================================================================================
TOC
============================================================
Set Adjustment
Page Scroller
============================================================
this script requires jQuery 1.3(http://jquery.com/)
================================================================================  */


/*  ================================================================================
Set Adjustment
================================================================================  */
var virtualTopId = "top",
    virtualTop,
    adjTraverser,
    adjPosition,
    callExternal = "pSc",
    delayExternal= 200;
var VarUsrAgt = navigator.userAgent.toLowerCase();//小文字
/* example
======================================================================  */
//    virtualTop = 0;    // virtual top's left position = 0
//    virtualTop = 1;    // virtual top's left position = vertical movement
//    adjTraverser = 0;  // left position = 0
//    adjTraverser = 1;  // horizontal movement.
//    adjPosition = -26;

/*  ================================================================================
Page Scroller
================================================================================  */
(function ($)
{
    var height = $.fn.height, width = $.fn.width, left = $.fn.left, top = $.fn.top;
    $.fn.extend(
    {
        width : function ()
        {
            if (!this [0]) {
                error();
            }
            if (this [0] == window)
            {
                return self.innerWidth || $.boxModel && document.documentElement.clientWidth || document.body.clientWidth;
            }
            if (this [0] == document)
            {
                return ((document.documentElement && document.compatMode == "CSS1Compat") ? document.documentElement.scrollWidth : document.body.scrollWidth);
            }
            return width.apply(this, arguments);
        },
        height : function ()
        {
			if (!this [0]) {
                error();
            }
			if (this [0] == window)
            {
                return self.innerHeight || $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
            }
            if (this [0] == document)
            {
				if(VarUsrAgt.indexOf("safari")!=-1||VarUsrAgt.indexOf("chrome")!=-1){
					return (document.body.scrollHeight);
				}else{
					return ((document.documentElement && document.compatMode == "CSS1Compat") ? document.documentElement.scrollHeight : document.body.scrollHeight);
				}
            }
            return height.apply(this, arguments);
        },
        left : function ()
        {
            if (!this [0]) {
                return true;
            }
            var obj = document.getElementById ? document.getElementById(this [0].id) : document.all(this [0].id);
            var tagCoords = new Object();
            tagCoords.x = obj.offsetLeft;
            while ((obj = obj.offsetParent) != null) {
                tagCoords.x += obj.offsetLeft
            }
            if ((tagCoords.x * 0) == 0) {
                return (tagCoords.x);
            }
            else {
                return (this [0].id);
            }
        },
        top : function ()
        {
            if (!this [0]) {
                return true;
            }
            var obj = document.getElementById ? document.getElementById(this [0].id) : document.all(this [0].id);
            var tagCoords = new Object();
            tagCoords.y = obj.offsetTop;
            while ((obj = obj.offsetParent) != null) {
                tagCoords.y += obj.offsetTop
            }
            if ((tagCoords.y * 0) == 0) {
                return (tagCoords.y);
            }
            else {
                return (this [0].id);
            }
        }
    })
})(jQuery);
$(function ()
{
    $('a[href^="#"]').click(function ()
    {
        var usrUrl = location.hostname + location.pathname;
        var anchorPath = ((this.href).substring(0, (((this.href).length) - ((this.hash).length)))).slice((this.href).indexOf("//") + 2);
        if (usrUrl.lastIndexOf("?") !=- 1) {
            usrUrlOmitQ = usrUrl.slice(0, (usrUrl.lastIndexOf("?")));
        }
        else {
            usrUrlOmitQ = usrUrl;
        }
        if (anchorPath.lastIndexOf("?") !=- 1) {
            anchorPathOmitQ = anchorPath.slice(0, (anchorPath.lastIndexOf("?")));
        }
        else {
            anchorPathOmitQ = anchorPath;
        }
        if (anchorPathOmitQ == usrUrlOmitQ) {
            coliss.toAnchor((this.hash).substr(1));
            return false;
        }
    });
    $("body").click(function ()
    {
        coliss.stopScroll()
    })
});
this.pageScrollTimer = null;
var coliss = 
{
    getScrollRange : function (type)
    {
        if (type == "x") {
            return (($(document).width()) - ($(window).width()));
        }
        else if (type == "y") {
            return (($(document).height()) - ($(window).height()));
        }
    },
    getWindowOffset : function (type)
    {
        if (type == "x")
        {
            return (window.pageXOffset || document.body.scrollLeft || document.body.parentNode.scrollLeft);
        }
        else if (type == "y")
        {
            return (window.pageYOffset || document.body.scrollTop || document.body.parentNode.scrollTop);
        }
    },
    pageScroll : function (toX, toY, frms, frX, frY)
    {
        var pageScrollTimer;
        if (pageScrollTimer) {
            clearTimeout(pageScrollTimer);
        }
        var spd = 7;
        var actX = coliss.getWindowOffset('x');
        var actY = coliss.getWindowOffset('y');
        if (!toX || toX < 0) {
            toX = 0;
        }
        if (!toY || toY < 0) {
            toY = 0;
        }
        if (!frms) {
            frms = $.browser.mozilla ? 10 : $.browser.opera ? 8 : 9;
        }
        if (!frX) {
            frX = 0 + actX;
        }
        if (!frY) {
            frY = 0 + actY;
        }
        frX += (toX - actX) / frms;
        if (frX < 0) {
            frX = 0;
        }
        frY += (toY - actY) / frms;
        if (frY < 0) {
            frY = 0;
        }
        var posX = Math.ceil(frX);
        var posY = Math.ceil(frY);
        window.scrollTo(posX, posY);
		if ((Math.floor(Math.abs(actX - toX)) < 1) && (Math.floor(Math.abs(actY - toY)) < 1)) {
            clearTimeout(this.pageScrollTimer);
            window.scroll(toX, toY);
        }
        else if ((posX != toX) || (posY != toY))
        {
            this.pageScrollTimer = setTimeout("coliss.pageScroll(" + toX + "," + toY + "," + frms + "," + frX + "," + frY + ")", 
            spd);
        }
        else {
            clearTimeout(this.pageScrollTimer);
        }
    },
    stopScroll : function ()
    {
        clearTimeout(this.pageScrollTimer)
    },
    cancelScroll : function (e)
    {
        coliss.stopScroll()
    },
    toAnchor : function (idName)
    {
        coliss.stopScroll();
        var anchorX, anchorY;
        if (!!idName)
        {
            if (idName == virtualTopId)
            {
                anchorX = (virtualTop == 0) ? 0 : (virtualTop == 1) ? window.pageXOffset || document.body.scrollLeft || document.body.parentNode.scrollLeft : $('#' + idName).left();
                anchorY = ((virtualTop == 0) || (virtualTop == 1)) ? 0 : $('#' + idName).top()
            }
            else
            {
                anchorX = (adjTraverser == 0) ? 0 : (adjTraverser == 1) ? ($('#' + idName).left()) : window.pageXOffset || document.body.scrollLeft || document.body.parentNode.scrollLeft;
                anchorY = adjPosition ? ($('#' + idName).top()) + adjPosition : ($('#' + idName).top())
            }
            var dMaxX = coliss.getScrollRange('x');
            var dMaxY = coliss.getScrollRange('y');
            if (((anchorX * 0) == 0) || ((anchorY * 0) == 0))
            {
				var setX = (anchorX < 1) ? 0 : (anchorX > dMaxX) ? dMaxX : anchorX;
                var setY = (anchorY < 1) ? 0 : (anchorY > dMaxY) ? dMaxY : anchorY;
//alert(anchorY);
//alert(dMaxY);
                coliss.pageScroll(setX, setY)
            }
            else {
                location.hash = idName;
            }
        }
        else {
            coliss.pageScroll(0, 0);
        }
    },
    initPageScroller : function ()
    {
        var usrUrl = location.href;
        var checkAnchor = usrUrl.indexOf("#", 0);
        var checkPageScroller = usrUrl.match(callExternal);
        if (!!checkPageScroller)
        {
            anchorId = usrUrl.slice(usrUrl.lastIndexOf("?" + callExternal) + 4, usrUrl.length);
            timerID = setTimeout("coliss.toAnchor(anchorId)", delayExternal)
        }
        if (!checkAnchor) {
            window.scroll(0, 0);
        }
        else {
            return true;
        }
    }
};
$(coliss.initPageScroller);
