Bug 7747 - Replace YUI autocomplete with jQueryUI
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / lib / jquery / plugins / jquery.fixFloat.js
1 /* Source: http://www.webspeaks.in/2011/07/new-gmail-like-floating-toolbar-jquery.html
2    Revision: http://jsfiddle.net/pasmalin/AyjeZ/
3 */
4 (function($){
5   $.fn.fixFloat = function(options){
6
7     var defaults = {
8       enabled: true
9     };
10     var options = $.extend(defaults, options);
11
12     var offsetTop;    /**Distance of the element from the top of window**/
13     var s;        /**Scrolled distance from the top of window through which we have moved**/
14     var fixMe = true;
15     var repositionMe = true;
16
17     var tbh = $(this);
18     var originalOffset = tbh.position().top;  /**Get the actual distance of the element from the top mychange:change to position better work**/
19
20     if (tbh.css('position')!='absolute') {
21       var tbhBis = $("<div></div>");
22       tbhBis.css({"display":tbh.css("display"),"visibility":"hidden"});
23       tbhBis.width(tbh.outerWidth(true));
24       tbhBis.height(tbh.outerHeight(true));
25       tbh.after(tbhBis);
26       tbh.width(tbh.width());
27       tbh.css({'position':'absolute'});
28     }
29
30     if(options.enabled){
31       $(window).scroll(function(){
32         var offsetTop = tbh.offset().top;  /**Get the current distance of the element from the top **/
33         var s = parseInt($(window).scrollTop(), 10);  /**Get the from the top of wondow through which we have scrolled**/
34         var fixMe = true;
35         if(s > offsetTop){
36           fixMe = true;
37         }else{
38           fixMe = false;
39         }
40
41         if(s < originalOffset){
42           repositionMe = true;
43         }else{
44           repositionMe = false;
45         }
46
47         if(fixMe){
48           var cssObj = {
49             'position' : 'fixed',
50             'top' : '0px',
51             'z-index' : '1000'
52           }
53           tbh.css(cssObj);
54           tbh.addClass("floating");
55         }
56         if(repositionMe){
57           var cssObj = {
58             'position' : 'absolute',
59             'top' : originalOffset,
60             'z-index' : '1'
61           }
62           tbh.css(cssObj);
63           tbh.removeClass("floating");
64         }
65       });
66     }
67   };
68 })(jQuery);