var layerAnimDuration = 300;

/* Layer */

function openLayer(ID, infotext) {
	var width = getWidth();
	var height = getHeight();
	
	var pageHeight = parseInt(xDocSize().h);
	
	$("#" + ID).css("display","block");
	
	var layerHeight = parseInt($("#" + ID).height()) ? $("#" + ID).height() : 150;
	
	// Position layer
	$("#" + ID).css("top",height / 2 - parseInt(layerHeight) / 2 + getScrollY());
	
	$("#" + ID).css("left",width / 2 - parseInt($("#" + ID).css("width")) / 2 + getScrollX());
	
	$("#shader").css("width",getWidth());
	$("#shader").css("height", pageHeight);
	$("#shader").css("opacity",0);
	$("#shader").css("display","block");
	
	// Animation
	$("#shader").animate({opacity: 0.5},layerAnimDuration);
	
	document.getElementById("shader").onmouseup = function(e) {
		closeLayer(ID);
	};
	
	//Set infotext if info box
	if(infotext != 'undefined'){
		$("#finishedbox_infotext").html("<h2>"+infotext+"</h2>");
	}
}

function closeLayer(ID) {
	$("#" + ID).css("display","none");
	/*$("#shader").css("opacity",0);*/
	/*$("#shader").css("display","none");*/
	
	$("#shader").animate({opacity: 0},layerAnimDuration,null,onLayerClose);
	
	document.getElementById("shader").onmouseup = null;
}

function onLayerClose() {
	$("#shader").css("display","none");
}

/* Shader tools */

function xDocSize()
{
  var b=document.body, e=document.documentElement;
  var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
  if (e) {
    esw = e.scrollWidth;
    eow = e.offsetWidth;
    esh = e.scrollHeight;
    eoh = e.offsetHeight;
  }
  if (b) {
    bsw = b.scrollWidth;
    bow = b.offsetWidth;
    bsh = b.scrollHeight;
    boh = b.offsetHeight;
  }
//  alert('compatMode: ' + document.compatMode + '\n\ndocumentElement.scrollHeight: ' + esh + '\ndocumentElement.offsetHeight: ' + eoh + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh + '\n\ndocumentElement.scrollWidth: ' + esw + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: ' + bsw + '\nbody.offsetWidth: ' + bow);
  return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
}

function getScrollX() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function getScrollY() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function getWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function getHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}


function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

