(function($) {
  $.fn.rotator = function(options) {
    return this.each(function() {

      function resetControls() {
        rotator.find('.next').css({visibility: (pos < items.length-1) ? 'visible' : 'hidden'});
        rotator.find('.prev').css({visibility: (pos > 0) ? 'visible' : 'hidden'});
      }
      
      function move(movement) {
        npos = pos + movement;
        if (npos >= 0 && npos < items.length)
          pos = npos;
        else
          return;

        l = items.width() * (-pos);
        inner.animate({marginLeft: l}); 
        resetControls();
      }


      var rotator = $(this);
      var items = rotator.find('.items').children(); 
      var pos = 0;

      items.wrapAll('<div class="inner" />');
      var inner = rotator.find('.inner');
      inner.css({width: items.width() * items.length});

      resetControls();

      rotator.find('.next').click(function() { move(1); });
      rotator.find('.prev').click(function() { move(-1); });
    });
  };
  
})(jQuery);

$(document).ready(function() {
  $('#item-gallery').rotator();
  $('#nav a[href=#kontakt]').click(function() {
    $('html, body').animate({ scrollTop: $(document).height() - $(window).height() }, 1000);
    $('#contact').animate({ backgroundColor: '#ffff99' }, 1200).animate({ backgroundColor: '#fcfcfc' }, 1000);
    return false;
  });
});

