function classReader(opt)
{
var root = this;
this.req    = null;
this.reader = null;
this.opt    = {};
this.func   = {};

this.func.statusMsg = function(func, str, err)
	{
//	root.opt.log('classReader.func.'+func+' ');
	SCREEN.func.message(' classReader.func.'+func+' '+str, err==true ? 'error' : '');
	}

this.func.logMsg = function(func, str, err)
	{
	root.opt.log('classReader.func.'+func+' '+str);
	if (err==true)
		{
		root.func.statusMsg('cbReader', 'Error 404', true);
		}
//	SCREEN.func.message(' classReader.func.'+func+' '+str, err==true ? 'error' : '');
	}

this.func.decode = function(ArrayBuffer)
	{
	return new TextDecoder('windows-1250').decode(ArrayBuffer);
//	return new TextDecoder('iso-8859-2').decode(ArrayBuffer);
	}

this.func.cbReader = function(event)
	{
//	root.opt.log('classReader.func.cbReader: File reading end.');
	root.func.logMsg('cbReader', 'File reading end.');
	var str, error;
	str = isExist(event.target.result) ? event.target.result : (root.reader.result ? root.reader.result : '');
	error = str.substring(0,1000);
	if (error.indexOf('Error 404')>=0)
		{
		root.func.logMsg('cbReader', 'Error 404', true);
//		SCREEN.func.message('cbReader: ' + 'Error 404', 'error');
		return;
		}
	root.opt.cbEnd(str);
	}

this.func.cbRequest = function()
	{
	root.opt.log('classReader.func.cbRequest: Request reading...');
	var file = root.req.response;
	if (!file)
		{
		root.opt.log('classReader.func.cbRequest: Chyba, neni soubor.');
		return;
		}
	root.reader = new FileReader();
	root.reader.onload = root.opt.cbReader;
	root.reader.readAsText(file, 'windows-1250');
	}

this.func.cbRequestError = function ()
	{
/*
	SCREEN.func.message('cbRequestError: '+
		'status = ' + root.req.status + ' ' + status_text +
		'readyState = ' + root.req.readyState
		,'error'
		);
*/
	var status_text = '';
	switch (root.req.status)
		{
		case 0:  status_text = 'Soubor nelze otevrit. "' + root.req.error_path + '"'; break;	// UNSENT
		case 1:  status_text = 'OPENED'; break;
		case 2:  status_text = 'HEADERS_RECEIVED'; break;
		case 3:  status_text = 'LOADING'; break;
		case 4:  status_text = 'DONE'; break;
		default: status_text = 'unknown'; break;
		}
	SCREEN.func.message('cbRequestError: status = ' + status_text, 'error');
	}

this.func.fileFromUrl = function(path)
	{
	root.func.logMsg('fileFromUrl', 'Reading start...');
	root.func.logMsg('fileFromUrl', '"'+path+'"');
//	opt.log('classReader.func.fileFromUrl: File reading start...');
	root.req = new XMLHttpRequest();
	root.req.responseType = 'blob';	// pro FileReader
	root.req.onload  = root.opt.cbRequest;
	root.req.onerror = root.opt.cbRequestError;
	root.req.error_path = path;
	root.req.open('GET', path, true);
	root.req.send();
	}

this.func.fileFromInput = function(elInput)
	{
	root.opt.log('classReader.func.fileFromInput: File reading...');
	var file = elInput.files[0];
	if (!file)
		{
		root.opt.log('classReader.func.fileFromInput: Chyba, neni soubor.');
		return;
		}
	root.reader = new FileReader();
	root.reader.onload = root.opt.cbReader;
	root.reader.readAsText(file,'windows-1250');
	}

this.func.cbDestroyClickedElement = function (event)
	{
	var event = event || window.event;
	document.body.removeChild(event.target);
	}

this.func.saveTextAsFile = function (file_name, textToWrite)
	{
	var textFileAsBlob, browser_name, el;
//	var BOM = "\uFEFF"; 
//	textToWrite    = BOM + textToWrite;
//	textFileAsBlob = new Blob([textToWrite], {type:'text/plain;charset=utf-8;'});	// ulozi jako utf8 bez BOM, ten se musi pridat
//	textFileAsBlob = new Blob([textToWrite], {type:'text/plain;windows-1250;'});
//	textFileAsBlob = new Blob([textToWrite], {type:'text/plain;'});
	textFileAsBlob = new Blob([textToWrite], {type:'text/plain;windows-1250;'});
	browser_name   = navigator.appName;
	if (browser_name=='Microsoft Internet Explorer')
		{
		window.navigator.msSaveBlob(textFileAsBlob, file_name);
		return;
		}
	el = document.createElement('a');
	el.download  = file_name;
	el.innerHTML = 'Download File';
	if (window.webkitURL != null)	// chrome
		{
		el.href = window.webkitURL.createObjectURL(textFileAsBlob);
		}
	else	{	// Firefox
		el.href    = window.URL.createObjectURL(textFileAsBlob);
		el.onclick = root.func.cbDestroyClickedElement;
		el.style.display = 'none';
		document.body.appendChild(el);
		}
	el.click();
	}

this.func.void = function()
	{
	}

this.func.init = function(opt)
	{
	var bool;
	bool = isObject(opt);
	root.opt.log       = bool && isFunction(opt.log)       ? opt.log       : root.func.void;
	root.opt.cbRequest = bool && isFunction(opt.cbRequest) ? opt.cbRequest : root.func.cbRequest;
	root.opt.cbRequestError = bool && isFunction(opt.cbRequestError) ? opt.cbRequestError : root.func.cbRequestError;
	root.opt.cbReader  = bool && isFunction(opt.cbReader)  ? opt.cbReader  : root.func.cbReader;
	root.opt.cbEnd     = bool && isFunction(opt.cbEnd)     ? opt.cbEnd     : root.func.void;
	}

this.func.init(opt);

}

var READER;
if (typeof SCREEN!='undefined')
	{
	READER = new classReader({log: SCREEN.log, cbEnd:SCREEN.log});
	}
READER = new classReader();

