//
// newsticker.js realizes a newsticker which runs from right to left.
// Hyperlinks are supported.
// The text of  newsticker must be written in a div element with the id "tickerdiv".
//
var message;
var begin = 0; 
var end;
var delay = 0;
var cycle = 1;
var maxcycle = 0;
var stopp;
var norun;

function init () {
    message = document.getElementById("tickerdiv").innerHTML;
    end = document.getElementById("tickerdiv").innerHTML.length;
    maxcycle = document.getElementById("maxcycle").value;
    delay = document.getElementById("delay").value;
    if ( document.getElementById("norun") === null) {
        norun = false;
    } else {
        norun = document.getElementById("norun").value;
        if (norun === "true") {
            norun = true;
        } else {
            norun = false;
        }
    }
}

var link_handler = function () {
    var link_text = "";
    var begin_of_a_start = 0;
    var begin_of_a_end = 0;
    var link_text_start = 0;
    var link_text_end = 0;
    var end_of_a_start = 0;
    var end_of_a_end = 0;
    var step = 0;
    return {
        set: function (l) {
            link_text = l;
            begin_of_a_start = find_string(link_text, "<a");
            if (begin_of_a_start === 0) {
                begin_of_a_start = find_string(link_text, "<A");
            }
            begin_of_a_end = find_string(link_text, ">");
            end_of_a_start = find_string(link_text, "</a>");
            if (end_of_a_start === 0) {
                end_of_a_start = find_string(link_text, "</A>");
            }
            end_of_a_end = end_of_a_start + 4;
            link_text_start = begin_of_a_end + 1;
            link_text_end = end_of_a_start - 1;
        },
        get: function () {        
            var part1 = link_text.substring(begin_of_a_start,begin_of_a_end + 1);
            var part2 = this.get_link_text();
            var part3 = link_text.substring(end_of_a_start, end_of_a_end);
            var part4 = link_text.substring(end_of_a_end, link_text.length);
            return part1 + part2 + part3 + part4;
        },
        next_step: function () {
            step ++;
        },
        get_link_text: function () {
            return link_text.substring(link_text_start + step, link_text_end + 1);
        },
        get_end_of_link: function () {
            return end_of_a_end;
        },
        reset: function () {
            link_text = "";
            begin += 4 + begin_of_a_end - begin_of_a_start + 1;
            begin_of_a_start = 0;
            begin_of_a_end = 0;
            link_text_start = 0;
            link_text_end = 0;
            end_of_a_start = 0;
            end_of_a_end = 0;
            step = 0;
        }
    };
}

var in_link = function () {
    var il = false;
    return {
        set_true: function () {
            il = true;
        },
        is_in_link: function () {
            return il;
        },
        reset: function () {
            il = false;
        }
    };
};

var inLink = in_link();
var link = link_handler();

function message_substring (message) {
    var return_value;
    if ( typeof message === "string") {
        if (message.substring(begin, begin + 2) === "<a" || message.substring(begin, begin + 2) === "<A" ) {
            inLink.set_true();
            link.set(message.substring(begin, end));
            return_value = link.get();
        } else {
            if (inLink.is_in_link()) {
                link.next_step();
                return_value = link.get();
                if (link.get_link_text().length === 0) {
                    link.reset();
                    inLink.reset();
                }
            } else {
                return_value = message.substring(begin,end);        
            }
        }
        return return_value  + " " + message.substring(0, begin);
    } else return "";
}

function find_string (text, s) {
    for (j = 0; j <= text.length; j += 1) {
        if (text.substring(j, j + s.length) === s) {
            return j;
        } 
    }
    return 0;
}

function ticker () {
    if (norun) return;
    if ( typeof maxcycle === undefined || typeof delay === undefined ) {
        stopp = true;
    }
    begin ++;
    if (begin >= end) {
        begin = 0;
        if (cycle >= maxcycle) {
            stopp = true;
        }
        cycle ++;
    }
    document.getElementById("tickerdiv").innerHTML = "" + message_substring(message);
    if (stopp) return;
    window.setTimeout("ticker()", delay);
}


