// --- table filters ---

function classArrayFilters(opt)
{
var root = this;
this.name = 'classArrayFilters';
this.func = {};
this.log = null;

/*
this.func.whereCmp = function(row,filter_all,col_by_id)
{
var bool_and, bool_or, col_id, i, i_end, j, j_end;
bool_and = true;
i_end    = col_by_id.length;
for (i=0; i<i_end; i++)
	{
	bool_or = true;
	col_id  = i;
	if (!isExist(filter_all[col_id]))
		{
		continue;
		}
	filter = filter_all[col_id];
	j_end  = filter.length;
	for (j=0; j<j_end; j++)	// projit vsechna slova ve filtru
		{
		if (!isExist(filter[j]))
			{
			continue;
			}
		if (filter[j]===row[col_id])
			{
			bool_or = true;
			break;
			}
		}
	bool_and &= bool_or;
	}
return bool_and;
}
*/

// add row_id to end
this.func.addRowId = function(arr)
	{
	if (!isArray(arr) || arr.length==0)
		{
		return false;
		}
	var i, i_end;
	i_end = arr.length;
	for (i=0; i<i_end; i++)
		{
		arr[i][arr[i].length] = i;
		}
	return arr;
	}

this.func.arrayFindValue = function(arr,value)
	{
	if (!isArray(arr) || arr.length==0)
		{
		return false;
		}
	var i, i_end;
	i_end = arr.length;
	for (i=0; i<i_end; i++)
		{
		if (!isExist(arr[i]))
			{
			continue;
			}
		if (arr[i]===value)
			{
			return true;
			}
		}
	return false;
	}

this.func.arrayBoolFindValue = function(arr, value, filter_checked_n)
	{
	if (!isExist(arr) || arr.length==0)
		{
		return false;
		}
	var i, i_end;
	i_end = arr.length;
	for (i=0; i<i_end; i++)
		{
		if (!isExist(arr[i]) || filter_checked_n[i]===false)
			{
			continue;
			}
		if (arr[i]===value)
			{
			return true;
			}
		}
//alert(['arrayBoolFindValue',arr,"\n",value,"\n",filter_readed])
	return false;
	}

this.func.where = function(arr,col_by_id,filter_all,filter_checked,start)
	{
//alert(filter_checked.toSource())
//alert(filter_all[0].toSource())
//alert(filter_checked[0].toSource())
	if (!isExist(col_by_id) || col_by_id.length==0 || !isExist(filter_all) || filter_all.length==0)
		{
alert('where: Err0')
		return arr;
		}
	if (!isExist(filter_checked))
		{
//alert('where: Err1')
		filter_checked = [];
//		return arr;
		}
	var i, i_end, j, j_end, bool_and, out, i_start;
	i_start = isExist(start) ? start * 1 : 0;
	i_start = i_start>=0     ? i_start   : 0;
//alert(i_start)
	out   = [];
	k     = 0;
	i_end = arr.length;
	for (i=i_start; i<i_end; i++)	// arr
		{
		j_end = arr[i].length;
		bool_and = true;
		for (j=0; j<j_end; j++)	// row
			{
			if (!isExist(filter_all[j]))	// filter words
				{
				continue;
				}
			if (!isExist(filter_checked[j]))
				{
				bool_and==false;
				continue;
				}
			if (!isArray(filter_checked[j].list))
				{
				filter_checked[j].list = [];
				}
			bool_and = root.func.arrayBoolFindValue(filter_all[j],arr[i][j],filter_checked[j].list);
			if (bool_and==false)
				{
//alert(['func.where','break'])
//alert(['func.where',j,"\n",filter_all[j],"\n",arr[i][j],"\n",filter_checked[j]])

break;}
			}
		if (bool_and==false)
			{
//alert(['func.where','false'])
			continue;}
		out[k] = arr[i];
		k++
		}
	return out;
	}


// sort; cislovani sloupcu je podle tableGroupBy
this.func.orderBy = function(arr,key_all,order_by)
	{
	root.func.log(root.name + '.func.orderBy (' + arr.length + ')');
//alert(order_by.toSource());
	var i, i_end, opt,j,a,b,c, ms, row;
	ms  = new MultiSortClass();
	opt = [];
	j   = 0;
	i_end = order_by.length;
	for (i=0;i<i_end;i++)
		{
		row = order_by[i];
		if (isExist(row.col_name) && isExist(key_all.out_key_by_name[row.col_name]))
			{
			a = key_all.out_key_by_name[row.col_name];
			b = isExist(row.cmp) ? row.cmp : '';	// MS si sam vyradi neexistujici sort funkci
			c = isExist(row.order) && row.order=='desc' ? 'desc' : 'asc';
			opt[j] = {col:a, cmp:b, order:c};
			j++;
			}
		}
//	opt = [
//		{col:5,cmp:'str',order:'asc'},	// ucitel jmeno
//		{col:9,cmp:'str',order:'asc'}	// predmet text
//	]
//alert(opt.toSource());
	root.order_by = opt;
	ms.setOpt(opt);
	arr.sort(ms.cmp);
	return arr;
	}

this.func.limit = function(arr, start, end)
	{
	root.func.log(root.name + '.func.limit (' + arr.length + ')');
	var start, end, end_max, start_min;
	start_min = 0;
	end_max   = arr.length - 1;
	var start = isExist(start) ? start * 1 : start_min;
	var end   = isExist(end)   ? end * 1   : end_max;
	start = start<start_min ? start_min : (start>end_max ? end_max : start);
	end   = end>end_max ? end_max : (end<start ? start : end);
	if (end-start == end_max)
		{
		return arrayClone2(arr);
		}
	end++;
	return arrayClone2(arr, start, end);
	}

this.func.init = function(opt)
	{
	if (root.func.log)
		{
		root.func.log(root.name + '.func.init');
		}
	var bool;
	bool = isObject(opt);
	root.func.log = bool && isFunction(opt.log) ? opt.log : null;
	}

this.func.init(opt);
}

var FILTER = new classArrayFilters({log:SCREEN.log});
// --- / table filters ---
