// Global variables
var scrollEngaged = false;
var scrollInterval;
var scrollBars = new Array( );
   
// Read effective style property
function getElementStyle(elemID, IEStyleAttr, CSSStyleAttr) {
    var elem = document.getElementById(elemID);
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleAttr];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleAttr);
    }
    return "";
}
   
// Abstract object constructor function
// gf added maxPages
function scrollBar(ownerID, ownerContentID, upID, dnID, maxPages) {
    this.ownerID = ownerID;
    this.ownerContentID = ownerContentID;
    this.index = scrollBars.length;
    this.upButton = document.getElementById(upID);
    this.dnButton = document.getElementById(dnID);
    this.upButton.index = this.index;
    this.dnButton.index = this.index;
    
    this.ownerHeight = parseInt(getElementStyle(this.ownerID, "height", "height"));
   
    this.contentElem = document.getElementById(ownerContentID);
    //this.contentFontSize = 15;
    this.contentFontSize = parseInt(getElementStyle(this.ownerContentID, 
        "fontSize", "font-size"));
    this.contentScrollHeight = (this.contentElem.scrollHeight) ? 
        this.contentElem.scrollHeight : this.contentElem.offsetHeight;
    this.initScroll = initScroll;
	
	this.position = 0;
	//maxPosition is height of host psuedo window plus a small offset
	this.maxPosition = (this.ownerHeight * maxPages) + (2 * this.contentFontSize);
	
}

function test(){
    alert("test");
}


// Assign event handlers to actual scroll buttons
function initScroll( ) {
    //this.upButton.onmouseover = handleScrollClick;
    //this.upButton.onmouseout = handleScrollStop;
    this.upButton.onmousedown = handleScrollClick;
    this.upButton.onmouseup = handleScrollStop;
    this.upButton.oncontextmenu = blockEvent;
   
    //this.dnButton.onmouseover = handleScrollClick;
    //this.dnButton.onmouseout = handleScrollStop;
    this.dnButton.onmousedown = handleScrollClick;
    this.dnButton.onmouseup = handleScrollStop;
    this.dnButton.oncontextmenu = blockEvent;
    
    //var isIEMac = (navigator.appName.indexOf("Explorer") != -1 && 
    //    navigator.userAgent.indexOf("Mac") != -1);
    //if (!isIEMac) {
    //    document.getElementById("innerWrapper0").style.overflow = "hidden";
    //}
}
   
/**************************
   Event Handler Functions
***************************/
// Turn off scrolling
function handleScrollStop( ) {
    scrollEngaged = false;
}
   
// Block contextmenu for Mac (holding down mouse button)
function blockEvent(evt) {
    evt = (evt) ? evt : event;
    evt.cancelBubble = true;
    return false;
}

// Initiate scrolling the content per the clicked button (up or down)
function handleScrollClick(evt) {
    var fontSize;
    evt = (evt) ? evt : event;
    var target = (evt.target) ? evt.target : evt.srcElement;
    var index = target.index;
    fontSize = scrollBars[index].contentFontSize;
	//controls up vs. down
    fontSize = (target.className == "lineup") ? fontSize : -fontSize;
    scrollEngaged = true;

    //if (((scrollBars[index].position - fontSize) > 0) && ((scrollBars[index].position - fontSize) <= scrollBars[index].maxPosition)) {
    // do single click scroll
    scrollBy(index, parseInt(fontSize));
    // trigger click-and-hold scrolling
    scrollInterval = setInterval("scrollBy(" + index + ", " + 
        parseInt(fontSize) + ")", 100);
    evt.cancelBubble = true;
	//}
    return false;
}
   
// Perform actual scroll singly or repeatedly (through setInterval( ))
function scrollBy(index, px) {
    var scroller = scrollBars[index];
    var elem = document.getElementById(scroller.ownerContentID);
    var top = parseInt(elem.style.top);
    var scrollHeight = parseInt(scroller.contentScrollHeight);
    var height = scroller.ownerHeight;
	//alert("scrollHeight = " + scrollHeight + " height= " + height + " top = " + top);
	//alert("px = " + px + " scroller.position = " + scroller.position + " max= " + scroller.maxPosition + " newposition = " + (scroller.position - px));
    if ((scrollEngaged) && ((scroller.position - px) > 0) && ((scroller.position - px) <= scroller.maxPosition)) {
    //if ((scrollEngaged) && (top + px >= -scrollHeight + height) && (top + px <= 0)) {
    //if (scrollEngaged ) {
        shiftBy(elem, 0, px);
		//alert("scroller.position = " + scroller.position + " max= " + scroller.minPosition);
		scroller.position -= px;
    } else {
   // } else {
        clearInterval(scrollInterval);
    }
}