var myrules = {
	/*
	 * Possibility to open and close elements
	 *
	 * Two things:
	 * - an opening or closing will open or close all sub elements
	 * - an opened element contained by a closed element will be closed if any
	 *	 sub element is also closed (due to technical limitations)
	 */
	'.eltSub' : function(elt) {
	}
};

//Behaviour.register(myrules);

/* Called for each element to set its knobs */
function setKnobs(elt) {
	// To not filter out the top element
	if (elt.parentNode.getAttribute('class') == 'top') return;

	var knobOpened = document.createElement("a");
	Element.addClassName(knobOpened, "knobOpened");
	knobOpened.href = "#";
	knobOpened.appendChild(document.createTextNode("V"));

	var knobClosed = document.createElement("a");
	Element.addClassName(knobClosed, "knobClosed");
	knobClosed.href = "#";
	knobClosed.appendChild(document.createTextNode(">"));

	elt.insertBefore(knobOpened, elt.firstChild);
	elt.insertBefore(knobClosed, elt.firstChild);

	knobOpened.onclick = function() { return closeElt(elt); }
	knobClosed.onclick = function() { return openElt(elt); }
}

/*
 * Called for each element as it is displayed, to set its status according to
 * the filter
 */
function setKnob(elt, result) {
	// To not filter out the top element
	if (elt.parentNode.getAttribute('class') == 'top') return;

	var f = document.filterForm.filter;
	var filter = parseInt(f.options[f.selectedIndex].value);

	if (filter == 'null' || result >= filter) {
		Element.removeClassName(elt, 'closed');
	} else if (result < filter) {
		Element.addClassName(elt, 'closed');
	}
}

function closeElt(elt) {
	Element.addClassName(elt, "closed");
	Element.removeClassName(elt, "opened");

	$A(document.getElementsByClassName("opened", elt)).each( function(e) {
		Element.removeClassName(e, "opened");
	});
	$A(document.getElementsByClassName("closed", elt)).each( function(e) {
		Element.removeClassName(e, "closed");
	});

	var openedParent = false;
	var current = elt;
	while (!openedParent && document != (current = current.parentNode)) {
		openedParent = Element.hasClassName(current, 'opened');
	}
	if (openedParent) {
		Element.removeClassName(current, "opened");
		Element.removeClassName(elt, "closed");
	}
	return false;
}

function openElt(elt) {
	var closedParent = false;
	var current = elt;
	while (!closedParent && document != (current = current.parentNode)) {
		closedParent = Element.hasClassName(current, 'closed');
	}
	if (closedParent) {
		Element.addClassName(elt, "opened");
	}

	Element.removeClassName(elt, "closed");
	$A(document.getElementsByClassName("opened", elt)).each( function(e) {
		Element.removeClassName(e, "opened");
	});
	$A(document.getElementsByClassName("closed", elt)).each( function(e) {
		Element.removeClassName(e, "closed");
	});
	return false;
}


function getCookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value) {
	var expires_date = new Date( new Date().getTime() + (3000 * 1000 * 60 * 60 * 24) );
	document.cookie = name + "=" +escape( value ) +
		(";expires=" + expires_date.toGMTString());
	/*+ 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );*/
}


/*
 * The parameter 'filter' is not the value itself, but an index!
 */
function setFilter(filter, elt) {
	setCookie('filter', document.filterForm.filter.selectedIndex);

	new Ajax.Updater('listByVote', '/elt/listByVote/'+elt, { asynchronous:true, evalScripts:true });
	new Ajax.Updater('listByDate', '/elt/listByDate/'+elt, { asynchronous:true, evalScripts:true });

	document.getElementsByClassName('result').each( function(result) {
		//alert(parseInt(result.innerHTML)+', '+filter);
		Element.removeClassName(result.parentNode.parentNode.parentNode, 'opened');
		if (filter == 'null' || parseInt(result.innerHTML) >= parseInt(filter)) {
			Element.removeClassName(result.parentNode.parentNode.parentNode, 'closed');
		} else if (parseInt(result.innerHTML) < parseInt(filter)) {
			Element.addClassName(result.parentNode.parentNode.parentNode, 'closed');
		}
	});
}

function setFilterFromCookie() {
	if (getCookie('filter') != null) {
		document.filterForm.filter.selectedIndex = getCookie('filter');
	}
}

function decrementFilter() {
	document.filterForm.filter.selectedIndex--;
	document.filterForm.filter.onchange();
	return false;
}

function incrementFilter() {
	document.filterForm.filter.selectedIndex++;
	document.filterForm.filter.onchange();
	return false;
}

/*
 * Set all choices to nothing.
 * Mostly used for login/logout
 */
function resetChoices() {
	$A(document.getElementsByClassName('selected', document.body)).each( function(choice) {
		Element.removeClassName(choice, 'selected');
	});
}

/*
 * Set all choices to user's values
 * Mostly used for login/logout
 */
function updateChoices(choices) {
	$A(document.getElementsByClassName('con', document.body)).each( function(choice) {
		if (choices[choice.parentNode.parentNode.parentNode.id] == "-1") {
			Element.addClassName(choice, 'selected');
		}
	});
	$A(document.getElementsByClassName('pro', document.body)).each( function(choice) {
		if (choices[choice.parentNode.parentNode.parentNode.id] == "1") {
			Element.addClassName(choice, 'selected');
		}
	});
}

function setSize(img) {
	var divHeight = findPosY(document.getElementsByClassName('choice', img.parentNode)[0]) - findPosY(img);

	if (false && img.offsetWidth > 1) {
		var debug = document.createElement("span");
		debug.appendChild(document.createTextNode(
					"divHeight: "+divHeight
					+"; img.offsetHeight:"+img.offsetHeight
					+"; img.offsetParent.offsetWidth: "+img.offsetParent.offsetWidth
					+"; img.offsetWidth:"+img.offsetWidth
					+"; findPosY(img):"+findPosY(img)
					+"; findPosY(document.getElementsByClassName('choice', img.parentNode)[0]):"
					+findPosY(document.getElementsByClassName('choice', img.parentNode)[0])));
		if (img.parentNode.parentNode) img.parentNode.parentNode.insertBefore(debug, img.parentNode);
	}

	if (divHeight > 0 && img.offsetHeight > divHeight) img.style.height = divHeight+"px";

	// For IE
	// Only touch the width, in order to keep the width/height ratio
	while (img.offsetHeight > 0 && img.offsetWidth > img.offsetParent.offsetWidth / 3) {
		img.style.height = (img.offsetHeight - 30) + "px";
	}
}

function findPosY(obj) {
	if (!obj) return 0;
	var curtop = 0;
	if (obj.offsetParent)
		while (1) {
			curtop += obj.offsetTop;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}