$(document).ready(animate_setup);
function animate_setup() {
    $('#footerCenter').prepend('<div id="falling-rocket"></div>');
    $('#footerCenter').prepend('<div id="rocket-crash"></div>');
    
    $(window).bind('scroll', anim_scroll_handler);
}


var anim_timer = null;
function anim_scroll_handler() {
    var s = $(this).attr('scrollY');
    if (s > $('#footerCenter').offset().top - $(window).height() - 60 && s < $('#footerCenter').offset().top - 96) {
        if (!anim_timer) {
            //LOG('countdown!' + new Date().getTime());
            anim_timer = setTimeout(function () {animate();}, 1500);
        }
        
    } else if (anim_timer) {
        //LOG('reset!' + new Date().getTime());
        clearTimeout(anim_timer);
        anim_timer = null;
    }
}


function animate() {
    $(window).unbind('scroll', anim_scroll_handler);
    
    // Launch nav rocket off screen... that way for really short blog entries shit still makes sense
    $('#rocket').animate({top: '-110px'}, 500);
    
    var rocketTimer = new Timer(
        function () {
            $('#falling-rocket').animate({top: '+=5px', left: '-=5px'}, 0);

            if ($('#falling-rocket').position().left <= 545) {
                new Timer(
                    function () {
                        $('#rocket-crash').animate({backgroundPosition: '-=125px 0'}, 0);
                        return true;
                    },
                    50,
                    5
                ).onBeforeStart(
                    function () {
                        $('#rocket-crash').css({left: '545px'});
                    }
                ).start();

                $('#falling-rocket').hide();
                return false;
            }

            return true;
        },
        50
    ).onBeforeStart(
        function () {
            var left = (Math.floor($('#footerCenter').outerWidth() / 2) + Math.floor($(window).width() / 2) - $('#falling-rocket').width() - 10);
            var top = -145 - (left - 545);

            $('#falling-rocket').css({top: top + 'px', left: left + 'px'});
        }
    ).start();
}


/* 
 * I should turn this into a jQuery plugin
 * note that the jquery way to deal with onBeforeStart and onStop events is to use bind and trigger
 *
 */

/* examples /*
var a = 0;
new Timer(function() {LOG('a'); a++; if (a < 10) return true; else return false;}, 500).onStop(function () {cTimer.stop();}).start();
new Timer(function() {LOG('b'); return true;}, 5000, 1).start();
var cTimer = new Timer(function() {LOG('c'); return true;}, 500).start();
*/

function Timer(func, timing, repeat) {
    var count = 0;
    
    if(!repeat) {
        repeat = 0;
    }
    
    var timer = null;
    
    var self = this;
    
    this.start = function () {
        if (onBeforeStartFunc) {
            onBeforeStartFunc();
        }
        timer = setInterval(
                    function () {
                        if (!func(count) || (repeat > 0 && ++count >= repeat)) {
                            self.stop();
                        }
                    },
                    timing
                    );
        
        return this;
    };
    
    this.stop = function () {
        if (timer) {
            clearTimeout(timer);
            timer = null;
            if (onStopFunc) {
                onStopFunc();
            }
        }
    };
    
    this.cancel = function () {
        if (timer) {
            clearTimeout(timer);
            timer = null;
        }
    };
    
    var onBeforeStartFunc = null;
    this.onBeforeStart = function (f) {
        onBeforeStartFunc = f;
        
        return this;
    };
    
    var onStopFunc = null;
    this.onStop = function (f) {
        onStopFunc = f;
        return this;
    };
}
