var background_hq = null;
var background_hq_src = null;
var backgroundimg = null;
var backgroundimg_container = null;
var cur_width = 0;
var cur_height = 0;
var temp_dimension = 0;
var img_ratio = .75;
var img_created = false;

function updateBackground() {
	img_ratio = (background_hq.height / background_hq.width);

	if(img_created == false) {
		if(background_hq_src.length > 0) {
			window.onresize = scaleBackgroundImage;
		}

		backgroundimg.src = background_hq.src;

		backgroundimg.id = 'backgroundimg';
		backgroundimg.style.position = 'absolute';
		backgroundimg.zIndex = 1;
		backgroundimg_container.style.position = 'absolute';
		backgroundimg_container.style.overflow = 'hidden';
		backgroundimg_container.style.top = '0';
		backgroundimg_container.zIndex = 1;
		backgroundimg_container.appendChild(backgroundimg);
		document.body.insertBefore(backgroundimg_container, document.body.firstChild);
		document.body.style.backgroundImage = 'none';
		img_created = true;
	} else {
		var temp_background = document.getElementById('backgroundimg');
		temp_background.src = background_hq.src;
	}

	scaleBackgroundImage();
}

function insertAfter(parent_node, newNode, refNode) {
	if(refNode.nextSibling) {
		return parent_node.insertBefore(newNode, refNode.nextSibling);
	} else {
		return parent_node.appendChild(newNode);
	}
}

function swapBackgroundImage(in_url, url) {
	background_hq = new Image();
	background_hq.onload = updateBackground;
	background_hq.src = in_url;
}

window.onload = function() {
	background_hq_src = new String();
	backgroundimg_container = document.createElement('div');
	backgroundimg = document.createElement('img');	

	if(typeof getComputedStyle == 'function') {
		background_hq_src = getComputedStyle(document.body,'').getPropertyValue('background-image');
		background_hq_src = background_hq_src.substring(4,background_hq_src.length - 1);
	} else if(document.body.currentStyle) {
		background_hq_src = document.body.currentStyle.backgroundImage;
		background_hq_src = background_hq_src.substring(5,background_hq_src.length - 1);
	}
	
	swapBackgroundImage(background_hq_src, false)
};

function scaleBackgroundImage() {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		} else if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}

	backgroundimg_container.style.width  = winW + 'px';
	backgroundimg_container.style.height = winH + 'px';

	if((winW - (winH / img_ratio)) > (winH - (winW * img_ratio))) {
		backgroundimg.style.width  = (winW + 'px');
		backgroundimg.style.height = ((winW * img_ratio) + 'px');
		backgroundimg.style.left = "0px";
		backgroundimg.style.top  = ((winH - (winW * img_ratio)) / 2) + "px";
	} else {
		backgroundimg.style.height  = (winH + "px");
		backgroundimg.style.width  = ((winH / img_ratio) + "px");
		backgroundimg.style.top = "0px";
		backgroundimg.style.left = ((winW - (winH / img_ratio)) / 2) + "px";
	}
}

