﻿// Global timer table
var ColorChangeTimers=new Array();

function ColorChangeStop_timeout(element) {
    // Stop timer
    clearTimeout(ColorChangeTimers[element]);

    // Clear register
    ColorChangeTimers[element]=false;
}

function setcolor(element, step, totalsteps, incArray, to)
    {
    // Get object
    obj = document.getElementById(element);

    var currentColor = obj.style.backgroundColor;
    // Tidy up timer and register
    ColorChangeStop_timeout(element);
    
    var Color = new Array();
    var ToColor = new Array();
    var NewColor = new Array();
    for(var i = 0; i < 3; i++){
        Color[i] = parseInt(getColor(currentColor,i));
        ToColor[i] = parseInt(getColor(to,i));
    }
    
    if(step == totalsteps || (Color[0] == ToColor[0] && Color[1] == ToColor[1] && Color[2] == ToColor[2])){
        obj.style.backgroundColor = to;
    } else {
        for (var j = 0; j < 3; j++){
            if(incArray[j] >= 0 && (Color[j] + incArray[j]) >= ToColor[j]){
                NewColor[j] = ToColor[j];
            } else if(incArray[j] < 0 && (Color[j] + incArray[j]) < ToColor[j]){
                NewColor[j] = ToColor[j];
            } else {
                NewColor[j] = Color[j] + incArray[j];
            }
        }
        obj.style.backgroundColor = '#' + inttohex(NewColor[0]) + inttohex(NewColor[1]) + inttohex(NewColor[2]);
        step++;
        ColorChangeTimers[element] = window.setTimeout("setcolor(\'" + element + "\'," + step + "," + totalsteps + ",[" + incArray[0] + "," + incArray[1] + "," + incArray[2] + "],\'" + to + "\')",10); 
    }
}

function findColorChangeInc(from,to,totalsteps){
    var returnColor = new Array();
    for(var i = 0; i < 3; i++){
        var tempInc = (parseInt(getColor(to,i)) - parseInt(getColor(from,i))) / totalsteps;
        if(tempInc > 0){
            returnColor[i] = Math.ceil(tempInc);
        } else {
            returnColor[i] = Math.floor(tempInc);
        }
    }
    return returnColor;
}

function getColor(color,colorSection){
    if(colorSection == 0){
        if(color.substr(0,1) == '#'){
            return hextoint(color.substr(1,2));
        } else if (color.substr(0,3) == 'rgb'){
            return parseInt(color.split(',')[0].replace('rgb(',''));
        }
    } else if(colorSection == 1){
        if(color.substr(0,1) == '#'){
            return hextoint(color.substr(3,2));
        } else if (color.substr(0,3) == 'rgb'){
            return parseInt(color.split(',')[1]);
        }
    } else if(colorSection == 2){
        if(color.substr(0,1) == '#'){
            return hextoint(color.substr(5,2));
        } else if (color.substr(0,3) == 'rgb'){
            return parseInt(color.split(',')[2].replace(')',''));
        }
    }
}

function inttohex(number){
    var hex = parseInt(number).toString(16).toUpperCase();
    if(hex.length < 2){
        hex = '0' + hex;
    }
    return hex;
}

function hextoint(number){
    return parseInt(number,16);
}

