(function ($) { "use strict"; // // $.fn.offsetRelative dependency (https://gist.github.com/3191472) // $.fn.offsetRelative = $.fn.offsetRelative || function (selector) { selector || (selector = '*'); var elOffset = this.offset(), parentOffset = this.parent().closest(selector).offset(); return { left: elOffset.left - parentOffset.left, top: elOffset.top - parentOffset.top }; }; // // Inside plugin // $.fn.inside = $.fn.inside || function (container, includeMargin) { includeMargin || (includeMargin = false); var $el = this.eq(0), $container = $(container); if (!$.contains($container.get(0), $el.get(0))) { return; } var o = $el.offsetRelative($container), w = $container.outerWidth(includeMargin), h = $container.outerHeight(includeMargin), W = $el.outerWidth(includeMargin), H = $el.outerHeight(includeMargin); return { left: (o.left + W) / (w + W), top: (o.top + H) / (h + H) }; }; // // 'inside' special event // $.event.special.inside = $.event.special.inside || (function () { var registry = {}; // Main handler function handler(key) { $(this).trigger('inside', $(this).inside(registry[key].handleObj.data.ancestor)); } return { add: function (handleObj) { var frequency = handleObj.data.frequency || 200, key = handleObj.guid; // Interval var interval = setInterval($.proxy(function () { return handler.apply(this, [key]); }, this), frequency); // Store to the registry registry[key] = { handleObj: handleObj, interval: interval }; }, remove: function (handleObj) { var key = handleObj.guid; // Clear from the registry clearInterval(registry[key].interval); delete registry[key]; } }; }()); }(jQuery));