// This was PLAY :)
// Jackify
(function($){
	jQuery.fn.jackify = function(options) {
		var self = this;
		var letterIndex = 0;

		settings = jQuery.extend({
			speed: 500,		// ms per character
			typos: .05		// typos proficiency, [0 - 1] : [best - worst]
		}, options);

		var text = this.text();
	
		var newMethods = {
			addFragment: function() {
				self.append(self.getNextLetter());
				return (this);
			},
			getNextLetter: function() {
				if (letterIndex > text.length) { letterIndex = 0; }

				l = text.charAt(letterIndex++);
				r = Math.random();

				if (r < settings.typos && l != ' ') {
					l = String.fromCharCode(97 + Math.round(r * 25));
					if (Math.round(r * 1000) % 2 == 0) {
						letterIndex--;
					}
					if (Math.round(r * 1000) % 2 == 0) {
						l = '<br />';
					}
				}
				return l;
			},
			nextIteration: function() {
				variableSpeed = Math.round((Math.random() * (settings.speed / 2) + (settings.speed / 2))); // 20%
				if (variableSpeed % 2 == 0) { variableSpeed *= -1; }

				this.delay((settings.speed + variableSpeed), function() {
					self.addFragment().linkify([
						{rx: ' (work|play) ', url: 'http://$1.ultimate.in.rs/'},
					]);
					self.nextIteration();
				});
			}
		};
		jQuery.each(newMethods, function(i) {
			jQuery.fn[i] = this;
		});

		this.nextIteration();
		return (this);
	};
})(jQuery);
