// script is was nice, but because of IE's "cool" JavaScript
// implementation finally looks like shit %(

typer = {
	targetElement		: false,

	messages : ["johno#blog", "johno#blok", "johno.blg", "johno#bog", "johno.blog"],

	deleteInterval		: 500,
	typeInterval		: 700,
	typeChaosInterval	: 100,	// typeInterval +- random(typeChaos)

	retypeInterval		: 10000,
	retypeProbability	: 40,	// probability that message will be retyped "now" (in percent)

	
	init : function(elm) {
		if (elm) { 
			this.targetElement = elm;
			this.stringChange = 0;
			this.targetString = elm.nodeValue;
			setInterval(typerRetypeMessage, this.retypeInterval);
		}
	},

	deleteCharacter : function() {
		var tmp = this.targetElement.nodeValue;
		if (this.stringChange < 0) {		
			// contract by 1 character	
			this.targetElement.nodeValue = tmp.substr(0, tmp.length - 1);
			this.stringChange++;

			setTimeout(typerDeleteCharacter, this.deleteInterval);
		} else if ((this.stringChange == 0) && (tmp != this.targetString)) {
			this.stringChange = this.targetString.length - tmp.length;
			setTimeout(typerAppendCharacter, this.typeInterval + Math.floor(Math.random() * 2 * this.typeChaosInterval) - this.typeChaosInterval);
		}
	},

	appendCharacter : function() {
		if (this.stringChange > 0) {
			var tmp = this.targetElement.nodeValue;
			// append 1 character from target string
			this.targetElement.nodeValue = tmp.concat(this.targetString.substr(tmp.length,1));

			this.stringChange--;
			setTimeout(typerAppendCharacter, this.typeInterval + Math.floor(Math.random() * 2 * this.typeChaosInterval) - this.typeChaosInterval);
		}
	},

 	retypeMessage : function() {
		var tmp = this.targetElement.nodeValue;
		
		if ((this.stringChange == 0) && equalStrings(tmp, this.targetString)) {
			if((Math.random() * 100) <= this.retypeProbability) {
				var i;
				do {
					i = Math.floor(Math.random() * (this.messages.length));
				} while (this.messages[i] == tmp);

				this.targetString = this.messages[i];
				i = 0;
			
				while ((i < tmp.length) && (this.targetString.substr(i,1) == tmp.substr(i,1))) {
					i++;
				}
				this.stringChange = i - tmp.length;
				setTimeout(typerDeleteCharacter, this.deleteInterval);
			}
		}
	}
}


// funcions called by timeout hold window object in this(variable) so
// i was forced to call these functions indirecly
function typerRetypeMessage() { typer.retypeMessage(); }
function typerAppendCharacter() { typer.appendCharacter(); }
function typerDeleteCharacter() { typer.deleteCharacter(); }


// silly IE just does it silly!
function equalStrings(s1, s2) {
	var cond = true;
	var i = 0;
	
	if (s1.length != s2.length) return false;

	
	while ((i < s1.length) && cond) {
		i++;

		// oh, yeah! no array-like strings in IE
		if (s1.substr(i,1) != s2.substr(i,1)) cond = false;
	}
	return cond;
}
