// needs optimizing (save calculated colors, only calculate for direct text-parents)
// needs extension (opacity of images)
function FadingObject( obj, color, colorType )
{
  new CoreObject( obj );

  obj.steps = 10;
  obj.fadeDelay = 100;

  obj.bg = getRGB( color );

  obj.number = 0;
  obj.elements = null;
  obj.step = 0;
  obj.type = colorType;
  obj.typeName = 'fade_'+colorType;

  obj.initFade = _FadingObject_initFade;
  obj.resetFade = _FadingObject_resetFade;
  obj.fade = _FadingObject_fade;
  obj.fadeOut = _FadingObject_fadeOut;
  obj.fadeIn = _FadingObject_fadeIn;

  obj.isFading = false;

  obj.fadeOutComplete = null;
  obj.fadeInComplete = null;
}

function _FadingObject_initFade()
{ this.resetFade();
  this.elements = getAllChildNodes( this );
  this.elements.push( this );
  for( var sklave=0; sklave<this.elements.length; sklave++ )
  if( this.elements[sklave].style )
  {    fade = getRGB( getColor( this.elements[sklave], this.type) ); // BackgroundColor
       fade.rStep = ( this.bg.r - fade.r ) / this.steps;
       fade.gStep = ( this.bg.g - fade.g ) / this.steps;
       fade.bStep = ( this.bg.b - fade.b ) / this.steps;
       fade.rCurr = fade.r;
       fade.gCurr = fade.g;
       fade.bCurr = fade.b;
       fade.step = 0;
       this.elements[sklave][this.typeName] = fade;
  }
}

function _FadingObject_resetFade()
{ if( this.elements )
      for( var sklave=0; sklave<this.elements.length; sklave++ )
      	if( this.elements[sklave].fade )
      	    this.elements[sklave].style[this.type]
}

function _FadingObject_fade( dir, check )
{ if( !check )
  { this.number++;
    check = this.number;
    this.isFading = dir;
  }else if( check!=this.number ) return;

  var fade;

  if( dir>0 && this.step<this.steps || dir<0 && this.step>0)
   {  for( var sklave=0; sklave<this.elements.length; sklave++ )
   	if( this.elements[sklave].style )
   	{ fade = this.elements[sklave][this.typeName];
   	  if( fade.step == this.step )
   	  {	fade.rCurr += fade.rStep*dir;
		fade.gCurr += fade.gStep*dir;
		fade.bCurr += fade.bStep*dir;
	  }else
	  {	fade.rCurr = fade.r - fade.rStep*(dir*this.steps);
		fade.gCurr = fade.g - fade.gStep*(dir*this.steps);
		fade.bCurr = fade.b - fade.bStep*(dir*this.steps);
		fade.step = this.step;
	  }

           fade.step += dir;

	  this.elements[sklave].style[this.type] = '#'+getFromRGB( Math.round(fade.rCurr),Math.round(fade.gCurr),Math.round(fade.bCurr) );
   	}
// getID(historyID).init();getID(historyID).fadeOut()
      this.step+=dir;
      this.timeOut(this.fadeDelay, 'fade('+dir+','+check+')');
   }else
   if( this.step==this.steps
       && this.fadeOutComplete )
   {	this.fadeOutComplete();
   	this.isFading = 0;
   }
   else if( this.step==0
       && this.fadeInComplete)
   {	this.fadeInComplete();
   	this.isFading = 0;
   }
}

function _FadingObject_fadeOut()
{ this.fade(1);
}


function _FadingObject_fadeIn()
{ this.fade(-1);
}

/* /FadingObject */


