/** scroll **/
function GalScroller(elid, thumber)
{
    this.element = document.getElementById( elid );
    this.thumber = thumber;
    this.increment = 0;
    this.interval = null;
    var links = document.getElementsByTagName( "A" );
    for( var j = 0; j < links.length; j ++ ) {
	if( links[j].href.indexOf( "#top" ) != -1 )
	    this.linkUp = links[j];
	else if( links[j].href.indexOf( "#bottom" ) != -1 )
	    this.linkDown = links[j];
    }

    this.hasUp = true;
    this.hasDown = true;
    this.enableUp( false );
    this.enableDown( true );
}

GalScroller.vertical = 518;

GalScroller.prototype.doScroll = function()
{
    if( !this.canScrollBy() ) {
	clearInterval( this.interval );
	this.increment = 0;
	this.interval = null;
	return;
    }

    // scroll actually
    this.element.scrollTop += this.increment;
    this.thumber.scrollOffset = this.element.scrollTop;

    // change increment
    if( Math.abs( this.increment ) < 16 )
	this.increment *= 2;
}

GalScroller.prototype.startScroll = function()
{
    this.thumber.finallyHideThumb();
    this.interval = setInterval( "gs.doScroll();", 50 );
}

GalScroller.prototype.stopScroll = function()
{
    if( this.interval != null ) {
	clearInterval( this.interval );
	this.interval = null;
    }
}

GalScroller.prototype.onupEnter = function()
{
    this.increment = -2;
    this.startScroll();
}

GalScroller.prototype.onupExit = function()
{
    this.increment = 0;
    this.stopScroll();
}

GalScroller.prototype.ondownEnter = function()
{
    this.increment = 2;
    this.startScroll();
}

GalScroller.prototype.ondownExit = function()
{
    this.increment = 0;
    this.stopScroll();
}

GalScroller.prototype.canScrollBy = function()
{
    if( !this.increment ) return false;

    var direction = (this.increment < 0) ? "up" : "down";
    var nextScroll = this.element.scrollTop + this.increment;
    if( nextScroll < 0 ) {
	this.increment = this.element.scrollTop * -1; // just enough to go to the end
    } else if( nextScroll > (this.element.scrollHeight - GalScroller.vertical) ) {
	this.increment = (this.element.scrollHeight - GalScroller.vertical) - this.element.scrollTop;
    }

    if( this.increment == 0 ) {
	if( direction == "up" ) {
	    this.enableUp( false );
	} else if( direction == "down" ) {
	    this.enableDown( false );
	}
	return false;
    } else {
	if( (direction == "down") && (!this.hasUp) && (this.element.scrollTop > 0) )
	    this.enableUp( true );
	else if( (direction == "up") && (!this.hasDown) && (this.element.scrollTop < (this.element.scrollHeight - GalScroller.vertical)) )
	    this.enableDown( true );
    }

    return true;
}

GalScroller.prototype.scrollIntoView = function(fim)
{
    if( fim ) {
	fim.scrollIntoView();
	this.thumber.scrollOffset = this.element.scrollTop;
	if( this.element.scrollTop >= (this.element.scrollHeight - GalScroller.vertical) )
	    this.enableDown( false );
	if( this.element.scrollTop > 0 )
	    this.enableUp( true );
    }
}

GalScroller.prototype.enableUp = function(yesno)
{
    if( this.linkUp !== undefined ) {
	if( yesno == true )
	    this.linkUp.style.background = "url(/layout/scrollup.jpg)";
	else
	    this.linkUp.style.background = "url(/layout/spacer.gif)";

	this.hasUp = yesno;
    }
}

GalScroller.prototype.enableDown = function(yesno)
{
    if( this.linkDown !== undefined ) {
	if( yesno == true )
	    this.linkDown.style.background = "url(/layout/scrolldown.jpg)";
	else
	    this.linkDown.style.background = "url(/layout/spacer.gif)";

	this.hasDown = yesno;
    }
}

