﻿FadeEffect = function(elId, sred, sgreen, sblue, ered, egreen, eblue, step, propertyTOFade) {
    this.id = FadeEffect.cnt;
    this.elId = elId;
    this.sred = sred;
    this.sgreen = sgreen;
    this.sblue = sblue;
    this.ered = ered;
    this.egreen = egreen;
    this.eblue = eblue;
    this.step = step;
    this.counter = step;
    this.propertyTOFade = propertyTOFade;

    FadeEffect.objects[FadeEffect.cnt++] = this;

    this.fade = function() {
        if (this.counter > 0) {
            var red = Math.floor(this.ered * this.counter / this.step + this.sred * ((this.step - this.counter) / this.step));
            var green = Math.floor(this.egreen * this.counter / this.step + this.sgreen * ((this.step - this.counter) / this.step));
            var blue = Math.floor(this.eblue * this.counter / this.step + this.sblue * ((this.step - this.counter) / this.step));
            var el = this.elId;

            if (this.propertyTOFade == "backgroundColor")
                el.style.backgroundColor = "#" + this.toHex(red) + this.toHex(green) + this.toHex(blue);
            else
                el.style.color = "#" + this.toHex(red) + this.toHex(green) + this.toHex(blue);

            setTimeout('FadeEffect.objects[' + this.id + '].fade();', (this.step - this.counter));
            this.counter--;
        }
        else {
            var el = this.elId;
            if (this.propertyTOFade == 'backgroundColor')
                el.style.backgroundColor = "transparent";
        }
    }

    this.toHex = function(dec) {
        var hexCharacters = "0123456789ABCDEF"
        if (dec < 0)
            return "00"
        if (dec > 255)
            return "FF"
        var i = Math.floor(dec / 16)
        var j = dec % 16
        return hexCharacters.charAt(i) + hexCharacters.charAt(j)
    }
}

FadeEffect.cnt = 0;
FadeEffect.objects = new Array();

function FadeFromYellow(elId, r, g, b, propertyTOFade) {
    f = new FadeEffect(elId, r, g, b, 204, 255, 0, 35, propertyTOFade);
    f.fade();
}

function FadeProductFeatureFromYellow(elId, r, g, b, propertyTOFade) {
    f = new FadeEffect(elId, r, g, b, 255, 255, 204, 50, propertyTOFade);
    f.fade();
}
