'use strict';

((function($) {

function hopIn($hasBg, opts) {
  var bpX = $hasBg.data('bpX');
  var bpY = $hasBg.data('bpY');
  function right() { $hasBg.css({backgroundPosition: bpX+3+'px '+bpY+'px'}); }
  function left() { $hasBg.css({backgroundPosition: bpX+'px '+bpY+'px'}); }
  right();
  window.setTimeout(function() {
    left(); window.setTimeout(function() {
      right(); window.setTimeout(function() {
        left(); window.setTimeout(function() {
          right(); }, 100); }, 100); }, 100); }, 100);
}
function hopOut($hasBg, opts) {
  var bpX = $hasBg.data('bpX');
  var bpY = $hasBg.data('bpY');
  $hasBg.css({backgroundPosition: bpX+'px '+bpY+'px'});
}

$.fn.hop = function(options) {
  var opts = $.extend({}, $.fn.hop.defaults, options||{});
  return this.each(function () {
    var $this = $(this);
    var pos = $this.css('background-position').replace(/px/g, '').split(' '); // e.g. ['0', '-100']
    $this.data('bpX', 1*pos[0]).data('bpY', 1*pos[1]);
    // mouseover & mouseout if we want the extra calls
    $this.mouseenter(function() { hopIn($this, opts); }).mouseleave(function() { hopOut($this, opts); });
  });
};

$.fn.hop.defaults = {};


})(jQuery));

((function($) {
  $(function() { $('.hop').hop(); });
  $(function() { $('a.openNewWin').attr('target','_blank'); }); // open external links in new window
})(jQuery));
