var lastOpen = 0;

function showLegend (where, what) {
	if (lastOpen != 0) {
		document.getElementById('legend_' + lastOpen).style['display'] = 'none';
		lastOpen = 0;
	}
	
	var legend = document.getElementById('legend_' + what);
	if (what == 'icons' || what == 'grades') {
	    var position = getPosition(where);
	    var mainPosition = getPosition(document.getElementById('main'));
	    var legendPosition = [(position[0] - mainPosition[0] + 15), (position[1] - mainPosition[1] + 15)];
	    
	    var p = getBrowserSize();
	    var p2 = getBrowserOffset();
		legend.style['display'] = 'block';
	    if (p[0] + p2[0] < position[0]+15+legend.offsetWidth) {
		    legendPosition[0] = (p[0] + p2[0])-legend.offsetWidth - mainPosition[0];
	    }
	    
	    if (p[1] + p2[1] < position[1]+15+legend.offsetHeight) {
		    legendPosition[1] = (p[1] + p2[1])-legend.offsetHeight - mainPosition[1];
	    }
	    
        legend.style['top'] = legendPosition[1] + 'px';
        legend.style['left'] = legendPosition[0] + 'px';
		lastOpen = what;
	}
}

function getPosition (el) {
	var pos = new Array();
	if (el.offsetParent) {
		for (pos[0] = 0, pos[1] = 0; el.offsetParent; el = el.offsetParent) {
			pos[0] += el.offsetLeft;
			pos[1] += el.offsetTop;
		}
		return [pos[0], pos[1]];
	} else {
		return [el.offsetLeft, el.offsetTop];
	}
}

function getBrowserSize() {
	var width = 0;
	var height = 0;
	if (typeof window.innerWidth == "number") {
		width = window.innerWidth;
		height = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	return [width, height];
}

function getBrowserOffset() {
	var width = 0;
	var height = 0;
	if (typeof window.pageYOffset == "number") {
		width = window.pageXOffset;
		height = window.pageYOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		width = document.body.scrollLeft;
		height = document.body.scrollTop;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		width = document.documentElement.scrollLeft;
		height = document.documentElement.scrollTop;
	}
	return [width, height];
}


function hideLegend (what) {
	var legend = what.parentNode;
	legend.style['display'] = 'none';
	lastOpen = 0;
}

function toggleFilter (filter) {
	var shown = document.getElementById(filter + '_rest').style['display']=='block';
	document.getElementById(filter + '_rest').style['display'] = (shown?'none':'block');
	document.getElementById(filter + '_morelink').style['display'] = (shown?'block':'none');
}

function offsetLeft2 (obj) {
	var curleft = obj.offsetLeft;
	while(obj.offsetParent) {
		curleft=curleft + (obj.offsetParent.offsetLeft);
		obj=obj.offsetParent;
	}
	return curleft;
}

function offsetTop2 (obj) {
	var curleft = obj.offsetTop;
	while(obj.offsetParent) {
		curleft=curleft + (obj.offsetParent.offsetTop);
		obj=obj.offsetParent;
	}
	return curleft;
}

function showPaperPopup(id) {
	jQuery(".paper-popup").css("display", "none");
	document.getElementById(id).style.display = "block";
}

function closePaperPopup(el) {
	el.parentNode.style.display = "none";
}

Event.observe(window, "load", function() {
	jQuery("a.new-window").click(function() {
		window.open(this.href);
		return false;
	});
});

