var win = {
    w: function() { return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; },
    h: function() { return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; }
}
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 = '/images/new_year/snow.png';
            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(15); }
