var $ = document.querySelector.bind(document);
var $$ = document.querySelectorAll.bind(document);

function elemGet(id, line_of_code) {/*console.log(id);*/ var el=$('#'+id); if (!isExist(el)) {console.log('elemGet: Not exist id #'+id+', line '+line_of_code+'.');}; return el;}

const isExist    = (ref) => typeof ref!='undefined' && ref!=null;
const isString   = (ref) => typeof ref=='string' && ref!=null;
const isArray    = (ref) => typeof ref=='object' && ref!=null;
const isObject   = (ref) => typeof ref=='object' && ref!=null;
const isFunction = (ref) => typeof ref=='function' && ref!=null;
//function isSet(arr,key) {return typeof(arr[key])!='undefined'&& arr!=null;}

function escapeHtml(str) {var map = {"&": "&amp;", "<": "&lt;", ">": "&gt;", "\"": "&quot;", "'":  "&apos;"}; return (''+str).replace(/[&<>"']/g, function(m) { return map[m]; });}
function escapeRegexp(str) {var spc = ('(['+'\\^$*+?.()|{}[]'.split('').join('\\'))+'])', reg = new RegExp(spc,'g'); return str.replace(reg,'\\$1');}
function escapeUrl(str) {var fn = encodeURIComponent || encodeURI || escape; return fn(str);} //encodeURIComponent(value).replace(/!/g,'%21').replace(/'/g,'%27').replace(/\(/g,'%28').replace(/\)/g,'%29').replace(/\*/g,'%2A').replace(/%20/g,'+');
function escapeTrim(str) {return str.replace(/(^\s+)|(\s+$)/g,'');}
function escapeNl2br(str) {return str.replace(/\n/g,'<br>');}

htmlClassFind = function (elem, list)
	{
	if (!isExist(elem))
		{
		return false;
		}
	var i, i_end, class_string, patt, reg, spc, name, bool;
	class_string = elem.className;
	spc   = '[^-A-Za-z0-9]+'; //space
	list  = isString(list) ? [list] : (isArray(list) ? list : []);
	i_end = list.length;
	bool  = false;
	for (i=0; i<i_end; i++)
		{
		if (list[i]=='')
			{continue;}
		name = escapeRegexp(list[i]);
		patt = spc+name+spc +'|'+ spc+name+'$' +'|'+ '^'+name+spc +'|'+ '^'+name+'$';
		reg  = new RegExp(patt, 'gm');
		bool = bool || Boolean(class_string.match(reg));
		}
	return bool;
	}

htmlClassRemove = function (elem, list)
	{
	if (!isExist(elem))
		{
		return false;
		}
	var i, i_end, class_string, patt, reg, spc, name;
	class_string = elem.className;
	spc   = '[^-A-Za-z0-9]+'; //space
	list  = isString(list) ? [list] : (isArray(list) ? list : []);
	i_end = list.length;
	for (i=0; i<i_end; i++)
		{
		if (list[i]=='')
			{continue;}
		name = escapeRegexp(list[i]);
		patt = spc+name+'('+spc+')' +'|'+ spc+name+'()$' +'|'+ '^()'+name+spc +'|'+ '^()'+name+'$';
		reg  = new RegExp(patt, 'gm');
		class_string = class_string.replace(reg, '$1');
		}
	elem.className = class_string;
	return class_string;
	}

htmlClassAdd = function (elem, list)
	{
	if (!isExist(elem))
		{
		return false;
		}
	var i, i_end, class_string;
	class_string = htmlClassRemove(elem, list);
	list  = isString(list) ? [list] : (isArray(list) ? list : []);
	i_end = list.length;
	for (i=0; i<i_end; i++)
		{
		if (list[i]=='')
			{continue;}
		class_string += (class_string!='' ? ' ' : '') + list[i]
		}
	elem.className = class_string;
	return class_string;
	}


function toggle2(btn_el, box_id, show)	// structure: <div> <span><span>btn</span>header-text</span> <div>box-text</div> </div>
	{
	var box_el, cl, list;
	box_el = elemGet(box_id, 190);
	if (!isExist(box_el))
		{
		return;
		}
	var show = isExist(show) ? show : htmlClassFind(box_el, 'show');
	show = !show;
	htmlClassRemove(box_el, ['hidden', 'show']);
	htmlClassAdd   (box_el, !show ? "hidden" : "show");
	if (!isExist(btn_el))
		{
		list   = box_el.parentNode.getElementsByTagName('span');
		list   = list[0] ? list[0].getElementsByTagName('span') : null;
		btn_el = list[0] ? list[0] : null;
		}
	if (!isExist(btn_el))
		{
		list   = box_el.parentNode.getElementsByTagName('div');
		list   = list[0] ? list[0].getElementsByTagName('button') : null;
		btn_el = list[0] ? list[0] : null;
		}
	if (!isExist(btn_el))
		{
		console.log('btn_el not exist (from='+box_id+'), line=336-sorting4-pref.js');
		return;
		}
	if (htmlClassFind(btn_el, ["icon-minus", "icon-plus"]))
		{
		htmlClassRemove(btn_el, ["icon-minus", "icon-plus"]);
		htmlClassAdd   (btn_el, show ? "icon-minus" : "icon-plus");
		}
	else	{
		if (htmlClassFind(btn_el, ["accordion-button"]))
			{
			htmlClassRemove(btn_el, ["collapsed", ""]);
			htmlClassAdd   (btn_el, show ? "" : "collapsed");
			}
		}
	}

function toggle2(btn_el, box_id, box_visibility)	// structure: <div> <span><span>btn</span>header-text</span> <div>box-text</div> </div>
	{
	var box_el, cl, list;
	box_el = elemGet(box_id, 190);
	if (!isExist(box_el))
		{
		return;
		}
	var box_visibility = isExist(box_visibility) ? box_visibility : !htmlClassFind(box_el, 'hidden'); //box_el.style.display!='none'
	if (!isExist(btn_el))
		{
		list   = box_el.parentNode.getElementsByTagName('span');
		list   = list[0] ? list[0].getElementsByTagName('span') : null;
		btn_el = list[0] ? list[0] : null;
		}
	if (!isExist(btn_el))
		{
		console.log('btn_el not exist (from='+box_id+'), line=197');
		return;
		}
	box_visibility = !box_visibility;
	htmlClassRemove(btn_el, ["icon-plus", "icon-minus"]);
	htmlClassAdd   (btn_el, box_visibility ? "icon-minus" : "icon-plus");
	htmlClassRemove(box_el, 'hidden');
	htmlClassAdd   (box_el, !box_visibility ? "hidden" : "");
	}
