
var word, cursorChar, letter, wordIndex, anima;

function AnimateText(text) {	  
	// Original:  dzzie@yahoo.com
	// The JavaScript Source!! http://javascript.internet.com 
	anima = getElem("anima");
	cursorChar = "|";
	word = text.split('.')
	run_It();
	setInterval('run_It()', 20000);
}

function run_It() {
	letter = -1;           //letter count
	wordIndex = 0;            //which word in array is to be spelled
	start = 0;      //variable to hold cycles of the blink
	scount = 0;  //variable to hold cycles of the mispell
	anima.innerHTML = "";
    call_It();
}

function checkIt() {
		var temp = anima.innerHTML;
		if(temp.charAt(temp.length-1) == cursorChar) {
			anima.innerHTML = temp.substring(0, temp.length - 1);
			return true;
		} else {
			anima.innerHTML += cursorChar;
			return false;
     	}
}

function blinkfor(count, timing) {
		if(start < count) {
			if(checkIt()) {
				start++;
			}
			setTimeout('blinkfor('+count+','+timing+')', timing);
		} else {
			start = 0;
			letter++;
			call_It();
     	}   	
}

function mispell(offset,correction) {
	var temp = anima.innerHTML;
		if(scount < offset) {
			if(!checkIt()) {
				anima.innerHTML = temp.substring(0, temp.length-1) + cursorChar;
				scount++;
			}
			setTimeout('mispell(' + offset + ', "' + correction + '")', 150);
		} else {
			if(correction == '' && scount == offset ) {
				letter -= offset;
				scount++;
			}
		if(offset > 0) {
			if(checkIt()) {
				anima.innerHTML += correction.charAt(correction.length - offset) + cursorChar;
				offset--;
			}
			setTimeout('mispell(' + offset + ',"' + correction + '")', 150);
		} else {
			scount = 0;
			wordIndex++;
			anima.innerHTML = temp.substring(0, temp.length - 1);
			call_It();
         }
      }
}

function spellit(string) {
		if(letter <= string.length) {
			if(string.charAt(letter) == '[') {
				NumErrs = string.charAt(letter + 1);
				NewLets = string.substring(letter + 2, letter + NumErrs);
				mispell(NumErrs,NewLets);
			}
			else if(letter <= string.length - 1) {
				letter++;
				anima.innerHTML = string.substring(0,letter) + cursorChar;
				setTimeout('spellit("' + string + '")', 150);
			}
			else {
				anima.innerHTML = string.substring(0, letter);
				wordIndex++;
				call_It();
         	}
      	}
}

function call_It() {
		if(letter == -1){
			blinkfor(4, 150);
		}
		else if(wordIndex <= word.length - 1) {
			spellit(anima.innerHTML + " " + word[wordIndex]);
		} 
}