// Modified by Antony Shen
// Handcrafted by Construct Internet Design, August 1997
//   under commission of Netscape Communications, Inc.
//
// Project Team:
// Project Management: Becky Price
// Quality Assurance: Cynsa Bonorris

//
//	 Tech:			Design:
//	   Sascha Becker	  David Collier
//	   Peter Molettiere 	  Josh Draper
//	   Lior Saar		  Annette Loudon
//	   James Waldrop	  Gabriella Marks
//
//
// http://www.construct.net/
//
// scroller.js
//

var scrollInterval = null;
var scrollSpeed = 10;
var justScrolled = 'null';
var counter = 0;
var docHeight=-1;

var upTop = 30;
var downTop = 60;
var closeTop = 0;

function scrollUP() {
	if( (upL.downImg != null)&&(justScrolled == 'null') ) { upL.background.src = upL.downImg; }
    docHeight = this.parentScroll.content.docHeight;
    contentLayer = this.parentScroll.content;
	justScrolled = 'up';
    if (scrollInterval == null) { scrollInterval = setInterval(scroll, 30, -scrollSpeed); }
    return false;
}

function scrollDOWN() {
	if( (downL.downImg != null)&&(justScrolled == 'null') ) { downL.background.src = downL.downImg; }
    contentLayer = this.parentScroll.content;
    docHeight = this.parentScroll.content.docHeight;
	justScrolled = 'down';
    if (scrollInterval == null) { scrollInterval = setInterval(scroll, 30, scrollSpeed); }
    return false;
}

function stopSCROLL() {
	if( (downL.overImg != null)&&(justScrolled == 'down') ) { downL.background.src = downL.overImg.src; }
	if( (upL.overImg != null)&&(justScrolled == 'up') ) { upL.background.src = upL.overImg.src; }
	justScrolled = 'null';
    clearInterval(scrollInterval);
    scrollInterval = null;
    return false;
}

function closeArticle() {
    window.viewArticlePause = false;
    visibility = 'hide';
    docHeight = -1;
    this.parentScroll.content.visibility = 'hide';
	this.parentScroll.exit();
	this.parentScroll.board.visibility = 'hide';
}

function scroll( amount )
{
	//contentLayer.bgColor="#ff0000" ;

    bugFactor = 3 ;

    if( amount > 0 )
        {
        if( contentLayer.clip.bottom >= docHeight )
            return ;

        delta = docHeight - contentLayer.clip.bottom ;  //println( "going down. delta: " + delta ) ;

        if( delta < amount)
            {
            amount = delta ; //println( "almost done " + delta ) ;
            amount -= bugFactor ;
            }
        }
    else
        {
        if( contentLayer.clip.top <= 0 )
            return ;
        delta = contentLayer.clip.top ; //println( "going up. delta: " + delta ) ;
        if( delta < -amount )
            amount = -delta ;
        }

        // 	println( "scrolling " + amount );
	contentLayer.top -= amount;
	contentLayer.clip.top += amount;
	contentLayer.clip.bottom += amount;
}

function Scroll( upI, downI, closeI, contentL ) {

    if( contentL.ScrollerDefined ) {  // Let's check to make sure one hasn't already been defined
	contentL.scroller.Redraw(); 
	return contentL.scroller;
    }

    upL = new imgButton( upI)
    downL = new imgButton( downI)
    closeL = new imgButton( closeI)

    upL.setTopLeft( upTop, 0 );
    downL.setTopLeft( downTop, 0 );
    closeL.setTopLeft( closeTop, 0 );

    downL.onmousedown = scrollDOWN;
	downL.onmouseup = stopSCROLL;
    upL.onmousedown = scrollUP;
	upL.onmouseup = stopSCROLL;
    closeL.onclick = closeArticle;

contentL.zIndex=30;
contentL.visibility='show';  //today inherit
    this.up = upL;
    this.down = downL;
    this.close = closeL;

    this.up.visibility='show';  // new
    this.down.visibility='show';
    this.close.visibility='show';

    this.content = contentL;
    if( this.content.docHeight + "" == 'undefined' ) { this.content.docHeight = contentL.clip.height; }
    this.content.origTop = this.content.top;
    this.content.ScrollerDefined = true;
    this.content.clipped = false;

    this.exit = S_exit;
    this.setClip = S_clip;
    this.reset = S_reset;
    this.Redraw = S_redraw ;   // LS

    this.clickCallback = null;  // LS
    this.setClickCallback = S_clickCallback;

    this.up.parentScroll = this;
    this.down.parentScroll = this;
    this.close.parentScroll = this;
	this.close.parentScroll.board = this.content.parentLayer; // Just added.
    this.content.scroller = this;
	this.content.onblur = closeArticle;

    return this;
}

function S_clickCallback( func ) {
	this.clickCallback = func;
}

function S_clip( l, r, t, b ) {
    if( this.content.clipped ) { return; }

    this.content.origClipL = l;
    this.content.origClipR = r;
    this.content.origClipT = t;
    this.content.origClipB = b;

    this.content.clip.left = l;
    this.content.clip.right = r;
    this.content.clip.top = t;
    this.content.clip.bottom = b--; // LS - the -- eliminates an off-by-one in the scroll
    this.content.clipped = true;
}

function S_reset() {
    this.content.top = this.content.origTop;
    this.content.clip.left = this.content.origClipL ;
    this.content.clip.right = this.content.origClipR ;
    this.content.clip.top = this.content.origClipT ;
    this.content.clip.bottom = this.content.origClipB--;
}

function S_exit() {
    this.up.visibility = 'hide';
    this.down.visibility = 'hide';
    this.close.visibility = 'hide';
    if( this.clickCallback != null ) { this.clickCallback(this); }
}

function S_redraw() {
    this.up.Redraw() ;
    this.down.Redraw() ;
    this.close.Redraw() ;
    this.reset();
}
