var background = null;
var backgroundWidth = 0;
var backgroundHeight = 0;

function setupBackground() {
	if ((jQuery.browser.msie && jQuery.browser.version == "6.0") || (jQuery.browser.mozilla && jQuery.browser.version.substr(0, 3) == "1.8"))
		$(document.body).css('background-image', 'url(/images/bakgrund_publiksajt.jpg)').css('background-attachment', 'fixed').css('background-repeat', 'no-repeat').css('background-position', 'top center');
	else
		background = $('<img />').attr('src', '/images/bakgrund_publiksajt.jpg').attr('alt', 'Bakgrundsbild').attr('id', 'background').load(backgroundLoaded);
}

function backgroundLoaded() {
	background.prependTo($(document.body));
	backgroundWidth = background.width();
	backgroundHeight = background.height();
	resizeBackground();
	$(window).bind('resize', resizeBackground);
}

function resizeBackground() {
	var w = $(window).width(), h = $(window).height();
	var wr = w / backgroundWidth, hr = h / backgroundHeight;
	
	if (wr > hr) {
		background.css('width', Math.round(w));
		background.css('height', Math.round(backgroundHeight * wr));
	}
	else {
		background.css('width', Math.round(backgroundWidth * hr));
		background.css('height', Math.round(h));
	}
	
	background.css('left', Math.round((w - parseInt(background.css('width'))) * 0.5));
	background.css('top', Math.round((h - parseInt(background.css('height'))) * 0.5));
}

$(document).ready(setupBackground);