(function($) {
  $.fn.FlickrScrollr = function(options)
  {
    var defaults = {
      thumbWidth : '241',
      totalPics : '20',
      displayNum : '2'
    }
    var opts = $.extend(defaults, options);

    return this.each(function() {
      $(this)
        .prepend('<a id="prev" href="#">Prev</a>')
        .append('<a id="next" href="#">Next</a>')
        .children('#fs_wrapper')
          .animate({
            'width' : opts.displayNum*opts.thumbWidth+1+'px'
          })
          .children('ul')
            .css('width', opts.totalPics+opts.thumbWidth+1+'px');
      $('#prev').css('opacity', '0').click(function(){
        var oldLeft = parseInt($('#fs_wrapper ul').css('left'));
        var newLeft = oldLeft + 241*opts.displayNum;
        if(newLeft <= 0) {
          $('#fs_wrapper ul').animate({
            'left' : newLeft+'px'
          });
          if(newLeft == 0) {
            $('#prev').animate({
              'opacity' : '0'
            });
            $('#next').animate({
              'opacity' : '1'
            });
          } else {
            $('#prev').animate({
              'opacity' : '1'
            });
            $('#next').animate({
              'opacity' : '1'
            });
          }
        }
      });
      $('#next').click(function(){
        var oldLeft = parseInt($('#fs_wrapper ul').css('left'));
        var newLeft = oldLeft - 241*opts.displayNum;
        var minLeft = (opts.totalPics-opts.displayNum)*241*-1;
        if(newLeft >= minLeft) {
          $('#fs_wrapper ul').animate({
            'left' : newLeft+'px'
          });
          if(newLeft == minLeft) {
            $('#next').animate({
              'opacity' : '0'
            });
            $('#prev').animate({
              'opacity' : '1'
            });
          } else {
            $('#next').animate({
              'opacity' : '1'
            });
            $('#prev').animate({
              'opacity' : '1'
            });
          }
        }
      });
    });
  };
})(jQuery);