// Prize Circle Animate //
$(".prize-circle").css({ marginTop: "-3000px" }).animate({ 
  marginTop: 0
  }, 1500 );

// Facebox //
$(document).ready(function($) {
  $('a[rel*=facebox]').facebox() 
})

$(document).bind('afterReveal.facebox', function() {
   
    var windowHeight = $(window).height();
    var faceboxHeight = $('#facebox').height();
   
    if(faceboxHeight < windowHeight) {
        $('#facebox').css('top', (Math.floor((windowHeight - faceboxHeight) / 2) + $(window).scrollTop()) );
    }
   
});

$(function() {
    
    Contest = {};
    
    // Waypoints //
        var waypoints = [];
        Contest.Waypoint = function(elem) {
            this.elem = elem;
            waypoints.push(this);
        };
        
        Contest.Waypoint.originModifier = 125;
        Contest.Waypoint.heightModifier = 150;
        
        Contest.Waypoint.prototype = {
            isVisible: function() {
                var elem = this.elem;
                var originY = Math.max(elem.offsetTop - Contest.Waypoint.originModifier);
                var height = elem.offsetHeight - Contest.Waypoint.heightModifier;
                var scrollY = window.scrollY;
                
                return scrollY >= originY && scrollY <= originY + height;
            },
            
            navNode: function() {
                if (!this._navNode)
                    this._navNode = $('#' + this.elem.getAttribute('data-nav'));
                
                return this._navNode;
            }
        };
        
        var current = null;
        Contest.Waypoint.getCurrent = function() {
            if (current && current.isVisible())
                return current;
            
            for (var i = -1, count = waypoints.length; ++i < count;) {
                var waypoint = waypoints[i];
                if (waypoint === current)
                    continue;
                
                if (waypoint && waypoint.isVisible()) {
                    Contest.Waypoint.currentWillChange(waypoint, current);
                    current = waypoint;
                    
                    return waypoint;
                }
            }
        };
        
        Contest.Waypoint.currentWillChange = function(newWaypoint, oldWaypoint) {
            oldWaypoint && oldWaypoint.navNode().removeClass('link-active');
            newWaypoint && newWaypoint.navNode().addClass('link-active');
        };
        
        $('.section').each(function(i, elem) {
            new Contest.Waypoint(elem);
        });
        
        // Render //
        Contest.renderLater = function() {
            if (Contest._renderTimeout)
                return;
            
            Contest._renderTimeout = setTimeout(Contest.render, 10);
        };
        
        Contest.render = function() {
            if (Contest._renderTimeout)
                Contest._renderTimeout = clearTimeout(Contest._renderTimeout);
            
            Contest.Waypoint.getCurrent();
        };
        
        // Nav Scroll //
        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;
            }
        });

        // Smooth scrolling for internal links //
        $("a[href^='#']").click(function(event) {
            event.preventDefault();
            
            var $this = $(this),
                target = this.hash,
                $target = $(target);
            
            $(scrollElement).stop().animate({
                'scrollTop': $target.offset().top
            }, 1000, 'swing');
          
        });

        
        $(window).scroll(function() {
            Contest.renderLater();
        });
        
        Contest.renderLater();
    });
