

(function() {
    
    $.fn.floatingAdvert = function() {
        
        $(this).each(function(){
            
            if ($(this).next().length == 0) {
                var _self = $(this);
            
                $(window).scroll(function(){
                    _self.stop();
                    var breakpoint = $('#content-navigation').offset().top;
                    
                    var margin = 0;
                    var scroll = $(this).scrollTop();
                    var height = _self.outerHeight();
                    var position = _self.offset().top - (_self.css('margin-top').match(new RegExp('^([0-9]+)'))[1]);
                    
                    if (scroll > position && scroll + height < breakpoint) {
                        margin = scroll - position;
                    } else if (scroll + height >= breakpoint) {
                        margin = breakpoint - position - height;
                    }
                    
                    _self.animate({'margin-top': margin + 'px'},40);
                    //_self.css('margin-top',margin);
                });
            }
            
        });
        
    }
    
}) (jQuery);


