var cpx = 100;
var ppx = 10;
var timer;

var root = '';

function scrollToV(dir, id) 
{
	clearTimeout(timer);
	div = document.getElementById(id);
	y = div.scrollTop;
	if(dir == 'top') y -= cpx;
	if(dir == 'bottom') y += cpx;
	if(y <= div.scrollHeight-div.offsetHeight +cpx && y >= 0-cpx)
	{
		div.scrollTop = y;
		timer = setTimeout('scrollToV(\''+dir+'\', \''+id+'\')', 50);
	}
	if(y < 0 || y > div.scrollHeight-div.offsetHeight)
	{
		clearTimeout(timer);
	}
}

function scrollToH(dir, id, speed) 
{
    clearTimeout(timer);
	div = document.getElementById(id);
	x = div.scrollLeft;
    $.scrollTo($(div), 1000, {axis: 'x'});
}

function scrollToStart(rootid, axis, speed)
{
    $('#'+rootid).scrollTo('0px', speed, {axis: axis});
}

function scrollToEnd(rootid, id, axis, speed)
{
    if(axis == 'x') end = $('#'+id).attr( 'offsetWidth' );
    if(axis == 'y') end = $('#'+id).attr( 'offsetHeight' );
    $('#'+rootid).scrollTo(end, speed, {axis: axis});
}

function scrollToDiv(rootid, destid, speed, axis)
{
	root = document.getElementById(rootid);
    div = document.getElementById(destid);
    
	x = root.scrollLeft;
    rw = document.body.clientWidth;
    sw = document.body.scrollWidth;
    cmid = x + (rw/2);
    cmid = Math.round(cmid);
    destx = div.offsetLeft;
    destmid = destx + (div.scrollWidth/2);
    destmid = Math.round(destmid);
    destleft = (destmid-(rw/2));

    if(rootid == 'body')
      $('#'+rootid).scrollTo(destleft, speed, {axis: axis});
    else
      $('#'+rootid).scrollTo($('#'+destid), speed, {axis: axis});
}

function goTo(x, w) 
{
	var fx = 2000;
	var cx = document.body.clientWidth;
	var sx = fx-cx;
	var rsx = sx-window.pageXOffset;
	
	if((cx + window.pageXOffset) <= (x + w))
	{
		dx = (x + w) - (cx + window.pageXOffset) + 20;
		window.scrollBy(dx, 0);
	} 
	else if(x < window.pageXOffset)
	{
		dx = window.pageXOffset - x + 20;
		window.scrollBy(-dx, 0);
	}
}

function scroll()
{
	if (window.addEventListener)
		window.addEventListener('DOMMouseScroll', wheel, false);
		
	window.onmousewheel = document.onmousewheel = wheel;
}

function handle(delta) {
        if (delta < 0)
		window.scrollBy(40, 0);
        else
		window.scrollBy(-40, 0);
}

function wheel(event)
{
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}
