function pad2(n) {   
    return (n < 10 ? '0' : '') + n;   
}

(function($){
    
    $.fn.countdown = function(options){
        
        var c = this,   // set this for scope consistency
            opt = {     // default options
                noWeeks: true,
                targetDate: false,
                callback: null,
                speed: 450
            }, 
            pre = {},   // a container for the previous time
            time = {},
            tar = new Date(),
            timer = null;
            
        $.extend(opt, options);
        $('.dash', c).html('<div class="now"></div><div class="next"></div>');
        
        if(opt.targetDate){
            tar = new Date(
                opt.targetDate.year+'/'+
                opt.targetDate.month+'/'+
                opt.targetDate.day+' '+
                opt.targetDate.hour+':'+
                opt.targetDate.min+':'+
                opt.targetDate.sec+' EST'
            );
        }
        
        var getSeconds = function(){            
            var now = new Date();
            return Math.floor((tar.valueOf() - now.valueOf()) / 1000);
        }
        
        var countDown = function(){
            
            var seconds = getSeconds();
            
            time = {
                secs:   seconds % 60,
                mins:   Math.floor(seconds / 60) % 60,
                hrs:    Math.floor(seconds / 60 / 60) % 24,
                days:   (opt.noWeeks) ? Math.floor(seconds / 60 / 60 / 24) : Math.floor(seconds / 60 / 60 / 24) % 7,
                wks:    Math.floor(seconds / 60 / 60 / 24 / 7)
            };
            
            if(seconds <= 0) seconds = 0;
            if(timer) clearTimeout(timer);
            if(time.secs != pre.secs) changeTime(time);
            if(seconds > 0) timer = setTimeout(countDown, 1000);
            else if(opt.callback) opt.callback();
            
        };
        
        var changeTime = function(time){
            
            $('#seconds-dash', c).animateTime(time.secs, opt.speed);
            if(time.mins != pre.mins){
                $('#minutes-dash', c).animateTime(time.mins, opt.speed);
                if(time.hrs != pre.hrs){
                    $('#hours-dash', c).animateTime(time.hrs, opt.speed);
                    if(time.days != pre.days){
                        $('#days-dash', c).animateTime(time.days, opt.speed);
                        if(!opt.noWeeks && time.wks != pre.wks){
                            $('#weeks-dash', c).animateTime(time.wks, opt.speed);
                        }
                    }
                }
            }
            
            pre = time; // store the new time for later comparison
              
        };
        
        countDown();
        
        return this;
        
    };    
    
    $.fn.animateTime = function(time, speed){
        
        $('.next', this).html('<span>'+pad2(time)+'</span>');
        
        $(this).stop(true, true).animate({ top: -50 }, speed, function(){
            $('.now', this).html('<span>'+pad2(time)+'</span>');
            $(this).css({ top: -1 }); 
            
            if(typeof Cufon != 'undefined') Cufon.replace('.next, .now');
        });
        
        if(typeof Cufon != 'undefined') Cufon.replace('.next, .now');
        
    };
    
})(jQuery);

