$(document).ready(function() {
    
    function getBeer() {
        $('#footer .content').append('<div class="beer beerfull"><div class="cap"></div></div>');
        
        $beerHolder = $('.beerHolder:last').animate({ bottom: '20px' }, 1500, 'easeOutBounce');
        $beer = $('.beer.beerfull:last');
        $beer.css({ 
            top: '-' + $(window).height() - $beer.height() + 'px', 
            left: '300px' 
        }).animate({ 
            top: '30px'
        }, 1500, 'easeOutBounce');
        
        $cap = $('.cap:last');
        $prex = $('#prex');
        
        $cap.click(function() {
            $beer.addClass('nocap');
            $cap.animate({ rotate: '180deg', top: '-15px', left: '25px' }, 200, 'linear', function() {
                $cap.animate({ rotate: '720deg', top: '100px', left: '50px' }, 400, 'linear', function() {
                    $cap.hide();
                });
            });
            
            var drop = $prex.position();
            drop.bottom = drop.top + $prex.height();
            drop.right = drop.left + $prex.width();
            
            $beer.mousedown(function(event) {
                event.preventDefault();
                
                $(window).bind('mousemove.beer', function(e) {
                    var tPos = e.pageY - $(window).scrollTop() - $(window).height() + $('#footer').height() - ($beer.height() / 2);
                    var lPos = e.pageX - $(window).scrollLeft() - (($('#footer').width() - $('#footer .content').width()) / 2) - ($beer.width() / 2)
   
                    $beer.css({ left: lPos + 'px', top: tPos + 'px'});
                });
            });
            
            $(window).mouseup(function() {
                $(window).unbind('mousemove.beer');
                
                var beercoords = $beer.position();
                beercoords.bottom = beercoords.top + $beer.height();
                beercoords.right = beercoords.left + $beer.width();
                
                if(
                    beercoords.top > drop.top &&
                    beercoords.right < drop.right &&
                    beercoords.bottom < drop.bottom &&
                    beercoords.left > drop.left
                ) {
                    $beer.animate({ top: drop.top + 80 + 'px', left: drop.left - 20 + 'px' }, 200, function() {
                        $beer.addClass('beerstep2').animate({ rotate: '6deg' }, 500, 'linear', function() {
                            $beer.addClass('beerstep3').css('transform', 'rotate(0deg)')
                            .animate({ rotate: '6deg', left: '-=2px' }, 500, 'linear', function() {
                                $beer.addClass('beerstep4').css('transform', 'rotate(0deg)')
                                .animate({ rotate: '6deg', left: '-=3px' }, 500, 'linear', function() {
                                    $beer.addClass('beerstep5').css('transform', 'rotate(0deg)')
                                    .animate({ rotate: '6deg', top: '-=10px', left: '+=10px' }, 500, 'linear', function() {
                                        $beer.animate({ left: drop.left + 'px', top: '30px', rotate: '300deg' }, 900, 'easeOutBounce', function() {
                                            $beer.unbind('mousedown').css({ cursor: 'default' });
                                            $(window).unbind('mouseup')
                                        });
                                    });
                                });
                            });
                        });
                    });
                } else {
                    $beer.animate({ top: '30px' }, 500, 'easeOutBounce');
                }
                
            });

        });
    }
    
    var kkeys = [], k = '38,38,40,40,37,39,37,39,66,65';
    $(window).bind('keydown', function(e) {
        kkeys.push( e.keyCode );
        if (kkeys.toString().indexOf(k) >= 0) {
            getBeer();
            kkeys.length = 0;
        }
    });
    
    
    function filterPath(string) {
        return string
                .replace(/^\//,'')
                .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                .replace(/\/$/,'');
    }
    
    var locationPath = filterPath(location.pathname);
    
    var scrollElement = 'html, body';
    $('html, body').each(function () {
        var initScrollTop = $(this).attr('scrollTop');
        $(this).attr('scrollTop', initScrollTop + 1);
        if ($(this).attr('scrollTop') == initScrollTop + 1) {
            scrollElement = this.nodeName.toLowerCase();
            $(this).attr('scrollTop', initScrollTop);
            return false;
        }    
    });
    
    $('a[href*=#]').each(function() {
        var thisPath = filterPath(this.pathname) || locationPath;
        if(
            locationPath == thisPath
            && (location.hostname == this.hostname || !this.hostname)
            && this.hash.replace(/#/, '')
        ) {
            if ($(this.hash).length) {
                $(this).click(function(event) {
                    var targetOffset = $(this.hash).offset().top;
                    var target = this.hash;
                    event.preventDefault();
                    $(scrollElement).animate({scrollTop: targetOffset}, 800, 'easeOutQuad', function() {
                            location.hash = target;
                    });
                });
            }
        }
    });
});