( function( $ ) {
    
    $.IN10Parralax = function ( box, options )
    {
        var $document = $(document);
        var $window   = $(window);

        var options = options || {};
        
        var animationSpeed = options.animationSpeed || 800;
        
        var animationDelay = options.animationDelay || 150;
        
        var maxTop = options.maxTop || 280;
        
        var bottomOffset = options.bottomOffset || 50;
        
        var standardTop = Number($window.height() - bottomOffset);
        
        var newTop;
        
        var currentScrollTop = options.scrollTop;
        
        var scrolling = false;
        
        var footer = false;
    
        init(box);

        function init( box ) {

            var s = window.setInterval(checkScrollingAnimation, 33);
            
            $(box).css({"top": standardTop, "margin-top":"0", "display":"block", "position":"fixed"});
            
            $window.scroll( function() {

                if(scrolling == false){
                    scrolling = true;
                    
                    if(Number($(box).css("top").replace("px", "")) == standardTop){
                        newTop = standardTop;
                    } else {
                        newTop = (Number($(box).css("top").replace("px", "")));
                    }
                }

            });

            $window.bind('scrollstop', function(e){
        
                if(scrolling == true && footer == false){
                    scrolling = false;
                    animateDown();
                    currentScrollTop = $document.scrollTop();
                }

            });
            
            $window.resize( function() {
                
                standardTop = Number($window.height() - bottomOffset);
                animateDown();
                
            });

        }

        function checkScrollingAnimation(currentBoxTop){
                
            var scrollBottom = Number($(window).scrollTop()) + Number($(window).height());
            
            if(scrollBottom >= ($document.height() - 100) && footer == false){
            
                footer = true;
                
                //console_log("footer");
                animateFooter();                
                
                //$(box).stop().css("top", ($(window).height() - $("#footer-placeholder").height())- bottomOffset); 
            
            } else if (scrollBottom <= ($document.height() - 100)){
                
                footer = false;
                
                if(scrolling == true){

                    var scrollBottom = Number($(window).scrollTop()) + Number($(window).height());

                    footer = false;

                    var scrollPos = currentScrollTop - $document.scrollTop();

                    var newBoxPos = newTop - ((scrollPos - (scrollPos * 2)) * 0.05);

                    //console_log("newBoxPos: "+newBoxPos);
                    //console_log("maxTop: "+maxTop);

                    if(newBoxPos < maxTop){
                        newBoxPos = maxTop;
                    }

                    $(box).stop().css("top", newBoxPos);

                }
            }
                
        }
        
        function animateDown(){
            var docHeight = $document.height();
            var scrollTop = $document.scrollTop();

            //console_log("scrollTop: "+$document.scrollTop());

            var newBoxPos = scrollTop + ($window.height() - bottomOffset);
            //console_log("newBoxPos: "+newBoxPos);

            //$(box).css("top", newBoxPos);

            $(box).stop().delay(animationDelay).animate({
                top:standardTop
            }, {
                duration: animationSpeed,
                specialEasing: {
                    top:'easeInOutBack'
                },
                complete: function() {

                }
            });
            
            
        }
        
        function animateFooter(){
            
            currentScrollTop = ($(window).height() - $("#footer-placeholder").height()) - bottomOffset;
            
            $(box).stop().delay().animate({
                top: currentScrollTop
            }, {
                duration: animationSpeed,
                specialEasing: {
                    top:'easeInOutBack'
                },
                complete: function() {

                }
            });
        }

    };

    $.fn.IN10Parralax = function ( options )
    {

        this.each( function() 
        {
            new $.IN10Parralax( this, options );
        }
    );

    return this;
};

})( jQuery );

