// {{{ description

/**
 * fade.js is a script which makes links fade in and out
 *
 * This script will force all links on the page (unless some hacking is done)
 * to fade from a start color to and end color when flying over the link and
 * vice-versa for mouseout.
 * Configuration is achieved by modifying the start and end color as well as
 * the speed of fade in and fade out.  Just uncomment the variables in the
 * configuration section.
 * As it stands right now, this will interrupt other onmouseover/onmouseout
 * registered functions, unless some hacking is done.
 *
 * @version Revision 2.0
 * @author  Dan Allen <dan@mojavelinux.com>
 * @access  public
 * @browser IE 5+, Mozilla/Netscape 6.x
 */

// }}}
// {{{ configuration (modifiable)

//var fade_startColor = '#990033';
//var fade_endColor = '#ffffff';
//var fade_stepIn = 20;
//var fade_stepOut = 30;

// }}}
// {{{ init (don't modify)

fade_hexa = new fade_makearray(16); 
for (var i = 0; i < 10; i++) {
    fade_hexa[i] = i; 
}

fade_hexa[10]="a";
fade_hexa[11]="b";
fade_hexa[12]="c"; 
fade_hexa[13]="d";
fade_hexa[14]="e";
fade_hexa[15]="f"; 

var fade_tags = new Array(); 

// MouseOut link color...just change what is between the 'fade_dehexize("' and '")'
fade_startColor = typeof(fade_startColor) == 'undefined' ? fade_dehexize("#990033") : fade_dehexize(fade_startColor);
// MouseOver link color...just change what is between the ()
fade_endColor = typeof(fade_endColor) == 'undefined' ? fade_dehexize("#ffffff") : fade_dehexize(fade_endColor);

// delay when fading in 
fade_stepIn = typeof(fade_stepIn) == 'undefined' || isNaN(Number(fade_stepIn)) ? 20 : Number(fade_stepIn); 
// delay when fading out 
fade_stepOut = typeof(fade_stepOut) == 'undefined' || isNan(Number(fade_stepOut)) ? 30 : Number(fade_stepOut); 

// }}}
// {{{ event registration (don't modify)

document.onmouseover = fade_mouseover; 
document.onmouseout = fade_mouseout; 

// }}}
// {{{ fade functions (don't modify)

function fade_dehexize(Color)
{ 
    Color = Color.toLowerCase();
    var colorArr = new fade_makearray(3); 
    for (i=1; i<7; i++){ 
        for (j=0; j<16; j++){ 
            if (Color.charAt(i) == fade_hexa[j]) { 
                if (i%2 !=0) {
                    colorArr[Math.floor((i-1)/2)]=eval(j)*16; 
                }
                else {
                    colorArr[Math.floor((i-1)/2)]+=eval(j); 
                }
            } 
        } 
    } 

    return colorArr; 
} 

function fade_mouseover(e) 
{ 
    var srcElement;
    var tagName;

    if (document.all) { 
        tagName = event.srcElement.tagName;
        srcElement = event.srcElement;
    }
    else if (document.getElementById) {
        srcElement = e.target.parentNode;
        tagName = e.target.parentNode.tagName;
    }

    if (tagName == "A") {
        fade_main(fade_startColor, fade_endColor, srcElement, fade_stepIn); 
    }
} 

function fade_mouseout(e) 
{ 
    var srcElement;
    var tagName;

    if (document.all) { 
        tagName = event.srcElement.tagName;
        srcElement = event.srcElement;
    }
    else if (document.getElementById) {
        srcElement = e.target.parentNode;
        tagName = e.target.parentNode.tagName;
    }

    if (tagName == "A") {
        fade_main(fade_endColor, fade_startColor, srcElement, fade_stepOut); 
    }
} 

function fade_makearray(n) 
{ 
    this.length = n; 
    for(var i = 1; i <= n; i++) {
        this[i] = 0; 
    }

    return this; 
} 

function fade_hex(i) 
{ 
    if (i < 0) {
        return "00"; 
    }
    else if (i > 255) {
        return "ff"; 
    }
    else { 
        return "" + fade_hexa[Math.floor(i/16)] + fade_hexa[i%16];
    }
} 

function fade_setColor(r, g, b, element) 
{ 
    var hr = fade_hex(r);
    var hg = fade_hex(g); 
    var hb = fade_hex(b); 
    element.style.color = "#"+hr+hg+hb; 
} 

function fade_main(s, e, element, step)
{ 
    var sr = s[0]; 
    var sg = s[1]; 
    var sb = s[2]; 
    var er = e[0]; 
    var eg = e[1]; 
    var eb = e[2]; 

    if (fade_tags[0] != null && fade_tags[0] != element) { 
        fade_setColor(sr, sg, sb, fade_tags[0]);
    }

    for (var i = 1; i < fade_tags.length; i++) {
        clearTimeout(fade_tags[i]); 
    }

    // we need to do this for the setTimeout method
    localElement = element;
    for (var i = 0; i <= step; i++) { 
        fade_tags[i+1] = setTimeout("fade_setColor(Math.floor(" +sr+ " *(( " +step+ " - " +i+ " )/ " +step+ " ) + " +er+ " * (" +i+ "/" + step+ ")), Math.floor(" +sg+ " * (( " +step+ " - " +i+ " )/ " +step+ " ) + " +eg+ " * (" +i+ "/" +step+ ")), Math.floor(" +sb+ " * ((" +step+ "-" +i+ ")/" +step+ ") + " +eb+ " * (" +i+ "/" +step+ ")), localElement);", i*step); 
    } 

    fade_tags[0] = element;
}

// }}}