/*
 * jQuery Imprint Plugin
 * version: 0.20 (09-OCT-2009)
 * @requires jQuery
 *
 * Examples and documentation at: http://vitalik.me/jquery/imprint/
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
;(function($) {

	$.fn.imprint = function(text,userOptions,callback) {
		var options = {
			timeout: 100,
			pause: 500,
			cmdSymbol: "#",
			autoNewLine: true
		};
		if(typeof text == 'undefined') text='Demo text';
		if(typeof userOptions != 'undefined') $.extend(options, userOptions);

		options.destination=this;
		options.text=text;
		if(typeof callback != 'undefined') options.callback=callback;
		var parameters={ options: options, index: 0 };
		print_with_timeout(parameters);

		function print_with_timeout(parameters) {
			var sym=parameters.options.text.charAt(parameters.index);
			if(parameters.options.autoNewLine && sym=="\n") {
				sym='<br/>';
			}
			if(sym=='#' && parameters.index<parameters.options.text.length-1) { // command
				var cmd=parameters.options.text.charAt(parameters.index+1).toUpperCase();
				if(cmd=='P') { // pause
					parameters.index+=2;
					setTimeout(function(parameters){return function(){print_with_timeout(parameters)}}(parameters),parameters.options.pause);
					return;
				} else if(cmd=='C') {
					sym='';
					parameters.options.destination.fadeOut('slow', function () {
						parameters.options.destination.html("");
						parameters.options.destination.show();
						parameters.index+=2;
						print_with_timeout(parameters);
					});
					return;
				} else if(cmd=='B') {
					parameters.index++;
					sym='<br/>';
				} else if(cmd=='D') {
					parameters.index++;
					var text=new String(parameters.options.destination.html());
					if(text.length>0) text=text.substr(0,text.length-1);
					parameters.options.destination.html(text);
					sym='';
				}
			}
			
			//Added by REZA (this condition only) to blink:
			if(parameters.options.destination.text().length != parameters.options.text.length)
				sym += parameters.options.destination.text().charAt(parameters.options.destination.text().length-1)=='-'? '_' : '-';
			parameters.options.destination.text(parameters.options.destination.text().substring(0,parameters.options.destination.text().length-1)+sym);
			//parameters.options.destination.append(sym);
			
			if(parameters.index>=parameters.options.text.length-1) {
				if (typeof parameters.options.callback != 'undefined') parameters.options.callback();
			}
			else {
				parameters.index++;
				setTimeout(function(parameters){return function(){print_with_timeout(parameters)}}(parameters),parameters.options.timeout);
			}
		}

	}

})(jQuery);
