﻿var goto_top_type = -1;
var goto_top_itv = 0;
var goto_left_type = -1;
var goto_left_itv = 0;

function go_up_timer(speed, y, el) {
    if (document.all) { } else { var el = $get(el); }
    var y_pos = el.scrollTop;

    y_pos -= Math.ceil((y_pos - y) * speed / 100);
    if (y_pos < y) y_pos = y;

    el.scrollTop = y_pos;

    if (y_pos <= y | y_pos <= 0) {
        clearInterval(goto_top_itv);
        goto_top_itv = 0;
    }
}

function goto_top(speed, el_id) {
    var el = $get(el_id);
    if (goto_top_itv == 0) {
        goto_top_type = 1;
        if (goto_top_type > 0)
            goto_top_itv = setInterval('go_up_timer(' + speed + ',1,' + el_id + ')', 25);
    }
}

function go_up(speed, y, el_id) {
    var el = $get(el_id);
    if (goto_top_itv == 0) {
        goto_top_type = 1;
        if (goto_top_type > 0) {
            if (document.all) {
                goto_top_itv = setInterval('go_up_timer(' + speed + ', ' + y + ',' + el_id + ')', 25);
            } else {
            goto_top_itv = setInterval("go_up_timer(" + speed + ", " + y + ",'" + el_id + "')", 25);
            }
        }
    }
}


function goto_bottom_timer(speed, y_max, el) {
    if (document.all) { } else { var el = $get(el); }
    var y = el.scrollTop;
    var moveby = speed;

    y += Math.ceil((y_max - y) * moveby / 100);
    if (y > y_max) y = y_max;

    el.scrollTop = y;
    y_max = Math.min(y_max, el.scrollHeight - el.offsetHeight);
    if (y >= y_max) {
        clearInterval(goto_top_itv);
        goto_top_itv = 0;
    }
}




function go_down(speed, y, el_id) {
    var el = $get(el_id);
    var y_max = el.scrollTop + y;
    var page_max = el.scrollHeight - el.offsetHeight;
    y_max = y_max > page_max ? page_max : y_max;
    if (goto_top_itv == 0) {
        goto_top_type = 1;
        if (goto_top_type > 0) {
            if (document.all) {
                goto_top_itv = setInterval('goto_bottom_timer(' + speed + ', ' + y_max + ', ' + el_id + ')', 25);
            } else {
            // FireFox
            goto_top_itv = setInterval("goto_bottom_timer(" + speed + ", " + y_max + ",'" + el_id + "')", 25);
            }
        }
    }
}


function go_downTo(speed, y, el_id) {
    var el = $get(el_id);
    var y_max = y;  //el.scrollTop + y;
    var page_max = el.scrollHeight - el.offsetHeight;
    y_max = y_max > page_max ? page_max : y_max;
    if (goto_top_itv == 0) {
        goto_top_type = 1;
        if (goto_top_type > 0) {
            if (document.all) {
                goto_top_itv = setInterval('goto_bottom_timer(' + speed + ', ' + y_max + ', ' + el_id + ')', 25);
            } else {
                // FireFox
                goto_top_itv = setInterval("goto_bottom_timer(" + speed + ", " + y_max + ",'" + el_id + "')", 25);
            }
        }
    }
}
















function go_left_timer(speed, x, el) {
    if (document.all) { } else { var el = $get(el); }
    var x_pos = el.scrollLeft;

    x_pos -= Math.ceil((x_pos - x) * speed / 100);
    if (x_pos < x) x_pos = x;

    el.scrollLeft = x_pos;

    if (x_pos <= x | x_pos <= 0) {
        clearInterval(goto_left_itv);
        goto_left_itv = 0;
    }
}


function go_leftss(speed, x, el_id) {
    
    var el = $get(el_id);
    if (goto_left_itv == 0) {
        goto_left_type = 1;
        if (goto_left_type > 0)
            goto_left_itv = setInterval('go_left_timer(' + speed + ','+ x +',' + el_id + ')', 25);
    }
}

function go_leftTo(speed, x, el_id) {
    x = Math.max(0, x);
    var el = $get(el_id);
    if (goto_left_itv == 0) {
        goto_left_type = 1;
        if (goto_left_type > 0) {
            if (document.all) {
                goto_left_itv = setInterval('go_left_timer(' + speed + ', ' + x + ',' + el_id + ')', 25);
            } else {
                goto_left_itv = setInterval("go_left_timer(" + speed + ", " + x + ",'" + el_id + "')", 25);
            }
        }
    }
}


