window.onresize = floating_window;
window.onscroll = floating_window;
window.onload = floating_window;

function floating_window(){
var div_id = 'floating_ad_right'; // id of your div
var pixels_from_center = 400; // distance from center of page in pixels (negative number to go left from center)
var min_from_top = 40; // div stops scrolling at XX distance from top
var max_from_top = 341; //div start scrolling at XX distance from top
var max_from_bottom = 440; // div stops scrolling at XX distance from bottom
var page_width = 980; // width in pixels when vertical scrollin appears when resizing window Body margin included
var Top_margin_of_body = 0; // top margin of body
var bottom_margin_of_body = 10; // bottom margin of body
var div_width = document.getElementById(div_id).offsetWidth;
var div_height = document.getElementById(div_id).offsetHeight;
var content_height = document.getElementById('centering_container').offsetHeight;
if(pixels_from_center < 0){
	var schift_horizontaly = pixels_from_center - div_width;
	}else{
	var schift_horizontaly = pixels_from_center;
	}
var largeur = 0, hauteur = 0, position_top = 0, position_bottom = 0;
if( typeof( window.innerWidth ) == 'number' ) {
  largeur = window.innerWidth;
  hauteur = window.innerHeight;
  position_top = window.pageYOffset;
  position_bottom = content_height - position_top - hauteur;
  } 
else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )){
  largeur = document.documentElement.clientWidth;
  hauteur = document.documentElement.clientHeight;
  position_top = document.documentElement.scrollTop;
  position_bottom = content_height - position_top - hauteur;
  } 
else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  largeur = document.body.clientWidth;
  hauteur = document.body.clientHeight;
  position_top = document.body.scrollTop;
  position_bottom = content_height - position_top - hauteur;
  }
else {
  largeur = -1;
  hauteur = -1;
  position_top = -1;
  position_bottom = -1;
  }
var changing_position_top = max_from_top - min_from_top + Top_margin_of_body; //if(position_top > changing_position_top)
var changing_position_bottom = (max_from_bottom - position_bottom + Top_margin_of_body) - (hauteur - (div_height + min_from_top)); // if(changing_position_bottom > 0)


  if((largeur < (page_width + 30))||(hauteur < (div_height + Top_margin_of_body +min_from_top + 10))){
		  document.getElementById(div_id).style.position = "absolute";
		  document.getElementById(div_id).style.top = max_from_top - Top_margin_of_body +"px";
  }else{
	  if(position_top > changing_position_top){
		  document.getElementById(div_id).style.position = "fixed";
		  document.getElementById(div_id).style.top = min_from_top +"px";
	  }
	  if(position_top < changing_position_top){
		  document.getElementById(div_id).style.position = "absolute";
		  document.getElementById(div_id).style.top = max_from_top +"px";
	  }
	  if(changing_position_bottom > 0){
		  document.getElementById(div_id).style.position = "absolute";
		  document.getElementById(div_id).style.top = (content_height - max_from_bottom - div_height) +"px";
	  }
  }
	
}