/**************************************************************** /
function _Fader_init( node )
{ //node.parentNode.removeChild( node );

  if( typeof this.ele == 'string' )
  	this.ele = getID( this.ele );

  if( this.color.substr(0,1)=='#' )
  	this.color = this.color.substr(1);

  if( this.color.length==3 )
	this.color = this.color.charAt(0)+this.color.charAt(0)+this.color.charAt(1)+this.color.charAt(1)+this.color.charAt(2)+this.color.charAt(2);

  if( this.color.length==6 )
  {	this.ele.style.color = '#'+this.color;

	  // colorCheck of 6 letters...removal of # and...and...and...

	  this.r = parseInt( this.color.substr(0,2), 16 );
	  this.g = parseInt( this.color.substr(2,2), 16 );
	  this.b = parseInt( this.color.substr(4,2), 16 );
  }else if( this.color.substr(0,3) == 'rgb' )
  {	this.color = this.color.substring(5, this.color.length-1);
  	this.color = this.color.split(', ');
  	if( this.color.length!=3 )  return;
  	this.r = this.color[0];
  	this.g = this.color[1];
  	this.b = this.color[2];
  	for( var sklave=0; sklave<3; sklave++ )
  	{	this.color[sklave] = Number( this.color[sklave] ).toString(16);
  		if( this.color[sklave].length==1 )
  			this.color[sklave] = "0" + this.color[sklave];
  	}
  	this.color = this.color.join();
  }

  if( !this.letters.length ) this.charsGet( this.ele );

  this.steps = this.letters.length;

  this.walkHead();
}
function _Fader_charsGet( parent )
{ var no = parent.firstChild;
  var tno;
  var chrs;
  var it;

  while( no )
    if( no.nodeType==3 ) //text-node
      { for( var sklave=0; sklave<no.nodeValue.length; sklave++ )
         { it = document.createElement('span');
           it.appendChild( document.createTextNode( no.nodeValue.charAt( sklave ) ) );
           no.parentNode.insertBefore( it, no );
           this.letters.push( it );
         }
        tno = no.nextSibling;
        no.parentNode.removeChild( no );
        no = tno;
      }else
      { if( no.firstChild )
            this.charsGet(no);
        no = no.nextSibling;
      }
}
function _Fader_setCharColor( pos )
{ var ppos = pos;

  ppos = this.step-this.letters.length;

  while( ppos<0 )
         ppos += this.letters.length;

  if( ppos > this.letters.length )
      ppos = ppos % this.letters.length;

  var perc = ( ppos>this.letters.length/2 ? this.letters.length-ppos : ppos )
               / (this.letters.length/2);

  var col = Math.round( 16 * perc );

  col = '#'+Math.round( this.r + (255-this.r)*perc ).toString(16)
            +Math.round( this.g + (255-this.g)*perc ).toString(16)
             +Math.round( this.b + (255-this.b)*perc ).toString(16);

  this.letters[ pos ].style.color = col;

//  this.letters[ pos ].innerHTML = ' ['+col + '] ';
}
function _Fader_walkHead()
{ var sklave = this.step-this.letters.length;
  if( sklave<0 ) sklave=0;
  for( ; sklave<this.step && sklave < this.letters.length; sklave++ )
    this.setCharColor( sklave );

  this.step++;

  if( this.step == this.steps )
    { this.step = 0;

      for( var sklave=0;sklave<this.letters.length;sklave++ )
           this.letters[sklave].style.color = '#'+this.color;

      // window.setTimeout( this.getInstanceName()+'.walkHead();', this.walkDelay );
    }else window.setTimeout( this.getInstanceName()+'.walkHead();', this.walkStepDelay );
}
function _Fader_reset( color )
{	for( var sklave=0;sklave<this.letters.length;sklave++ )
		this.letters[sklave].style.color = color;
}
function Fader(element,color)
{ new CoreObject( this );

  this.ele = element;
  this.text = 'initialze Headline first';
  this.color = color;

  this.r = 0;
  this.g = 0;
  this.b = 0;

  this.walkStepDelay = 10;
  this.walkDelay = 30000;

  this.step = 0;
  this.steps = 0;

  this.letters = new Array();

  this.create = _Fader_init;
  this.charsGet = _Fader_charsGet;
  this.walkHead = _Fader_walkHead;
  this.setCharColor = _Fader_setCharColor;
  this.reset = _Fader_reset;
}
/****************************************************************/
