var facets = {
	'classes': {
		'title': 'facet-title',
		'list': 'facet-list'
	},
	'prefix': {
		'container': 'facet-container-',
		'list': 'facet-list-',
		'title': 'facet-title-'
	},
	'isIE': (navigator.userAgent.indexOf('MSIE') > -1 &&
		navigator.userAgent.indexOf('Opera') == -1)
};

String.prototype.normalizeSpace = function() {
	return this.replace(/\s+/, ' ');
}

String.prototype.trim = function() {
	return this.replace(/^\s+/, '').replace(/\s+$/, '');
}

function dbg(s) {
	var d = document.getElementById('debug');
	d.value = d.value + s + "\r\n";
}

function getElementsInClass(el, cn) {
	var nl = [];
	switch (el.nodeType) {
	/* 1 = ELEMENT_NODE, 9 = DOCUMENT_NODE, 11 = DOCUMENT_FRAGMENT_NODE */
	case 1: case 9: case 11:
		if (inClass(el, cn)) nl.push(el);
		if (el.hasChildNodes())
			for (var i = 0; i < el.childNodes.length; i++)
				nl = nl.concat(getElementsInClass(el.childNodes.item(i), cn));
		break;
	default:
		break;
	}
	return nl;
}

function getX(e) {
	var x = 0;
	if (e.offsetParent)
		while (e.offsetParent) {
			x += e.offsetLeft;
			e = e.offsetParent;
		}
	else if (e.x)
		x += e.x;
	return x;
}

function getY(e) {
	var y = 0;
	if (e.offsetParent)
		while (e.offsetParent) {
			y += e.offsetTop;
			e = e.offsetParent;
		}
	else if (e.y)
		y += e.y;
	return y;
}

function hide(en) {
	var e = document.getElementById(facets.prefix.list + en);
	var c = document.getElementById(facets.prefix.title + en);
	e.style.display = 'none';
	c.className = c.className.replace(/ is-active/, '');
	stillIE();
}

function hideAll() {
	var af =
		getElementsInClass(document.getElementsByTagName('body').item(0),
			facets.classes.list);
	for (var i = 0; i < af.length; i++)
		hide(af[i].id.replace(/[^0-9]/g, ''));
}

function inClass(el, cn) {
	if (!el.className) return false;
	var cns = el.className.normalizeSpace().trim().split(' ');
	for (var i = 0; i < cns.length; i++)
		if (cns[i] == cn) return true;
	return false;
}

function show(en) {
	hideAll();
	var e = document.getElementById(facets.prefix.list + en);
	var c = document.getElementById(facets.prefix.title + en);
	var fu = e.getElementsByTagName('ul');

	if (fu.length > 1)
		if (e.className.indexOf('is-long') == -1)
			e.className += ' is-long';

	e.style.left = (getX(c) + 30) + 'px';
	e.style.top = (getY(c) + c.offsetHeight - 1) + 'px';
	c.className += ' is-active';
	e.style.display = 'block';
	if (e.offsetWidth + parseInt(e.style.left) > 760) {
		var n = getX(c) + c.offsetWidth;
		e.style.left = (n - e.offsetWidth - 30) + 'px';
	}
	forIE(e);
	setHeight(e);
}

function showHide(en) {
	var e = document.getElementById(facets.prefix.list + en);
	if (e.style.display == 'block') hide(en);
	else show(en);
	return;
}

function setHeight(e) {
	var b = document.getElementsByTagName('body').item(0);
	var nh = parseInt(e.offsetTop) + parseInt(e.offsetHeight);
	if (parseInt(b.offsetHeight) < nh)
		b.style.height = (nh + 'px');
}

/*
 * these functions work around a problem in IE, where SELECT
 * elements will bleed through any box that is positioned on top of
 * them.  z-index doesn't help.  it has something to do w/SELECTs
 * being implemented as separate, decorationless windows.
 */
function forIE(el) {
	if (!facets.isIE) return;
	var ifr = document.getElementById('__ifr');
	if (ifr == null)
		ifr = initIE();
	ifr.style.left = getX(el) + 'px';
	ifr.style.top = getY(el) + 'px';
	ifr.style.height = el.offsetHeight + 'px';
	ifr.style.width = el.offsetWidth + 'px';
	ifr.style.display = 'block';
}

function stillIE() {
	if (!facets.isIE) return;
	var ifr = document.getElementById('__ifr');
	if (ifr == null)
		ifr = initIE();
	ifr.style.display = 'none';
}

function initIE() {
	var ifr = document.createElement('iframe');
	ifr.id = '__ifr';
	ifr.style.backgroundColor = '#FFFFFF';
	ifr.style.position = 'absolute';
	ifr.style.zIndex = 8;
	ifr.style.display = 'none';
	ifr.style.borderWidth = '0px';
	document.getElementsByTagName('body').item(0).appendChild(ifr);
	return ifr;
}


