var firstLoad = 1;

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) { // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight) { // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	//~ return [pageWidth,pageHeight,windowWidth,windowHeight];
	return {x1:pageWidth, y1:pageHeight, x2:windowWidth, y2:windowHeight};
}

/*
function screenSize() {
  var w, h; // Объявляем переменные, w - длина, h - высота
  w  = (window.innerWidth  ? window.innerWidth  : (document.documentElement.clientWidth  ? document.documentElement.clientWidth  : document.body.offsetWidth));
  h  = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
  
  if (1 == firstLoad && 'Microsoft Internet Explorer' == navigator.appName) {
    //~ w += 32;
    //~ h += 16;
    firstLoad = 0;
  }
  return {w:w, h:h};
}
*/

function getBrowser()
{
  var ua        = navigator.userAgent.toLowerCase();
  var isOpera   = (ua.indexOf('opera')  > -1);
  var isIE      = (!isOpera && ua.indexOf('msie') > -1);
  var isFirefox = (!isOpera && !isIE && ua.indexOf('mozilla') > -1);
  
  return {isOpera: isOpera, isIE: isIE, isFirefox: isFirefox};
}

$(document).ready(function() {
  function imageResize() {
    var pageSize = getPageSize();
    var browser  = getBrowser();
    var width    = (browser.isFirefox && (pageSize.y1 > pageSize.y2)) ? (pageSize.x1 - 16) : pageSize.x1;
    var height   = pageSize.y1;
    $('#background').attr('width',  width);
    $('#background').attr('height', height);
  }
  
  imageResize();  
  
  $(window).bind("resize", function(){
    imageResize();
  });
});
