/*
    * stickyfloat - jQuery plugin for verticaly floating anything in a constrained area
    * 
    * parameters:
    * 		duration 	- the duration of the animation
    *		startOffset - the amount of scroll offset after it the animations kicks in
    *		offsetY		- the offset from the top when the object is animated
    *		lockBottom	- 'true' by default, set to false if you don't want your floating box to stop at parent's bottom
    * $Version: 05.16.2009 r1
    * Copyright (c) 2009 Yair Even-Or
    * vsync.design@gmail.com
    */
$.fn.stickyfloat = function (options, lockBottom) {
    var $obj = this;
    var parentPaddingTop = parseInt($obj.parent().css('padding-top'));
    var startOffset = $obj.parent().offset().top;
    var opts = $.extend({ startOffset: startOffset, offsetY: parentPaddingTop, duration: 400, lockBottom: true }, options);

    $obj.css({ position: 'absolute' });

    if (opts.lockBottom) {
        var bottomPos = $obj.parent().height() - $obj.height() + parentPaddingTop;
        if (bottomPos < 0)
            bottomPos = 0;
    }

    $(window).scroll(function () {
        $obj.stop();

        var pastStartOffset = $(document).scrollTop() > opts.startOffset;
        var objFartherThanTopPos = $obj.offset().top > startOffset;
        var objBiggerThanWindow = $obj.outerHeight() < $(window).height();

        if ((pastStartOffset || objFartherThanTopPos) && objBiggerThanWindow) {

//            if ($(window).scrollTop() > 0) $obj.show(); else $obj.hide();

            var newpos = ($(document).scrollTop() - startOffset + opts.offsetY);
            if (newpos > bottomPos)
                newpos = bottomPos;
            if ($(document).scrollTop() < opts.startOffset)
                newpos = parentPaddingTop;

            $obj.animate({ top: newpos }, opts.duration);
        }
    });
};
