//
// Zachary Johnson
// http://www.zachstronaut.com/
//

/*
javascript:(function(){s=document.createElement('script');s.type='text/javascript';s.src='http://tech.no.logi.es/woodshop/kaboomer.js?'+(new%20Date().getTime());document.body.appendChild(s);})();
*/

(function () {
    //var audioBooms = new Array();
    
    var path = 'http://tech.no.logi.es/woodshop/';
    
    var boomAudioFiles = ['bomb-02.wav', 'explosion-01.wav', 'explosion-02.wav'];

    var boomImage = new Image();
    boomImage.onload = preload;
    boomImage.src = path + 'boom.gif';
    
    var filesLoaded = 0;
    
    var soundCounter = 0;
    
    if (typeof Audio == 'undefined') {
        window.onmousedown = mouseHandler;
        
    } else {
        var audio;
        for (var i = 0; i < boomAudioFiles.length; i++) {
            audio = new Audio();
            audio.addEventListener('load', preload, false);
            audio.src = path + boomAudioFiles[i];
            audio.load();
            //audioBooms.push(audio);
            /*
            // copy for better sound duplexing
            audio = new Audio();
            audio.src = path + boomAudioFiles[i];
            audioBooms.push(audio);
            */
        }
        window.onmousedown = mouseHandler;
    }
    
    function preload() {
        filesLoaded++;
        if (filesLoaded == 1 + boomAudioFiles.length) {
            //boomSound();
            //window.onmousedown = mouseHandler;
        }
    }

    function mouseHandler(e) {
        var x = e.pageX;
        var y = e.pageY;
    
        kaboomer(x, y);
    }


    function boomSound() {
        //audioBooms[Math.floor(Math.random() * audioBooms.length)].play();
        //audioBooms[soundCounter++ % audioBooms.length].play();
        new Audio(path + boomAudioFiles[Math.floor(Math.random() * boomAudioFiles.length)]).play();
    }

    function kaboomer(x, y) {
        var div;
        var styles = {
            width: '50px',
            height: '50px',
            position: 'absolute',
            background: 'url(' + path + 'boom.gif) top left no-repeat',
            visibility: 'hidden',
            zIndex: '999'
        }
        
        for (var i = 0; i < 20; i++) {
            div = document.createElement('div');
            for (property in styles) {
                div.style[property] = styles[property];
            }
            
            div.style.top = (y + Math.floor(Math.random() * 100) - 100) + 'px';
            div.style.left = (x + Math.floor(Math.random() * 100) - 100) + 'px';
            
            document.body.appendChild(div);
        
            if (Math.random() > 0.75) {
                spark(div, Math.floor(Math.random() * 600));
            } else {
                spark(div, Math.floor(Math.random() * 250));
            }
        }
        
        boomSound();
    }


    function spark(div, time) {
        setTimeout(function () {boom(div, 0);}, time);
    }


    function boom(div, a) {
        div.style.backgroundPosition = (a * -50) + 'px 0';
    
        if (a == 0) {
            div.style.visibility = 'visible';
        }
    
        if (a < 7) {
            setTimeout(function () {boom(div, ++a);}, 100);
            
        } else {
            document.body.removeChild(div);
        }
    }
})();
