(function($) {
  $.fn.multipleElementsCycle = function(l) {
    var m = {
      container: '#cycle',
      prev: '#cycle-prev',
      next: '#cycle-next',
      speed: 500, containerSize: false,
      show: 4, start: false,
      jumpTo: false,
      vertical: false,
      scrollCount: 1,
      element: "li",
      parent: "ul"
    };
    var l = $.extend(m, l);
    return this.each(function() {
      var c = $(this);
      var d = c.find(l.element);
      var f = d.length - 1;
      var g = (l.start === false) ? Math.floor((f - l.show + 1) / 2) : l.start; var h = g + l.show;
      var i = (l.vertical === false) ? d.outerWidth(true) : d.outerHeight(true);
      var j = ((g) * i) * -1;
      if (h >= d.length) $(l.next).hide();
      if (g <= 0) $(l.prev).hide();
      if (l.vertical === false) {
        $(this).find(l.container).css({ width: (l.containerSize) ? l.containerSize : i * l.show, overflow: 'hidden' });
        $(this).find(l.parent).css({ width: (d.length) * i, padding: '0' }); $(l.parent, c).animate({ marginRight: j }, l.speed)
      } else {
        $(this).find(l.container).css({ height: (l.containerSize) ? l.containerSize : i * l.show, overflow: 'hidden' });
        $(this).find(l.parent).css({ height: (d.length) * i, padding: '0' }); $(l.parent, c).animate({ marginTop: j }, l.speed)
      } var k = { next: function() {
        if (h <= f) {
          $(l.prev).show();
          var a = ((h + l.scrollCount) > f) ? d.length - h : l.scrollCount;
          j = j - (i * a); h = h + a; g = g + a;
          if (l.vertical === false) {
            $(l.parent, c).animate({ marginRight: j }, l.speed)
          }
          else {
            $(l.parent, c).animate({ marginTop: j }, l.speed)
          }
          if (h > f) $(l.next).hide()
        }
      }, prev: function() {
        if (g >= 0) {
          $(l.next).show();
          var a = ((g - l.scrollCount) < 0) ? g : l.scrollCount;
          h = h - a; g = g - a; j = j + (i * a);
          if (l.vertical === false) {
            $(l.parent, c).animate({ marginRight: j }, l.speed)
          }
          else {
            $(l.parent, c).animate({ marginTop: j }, l.speed)
          }
          if ((g - 1) < 0) $(l.prev).hide()
        }
      }, toPoint: function(a) {
      var b = h;
      if (a == 0) {
        h = f + 1; g = h - l.show
      }
      else if (a < 0) {
      h = f + parseInt(a);
      g = g + parseInt(a)
    }
    else {
      g = a - 1; h = g + l.show
    }
    j = j + (i * (b - h));
    if (l.vertical === false) {
      $(l.parent, c).animate({ marginRight: j }, l.speed)
    }
    else {
      $(l.parent, c).animate({ marginTop: j }, l.speed)
    }
    if (h >= f) $(l.next).hide();
    else $(l.next).show();
    if (g == 0) $(l.prev).hide();
       else $(l.prev).show() }
      }; $(l.next).live('click', function(e) { k.next(); e.preventDefault() }); $(l.prev).live('click', function(e) { k.prev(); e.preventDefault() }); if (l.jumpTo) { $(l.jumpTo).live('click', function(e) { k.toPoint($(this).data('position')); e.preventDefault() }) }
    })
  }
})(jQuery);
