// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1
// @author    Adam Michela

function make_hex (r,g,b) 
{
    r = r.toString(16); if (r.length == 1) r = '0' + r;
    g = g.toString(16); if (g.length == 1) g = '0' + g;
    b = b.toString(16); if (b.length == 1) b = '0' + b;
    return "#" + r + g + b;
}

function fade_element (id, fps, duration, from, to) 
{
    if (!fps) fps = 20;
    if (!duration) duration = 1000;
    if (!from || from=="#") from = "#FFDD00";
    // if (!to) to = get_bgcolor(id); // pour eviter de "coincer" le mécanisme lors d'appels successifs
    if (!to) to = "#DDDDDD";

    var frames = Math.round(fps * (duration / 1000));
    var interval = duration / frames;
    var delay = interval;
    var frame = 0;

    if (from.length < 7) from += from.substr(1,3);
    if (to.length < 7) to += to.substr(1,3);

    var rf = parseInt(from.substr(1,2),16);
    var gf = parseInt(from.substr(3,2),16);
    var bf = parseInt(from.substr(5,2),16);
    var rt = parseInt(to.substr(1,2),16);
    var gt = parseInt(to.substr(3,2),16);
    var bt = parseInt(to.substr(5,2),16);

    var r,g,b,h;
    while (frame < frames)
    {
        r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
        g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
        b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
        h = make_hex(r,g,b);

        setTimeout("set_bgcolor('"+id+"','"+h+"')", delay);

        ++frame;
        delay = interval * frame; 
    }
    setTimeout("set_bgcolor('"+id+"','"+to+"')", delay);
}

function set_bgcolor (id, c)
{
    document.getElementById(id).style.backgroundColor = c;
}

function set_opacity (id, c)
{
    document.getElementById(id).style.MozOpacity = c;
    document.getElementById(id).style.KhtmlOpacity = c;
    document.getElementById(id).style.filter = "alpha(opacity="+parseInt(c*100)+")";
}

function set_position (id, c)
{
    document.getElementById(id).style.top = c+'px';
    document.getElementById(id).style.left = c+'px';
    document.getElementById(id).style.right = c+'px';
    document.getElementById(id).style.bottom = c+'px';
    window.status = c;
}

function get_bgcolor (id)
{
    var o = document.getElementById(id);
    while(o)
    {
        var c;
        if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
        if (o.currentStyle) c = o.currentStyle.backgroundColor;
        if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
        o = o.parentNode;
    }
    if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
    var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
    if (rgb) c = make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
    return c;
}

function fade_element_alpha (id, fps, duration, from, to) 
{
    if (!fps) fps = 20;
    if (!duration) duration = 1000;
    if (!from || from=="#") from = "100";
    if (!to) to = "0";

    var frames = Math.round(fps * (duration / 1000));
    var interval = duration / frames;
    var delay = interval;
    var frame = 0;

    var rf = parseInt(from);
    var rt = parseInt(to);

    var r;
    while (frame < frames)
    {
        r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames))/100;

        setTimeout("set_opacity('"+id+"','"+r+"')", delay);

        ++frame;
        delay = interval * frame; 
    }
    if (0 == to) setTimeout("closediv('"+id+"')", delay);
    setTimeout("set_opacity('"+id+"','"+to+"')", delay);
}

function redim_element (id, fps, duration, from, to) 
{
    if (!fps) fps = 20;
    if (!duration) duration = 1000;
    if (!from || from=="#") from = "400";
    if (!to) to = "200";

    var frames = Math.round(fps * (duration / 1000));
    var interval = duration / frames;
    var delay = interval;
    var frame = 0;

    var rf = parseInt(from);
    var rt = parseInt(to);

    var r;
    while (frame < frames)
    {
        r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));

        setTimeout("set_position('"+id+"','"+r+"')", delay);

        ++frame;
        delay = interval * frame; 
    }
    setTimeout("set_position('"+id+"','"+to+"')", delay);
}
