1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
| //********************************************
// spinner
// hotels.com
//
(function($) {
//********************************************
$.spinner = function(element, options) {
//references
var plugin = this;
var $element = $(element); //jquery instance
var div = $element;
//public settings
plugin.settings = $.extend({}, $.spinner.defaults, options);
//private
var _list = div.children('ul').children('li');
var _list_total = _list.length;
var _is_spinning = false;
var _current_speed = plugin.settings['speed'];
var _stop_div = $(plugin.settings['stop_div']);
var _slow_spinner = false;
var _spinner_multiplier = plugin.settings['slow_rate'];
var _popup_div = $(element).children('.popup');
var _label_div = $(plugin.settings['label_div']);
var _selected_index = (plugin.settings['index'] !== undefined) ? plugin.settings['index'] : 0;
//*****************
plugin.start = function(parameters) {
if (_is_spinning) {return;}
if (!parameters) {parameters = {};}
//which to end on
_selected_index = (plugin.settings['index'] !== undefined) ? plugin.settings['index'] : 0;
//reset parameters
_is_spinning = true;
_current_speed = plugin.settings['speed'];
_slow_spinner = false;
plugin.select_random();
}
//*****************
plugin.stop = function(parameters) {
if (_slow_spinner) {return;}
//start slowing the spinner
_slow_spinner = true;
//setup random multiplier speed (don't allow it to be less that 1)
_spinner_multiplier = plugin.settings['slow_rate'] + (Math.random() * plugin.settings['slow_variance']);
if (_spinner_multiplier <= 1) {_spinner_multiplier = 1.1;}
//fade out label
if (_label_div.length > 0) {
_label_div.fadeTo(500, .01); //fade but don't hide since we need this block for padding
}
//callback
if (typeof plugin.settings['on_click'] == 'function') {plugin.settings['on_click']();}
}
//*****************
plugin.select_random = function(parameters) {
if (!parameters) {parameters = {};}
var index = (parameters['index'] !== undefined) ? parameters['index'] : null;
var has_timer = (parameters['has_timer'] !== undefined) ? parameters['has_timer'] : true;
//adjust speed
if (_slow_spinner) {
_current_speed = _current_speed * _spinner_multiplier;
//stop the animation (and then rotate)
if (_current_speed > plugin.settings['max_speed']) {
_is_spinning = false;
index = _selected_index;
has_timer = false;
}
}
//build list of available items
var available_items = [];
for (var i=0; i<_list_total; i++) {
available_items.push(i);
}
var selected = _list.filter('.selected');
//if previously selected, adjust available items and remove the selection
if (selected.length > 0) {
//remove this index from available to avoid the same one being picked
available_items.splice(_list.index(selected), 1);
$(selected).removeClass('selected');
}
//if not provided an index, randomly pick one from the available list
if (index === null) {
index = available_items[Math.floor(Math.random() * available_items.length)];
}
//select item
_list.eq(index).addClass('selected');
//set next timer (only if still spinning)
if (has_timer && _is_spinning) {
div.oneTime(_current_speed, function() {
plugin.select_random();
});
}
//if no longer spinning, then start the final animation
if (!_is_spinning) {
plugin.blink({
index: index
});
}
}
//*****************
plugin.blink = function(parameters) {
if (!parameters) {parameters = {};}
var index = (parameters['index'] !== undefined) ? parameters['index'] : _selected_index;
var blink_speed = plugin.settings['blink_speed'];
for(var i=0; i<plugin.settings['blink']; i++) {
_list.eq(index).fadeTo(blink_speed, 0.5).fadeTo(blink_speed, 1.0);
}
//wait a second before rotating
div.oneTime((blink_speed * (plugin.settings['blink'] * 2)) + plugin.settings['pause_speed'], function() {
plugin.rotate_to(parameters);
});
}
//*****************
plugin.rotate_to = function(parameters) {
if (!parameters) {parameters = {};}
var index = (parameters['index'] !== undefined) ? parameters['index'] : _selected_index;
//no need to rotate
if (index == 0) {
plugin.on_complete();
return;
}
//chose animation direction
var is_clockwise = (index >= 7) ? true : false;
//number of moves
var count = (is_clockwise) ? (_list_total - index) : index;
div.everyTime(plugin.settings['rotate_speed'], function(current_step) {
if (is_clockwise) {
//clockwise
var temp_html = _list.eq(_list_total - 1).html();
for (var i=_list_total-1; i>0; i--) {
_list.eq(i).html(_list.eq(i - 1).html());
}
_list.eq(0).html(temp_html);
} else {
//counter clockwise
var temp_html = _list.eq(0).html();
for (var i=0; i<_list_total; i++) {
_list.eq(i).html(_list.eq(i + 1).html());
}
_list.eq(_list_total - 1).html(temp_html);
}
//remove selected
_list.removeClass('selected');
//select new item
var new_index = (is_clockwise) ? index + current_step : index - current_step;
if (new_index >= _list_total) {new_index = 0;}
_list.eq(new_index).addClass('selected');
}, count);
//complete
_popup_div.oneTime((count + 1) * plugin.settings['rotate_speed'], function() {
plugin.on_complete();
});
}
//*****************
plugin.fade_hint = function() {
var hint_div = _stop_div.children('.hint');
if (hint_div.length == 0 || hint_div.hasClass('hidden')) {return;}
//stop auto hide
_stop_div.stopTime();
$(hint_div).addClass('hidden').fadeOut(500);
}
//*****************
plugin.on_complete = function() {
_popup_div.show();
//callback
if (typeof plugin.settings['on_complete'] == 'function') {plugin.settings['on_complete']();}
}
//*****************
//events
//auto hide the hint
if (plugin.settings['hint_delay'] > 0) {
_stop_div.oneTime(plugin.settings['hint_delay'], function() {
plugin.fade_hint();
});
}
_stop_div.click(function(e) {
e.preventDefault();
plugin.fade_hint();
plugin.stop();
});
//*****************
//private events
var _update = function() {
}
//autostart
if (plugin.settings['autostart'] === true) {
plugin.start();
}
}
//********************************************
$.spinner.defaults = {
stop_div: '#bell',
label_div: '#game_label',
slow_rate: 1.2,
slow_variance: .2,
speed: 100, //base speed of the spinner
max_speed: 650, //max speed where the spinner stops
blink: 3,
blink_speed: 100,
pause_speed: 500, //amount of pause between blink and rotate animation
rotate_speed: 200,
hint_delay: 5000, //amount of delay before auto hiding the hint box
autostart: true, //flag to tell spinner to start immediately, otherwise, waits for start() call
on_click: function() {}, //called once the bell is clicked
on_complete: function() {} //called once the spinner lands on a space
};
//********************************************
//register the plugin
$.fn.spinner = function(method) {
var options = arguments;
var plugin = $(this).data('spinner');
if (plugin !== undefined && typeof plugin[method] == 'function') {
//method exists
return plugin[method].apply(this, Array.prototype.slice.call(options, 1));
} else if (typeof method === 'object' || !method) {
return this.each(function() {
//create reference to plugin
if ($(this).data('spinner') === undefined) {
$(this).data('spinner', (new $.spinner(this, options[0])));
}
});
} else {
$.error(method + '() does not exist for jQuery.spinner');
}
}
})(jQuery); |
Partager