﻿function fadeObject(id, fromclr, toclr, fadingtime, displtime, nodispltime, startindex) {
    var self = this;
    this.inc = 100;
    this.id = id;
    this.elem = false;
    this.msg = [];
    this.dir = false;
    this.delayout = displtime;
    this.delayin = nodispltime;
    this.colour = {
        starting: [parseInt(fromclr.substr(0, 2), 16), parseInt(fromclr.substr(2, 2), 16), parseInt(fromclr.substr(4, 2), 16)],
        ending: [parseInt(toclr.substr(0, 2), 16), parseInt(toclr.substr(2, 2), 16), parseInt(toclr.substr(4, 2), 16)],
        now: [parseInt(fromclr.substr(0, 2), 16), parseInt(fromclr.substr(2, 2), 16), parseInt(fromclr.substr(4, 2), 16)]
    };
    this.colourdir = [((this.colour.ending[0] - this.colour.starting[0]) / (fadingtime / this.inc)), ((this.colour.ending[1] - this.colour.starting[1]) / (fadingtime / this.inc)), ((this.colour.ending[2] - this.colour.starting[2]) / (fadingtime / this.inc))];
    function d2h(num) {
        num = Math.round(num);
        if (num > 255) num = 255;
        if (num < 0) num = 0;
        return ((num < 16) ? "0" : "") + num.toString(16);
    };
    this.msgindex = 0;
    this.nextmsg = function() {
    if (this.msg.length > 0) {
            this.msgindex++;
            while (this.msgindex > this.msg.length) this.msgindex -= this.msg.length;
        }
    }
    this.fadeIn = function() {
        this.elem = this.elem || document.getElementById(this.id);
        if (!this.msg[this.msgindex]) if(this.msg.length > 0) this.nextmsg();
        if (this.msg[this.msgindex]) this.elem.innerHTML = this.msg[this.msgindex];
        for (var i = 0; i < 3; i++) if (this.colour.now[i] != this.colour.ending[i]) this.colour.now[i] += this.colourdir[i];
        this.elem.style.color = "#" + d2h(this.colour.now[0]) + d2h(this.colour.now[1]) + d2h(this.colour.now[2]);
        if ((Math.round(this.colour.now[0]) != Math.round(this.colour.ending[0])) || (Math.round(this.colour.now[1]) != Math.round(this.colour.ending[1])) || (Math.round(this.colour.now[2]) != Math.round(this.colour.ending[2]))) setTimeout(function() { self.fadeIn(); }, this.inc);
        else 
        {
            setTimeout(function() { self.waitForFadeOut(); }, this.inc);
        }
        try 
        {
            BrowserFixes();
        }
        catch (err) 
        {
        }
    };
    this.waiting = 0;
    this.waitForFadeOut = function() {
    if (this.waiting == 0) this.waiting = this.delayout;
    this.waiting -= this.inc;
    if (this.waiting <= 0) {
        this.waiting = 0;
            setTimeout(function() { self.fadeOut(); }, this.inc);
        }
        else setTimeout(function() { self.waitForFadeOut(); }, this.inc);
        try {
            BrowserFixes();
        }
        catch (err) {
        }
    }
    this.fadeOut = function() {
        this.elem = this.elem || document.getElementById(this.id);
        if (!this.msg[this.msgindex]) if (this.msg.length > 0) this.nextmsg();
        if (this.msg[this.msgindex]) this.elem.innerHTML = this.msg[this.msgindex];
        for (var i = 0; i < 3; i++) if (this.colour.now[i] != this.colour.ending[i]) this.colour.now[i] -= this.colourdir[i];
        this.elem.style.color = "#" + d2h(this.colour.now[0]) + d2h(this.colour.now[1]) + d2h(this.colour.now[2]);
        if ((Math.round(this.colour.now[0]) != Math.round(this.colour.starting[0])) || (Math.round(this.colour.now[1]) != Math.round(this.colour.starting[1])) || (Math.round(this.colour.now[2]) != Math.round(this.colour.starting[2]))) setTimeout(function() { self.fadeOut(); }, this.inc);
        else {
            this.nextmsg();
            setTimeout(function() { self.waitForFadeIn(); }, this.delayin);
        }
        try {
            BrowserFixes();
        }
        catch (err) {
        }
    }
    this.waitForFadeIn = function() {
    this.elem.innerHTML = '&nbsp;';
    if (this.waiting == 0) this.waiting = this.delayin;
    this.waiting -= this.inc;
    if (this.waiting <= 0) {
        this.waiting = 0;
            setTimeout(function() { self.fadeIn(); }, this.inc);
        }
        else setTimeout(function() { self.waitForFadeIn(); }, this.inc);
        try {
            BrowserFixes();
        }
        catch (err) {
        }
    }
    this.start = function() {
        this.msgindex = Math.round(startindex);
        if (window.addEventListener) {
            window.addEventListener('load', function() { self.fadeIn(); }, false);
        }
        else if (window.attachEvent) {
            window.attachEvent('onload', function() { self.fadeIn(); });
        }
    }
    this.start();
}
//var fader = new Array();
////fader[0] = new fadeObject('fadediv', 'FFFFFF', 'E82525', 3000, 10000, 1000, (Math.random() * 6));
////fader[0] = new fadeObject('fadediv', '000000', '000000', 3000, 5000, 1000, (Math.random() * 6));
//fader[0] = new fadeObject('fadediv', 'FFFFFF', '000000', 2000, 3000, 1000, 1);
//fader[0].msg[1] = "&quot;Het ERP systeem voor de voedings- en genotsmiddelen industrie.&quot;";
//fader[0].msg[2] = "&nbsp;";
////fader[0].msg[2] = "&quot;Must add text.&quot;";
