﻿/************************* the scrolll function **********************************************/

var slowStep = 2;
var fastStep = 10;
var stepSize = slowStep;
var scrollInterval;
var intervalSpeed = 5;

function scrollDown() {

    var content = document.getElementById('SubPageContent');
    var y = content.scrollTop;
    var h = content.scrollHeight - content.offsetHeight;

    if (y < h) {
        content.scrollTop = y + stepSize;
    } else {
        stopScroll();
        document.getElementById('arrowdown').style.display = "none";
    }

}


function scrollUp() {
    var content = document.getElementById('SubPageContent');
    var y = content.scrollTop;

    if (y > 0) {
        content.scrollTop = y - stepSize;
    } else {
        stopScroll();
        document.getElementById('arrowup').style.display = "none";
    }

}


function startScrollUp() {

    if (scrollInterval != null) {
        clearInterval(scrollInterval);
    }

    stepSize = slowStep;
    scrollInterval = setInterval("scrollUp()", intervalSpeed);
    document.getElementById('arrowdown').style.display = "block";

}

function startScrollDown() {

    if (scrollInterval != null) {
        clearInterval(scrollInterval);
    }

    stepSize = slowStep;
    scrollInterval = setInterval("scrollDown()", intervalSpeed);
    document.getElementById('arrowup').style.display = "block";

}


function stopScroll() {

    clearInterval(scrollInterval);
    scrollInterval = null;

}

function stepUp() {

    stepSize = fastStep;

}

function stepDown() {

    stepSize = slowStep;

}

function initScroll() {
    var content = document.getElementById('SubPageContent');
    var y = content.scrollTop;
    var h = content.scrollHeight - content.offsetHeight;



    if (y != 0) {
        document.getElementById('arrowup').style.display = "block";
    }

    if ((y != h) && (h > 0)) {
        document.getElementById('arrowdown').style.display = "block";
    }

}

/************** Mouse Wheel *****************************************/
/** This is high-level function; REPLACE IT WITH YOUR CODE.
* It must react to delta being more/less than zero.
*/
function handle(delta) {
    stepSize = fastStep * 3;
    if (delta < 0) {
        scrollDown();
        document.getElementById('arrowup').style.display = "block";
    }
    else {
        scrollUp();
        document.getElementById('arrowdown').style.display = "block";
    }
    document.getElementById('arrowup').style.display = "none";
    document.getElementById('arrowdown').style.display = "none";
    initScroll();
}

function wheel(event) {
    var delta = 0;
    if (!event) event = window.event;
    if (event.wheelDelta) {
        delta = event.wheelDelta / 120;
        if (window.opera) delta = -delta;
    } else if (event.detail) {
        delta = -event.detail / 3;

    }
    if (delta)
        handle(delta);
    if (event.preventDefault)
        event.preventDefault();
    event.returnValue = false;
}


function KeyCheck(e) {

    var KeyID = (window.event) ? event.keyCode : e.keyCode;

    //alert(KeyID);
    switch (KeyID) {

        case 38:
            //alert("Arrow Up");
            stepSize = fastStep;
            stopScroll();
            scrollUp();
            document.getElementById('arrowdown').style.display = "block";
            break;

        case 40:
            //alert("Arrow Down");
            stepSize = fastStep;
            stopScroll();
            scrollDown();
            document.getElementById('arrowup').style.display = "block";
            break;

        case 33:
            //alert("Page Up");
            stepSize = fastStep * 10;
            stopScroll();
            scrollUp();
            document.getElementById('arrowdown').style.display = "block";
            break;

        case 34:
            //alert("Page Down");
            stepSize = fastStep * 10;
            stopScroll();
            scrollDown();
            document.getElementById('arrowup').style.display = "block";
            break;
    }
    document.getElementById('arrowup').style.display = "none";
    document.getElementById('arrowdown').style.display = "none";
    initScroll();
}

/* Initialization code. */
if (window.addEventListener)
    window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;

//document.onkeyup = KeyCheck;
document.onkeydown = KeyCheck;
document.onkeyup = stopScroll;


