//addLoadEvent funct by Simon Willison, http://simonwillison.net/ 
function addLoadEvent(func) { 
	var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
	  window.onload = func; 
	} 
	else { 
		window.onload = function() { 
			if (oldonload) { 
				oldonload(); 
			} 
			func(); 
		} 
	} 
} 
function addResizeEvent(func) { 
	var oldonresize = window.onresize; 
  if (typeof window.onresize != 'function') { 
	  window.onresize = func; 
	} 
	else { 
		window.onresize = function() { 
			if (oldonresize) { 
				oldonresize(); 
			} 
			func(); 
		} 
	} 
} 

/* Events */
addResizeEvent(function() { /*apply correct measures on window resize*/
	if (document.body.offsetWidth != current_width) /* only for horizontal resize */
	{
		fixwidth();
	}												
}); 

addLoadEvent(function(){ fixwidth(); }); // onload

/* 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 = "4em 0 0 0";
	var ads_side_display = "block";
	var ads_side_left = "50%";
	var ads_side_margin = "0 0 0 405px";
	//site specific
	var container_bg = "url(img/header_decor.png) center 60px no-repeat";
	
	if ((document.body.offsetWidth < 955 ) || !(document.getElementById('beyondpage'))) /* too small for ads */
	{
		header_margin = "0 auto 0 auto"; /* center the content */
		content_margin = "0 auto 0 auto";
		footer_margin = "4em 0 0 0";
		ads_side_display = "none"; /* do not draw the whole hideable thing */
	}
	else
	{
		if ((document.body.offsetWidth >= 955) && (document.body.offsetWidth < 1130 )) /* 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 - 955)/2) + 790) + "px";

			header_margin = "0 0 0 " + ((document.body.offsetWidth - 955)/2) + "px";
			content_margin = "0 0 0 " + ((document.body.offsetWidth - 955)/2) + "px";
			footer_margin = "4em 0 0 0";
			//site specific
			container_bg = "url(img/header_decor.png) -160px 60px no-repeat";
		}
		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 = "4em 0 0 0";
			ads_side_left = "50%"; /* restore default css */
			ads_side_margin = "0 0 0 405px";
		}
	}
		/* set new values */
		document.getElementById('header').style.margin = header_margin;
		document.getElementById('content').style.margin = content_margin;
		document.getElementById('footer').style.margin = footer_margin;
		//site specific
		document.getElementById('container').style.background = container_bg;
		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;
}
