// automatic scrollable area code

// #containerDiv {
//     position: relative;
//     left: 0px;
//     top: 0px;
//     width: 300px;
//     height: 100px;
//     overflow: hidden;
//     clip: rect(0px 300px 100px 0px); /* top, right, bottom, left */
// }
// 
// #contentDiv {
//     left: 0px;
//     top: 0px;
//     position: absolute;
// }

var resumeScrollingTimeout = null;

var NO_OVERRIDE = -1;

var SLOW_INC = 2;
var SLOW_WAIT = 200;
var STANDARD_INC = 1;
var STANDARD_WAIT = 25;

var INITIAL_PAUSE_MS = 3000;
var PAUSE_MS = 4000;

var USER_AGENT_HEADER = navigator.userAgent.toLowerCase();

var supportedOS = (USER_AGENT_HEADER.indexOf("windows") > -1 || USER_AGENT_HEADER.indexOf("mac") > -1);

var isMacIE = (USER_AGENT_HEADER.indexOf("msie") > -1 && USER_AGENT_HEADER.indexOf("mac") > -1);

var scrollIncrementPx;
var scrollIntervalWaitMs;

var supportedBrowser;

if (!supportedOS) {
    supportedBrowser = false;
} else if (
    USER_AGENT_HEADER.indexOf("msie") > -1 ||    // IE, Opera
    USER_AGENT_HEADER.indexOf("firefox") > -1 || // firefox
    USER_AGENT_HEADER.indexOf("safari") > -1 ||  // safari
    USER_AGENT_HEADER.indexOf("netscape") > -1   // netscape
  ) {
    scrollIncrementPx = STANDARD_INC;
    scrollIntervalWaitMs = STANDARD_WAIT;
    supportedBrowser = true;
} else if (USER_AGENT_HEADER.indexOf("mozilla") > -1) {
    scrollIncrementPx = SLOW_INC;
    scrollIntervalWaitMs = SLOW_WAIT;
    supportedBrowser = true;
} else {
    supportedBrowser = false;
}

if (isMacIE) {
    supportedBrowser = false;
}

var scrollingEnabled = true;

var contentDivObj = null;
var contentDivHeight = 0;

var scrollingActive = true;

var unique_row_count = 7;
var inBorder = 0;
var inPadding = 0;
var xtBorder = 0;
var xtPadding = 0;

var currentRow;
var currentRowDistToTopPx;
var rowHeights = [];

var pauseMs = PAUSE_MS;
var initialPauseMs = INITIAL_PAUSE_MS;

var pauseMsOverride = -1;
var initialPauseMsOverride = -1;

if (pauseMsOverride != NO_OVERRIDE) {
    pauseMs = pauseMsOverride;
}
if (initialPauseMsOverride != NO_OVERRIDE) {
    initialPauseMs = initialPauseMsOverride;
}

function initScroller() {
    if (
        !supportedBrowser ||
        (typeof document.all == "undefined" && typeof document.getElementById == "undefined") ||
        document.getElementById("contentDiv") == null
      ) {
        scrollingEnabled = false;
    } else {
        setTimeout("startScroll()", initialPauseMs);
    }
}

function startScroll() {
    if (scrollingEnabled) {
        calculateRowHeights();
        currentRow = 2;
        currentRowDistToTopPx = rowHeights[1];
        contentDivObj = document.getElementById("contentDiv");
        contentDivHeight = contentDivObj.offsetHeight;
        currTop = 0;
        doSetInterval(doScrollStepIfActive, "doScrollStepIfActive()", scrollIntervalWaitMs);
    }
}

function calculateRowHeights() {
    var heightsSum = 0;
    var rows = document.getElementById('contentDiv').getElementsByTagName('tr');
    for (var i=1; i<=unique_row_count; i++) {
        var rowHeight = 0;
        var cells = rows[i-1].getElementsByTagName('td');
        for (var j=0; j<cells.length; j++) {
            rowHeight += cells[j].offsetHeight;
        }
        rowHeights[i]=rowHeight+xtBorder-1; // +inBorder*2+inPadding*2+xtPadding*2
        heightsSum+=rowHeights[i];
        if (i == unique_row_count) {
            // calculations may not have accounted for all height-increasing
            // factors on each cell, so even it out on the last cell:
            rowHeights[i]+=Math.round((document.getElementById("contentDiv").offsetHeight/2-heightsSum));
        }
    }
}

function doSetInterval(fn, fnStr, ms) {
    if (typeof window.setInterval != "undefined") {
        return setInterval(fn, ms);
    } else {
        var timeouts = [];
        var currMs = 0;
        for (var i=0; i<50; i++) {
            timeouts[i]=setTimeout(fnStr, currMs);
            currMs+=ms;
        }
        setTimeout("doSetInterval(null, \""+fnStr+"\", "+ms+")", currMs);

        return timeouts;
    }
}

function doScrollStepIfActive() {
    if (scrollingActive) {
        doScrollStep();
    }

}

function doScrollStep() {
    if (scrollingEnabled) {
        currTop -= scrollIncrementPx;
        currentRowDistToTopPx -= scrollIncrementPx;
        var minTop = Math.floor(contentDivHeight/-2)+1;
        if (currTop < minTop) {
            currTop = minTop;
        }
        if (currentRowDistToTopPx < 0) {
            pauseScrolling();
            resumeScrollingTimeout = setTimeout("resumeScrolling()", pauseMs);
            currentRow+=1;
            if (currentRow == 2) {
                currTop = 0;
            } else if (currentRow > unique_row_count) {
                currentRow = 1;
            }
            var prevRow = currentRow-1;

            if (prevRow < 1) {
                prevRow = unique_row_count;
            }
            currentRowDistToTopPx = rowHeights[prevRow];
        } else {
            var newTop = currTop-1;
            if (newTop <= minTop) {
                newTop = minTop;
            }
            contentDivObj.style.top = ""+(newTop)+"px";
        }
    }

}

// call from contentDiv's onMouseOver for pause on mouse over
function pauseScrolling() {
    scrollingActive = false;
}

function resumeScrolling() {
    // to be safe...
    if (resumeScrollingTimeout != null) {
        clearTimeout(resumeScrollingTimeout);
    }
    // ...then allow scrolling again:
    scrollingActive = true;
}

// call from contentDiv's onMouseOut for pause on mouse over
function doResumeScrolling() {
    // if we were waiting to resume scrolling, don't: do it now, then
    // cancel the resume scheduled for the future (otherwise we'll
    // end up with scrolling breaks between ads that are not as long
    // as they should be):
    if (resumeScrollingTimeout != null) {
        clearTimeout(resumeScrollingTimeout);
    }
    resumeScrolling();
}

