/* Retrieves array of all features stored in `features' cookie,
 * where they are delimited by coma.
 */
function getFeatures ()
{
	var cks = document.cookie.split(";");
	for (i = 0; i < cks.length; i++) {
		ck = cks[i].split("=");
		if ("features" == ck[0])
			return ck[1].split(",");
	}
	return null;
}

/* Sets features list cookie.
 */
function setFeatures (ftrs)
{
	if ("string" == typeof(ftrs))
		ftrs = ftrs.split(",");
	document.cookie = "features=" + ftrs.join(",");
}

/* Works like document.getElementsByTagName() except it accepts more
 * tag names either as an array or coma-separated list and return type
 * is not a collection, but an array.
 */
document.getElementsByTagNames = function (names)
{
	var res = new Array();
	var q;

	if ("string" == typeof(names))
		names = names.split(",");

	for (i = 0; i < names.length; i++) {
		if (! (q = document.getElementsByTagName(names[i])))
			continue;
		for (j = 0; j < q.length; j++)
			res.push(q[j]);
	}

	return res;
}

/* Removes <p> and <pre> tags that does not have
 * feature-<one of ftrs> in their class list.
 */
function initFeatures ()
{
	var ps = document.getElementsByTagNames("p,pre");
	var ftrs = getFeatures();
	var ftred;

	if (getFeatures() != null) {
		for (i = 0; i < ps.length; i++ ) {
			if (! ps[i].className)
				continue;

			if (! ps[i].className.match(/(^| )feature-/))
				continue;

			ftred = false;
			for (j = 0; j < ftrs.length; j++)
				if (ps[i].className.match(
				    "(^| )feature-" + ftrs[j] + "( |$)"))
					ftred = true;

			if (! ftred)
				ps[i].style.display = "none";
		}
	}
}

initFeatures();

