/////////////////////////////////////////////
// An Anagram Animation Transformation Script
// By Ryan McGuire
// Feb 03 2004
//
// visit EnigmaCurry.com
//
// Anyone may use this script under the terms
// of the GNU General Public License version 2
// http://www.gnu.org/licenses/gpl.txt
//
// If you do find this script useful, I would
// love to hear about it. email me at: 
// ryan AT enigmacurry.com
//
////////////////////////////////////////////

/* Things to watch out for.
Make sure that the toText is actually an Anagram of fromText!!
Also make sure that the string length of each is the same.
You may need to add extra spaces onto the end of one of them 
to make them the same length.
*/

currentTransitionFound = 0;
currentTransition = 0;
transitions = new Array();
timers = new Array();
delay = 0;

//States:
//There are three different possible states.
//-1 : We are moving to the fromText currently
//0  : We are doing nothing.
//1  : We are moving to the toText currently
currentState = 0; 

function transformInit(fromText, toText, animdelay, forever, cycleseconds){

  //if forever is set to 1, then this will be an ongoing transformation.

   setText(fromText);
   delay = animdelay;

   //Find the transitions
   findTransitions(fromText, toText);

  if(forever == 1)
   {
	setTimeout("onGoingTransform(" + cycleseconds + ",1)",cycleseconds*1000);
   }  
}

function gotoToText(){
   //Don't perform this function if we are already going to toText
   if(currentState == 1)
     return;
   else
     currentState = 1;

   if(currentTransition < 0)
     currentTransition = 0;

   //Clear Existing Timers
   clearTimers();

   //Schedule transformations
   for(i=currentTransition, iter=0;i<transitions.length;i++,iter++){
      timers[i] = setTimeout("setText(transitions[currentTransition]); currentTransition++", delay*iter);
   }
}

function gotoFromText(){

   //Don't perform this function if we are already going to fromText
   if(currentState == -1)
     return;
   else
     currentState = -1;


   if(currentTransition > transitions.length-1)
      currentTransition = transitions.length-1
   //Clear Existing Timers
   clearTimers();
   //Schedule transformations
   for(i=currentTransition,iter=0;i>=0;i--,iter++){
      timers[i] = setTimeout("setText(transitions[currentTransition]); currentTransition--", delay*iter);
   }  
}

function clearTimers(){
   for(i=0;i<=timers.length;i++){
      clearTimeout(timers[i]);
   }
}

function setText(text){
    document.getElementById('anagram_container').innerHTML = text;
    //document.getElementById('invisibleSpan').innerHTML = text;

    //reset state if we are done.
    if(currentTransition == transitions.length-1 && currentState == 1)
       currentState = 0;
    else if(currentTransition == 0 && currentState == -1)
       currentState = 0;

}

function findTransitions(fromText, toText){
                                                                                         
    iterim = fromText;
    transitions[0] = fromText;                                                                   
    for( toTextPosition=0;toTextPosition<=toText.length;toTextPosition++ ){
       
       for(fromTextPosition=toTextPosition;fromTextPosition < toText.length;fromTextPosition++){
          if(iterim.charAt(fromTextPosition) == toText.charAt(toTextPosition))
             break;
       }
       //Found the fromTextPosition
       for( swapPosition = fromTextPosition;swapPosition>toTextPosition;swapPosition--){
          iterim = swapChars(iterim, swapPosition, swapPosition - 1);
	  currentTransitionFound++;
	  transitions[currentTransitionFound] = iterim;
       }
    }
}
       
function swapChars( theString, position1, position2 ){
    //convert theString to an array
    bytes = theString.split('');
    char1 = bytes[position1];
    char2 = bytes[position2];
    bytes[position1] = char2;
    bytes[position2] = char1;
    theString = bytes.join('');
    return theString;
}   

function onGoingTransform(seconds, toOrFrom){
   //Once this function is called, it will run recursively forever, continuously animating the text.

  milliseconds = seconds * 1000;  

  if(toOrFrom == 1){
    setTimeout("onGoingTransform(" + seconds + ",0);", milliseconds);
    gotoToText();
  } else {
    setTimeout("onGoingTransform(" + seconds + ",1);", milliseconds);
    gotoFromText();
  }
}



window.onload = function() {
    transformInit('ENIGMA CURRY','RYAN MCGUIRE',40,1,8);
}
