02daa519fa42e498eefd6d62701e1ad9b0495fb1
[profile/ivi/cowhide.git] / src / javascripts / cowhide-simple-scrollable.js
1 /* vi: set et sw=4 ts=4 si: */
2 (function($, undefined) {
3     var ChSimpleScrollable = function(element, options) {
4         $.fn.ch_widget.Constructor(element, options);
5         this.$element = $(element);
6         this.options = $.extend(
7             options,
8             $.fn.ch_widget.defaults,
9             {
10             });
11     };
12
13     ChSimpleScrollable.prototype = $.extend(
14         {},
15         $.fn.ch_widget.Constructor.prototype,
16         {
17             constructor: ChSimpleScrollable,
18
19             enable: function() {
20                 var self = this,
21                     $this = self.$element,
22                     $up = $('<div/>').addClass('ch-simple-scrollable-up'),
23                     $dn = $('<div/>').addClass('ch-simple-scrollable-dn'),
24                     $child =  $this.find('ul, ol, div, p'),
25                     scrollAmount;
26
27                 $child.addClass('ch-simple-scrollable-content');
28                 $child.height($child.parent().height() - 160);
29                 scrollAmount = $child.height() - 40;
30
31
32                 $up.css({top: $child.offset().top});
33
34                 $up.html('<a href="#"><i class="icon-chevron-up"></i></a>');
35                 $dn.html('<a href="#"><i class="icon-chevron-down"></i></a>');
36
37                 $dn.click(function() {
38                     $child.animate({
39                         scrollTop: $child.scrollTop() + scrollAmount
40                     }, 200);
41                 });
42
43                 $up.click(function() {
44                     $child.animate({
45                         scrollTop: $child.scrollTop() - scrollAmount
46                     }, 200);
47                 });
48
49
50                 $up.insertBefore($child);
51                 $dn.insertAfter($child);
52             }
53         }
54     );
55
56
57     /* CHSIMPLESCROLLABLE PLUGIN DEFINITION
58      * ==================================== */
59
60     var old = $.fn.ch_simple_scrollable;
61
62     $.fn.ch_simple_scrollable = function(option) {
63         return this.each(function() {
64             var $this = $(this),
65                 data = $this.data('ch_simple_scrollable'),
66                 options = typeof option == 'object' && option;
67
68             if (!data) {
69                 $this.data('ch_simple_scrollable', (data = new ChSimpleScrollable(this, options)));
70                 data.register();
71             }
72
73             if(typeof option == 'string')
74                 data[option]();
75         });
76     };
77
78     $.fn.ch_simple_scrollable.Constructor = ChSimpleScrollable;
79
80
81     /* CHSIMPLESCROLLABLE NO CONFLICT
82      * ============================== */
83
84     $.fn.ch_simple_scrollable.noConflict = function() {
85         $.fn.ch_simple_scrollable = old;
86         return this;
87     };
88
89
90     /* CHSIMPLESCROLLABLE DATA-API
91      * =========================== */
92
93     $(function() {
94         $('div.ch-simple-scrollable').ch_simple_scrollable('enable');
95     })
96 })(window.jQuery);