﻿var sPreviousImage = "";
var sSection = "";

window.onresize = function() {
	setBackground();
};

function getPageSize() {
	var xScroll, yScroll;	
	if ( window.innerHeight && window.scrollMaxY ) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if ( document.body.scrollHeight > document.body.offsetHeight ) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if ( self.innerHeight ) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if ( document.documentElement && document.documentElement.clientHeight ) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if ( document.body ) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	var pageWidth, pageHeight;
	if( yScroll < windowHeight ) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	if( xScroll < windowWidth ) {	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return new Array( pageWidth, pageHeight, windowWidth, windowHeight );
}

function setBackground() {
	var aPageSize = getPageSize();
	var iPageWidth = aPageSize[ 0 ];
	var sImage = "_bg1920.png";

	if ( iPageWidth <= 800 )
		sImage = "_bg800.png";
	else if ( iPageWidth <= 1024 )
		sImage = "_bg1024.png";
	else if ( iPageWidth <= 1280 )
		sImage = "_bg1280.png";
	else if ( iPageWidth <= 1440 )
		sImage = "_bg1440.png";
	else if ( iPageWidth <= 1680 )
		sImage = "_bg1680.png";
	if ( sImage != sPreviousImage ) {
		document.body.style.backgroundImage = 'url( /images/background/' + sSection + sImage + ')';
	}
}