// --- functions ---

function isExist(item) {return typeof(item)!='undefined' && item!=null;}
function isSet(item,key) {return typeof(item[key])!='undefined' && item!=null;}
function isObject(item) {return typeof(item)=='object' && item!=null;}
function isArray(item) {return (typeof(item)=='array' || typeof(item)=='object') && item!=null;}
function isFunction(item) {return typeof(item)=='function' && item!=null;}

function escapeTrim(str)
{
return str.replace(/^[ \r\n\t]+|[ \r\n\t]+$/g,'');
}

function escapeHtml(str) {
    var map = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
        "\"": "&quot;",
        "'":  "&apos;"
    };
    return str.replace(/[&<>"']/g, function(m) { return map[m]; });
}

function escapeRegexp(str)
{
var reg, symbols;
spc = ('(['+'\\^$*+?.()|{}[]'.split('').join('\\'))+'])';
reg = new RegExp(spc,'g');
//alert(str)
return str.replace(reg,'\\$1');
}

function escapeUrl(str)
{
var func;
func = encodeURIComponent || encodeURI || escape;
return func(str);
//encodeURIComponent(value).replace(/!/g,'%21').replace(/'/g,'%27').replace(/\(/g,'%28').replace(/\)/g,'%29').replace(/\*/g,'%2A').replace(/%20/g,'+');
}

function unEscapeUrl(str)
{
var func;
func = decodeURIComponent || decodeURI || unescape;
return func(str);
}

function escapeTrim(str)
{
return str.replace(/(^\s*)|(\s*$)/g,'');
}


function $(id,win) {var win = win || window;
//if (!win.document.getElementById(id)) alert(id)
 return win.document.getElementById(id);}
function getSel(x)	{return x[x.selectedIndex].value;}

function toggle(obj,bool)
{
if (!obj) {return;}
var bool = typeof(bool)!='undefined' ? bool : obj.style.display=='none';
obj.style.display = bool==true ? '' : 'none';
return bool;
}

function render(tpl,data)	//tpl = "{telo}{radek}"; data = {telo:'body',radek:'tr'};
{
str = tpl;
if (isObject(data))
	{
	for (i in data)
		{
		reg = new RegExp(escapeRegexp('{'+i+'}'),'g');
		str = str.replace(reg,data[i]);
		}
	}
return str;
}

function renderHtml(tpl,data)	//tpl = "{telo}{radek}"; data = {telo:'body',radek:'tr'};
{
str = tpl;
if (isObject(data))
	{
	for (i in data)
		{
		reg = new RegExp(escapeRegexp('{'+i+'}'),'g');
		str = str.replace(reg,escapeHtml(data[i]));
		}
	}
return str;
}

function arrayClone1(arr,start,end)	//substring
{
if (!isArray(arr)) {alert('arrayClone1 typeof(arr)='+typeof(arr));}
var i, j, out;
var start = isExist(start) ? start : 0;
var end   = isExist(end)   ? end   : arr.length;
out = [];
i   = start;
j   = 0;
while (i<end)
	{
	out[j] = arr[i];
	i++;
	j++;
	}
return out;
}

function arrayClone1Clear(arr,def,start,end)	//substring
{
if (!isArray(arr)) {alert('arrayClone1Clear typeof(arr)='+typeof(arr));}
var i, j, out;
var start = isExist(start) ? start : 0;
var end   = isExist(end)   ? end   : arr.length;
var def   = isExist(def)   ? def   : '';
out = [];
i = start;
j = 0;
while (i<end)
	{
	out[j] = def;
	i++;
	j++;
	}
return out;
}

function arrayClone2(arr,start,end)
{
var i, j, k, out;
var start = isExist(start) ? start : 0;
var end   = isExist(end)   ? end   : arr.length;
out = [];
if (typeof(arr)=='undefined' || arr.length==0) {return out;}
i = start;
k = 0;
while (i<end)
	{
	out[k] = [];
	j = 0;
//if (typeof(arr[i])=='undefined')
//alert([i,arr[i],arr])
	j_end = arr[i].length;
	while (j<j_end)
		{
		out[k][j] = arr[i][j];
		j++;
		}
	i++;
	k++;
	}
return out;
}

function arrayUniq2(arr,start,end)
{
var i, j, k, out;
var start = isExist(start) ? start : 0;
var end   = isExist(end)   ? end   : arr.length;
out = [];
if (typeof(arr)=='undefined' || arr.length==0) {return out;}
i = start;
j_end = arr[0].length;
j = 0;
while (j<j_end)
	{
	out[j] = [];
	j++;
	}
while (i<end)
	{
	j = 0;
	while (j<j_end)
		{
		out[j][arr[i][j]] = true;
		j++;
		}
	i++;
	}
//alert(out.toSource())
return out;
}

//-------------

function arrayFindIndex(arr,val)
{
var i, i_end;
i     = 0;
i_end = arr.length;
while(i<i_end)
	{
	if (arr[i]==val)
		{
		return i;
		}
	i++;
	}
return false;
}


function newWindowPlainText(name, data)
{
var win, doc;
win = window.open('', name, '');//data:text/plain;
//win.location.href='#zasuvky_bn13_vyber.csv'; // pokus o zmenu title, url, neuspesny
doc = win.document;
doc.write('<plaintext>'+data); // FF v source-code tag ukazuje, ale pri ulozeni do souboru nee.
doc.close();
}

//var ee;
//ee = {};
//ee.obj_id = 'tbl_show';
//ee.obj = objGet(ee.obj_id);


//function show(pos,count)
//{
//ee.obj.innerHTML = rowsTable(rowsGet(db_map,pos,count),'zas');
//}



var compare_func = {
'asc':	{
	// compare string with number
	'str': function(a,b) {if (a.length<b.length||a<b) {return -1;}; if (a.length>b.length||a>b) {return 1;}; return 0;},
	// compare number
	'num': function(a,b) {return a*1-b*1;},
	// compare string
	'strnum': function(a,b) {return ((a.replace(/[^-.0-9]/g,''))*1-(b.replace(/[^-.0-9]/g,''))*1);}
	},
'desc':	{
	// compare string
	'str': function(b,a) {if (a.length<b.length||a<b) {return -1;}; if (a.length>b.length||a>b) {return 1;}; return 0;},
	// compare number
	'num': function(b,a) {return a*1-b*1;},
	// compare string with number
	'strnum': function(b,a) {return ((a.replace(/[^-.0-9]/g,''))*1-(b.replace(/[^-.0-9]/g,''))*1);}
	},
// compare random, >0.5 for more swaping
//'rnd': {'rnd': function(a,b) {return 0.66 - Math.random();}}
}
// compare date 2010-07-22 15:30
compare_func.asc.date  = compare_func.asc.strnum;
compare_func.desc.date = compare_func.desc.strnum;




function getColWidth(table) {
//var table = document.querySelector('table');
    var timer = PERF.getTime();

var tr, th_all;
    tr = table.querySelector('tr');

//    var colsInRow = firstRow.querySelectorAll('td');
    th_all = tr.querySelectorAll('th');
//alert(colsInRow)

    var tableProps = {
        'num_of_cols': 0,
        'cols_width': [],
        'cols_left': [],
        'exec_time' : 0
    };
    tableProps.num_of_cols = colsInRow.length;

    var i;
    for (i = 0; i < tableProps.num_of_cols; i++) {

        var col = colsInRow[i];
//        tableProps.cols_width.push(col.offsetWidth);
//        tableProps.cols_left.push(col.offsetLeft);
        tableProps.cols_width[i] = col.offsetWidth;
        tableProps.cols_left[i] = col.offsetLeft;
    }

//    tableProps.exec_time = +new Date() - timer;
    tableProps.exec_time = PERF.getTime();
    
    return tableProps;
}

//console.log(getColWidth(table));

// ---

function canvasTextWidth(text, font) {
    var canvas = canvasTextWidth.canvas || (canvasTextWidth.canvas = document.createElement("canvas"));
    var ctx    = canvas.getContext("2d");
    ctx.font   = font;
    var text_metrics = ctx.measureText(text);
    return text_metrics.width;
}

// ---

//CSV  = new classCsv();



function classScreen()
{
var root = this;
this.func = {};
//this.func = {};

this.log = function (str)
	{
	if (!$('log_container'))
		{alert('log_container err');}
//	$('log_container').innerHTML += str;	// asc
	$('log_container').innerHTML  = str + '<br>' + $('log_container').innerHTML; // desc
	}

this.func.log = function(str)
	{
	root.log(str);
	}

this.func.logClose = function()
	{
	if (!$('log'))
		{alert('log err');}
	$('log').style.display = 'none';
	}

this.func.table = function(str)
	{
	$('table').innerHTML = str;
	};

this.func.fixed1 = function(str)
	{
	$('fixed1').innerHTML = str;
	};

this.func.message = function (str, type)
	{
	if (!$('message_container'))
		{alert('message_container err');}
	var type = isExist(type) ? type.replace(/[^a-z0-9_]/g,'') : 'info';
	$('message_container').innerHTML += '<div class="'+type+'">' + str + '<\/div>';
	}

this.func.messageClear = function ()
	{
	if (!$('message_container'))
		{return;}
	$('message_container').innerHTML = '';
	}
}
var SCREEN = new classScreen();