function getDocumentSize(doc) {
  var r = { width: 0, height: 0 };
 
  var width1=0, width2=0, width3=0, width4=0, maxWidth=0;
  var height1=0, height2=0, height3=0, height4=0, maxHeight=0;
  
  
  if (doc.width) maxWidth = doc.width; 
  if (doc.body) { 
	if (doc.body.scrollWidth) width1 = doc.body.scrollWidth;
	if (doc.body.offsetWidth) width2 = doc.body.offsetWidth; 
  }
  if (doc.documentElement) {
	width3 = doc.documentElement.scrollWidth; 
	width4 = doc.documentElement.clientWidth;
  } 
  
  maxWidth = Math.max(Math.max(Math.max(width1, width2), Math.max(width3, width4)),maxWidth);
  
  if (doc.height) maxHeight = doc.height;
  if (doc.body) {
	if (doc.body.scrollHeight) height1 = doc.body.scrollHeight;
	if (doc.body.offsetHeight) height2 = doc.body.offsetHeight;
  }
  if (doc.documentElement) {
	height3 = doc.documentElement.scrollHeight;
	height4 = doc.documentElement.clientHeight;
  }
  maxHeight = Math.max(Math.max(Math.max(height1, height2), Math.max(height3, height4)),maxHeight);
  
  r.width = maxWidth;
  r.height = maxHeight;
  
  return r;
}

var win = {
    w: function() { return getDocumentSize(document).width; },
    h: function() { return getDocumentSize(document).height; }
}
var snow = {
    flakes: [],
    timer: false,
    init: function(flake_count) {
        this.flakes = [];
        for (var i = 0; i < flake_count; i++) {
            var img = new Image();
            img.src = '/snow.gif';
            img.style.position = 'absolute';
            img.style.zIndex = i;
            document.body.appendChild(img);
            this.flakes[i] = {
                img: img, 
                pos: function (x, y) {
                    this.img.style.left = x + 'px';
                    this.img.style.top  = y + 'px';
                    return this;
                },
                x: Math.random() * (win.w() - 50),
                y: Math.random() * win.h(),
                a: Math.random() * 20,
                s: { x: Math.random() * 0.1 + 0.02, y: Math.random() + 0.7 },
                d: 0
            }
        }
    },
    start: function(flake_count) {
        this.init(flake_count || 10);
        this.timer = setInterval(function() { snow.live(); }, 50);
    },
    stop: function() { if (this.timer) { clearInterval(this.timer); this.timer = false; }; },
    live: function() {
        for (var i in this.flakes) {
            var f = this.flakes[i];
            if ((f.y += f.s.y) > (win.h() - 50)) {
                f.pos(f.x = (Math.random() * (win.w() - 50)), f.y = 0);
            } else {
                f.pos(f.a * Math.sin(f.d += f.s.x) + f.x, f.y);
            }
        }
    }
}
window.onload = function() { snow.start(50); }

