var sFadeStartColor = "#666666";
var sFadeFinishColor = "#F47320";
var aRGBStart = sFadeStartColor.replace("#","").match(/.{2}/g); 
var aRGBFinish = sFadeFinishColor.replace("#","").match(/.{2}/g);

var T = 750;
var n = 50;
var t = T / n;

function fade(elem, m)
{
  m++;
  elem.style.color = getFadeMiddleColor(m);                
  if ( m < n ) setTimeout(function () { fade(elem, m);}, t);
}

function antiFade(elem, am)
{
  am--;
  elem.style.color = getFadeMiddleColor(am);
  if ( am > 0 ) setTimeout(function () { antiFade(elem, am);}, t);   
}

function getFadeMiddleColor(m) 
{
  var finishPercent = m/n;
  var startPercent = 1 - finishPercent;
  var R,G,B;

  R = Math.floor( ('0x'+aRGBStart[0]) * startPercent + ('0x'+aRGBFinish[0]) * finishPercent );
  G = Math.floor( ('0x'+aRGBStart[1]) * startPercent + ('0x'+aRGBFinish[1]) * finishPercent );
  B = Math.floor( ('0x'+aRGBStart[2]) * startPercent + ('0x'+aRGBFinish[2]) * finishPercent );
  
  return 'rgb('+R+ ',' + G + ',' + B +')'; 
}

function go(id, amount_elements, i)
{
    i = i % amount_elements + 1;  
    elem = document.getElementById(id+i); 
    fade(elem, 0);       

}

function goback(id, amount_elements, i)
{
    i = i % amount_elements + 1;
    elem = document.getElementById(id+i); 
    antiFade(elem, n);
               
}

function starteffect(id, amount_elements, i, j)
{

    setTimeout( function(){
            starteffect(id, amount_elements, ++i, ++j) }, T); 
    setTimeout( function(){               
        go(id, amount_elements, i)}, T); 
    setTimeout( function(){
            goback(id, amount_elements, j)}, T*2.5);        
}

function checkpresence(id, amount_elements)
{
    for(i=1; i<=amount_elements; i++)
    {
        elem = document.getElementById(id+i); 
        if (elem == null) return 0;
    }
    return 1;        
}

function fireeffect(id, amount_elements)
{
   if (checkpresence(id, amount_elements) == 1 )
    {    
        
        starteffect(id, amount_elements, 1, 1); 
    }             
}
        