rollover = new function()
{


    // 初期設定 --> START
    
    this.scrollLength   = 10;   // [ number ] スクロールさせる距離の間隔 
                                // 1〜100 ( 単位 : % ) 推奨値 5〜10
                                
    this.scrollSpeed    = 10;   // [ number ] スクロールさせる時間の間隔
                                // 推奨値 10〜100 ( 単位 : 1000 = 1秒 )
                                
    this.clickCheck     = 1;    // [ bool ]
                                // リンクの監視 ( 1 -> Yes, 0 -> No )
    
    var useMoziHiVer    = 1;    // [ bool ]
                                // 最近の Gecko でも動作させる ( 1 -> Yes, 0 -> No )

    // 初期設定 <-- END
    
    
    
    
    
    var _this           = this;
    var isIE            = !!(document.all && !window.opera);
    var isDOM           = !!document.getElementById;
    var isMHVer         = navigator.product == "Gecko" &&
                          navigator.productSub > 20040000;
    var ancz            = new Array();
    var f_scroll        = "scroller";
    var initFlag        = false;
    var ancLen          = 0;
    var timeId;
    var winHeight;
    var docHeight;
    var pTopPos;
    var pLeftPos;
    var tgtTopPos;
    var upFlag;
    var isEvent;

    var getAnchorNodes = function()
    {
        var ac = document.anchors;
        if (initFlag || ac.length == ancLen) return;

        for (var i=ancLen; i<ac.length; i++) {
            if (ac[i].name.length > 1) ancz[ac[i].name] = ac[ancLen = i];
        }
    }

    var fPath = function(_p) { return (_p.charAt(0) != "/")? "/"+ _p: _p }
    
    var getPosition = function()
    {
        if (isIE) { with (document.body)
        {
            winHeight   = clientHeight;
            docHeight   = scrollHeight;
            pTopPos     = scrollTop;
            pLeftPos    = scrollLeft;
        } }
        else
        {
            winHeight   = window.innerHeight;
            docHeight   = document.height || document.body.scrollHeight;
            pTopPos     = window.pageYOffset;
            pLeftPos    = window.pageXOffset;
        }
    }
    
    this[f_scroll] = function(_node, _e, _hash)
    {
        var targetName;
        var oTarget;

        if (timeId) timeId = clearInterval(timeId);
        if (_hash) targetName = _hash.charAt(0) == "#"? _hash.substr(1): _hash;
        else if (_node && _node.hash) targetName = _node.hash.substr(1);
 
        if (!targetName) return true;

        getAnchorNodes();
        getPosition();
   
        oTarget = ancz[targetName];  
        if (!oTarget || !docHeight) return true;

        tgtTopPos   = oTarget.offsetTop;
        udFlag      = !!(tgtTopPos < _node.offsetTop);
        timeId      = setInterval( _this.pScrolling, this.scrollSpeed );

        if (_e)
        {
            if (isIE) event.cancelBubble = true;
            else if (_e.stopPropagation) _e.stopPropagation();
        }
        return false;
    }

    if (isIE)
    var getPageTopOffset = new Function(" return document.body.scrollTop ");
    else
    var getPageTopOffset = new Function(" return window.pageYOffset ");

    
    this.pScrolling = function()
    {
        var tempPTop = getPageTopOffset();
        var endFlag=0;
       
        if (!udFlag) {
            pTopPos += Math.ceil((tgtTopPos- tempPTop) * (_this.scrollLength/100));
            if (tgtTopPos <= pTopPos) endFlag = 1;
        }
        else {
            pTopPos -= Math.ceil((tempPTop- tgtTopPos)* (_this.scrollLength/100));
            if (tgtTopPos >= pTopPos) endFlag = 1;
        }
        
        if (endFlag) {
            pTopPos = tgtTopPos;
            timeId  = clearInterval(timeId);
        }
        scrollTo( pLeftPos, pTopPos );
    }
    
    this.init = function()
    {
        if (initFlag) return;
        
        getAnchorNodes();
        initFlag = 1;
        autoScroll();
        
        if (_this.clickCheck) {
        document.onclick = document.onclick? new checkClick: checkClick;
        document.onkeyup = document.onkeyup? new checkClick: checkClick;
        }
    }

    var autoScroll = function()
    {
        var aName = location.hash;
        if (aName.length > 1)
        _this[f_scroll]({
            hash        : aName,
            offsetTop   : getPageTopOffset()
            });
    }
    
    var checkClick = function(_e)
    {
        
        if (timeId) timeId = clearInterval(timeId);
        if (_e) {
            _e = _e.target;
            if (_e.nodeType && _e.nodeType != 1) _e = _e.parentNode;
        }
        else 
            _e = event.srcElement;

        
        if (_e && _e.tagName == "A")
        {   
            if (_e.hash.length > 1 && fPath(location.pathname) == fPath(_e.pathname) )
                return _this[f_scroll](_e);
            else
                return true;
        }

    }
    

    if ( !(isIE || isDOM) || (!useMoziHiVer && isMHVer) )
    {
        this[f_scroll]  = new Function(" return true ");
        this.init       = new Function("");
    }
    
    if (isIE || isDOM)
    {
        if (window.onload) window.onload = new this.init;
        else window.onload = this.init;
        
        if (window.onerror) window.onerror = new this.init;
        else window.onerror = this.init;
    }
}



