6 // If there's no jQuery, Unslider can't work, so kill the operation.
\r
9 var Unslider = function() {
\r
10 // Set up our elements
\r
27 delay: 3000, // f for no autoplay
\r
28 complete: f, // when a slide's finished
\r
29 keys: !f, // keyboard shortcuts - disable if it breaks things
\r
30 dots: f, // display ••••o• pagination
\r
31 fluid: f // is it a percentage width?,
\r
34 // Create a deep clone for methods where context changes
\r
37 this.init = function(el, opts) {
\r
39 this.ul = el.children('ul');
\r
40 this.max = [el.outerWidth(), el.outerHeight()];
\r
41 this.items = this.ul.children('li').each(this.calculate);
\r
43 // Check whether we're passing any options in to Unslider
\r
44 this.opts = $.extend(this.opts, opts);
\r
46 // Set up the Unslider
\r
52 // Get the width for an element
\r
53 // Pass a jQuery element as the context with .call(), and the index as a parameter: Unslider.calculate.call($('li:first'), 0)
\r
54 this.calculate = function(index) {
\r
56 width = me.outerWidth(), height = me.outerHeight();
\r
58 // Add it to the sizes list
\r
59 _.sizes[index] = [width, height];
\r
61 // Set the max values
\r
62 if(width > _.max[0]) _.max[0] = width;
\r
63 if(height > _.max[1]) _.max[1] = height;
\r
66 // Work out what methods need calling
\r
67 this.setup = function() {
\r
68 // Set the main element
\r
72 height: this.items.first().outerHeight()
\r
75 // Set the relative widths
\r
76 this.ul.css({width: (this.items.length * 100) + '%', position: 'relative'});
\r
77 this.items.css('width', (100 / this.items.length) + '%');
\r
79 if(this.opts.delay !== f) {
\r
81 this.el.hover(this.stop, this.start);
\r
84 // Custom keyboard support
\r
85 this.opts.keys && $(document).keydown(this.keys);
\r
88 this.opts.dots && this.dots();
\r
90 // Little patch for fluid-width sliders. Screw those guys.
\r
91 if(this.opts.fluid) {
\r
92 var resize = function() {
\r
93 _.el.css('width', Math.min(Math.round((_.el.outerWidth() / _.el.parent().outerWidth()) * 100), 100) + '%');
\r
97 $(window).resize(resize);
\r
100 if(this.opts.arrows) {
\r
101 this.el.parent().append('<p class="arrows"><span class="prev">←</span><span class="next">→</span></p>')
\r
102 .find('.arrows span').click(function() {
\r
103 $.isFunction(_[this.className]) && _[this.className]();
\r
108 if($.event.swipe) {
\r
109 this.el.on('swipeleft', _.prev).on('swiperight', _.next);
\r
113 // Move Unslider to a slide index
\r
114 this.move = function(index, cb) {
\r
115 // If it's out of bounds, go to the first slide
\r
116 if(!this.items.eq(index).length) index = 0;
\r
117 if(index < 0) index = (this.items.length - 1);
\r
119 var target = this.items.eq(index);
\r
120 var obj = {height: target.outerHeight()};
\r
121 var speed = cb ? 5 : this.opts.speed;
\r
123 if(!this.ul.is(':animated')) {
\r
124 // Handle those pesky dots
\r
125 _.el.find('.dot:eq(' + index + ')').addClass('active').siblings().removeClass('active');
\r
127 this.el.animate(obj, speed) && this.ul.animate($.extend({left: '-' + index + '00%'}, obj), speed, function(data) {
\r
129 $.isFunction(_.opts.complete) && !cb && _.opts.complete(_.el);
\r
134 // Autoplay functionality
\r
135 this.start = function() {
\r
136 _.interval = setInterval(function() {
\r
137 _.move(_.current + 1);
\r
142 this.stop = function() {
\r
143 _.interval = clearInterval(_.interval);
\r
148 this.keys = function(e) {
\r
159 if($.isFunction(map[key])) {
\r
164 // Arrow navigation
\r
165 this.next = function() { return _.stop().move(_.current + 1) };
\r
166 this.prev = function() { return _.stop().move(_.current - 1) };
\r
168 this.dots = function() {
\r
170 var html = '<ol class="dots">';
\r
171 $.each(this.items, function(index) { html += '<li class="dot' + (index < 1 ? ' active' : '') + '">' + (index + 1) + '</li>'; });
\r
174 // Add it to the Unslider
\r
175 this.el.addClass('has-dots').append(html).find('.dot').click(function() {
\r
176 _.move($(this).index());
\r
181 // Create a jQuery plugin
\r
182 $.fn.unslider = function(o) {
\r
183 var len = this.length;
\r
185 // Enable multiple-slider support
\r
186 return this.each(function(index) {
\r
187 // Cache a copy of $(this), so it
\r
189 var instance = (new Unslider).init(me, o);
\r
191 // Invoke an Unslider instance
\r
192 me.data('unslider' + (len > 1 ? '-' + (index + 1) : ''), instance);
\r
195 })(window.jQuery, false);