var img = document.getElementById('bgimg');

img.onload = function() {
	var ratio = document.body.offsetWidth / img.width;
	if (ratio < 1) {
		img.height = img.height * ratio;
		img.width = img.width * ratio;
	}
}

window.onload = window.onresize = function(){
	var copy = document.getElementById('main'); 
	var myWidth = 0, myHeight = 0;
  	var mainHeight = 0, mainWidth = 0;
  	
	if( typeof( window.innerWidth ) == 'number' ) {
    	//Non-IE
    	myWidth = window.innerWidth;
    	myHeight = window.innerHeight;
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    	//IE 4 compatible
    	myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
  	}
 
  
  	if(myHeight > img.height) {
  		mainHeight = img.height;
  	} else { 
  		mainHeight=myHeight;
	}
	
  	if(myWidth>img.width){ 
  		mainWidth=img.width; 
  	} else { 
  		mainWidth=myWidth; 
  	}
   
  	var offsetLeft = mainWidth/2 - copy.clientWidth/2;
  	var offsetTop = mainHeight/2 - copy.clientHeight/2;
  
  	if(offsetLeft <= 0){
  		copy.style.left = '10px';
  	}else {
  		copy.style.left = offsetLeft + 'px';
  	}
  	
  	if(offsetTop <= 0){
  		copy.style.top = '10px';
  	} else {
  		copy.style.top = offsetTop + 'px';
  	}
}