|
| 1 | +/* CTK: Cherokee Toolkit |
| 2 | + * |
| 3 | + * Authors: |
| 4 | + * Alvaro Lopez Ortega <alvaro@alobbs.com> |
| 5 | + * |
| 6 | + * Copyright (C) 2010 Alvaro Lopez Ortega |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of version 2 of the GNU General Public |
| 10 | + * License as published by the Free Software Foundation. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | + * 02110-1301, USA. |
| 21 | + */ |
| 22 | + |
| 23 | +(function($) { |
| 24 | + var Carousel = function (element) { |
| 25 | + var obj = this; // Object {} |
| 26 | + var self = $(element); // $(<div>) |
| 27 | + |
| 28 | + // PRIVATE |
| 29 | + // |
| 30 | + function set_item (num) { |
| 31 | + for (var n=0; n<obj.child_num; n++) { |
| 32 | + if (n == num) { |
| 33 | + obj.$child.eq(n).show(); |
| 34 | + obj.$pager_child.eq(n).addClass('selected'); |
| 35 | + } else { |
| 36 | + obj.$child.eq(n).hide(); |
| 37 | + obj.$pager_child.eq(n).removeClass('selected'); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (num == 0) { |
| 42 | + obj.$prev.hide(); |
| 43 | + } else { |
| 44 | + obj.$prev.show(); |
| 45 | + } |
| 46 | + |
| 47 | + if (num == obj.child_num -1) { |
| 48 | + obj.$next.hide(); |
| 49 | + } else { |
| 50 | + obj.$next.show(); |
| 51 | + } |
| 52 | + |
| 53 | + obj.current = num; |
| 54 | + } |
| 55 | + |
| 56 | + function advance_step (val) { |
| 57 | + var new_current = obj.current + val; |
| 58 | + |
| 59 | + if ((new_current >= 0) && |
| 60 | + (new_current < obj.child_num)) |
| 61 | + { |
| 62 | + set_item (new_current); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + function pager_click_cb() { |
| 67 | + set_item (parseInt ($(this).text()) -1); |
| 68 | + } |
| 69 | + |
| 70 | + // PUBLIC |
| 71 | + // |
| 72 | + this.init = function (self, options) { |
| 73 | + obj.$pager = self.find('.pager'); |
| 74 | + obj.$pager_child = obj.$pager.children(); |
| 75 | + obj.$prev = self.find('.prev'); |
| 76 | + obj.$next = self.find('.next'); |
| 77 | + obj.$child = self.find('.overview').children(); |
| 78 | + obj.child_num = obj.$child.length; |
| 79 | + obj.current = 0; |
| 80 | + |
| 81 | + obj.$prev.bind ('click', function(){ advance_step (-1); }) |
| 82 | + obj.$next.bind ('click', function(){ advance_step (+1); }) |
| 83 | + |
| 84 | + obj.$pager_child.each (function() { |
| 85 | + $(this).bind ('click', pager_click_cb); |
| 86 | + }); |
| 87 | + |
| 88 | + set_item (0); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + $.fn.Carousel = function (options) { |
| 93 | + var self = this; |
| 94 | + return this.each (function() { |
| 95 | + if ($(this).data('carousel')) return; |
| 96 | + var carousel = new Carousel (this); |
| 97 | + $(this).data('carousel', carousel); |
| 98 | + carousel.init (self, options); |
| 99 | + }); |
| 100 | + }; |
| 101 | +})(jQuery); |
0 commit comments