function go_left(speed, x, el_id) {
    var el = $get(el_id);
    var x_max = el.scrollLeft - x;
    x_max = Math.max(0, x_max);
    if (goto_left_itv == 0) {
        goto_left_type = 1;
        if (goto_left_type > 0) {
            if (document.all) {
                goto_left_itv = setInterval('go_left_timer(' + speed + ', ' + x_max + ', ' + el_id + ')', 25);
            } else {
                // FireFox
                goto_left_itv = setInterval("go_left_timer(" + speed + ", " + x_max + ",'" + el_id + "')", 25);
            }
        }
    }
}




function go_toX(speed, x, el_id) {
    var el = $get(el_id);
    var x_cur = el.scrollLeft;
    if (x_cur > x) {
        go_leftTo(speed, x, el_id);
    } else {
        go_rightTo(speed, x, el_id);
    }    
}



function goto_right_timer(speed, x_max, el) {
    if (document.all) { } else { var el = $get(el); }
    var x = el.scrollLeft;
    var moveby = speed;

    x += Math.ceil((x_max - x) * moveby / 100);
    if (x > x_max) x = x_max;

    el.scrollLeft = x;
    x_max = Math.min(x_max, el.scrollWidth - el.offsetWidth);
    if (x >= x_max) {
        clearInterval(goto_left_itv);
        goto_left_itv = 0;
    }
}



function go_right(speed, x, el_id) {
    var el = $get(el_id);
    var x_max = el.scrollLeft + x;
    var page_max = el.scrollWidth - el.offsetWidth;
    x_max = x_max > page_max ? page_max : x_max;
    if (goto_left_itv == 0) {
        goto_left_type = 1;
        if (goto_left_type > 0) {
            if (document.all) {
                goto_left_itv = setInterval('goto_right_timer(' + speed + ', ' + x_max + ', ' + el_id + ')', 25);
            } else {
                // FireFox
                goto_left_itv = setInterval("goto_right_timer(" + speed + ", " + x_max + ",'" + el_id + "')", 25);
            }
        }
    }
}


function go_rightTo(speed, x, el_id) {
    var el = $get(el_id);
    var x_max = x;  //el.scrollTop + y;
    //var page_max = el.scrollLeft;   // - el.width;
    var page_max = el.scrollWidth - el.offsetWidth;
    x_max = x_max > page_max ? page_max : x_max;
    if (goto_left_itv == 0) {
        goto_left_type = 1;
        if (goto_left_type > 0) {
            if (document.all) {
                goto_left_itv = setInterval('goto_right_timer(' + speed + ', ' + x_max + ', ' + el_id + ')', 25);
            } else {
                // FireFox
                goto_left_itv = setInterval("goto_right_timer(" + speed + ", " + x_max + ",'" + el_id + "')", 25);
            }
        }
    }
}








/*  BODY FUNCTIONS ****************** */

function body_goto_bottom_timer(speed, y_max) {
    //if (document.all) { } else { var el = $get(el); }
    el = window;
    var y = el.scrollTop;
    var moveby = speed;
    
    y += Math.ceil((y_max - y) * moveby / 100);
    if (y > y_max) y = y_max;

    //el.scrollTop = y;
    window.scrollTo(0, y);
    
    y_max = Math.min(y_max, el.scrollHeight - el.offsetHeight);
    if (y >= y_max) {
        clearInterval(goto_top_itv);
        goto_top_itv = 0;
    }
}

function body_go_downTo(speed, y) {
    var el = window;
    var y_max = y;  //el.scrollTop + y;
    var page_max = el.scrollHeight - el.offsetHeight;
    y_max = y_max > page_max ? page_max : y_max;
    if (goto_top_itv == 0) {
        goto_top_type = 1;
        if (goto_top_type > 0) {
            if (document.all) {
                goto_top_itv = setInterval('body_goto_bottom_timer(' + speed + ', ' + y_max + ')', 25);
            } else {
                // FireFox
                goto_top_itv = setInterval("body_goto_bottom_timer(" + speed + ", " + y_max + ")", 25);
            }
        }
    }
}


function body_go_up_timer(speed, y) {
    var moveby = speed;
    var y_pos = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;

    y_pos -= Math.ceil((y_pos - y) * moveby / 100);
    if (y_pos < y) y_pos = y;

    if (goto_top_type == 1)
        document.documentElement.scrollTop = y_pos;
    else
        document.body.scrollTop = y_pos;

    if (y_pos <= y) {
        clearInterval(goto_top_itv);
        goto_top_itv = 0;
    }
}




function body_go_to(pos) {
    if (goto_top_itv == 0) {
        if (document.documentElement && document.documentElement.scrollTop) {
            goto_top_type = 1;

        } else if (document.body && document.body.scrollTop) {
            goto_top_type = 2;
        } else
            goto_top_type = 0;

        if (goto_top_type > 0)
            goto_top_itv = setInterval('body_go_up_timer(1, 500)', 25);
    }
}
 




function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
