function ChkearthGenerator(console) {

	// generator settings
	this.persons = ["baby", "baby", "teenager", "teenager", "child", "child", "unborn baby", "man", "woman", "man", "woman", "man", "man", "woman", "man", "woman", "man", "woman", "man", "woman", "man", "woman", "man", "woman", "man", "woman", "man", "woman", "man", "woman", "man"];	
	this.counter = 0;
	this.currentTimeout = 700;
	this.newLineTimeout = 1000;
	
	// set-up line element to 0 and contener to Array
	this.console = console
	this.currentLineElement = 0;
	this.consoleLine = new Array();
}

ChkearthGenerator.prototype.start = function (){
	this.generateNewLine();
	doPrint(this);
}

ChkearthGenerator.prototype.generateNewLine = function (){
	randomnumber = Math.floor(Math.random()*1000001);
	person = this.persons[Math.floor(Math.random()*this.persons.length)];
	recoveringFrom = accidents[Math.floor(Math.random()*accidents.length)];
	this.consoleLine = ["_","CHKEARTH ","is"," recovering ", person, " No. " + randomnumber, " from ", recoveringFrom + " ", ".",""," done!"];		
}

function doPrint(generator) {
		
	// if element is first - blinking cursor
	if (generator.currentLineElement == 0){
		doBlink(generator);
	}else if (generator.currentLineElement == 8){
		doDotsProgress(generator);
	}else if(generator.currentLineElement == 9){
		doCounter(generator);
	}else{
		generator.console.print(generator.consoleLine[generator.currentLineElement]);
		generator.currentLineElement++;
		
		// if element is last
		if (generator.currentLineElement >= generator.consoleLine.length){
			generator.currentLineElement = 0;
			generator.console.println();
			generator.generateNewLine();
			generator.currentTimeout = generator.newLineTimeout;
		}else
			generator.currentTimeout = Math.floor(Math.random()*701);
			
		setTimeout (function(){doPrint(generator);},generator.currentTimeout);
	}
}	

function doBlink (generator){
	if (generator.counter < 6){
		if (generator.counter%2==0){
			generator.console.print("_");
		}else{
			generator.console.print("\u00A0");
		}
		generator.console.cursorPosition.column = generator.console.cursorPosition.column-1;				
		generator.counter++;
		setTimeout (function(){doBlink(generator);},250);
	}else{
		generator.currentLineElement++;
		generator.counter = 0;
		generator.console.print("_");				
		doPrint(generator);
	}	
}		
	
function doDotsProgress(generator) {
	if (generator.counter < 3){
		generator.console.print(generator.consoleLine[generator.currentLineElement]);
		generator.counter++;
		setTimeout (function(){doDotsProgress(generator);},150);
	}else{
		generator.currentLineElement++;
		generator.counter = 0;
		generator.console.print(" ");
		doPrint(generator);
	}	
}
	
function doCounter(generator){
	if (generator.counter < 100){
		if (generator.counter>0){
			generator.console.cursorPosition.column = generator.console.cursorPosition.column-generator.counter.toString().length-1; 
		}				
		generator.counter++;					
		generator.console.print(generator.counter+"%");
		setTimeout (function(){doCounter(generator);},30);
	}else{
		generator.currentLineElement++;
		generator.counter = 0
		doPrint(generator);
	}				
}
