Finally, this is what I ended up with and I hope it will help considering the time I spent writing THIS fiddle Oo
I attached the click handler on the .slider element to avoid multiple listener.. Here it the handler.
$('.slider').click(function (e) {
var $slider = $(this),
data = $slider.data('slider'),
$slides = $slider.find('.popup'),
$first = $slides.eq(0),
$last = $slides.last(),
$activeItem = $slides.filter('.active'),
$next = $('.popup-nav.next', this),
$prev = $('.popup-nav.prev', this),
$t = $(e.target).closest('.popup, .popup-nav'),
$nextItem = $activeItem.next('.popup'),
$prevItem = $activeItem.prev('.popup'),
way = (
// Is the target be the next item in the DOM or the 'next arrow'
($t.is('.popup-nav.next') || $t.is($nextItem)) ? -1 :
// Is the target be the next item in the DOM or the 'prev arrow'
($t.is('.popup-nav.prev') || $t.is($nextItem)) ? 1 : 0
);
switch (way) {
// previous
case 1:
// if the active slide is the last project
if ($activeItem.is($first)) {
$first.before($last.css({
left: +($first.css('left').replace('px', '')) - data.width
}));
}
$activeItem.removeClass('active').prev().addClass('active');
play.call($slides, {
way: way,
width: data.width
});
break;
// next
case -1:
// if the active slide is the last project
if ($activeItem.is($last)) {
$last.after($first.css({
left: +($last.css('left').replace('px', '')) + data.width
}));
}
$activeItem.removeClass('active').next().addClass('active');
play.call($slides, {
way: way,
width: data.width
});
break;
}
});