var boxPrefix = 'kvadratum_';
/* Return left and top values for centering the box with width boxWidth and height boxHeight. */
function centerBoxPosition(boxWidth, boxHeight) {
	var myWidth;
	var myHeight;

	if( typeof( window.innerWidth ) == 'number' ) { 
		//Non-IE 
		myWidth = window.innerWidth;
		myHeight = window.innerHeight; 
	} 
	else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { 
		//IE 6+ in 'standards compliant mode' 
		myWidth = document.documentElement.clientWidth; 
		myHeight = document.documentElement.clientHeight; 
	}
	//alert(boxWidth + " x " + boxHeight)
	var leftVal = (myWidth - boxWidth) / 2;
	var topVal = (myHeight - boxHeight) / 2;
	//alert(myWidth + "->" + leftVal + " x " + myHeight + "->" + topVal);
	
	var arr = new Array(topVal, leftVal);
	return arr;
}

function centerBox(theBox) {
	var aBox = document.getElementById(theBox);
	
	var boxWidth = aBox.offsetWidth;
	var boxHeight = aBox.offsetHeight;
	//alert(boxWidth + ' x ' + boxHeight)
	/*boxWidth = boxWidth.substr(0, (boxWidth.length-2));
	boxHeight = boxHeight.substr(0, (boxHeight.length-2));*/
	
	var dim = centerBoxPosition(boxWidth, boxHeight);
	
	aBox.style.top = dim[0] + "px";
	aBox.style.left = dim[1] + "px";
}	

function closeBox() {
	var container = document.getElementById(boxPrefix + 'container');
	var overlay = document.getElementById(boxPrefix + 'overlay');
	document.body.removeChild(container);
	document.body.removeChild(overlay);
}

function openBox(url, boxHeader, boxWidth, boxHeight){
	var box = document.createElement('div');
	box.id = boxPrefix + 'container';
	var headerDiv = document.createElement('div');
	headerDiv.id = boxPrefix + 'header';
	
	var closeLink = document.createElement('a');
	closeLink.id = boxPrefix + 'close';
	closeLink.setAttribute('onclick', 'closeBox()')
	closeLink.setAttribute('href', "javascript: void('Close')")
	closeLink.innerHTML = 'Stäng';
	
	box.style.position = 'fixed';
	//box.style.backgroundColor = '#0F0';
	headerDiv.innerHTML = boxHeader;
	headerDiv.appendChild(closeLink);
	
	var iframe = document.createElement("iframe");

	iframe.id = boxPrefix + "iframe";
	iframe.name = boxPrefix + "iframe";
	iframe.frameborder = 0;
	//iframe.style.top = '100px';
	//iframe.style.left = '100px';
	iframe.height = boxHeight;
	iframe.width = boxWidth;
	//iframe.style.position = "absolute";
	iframe.style.display = "block";
	iframe.style.margin = 0;
	iframe.style.border = "0px";
	iframe.scrolling = "auto";
	iframe.src = url;

	box.appendChild(headerDiv);
	box.appendChild(iframe);
	document.body.appendChild(box);
	
	
	var boxOverlay = document.createElement("div");
	boxOverlay.id = boxPrefix + 'overlay';
	//boxOverlay.style.backgroundImage = '/image/transparentOverlay.png';
	boxOverlay.style.display = "block";
	box.style.display = 'block';
	centerBox(box.id);
	document.body.appendChild(boxOverlay);

}
