On gists
Trolling literal object
JavaScript-OOP
JavaScript
trolling.js
Raw
#
$(function () {
var Trolling = {
displayRandom: 210,
carouselDelay: 2000,
Trolling: $('.face'),
input: $('#searchQuery'),
init: function () {
this.Trolling.hide();
this.input.on('keyup', $.proxy(this.search, this));
this.input.focus();
this.createIndex();
this.carouselPlay();
},
index: {},
createIndex: function () {
var me = this;
this.Trolling.each(function () {
me.index[$.trim($(this).text()).toLowerCase()] = $(this);
});
},
search: function () {
var searching = $.trim(this.input.val()).toLowerCase();
if (searching == "") {
this.Trolling.hide();
this.carouselPlay();
} else {
this.carouselStop();
$.each(this.index, function (key, val) {
if (key.indexOf(searching) != -1) {
val.show();
} else {
val.hide();
}
});
}
},
carouselTimer: null,
carouselPlay: function () {
if (this.carouselTimer) {
this.carouselStop();
}
this.carouselTimer = setInterval($.proxy(this.carousel, this), this.carouselDelay);
this.randomTrolling(this.displayRandom).show();
},
carouselStop: function () {
clearInterval(this.carouselTimer);
this.carouselTimer = false;
this.Trolling.stop();
this.Trolling.css({opacity:1});
this.Trolling.hide();
},
carousel: function () {
var hidden = this.randomHidden();
var visible = this.randomVisible();
visible.before(hidden);
visible.fadeOut('slow', $.proxy(function () {
hidden.fadeIn('slow');
}, this));
},
randomVisible: function (){
return this.randomTrolling(1, this.Trolling.find(':visible')).closest('.face');
},
randomHidden: function (){
return this.randomTrolling(1, this.Trolling.find(':hidden')).closest('.face');
},
randomTrolling: function (num, Trolling) {
if (typeof num === 'undefined') {
num = 1;
}
if (typeof Trolling === 'undefined') {
Trolling = this.Trolling;
}
var max = Trolling.length - num;
var start = Math.floor(Math.random() * max);
return Trolling.slice(start, num + start);
}
};
Trolling.init();
});