function calendarPrzesunGora(step, divCalendarId,rowCount,dispRowCount) {
    poprzedniaPozycja = String(document.getElementById(divCalendarId).style.top).replace('px', '');
    if (poprzedniaPozycja == "") {
        poprzedniaPozycja = 0;
    }

    if (poprzedniaPozycja < 0) {
        pozycja = Number(poprzedniaPozycja) + step;
        przewinDol(step, poprzedniaPozycja, pozycja, divCalendarId);
    }
    
}
function przewinGora(step, poprzedniaPozycja, pozycja, divCalendarId) {
    window.setTimeout(function() {
        element = document.getElementById(divCalendarId.replace("all_contents_calendar", "calendarAll"));
        if (element.addEventListener) {
            element.removeEventListener('DOMMouseScroll', scroll, false);
            element.removeEventListener('mousewheel', scroll, false);
        }
        else {
            element.detachEvent('onmousewheel', scroll);
        }
        stepMin = Math.round(step / 10);
        pozycjaMin = Number(poprzedniaPozycja) + stepMin;
        if (pozycjaMin >= pozycja) {
            document.getElementById(divCalendarId).style.top = String(pozycja) + 'px';
            element = document.getElementById(divCalendarId.replace("all_contents_calendar", "calendarAll"));
            if (element.addEventListener) {
                element.addEventListener('DOMMouseScroll', scroll, false);
                element.addEventListener('mousewheel', scroll, false);
            }
            else {
                element.attachEvent('onmousewheel', scroll);
            }
        }
        else {
            document.getElementById(divCalendarId).style.top = String(pozycjaMin) + 'px';
            przewinDol(step, pozycjaMin, pozycja, divCalendarId);
        }

    }, 5);
}

function calendarPrzesunDol(step, divCalendarId, rowCount, dispRowCount) {
    poprzedniaPozycja = String(document.getElementById(divCalendarId).style.top).replace('px', '');
    if (poprzedniaPozycja == "") {
        poprzedniaPozycja = 0;
    }
    if (dispRowCount * step - rowCount * step < Number(poprzedniaPozycja)) {
        //alert(step + "," + rowCount + "," + Number(poprzedniaPozycja) + "," + dispRowCount);
        pozycja = Number(poprzedniaPozycja) - step;
        przewinGora(step, poprzedniaPozycja, pozycja, divCalendarId);
    }
}

function przewinDol(step, poprzedniaPozycja, pozycja, divCalendarId) {
    window.setTimeout(function() {
        element = document.getElementById(divCalendarId.replace("all_contents_calendar", "calendarAll"));
        if (element.addEventListener) {
            element.removeEventListener('DOMMouseScroll', scroll, false);
            element.removeEventListener('mousewheel', scroll, false);
        }
        else {
            element.detachEvent('onmousewheel', scroll);
        }
        stepMin = Math.round(step / 10);
        pozycjaMin = poprzedniaPozycja - stepMin;
        if (pozycjaMin <= pozycja) {
            document.getElementById(divCalendarId).style.top = String(pozycja) + 'px';
            element = document.getElementById(divCalendarId.replace("all_contents_calendar", "calendarAll"));
            if (element.addEventListener) {
                element.addEventListener('DOMMouseScroll', scroll, false);
                element.addEventListener('mousewheel', scroll, false);
            }
            else {
                element.attachEvent('onmousewheel', scroll);
            }
        }
        else {
            document.getElementById(divCalendarId).style.top = String(pozycjaMin) + 'px';
            przewinGora(step, pozycjaMin, pozycja, divCalendarId);
        }
    }, 5);
}

function hookEvent(element, eventName, callback) {
    if (typeof (element) == "string")
        element = document.getElementById(element);
    if (element == null)
        return;
    if (element.addEventListener) {
        if (eventName == 'mousewheel')
            element.addEventListener('DOMMouseScroll', callback, false);
        element.addEventListener(eventName, callback, false);
    }
    else if (element.attachEvent)
        element.attachEvent("on" + eventName, callback);
}

function cancelEvent(e) {
    e = e ? e : window.event;
    if (e.stopPropagation)
        e.stopPropagation();
    if (e.preventDefault)
        e.preventDefault();
    e.cancelBubble = true;
    e.cancel = true;
    e.returnValue = false;
    return false;
}

function scroll(e) {
    e = e ? e : window.event;
    var wheelData = e.detail ? e.detail : e.wheelDelta;
    if (nav == "Microsoft Internet Explorer") {
        currentElement = e.srcElement;
    }
    else {
        currentElement = this;
    }
    //   all_contents_calendar
    while (String(currentElement.id).indexOf('calendarAll', 0) == -1) {
        currentElement = currentElement.parentNode;
    }
    nazwa = String(currentElement.id).replace('calendarAll', '');
    // nazwa = String(nazwa);
    step = Number(document.getElementById('calendarHiddenStep' + nazwa).innerHTML);
    divCalendarId = document.getElementById('calendarHiddenId' + nazwa).innerHTML;
    rowCount =  Number(document.getElementById('calendarHiddenRowCount' + nazwa).innerHTML);
    dispRowCount =  Number(document.getElementById('calendarHiddenDispRowCount' + nazwa).innerHTML);
    if (wheelData > 0) {
        calendarPrzesunGoraScroll(step, divCalendarId, rowCount, dispRowCount);
    }
    else{
        calendarPrzesunDolScroll(step, divCalendarId, rowCount, dispRowCount);
    }
    cancelEvent(e);
}

function calendarPrzesunDolScroll(step, divCalendarId, rowCount, dispRowCount) {
    poprzedniaPozycja = String(document.getElementById(divCalendarId).style.top).replace('px', '');
    pozycja = Number(poprzedniaPozycja) - step;
    if (poprzedniaPozycja == "") {
        poprzedniaPozycja = 0;
    }

    if (dispRowCount * step - rowCount * step < Number(poprzedniaPozycja)) {
         document.getElementById(divCalendarId).style.top = String(pozycja) + 'px';
    }
}

function calendarPrzesunGoraScroll(step, divCalendarId, rowCount, dispRowCount) {
    poprzedniaPozycja = String(document.getElementById(divCalendarId).style.top).replace('px', '');
    pozycja = Number(poprzedniaPozycja) + step;
    if (poprzedniaPozycja == "") {
        poprzedniaPozycja = 0;
    }
    if (poprzedniaPozycja < 0) {
        pozycja = Number(poprzedniaPozycja) + step;
        document.getElementById(divCalendarId).style.top = String(pozycja) + 'px';
    }
}

var nav = navigator.appName;
if (nav == "Netscape") {
    if (String(navigator.appVersion).indexOf("Safari", 0) != false) {
        nav = "Safari";
    }
}
