(function ($,window,document) {
    
    var contentTop = [],
    content = [],
    lastScrollTop = 0,
    scrollDir = '',
    itemClass = '',
    itemHover = '',
    menuSize = null,
    active = false,
    stickyHeight = 0,
    currentMarginT = 0,
    $element = $(),
    currentMarginT = 0,
    topMargin = 0,
    top,

    getTopMargin = function(options) {
        if (options.topMargin == 'auto') {
            return parseInt($('.stuckMenu').css('margin-top'));
        } else {
            if (isNaN(options.topMargin) && options.topMargin.search("px") > 0) {
                return parseInt(options.topMargin.replace("px", ""));
            } else if (!isNaN(parseInt(options.topMargin))) {
                return parseInt(options.topMargin);
            } else {
                console.log("incorrect argument, ignored.");
                return 0;
            }
        }
    },

    baseScrollHandlerFn = function(event) {
        var st = $(event.target).scrollTop();
        scrollDir = (st >= lastScrollTop) ? 'down' : 'up';
        lastScrollTop = st;
        if(!active){
            stickyHeight = parseInt($element.outerHeight(true));
            top = parseInt($element.offset().top);
            currentMarginT = parseInt($element.next().closest('div').css('margin-top'));
        }
    },
     
    stickUpScrollHandlerFn = function() {
        var scroll = parseInt($(document).scrollTop());
        if (menuSize != null && content.length > 0) {
            for (var i = 0; i < menuSize; i++) {
                contentTop[i] = $('#' + content[i] + '').offset().top;
                function bottomView(i) {
                    var contentView = $('#' + content[i] + '').height() * .4;
                    if (scroll > (contentTop[i] - contentView)) {
                        setItemHover(i);
                    } else if (scroll < 50) {
                        setItemHover(0);
                    }
                }

                if (scrollDir == 'down' && scroll > contentTop[i] - 50 && scroll < contentTop[i] + 50) {
                    setItemHover(i);
                }

                if (scrollDir == 'up') {
                    bottomView(i, contentTop[i]);
                }
            }
        }
        
        //STICK IT
        if (top < scroll + topMargin && !$element.hasClass('isStuck')) {
            active = true;
            window.requestAnimationFrame(function() {
                $element
                    .addClass('isStuck')
                    .next()
                    .closest('div')
                    .css({
                        'margin-top': stickyHeight + currentMarginT + 'px'
                    });

                $element.css("position", "fixed");

                $element.css({ 
                    top: '0px'
                });
            });
        }
        
        //UNSTICK
        if (scroll + topMargin < top && $element.hasClass('isStuck')) {
            window.requestAnimationFrame(function() {
                $element
                    .removeClass('isStuck')
                    .next()
                    .closest('div')
                    .css({
                        'margin-top': currentMarginT + 'px'
                    });

                $element.css("position", "relative");
            });
            active = false;
        }
    },

    setItemHover = function(position) {
        $('.' + itemClass).removeClass(itemHover);
        $('.' + itemClass + ':eq(' + position + ')').addClass(itemHover);
    };

    $.fn.stickUp = function (options) {
        $element = $(this);
        // adding a class to users div
        $element.addClass('stuckMenu');
        //getting options
        var objn = 0;
        if (options != null) {
            var parts = options.parts;
            for (var key in  parts) {
                if (parts.hasOwnProperty(key)) {
                    content[objn] = parts[objn];
                    objn++;
                }
            }

            itemClass = options.itemClass;
            itemHover = options.itemHover;

            topMargin = (options.topMargin != null) ? getTopMargin(options) : 0;

            menuSize = $('.' + itemClass).size();
        }
        $(window).on('scroll.stickUp', baseScrollHandlerFn);
        $(document).on('scroll.stickUp', stickUpScrollHandlerFn);
        
        //initial round ;-)
        baseScrollHandlerFn({target:window});
        stickUpScrollHandlerFn({target:document});
        
        //chainability
        return this;
    };
}(jQuery,window,document));
@@ -302,24 +302,78 @@
 Margin = 0,%0A
+    $placeholder = $('%3Cdiv style=%22margin:0%22%3E%3C/div%3E'),%0A
     top,%0A%0A  
@@ -1257,97 +1257,8 @@
 p);%0A
-            currentMarginT = parseInt($element.next().closest('div').css('margin-top'));%0A
     
@@ -2406,279 +2406,173 @@
 ment
-%0A                    .addClass('isStuck')%0A                    .next()%0A                    .closest('div')%0A                    .css(%7B%0A                        'margin-top': stickyHeight + currentMarginT + 'px'%0A                    %7D);%0A%0A                $element.css(%22
+.before($placeholder.css('height',$element.outerHeight()));%0A                $element.addClass('isStuck')%0A                    .css(%7B %0A                        
 position
 %22, %22
@@ -2571,18 +2571,17 @@
 tion
-%22,
+:
  %22fixed%22
 );%0A%0A
@@ -2580,44 +2580,14 @@
 xed%22
-);%0A
+,
 %0A    
-            $element.css(%7B %0A
     
@@ -2609,24 +2609,28 @@
  top: '0px'%0A
+    
             
@@ -2808,32 +2808,71 @@
 me(function() %7B%0A
+                $placeholder.remove();%0A
                 

replace margin method with dummy for maintaining distance