/* Events */
window.onresize = function() /*apply correct measures on window resize*/
{
	if (document.body.offsetWidth != current_width) /* only for horizontal resize */
	{
		fixwidth();
	}
}
window.onload = function() /*apply correct measures on page reloads*/
{
	fixwidth();
	onclickElementsByClassName(document, "*", "print_page");
}

/* Advertisment placement */
var current_width = 0;

function fixwidth()
{
	/* window is too small to fit main content + advert (700 + 20 + 160) + 10 reserve :) */ 
	/* or if advert is not present */

	/*defaults*/
	var header_margin = "0 auto 0 auto";
	var content_margin = "0 auto 0 auto";
	var footer_margin = "0 auto 0 auto";
	var ads_side_display = "block";
	var ads_side_left = "50%";
	var ads_side_margin = "0 0 0 370px";
	
	if ((document.body.offsetWidth < 900 ) || !(document.getElementById('beyondpage'))) /* too small for ads */
	{
		header_margin = "0 auto 0 auto"; /* center the content */
		content_margin = "0 auto 0 auto";
		footer_margin = "0 auto 0 auto";
		ads_side_display = "none"; /* do not draw the whole hideable thing */
	}
	else
	{
		if ((document.body.offsetWidth >= 880) && (document.body.offsetWidth < 1060 )) /* browser window is too small to fit the center aligned main contend and the advert on its side (so that only main content is centered) */
		{
			/* position main content and reklame on center as if they were one object (this way its easier to fit it on a page) */

			ads_side_display = "block"; 
			ads_side_left = "0"; /* clear previous css */
			ads_side_margin = "0 0 0 " + (((document.body.offsetWidth - 880)/2) + 720) + "px";

			header_margin = "0 0 0 " + ((document.body.offsetWidth - 880)/2) + "px";
			content_margin = "0 0 0 " + ((document.body.offsetWidth - 880)/2) + "px";
			footer_margin = "0 0 0 " + ((document.body.offsetWidth - 880)/2) + "px";
		}
		else
		{
			/* align main content on the center and place reklame on the side, nice there is enough space not to center the reklame */
			header_margin = "0 auto 0 auto"; /* center the main content */
			content_margin = "0 auto 0 auto";
			footer_margin = "0 auto 0 auto";
			ads_side_left = "50%"; /* restore default css */
			ads_side_margin = "0 0 0 370px";
		}
	}
		/* set new values */
		document.getElementById('header').style.margin = header_margin;
		document.getElementById('content').style.margin = content_margin;
		document.getElementById('footer').style.margin = footer_margin;
		if (document.getElementById('ads_side')) /* set only if exists */
		{
			document.getElementById('ads_side').style.display = ads_side_display;
			document.getElementById('ads_side').style.left = ads_side_left;
			document.getElementById('ads_side').style.margin = ads_side_margin;
		}
		
		current_width = document.body.offsetWidth;
}

/* print page*/
function onclickElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
			oElement.onclick = function() { window.print(); return false;  }
		}
	}
}
