var x, x2, steps, steps2;

// Adjustable parameters for configuration
// of the scroller
var WIDTH;
var LINK_WAIT;
setWidth(235);
setLinkWait(40);

// Internal variables
var linkFeed = new Array();
linkFeed[0] = new Array('', '');
var linkFeedLength = 1;
var lineLength;
var dt = 0;
var bIndex = 0;
var browserType = browserCheck();
var linkCounter;
var target = '';
var classid = '';
var path = new Array(90);
var point = false;
var initCalled = 0;

// Method for adding a text (+ link) to the scroller.
function addLink(url, title) {
	if (initCalled == 0) {
        linkFeed[linkFeedLength] = new Array(url, title);
        linkFeedLength++;
    } else {
        // You are not allowed to add a link
        // after that the scroller is initialized
    }
}

function setWidth(myWidth) {
    if (initCalled == 0) {
        WIDTH = myWidth;
        x = WIDTH;
        x2 = WIDTH + WIDTH;
        steps = WIDTH;
        steps2 = WIDTH;
    }
}

function setLinkWait(myLinkWait) {
    if (initCalled == 0) {
        LINK_WAIT = myLinkWait;
    }
}

// Initialize needed variables, and start scroller
function init(link) {
    initCalled = 1;
    lineLength = new Array(linkFeedLength);
    for (i = 0; i < linkFeedLength; i++) {
        lineLength[i] = linkFeed[i][1].length * 10;
    }
    precalcPath();
    linkCounter = link;
    setLink();
    scroller();
}


// Precalculate numbers for the 'brake' effect.
function precalcPath() {
    var v = 0;
    for (i = 0; i < 90; i++) {
        v = ((i + 180) / 180) * Math.PI;
        path[i] = Math.sin(v);
    }
}


// Move the two text layers to the new coordinates.
function moveLayers() {
    if (browserType == B_MOZILLA) {
        document.getElementById("linkscroller").style.left = x;
        document.getElementById("linkscroller2").style.left = x2;
    } else if (browserType == B_NS4) {
        document.linkscroller.left = x;
        document.linkscroller2.left = x2;
    } else {
        document.all.linkscroller.style.left = x*2;
        document.all.linkscroller2.style.left = x2*2;
    }
}


// Calculate new scroller coordinates, and call the delay
// method.
function scroller() {
    moveLayers();
    if (point == false) {
        if (steps2 > 34) {
            x--;
            x2--;
            steps--;
            steps2--;
        } else {
            x = x - 1 - path[bIndex];
            x2= x2 - 1 - path[bIndex];
            steps= steps - 1 - path[bIndex];
            steps2= steps2 - 1 - path[bIndex];
            bIndex++;
            if (bIndex > 89) {
                bIndex = 0;
            }
        }
    }
    if (steps < 1) {
        setLink();
        steps = WIDTH;
    }
    var t = setTimeout("scroller()", 8);
    if (steps2 < 1) {
        clearTimeout(t);
        delay();
    }
}


// Method for stopping the scroller each time a text line
// has reached the start of the scroller window.
function delay() {
    dt++;
    var t2 = setTimeout("delay()", 10);
    if (dt > (LINK_WAIT * 7)) {
        clearTimeout(t2);
        dt = 0;
        steps2 = WIDTH;
        bIndex = 0;
        scroller();
    }
}


// Insert text into the text layers
function setLink() {
    var output = '' + linkFeed[linkCounter][1] + '';

    var output2 = '' + linkFeed[linkCounter + 1][1] + '';

    if (browserType == B_MOZILLA) {
        document.getElementById("linkscroller").innerHTML = output;
    } else if (browserType == B_NS4) {
        t = setTimeout("replaceNS4('" + output + "', 'linkscroller')", 0);
    } else if (browserType == B_MSIE) {
        document.all.linkscroller.innerHTML = '<nobr>' + output + '</nobr>';
    } else {
        document.all.linkscroller.innerHTML = output;
    }
    x = 0;
    x2 = WIDTH;
    moveLayers();
    if (browserType == B_MOZILLA) {
        document.getElementById("linkscroller2").innerHTML = output2;
    } else if (browserType == B_NS4) {
        t = setTimeout("replaceNS4('" + output2 + "', 'linkscroller2')", 0);
    } else if (browserType == B_MSIE) {
        document.all.linkscroller2.innerHTML = '<nobr>' + output2 + '</nobr>';
    } else {
        document.all.linkscroller2.innerHTML = output2;
    }
    linkCounter++;
    if (linkCounter == linkFeedLength - 1) {
        linkFeed[0][0] = linkFeed[linkFeedLength - 1][0];
        linkFeed[0][1] = linkFeed[linkFeedLength - 1][1];
        linkCounter = 0;
    }
}


// Special method for dealing with layers in Netscape 4 versions
function replaceNS4(str, layer) {
   document.layer.document.open();
   document.layer.document.write(str);
   document.layer.document.close();
}
