/************************************************************************/
/* Fade Link Version 1.00 (2001.3.13)                                   */
/*                                                                      */
/* Copyright (C) 1999-2001 TAKANASHI Mizuki                             */
/* takanasi@hamal.freemail.ne.jp                                        */
/*----------------------------------------------------------------------*/
/* Read it somehow even if my English text is a little wrong! ;-)       */
/*                                                                      */
/* Usage:                                                               */
/*  Insert '<script src="fadelink.js"></script>' into the BODY section, */
/*  right after the BODY tag itself, before anything else.              */
/*  You don't need to add "onMouseover" and "onMouseout" attributes!!   */
/*                                                                      */
/*  If you'd like to add effect to other texts(not link texts), then    */
/*  add 'onmouseover="doFade();"' and 'onmouseout="stopFade();"'        */
/*  to the target tags.                                                 */
/*                                                                      */
/* This Script works with IE4 and above only, but no error occurs on    */
/* other browsers.                                                      */
/************************************************************************/


////////////////////////////////////////////////////////////////////
// Setting

var step = 16;          // Change steps
var clrA = "#0000ff";   // Color A
var clrB = "#ff0000";   // Color B


////////////////////////////////////////////////////////////////////
// Main routine

var obj;                        // The object which event occured in
var act = 0;                    // Flag during the action
var cstep = 0;                  // Current step
var elmC = new Array(0,0,0);    // Current colors elements
var clrOrg;                     // A color before the change
var TimerID;                    // Timer ID


if (navigator.appName.indexOf("Microsoft",0) != -1 && parseInt(navigator.appVersion) >= 4) {
    Browser = true;
} else {
    Browser = false;
}

if (Browser) {
    document.onmouseover = doFadeAnchor;
    document.onmouseout = stopFadeAnchor;
}


//=============================================================================
// doFade
//  This function begins to change a color.
//=============================================================================
function doFade()
{
    if (Browser && act == 0) {
        act = 1;
        obj = event.srcElement;
        clrOrg = obj.style.color;
        TimerID = setInterval("ChangeColor()",35);
    }
}


//=============================================================================
// stopFade
//  This function stops to change a color.
//=============================================================================
function stopFade()
{
    if (Browser && act != 0) {
        obj.style.color = clrOrg;
        clearInterval(TimerID);
        act = 0;
        cstep = 0;
    }
}


//=============================================================================
// doFadeAnchor
//  This function begins to change a color. (of a anchor, automatically)
//=============================================================================
function doFadeAnchor()
{
    if (Browser && act == 0) {
        obj = event.srcElement;

        while (obj.tagName != 'A' && obj.tagName != 'BODY') {
            obj = obj.parentElement;
            if (obj.tagName == 'A' || obj.tagName == 'BODY')
                break;
        }

        if (obj.tagName == 'A' && obj.href != '') {
            act = 1;
            clrOrg = obj.style.color;
            TimerID = setInterval("ChangeColor()",100);
        }
    }
}


//=============================================================================
// stopFadeAnchor
//  This function stops to change a color. (of a anchor, automatically)
//=============================================================================
function stopFadeAnchor()
{
    if (Browser && act != 0) {
        if (obj.tagName == 'A') {
            obj.style.color = clrOrg;
            clearInterval(TimerID);
            act = 0;
            cstep = 0;
        }
    }
}


//=============================================================================
// Change Color
//  This function changes a color actually.
//=============================================================================
function ChangeColor()
{
    obj.style.color = makeColor();
}


//=============================================================================
// makeColor
//  This function makes colors.
//=============================================================================
function makeColor()
{
    var elmA = new Array(3);
    var elmB = new Array(3);
    var clrRGB, i;

    elmA[0] = parseInt(clrA.substring(1,3),16);
    elmA[1] = parseInt(clrA.substring(3,5),16);
    elmA[2] = parseInt(clrA.substring(5,7),16);

    elmB[0] = parseInt(clrB.substring(1,3),16);
    elmB[1] = parseInt(clrB.substring(3,5),16);
    elmB[2] = parseInt(clrB.substring(5,7),16);

    for (i=0; i<3; i++) {
        if (act < 0)
            elmC[i] = (cstep + 1) * (elmA[i] - elmB[i]) / step + elmB[i];
        else
            elmC[i] = (cstep + 1) * (elmB[i] - elmA[i]) / step + elmA[i];
        elmC[i] = Math.floor(elmC[i]);
        if (elmC[i] < 0)    elmC[i] = 0;
        if (elmC[i] > 255)  elmC[i] = 255;
        elmC[i] = elmC[i].toString(16);
        if (elmC[i].length == 1)    elmC[i] = "0" + elmC[i];
    }

    cstep++;
    if (cstep > step) {
        cstep = 0;
        act = act * -1;
    }

    return "#" + elmC[0] + elmC[1] + elmC[2];
}
