﻿

var newsbar = function(name, targetId, speed, timeout) {
    this.id = targetId;
    this.speed = speed || 100;
    this.timeout = timeout || 2000;
    this.position = 0;
    this.index = 0;
    this.name = name;
    this.wait = false;
};
newsbar.prototype.setWait = function(bool) {
    this.wait = bool;
    if (!bool)
        this.start();
    else if (this.end && bool)
        this.stop();
};
newsbar.prototype.init = function() {
    var n = this.name;
    var aTag = '<a onmouseover="' + n + '.setWait(true);this.style.color=\'red\'" onmouseout="' + n + '.setWait(false)" id="' + n + '_newsbar" href="" title=""></a>';
    document.getElementById(this.id).innerHTML = aTag;
    this.id = this.name + '_newsbar';
    this.start();
};
newsbar.prototype.setData = function(data) {
    this.data = data;
};
newsbar.prototype.next = function() {
    if (!(this.end && this.wait)) {
        this.position++;
        var spd = this.speed;
        var item = this.data[this.index];
        if (this.position > item.text.length) {
            this.clear();
            this.index++;
            if (this.index > (this.data.length - 1)) {
                this.index = 0;
            };
            item = this.data[this.index];
            document.getElementById(this.id).href = item.link;
            document.getElementById(this.id).title = item.title;
        } else {
        	document.getElementById(this.id).href = item.link;
            document.getElementById(this.id).title = item.title;
            document.getElementById(this.id).style. color = "white";
            document.getElementById(this.id).innerHTML = item.text.substring(0, this.position) ;
            if (this.position == item.text.length) {
                spd = this.timeout;
                this.end = true;
            };
        };
        this.timer = window.setTimeout(this.name + ".next()", spd);
    };
};
newsbar.prototype.clear = function() {
    this.position = 0;
    this.end = false;
    document.getElementById(this.id).innerHTML = "";
};
newsbar.prototype.stop = function() {
    window.clearTimeout(this.timer);
};
newsbar.prototype.start = function() {
    window.clearTimeout(this.timer);
    this.end = false;
    this.timer = window.setTimeout(this.name + ".next()", this.speed);
};
newsbar.prototype.position = 0;

