var paginator = Class.create({

  _selectors: {
    LIST_ITEM:      ".content_item",
    CONTENT_DATA:   "data-content",
    PN_PLACEHOLDER: ".paginator_placeholder",
    PN_PREV:        ".paginator_placeholder > .prev",
    PN_PAGES:       ".paginator_placeholder > .pages",
    PN_NEXT:        ".paginator_placeholder > .next"
  },

  initialize: function ( container_id, options ) {
    this._container = $( container_id );
    this._options   = options || {};

    this._listItems     = this._container.select( this._selectors.LIST_ITEM );
    this._pnPlaceholder = $$( this._selectors.PN_PLACEHOLDER );
    this._pnPrev        = $$( this._selectors.PN_PREV );
    this._pnPages       = $$( this._selectors.PN_PAGES );
    this._pnNext        = $$( this._selectors.PN_NEXT );
    this._itemsPerPage  = this._options.itemsPerPage || 1;

    this._pageCount     = Math.floor( this._listItems.size() / this._itemsPerPage ) || 1;

    if ( this._listItems.size() % this._itemsPerPage ) {
      this._pageCount += 1;
    }

    this._listItems.sort(
      function ( a, b ) {
        return a.readAttribute( this._selectors.CONTENT_DATA ) - b.readAttribute( this._selectors.CONTENT_DATA );
      }.bind(this)
    ).each(
      function ( elem ) {
        var num = elem.readAttribute( this._selectors.CONTENT_DATA );
        var mod = num % this._itemsPerPage;
        if ( mod == 1 || this._itemsPerPage == 1 ) {
          elem.addClassName( 'first' );
        }
        if ( mod == 0 ) {
          elem.addClassName( 'last' );
        }
      }.bind(this)
    );

    this.setPage( 1 );
  },

  setPage: function ( page ) {
    page = page || 1;
    if ( page > this._pageCount ) {
      page = this._pageCount;
    }
    this._listItems.sort(
      function ( a, b ) {
        return a.readAttribute( this._selectors.CONTENT_DATA ) - b.readAttribute( this._selectors.CONTENT_DATA );
      }.bind(this)
    ).each(
      function ( elem ) {
        var num = elem.readAttribute( this._selectors.CONTENT_DATA );
        if ( ( num > ( ( page - 1 ) * this._itemsPerPage ) ) && num <= ( page * this._itemsPerPage ) ) {
          if ( ! elem.hasClassName( 'visible' ) ) {
            elem.addClassName( 'visible' );
          }
        }
        else {
          if ( elem.hasClassName( 'visible' ) ) {
            elem.removeClassName( 'visible' );
          }
        }
      }.bind(this)
    );

    this.setPaginator( page );
  },

  setPaginator: function ( page ) {
    page = page || 1;

    if ( page == 1 ) {
      this._pnPrev.invoke(
        'update',
        '&nbsp;'
      );
    }
    else {
      this._pnPrev.each(
        function ( prev ) {
          var a = new Element(
            'a',
            { href: 'javascript:void(0);' }
          );
          prev.update( a );
          a.update( '&lt;&lt; zurück' );
          a.observe(
            'click',
            function () {
              this.setPage( page - 1 );
            }.bind( this )
          );
        }.bind(this)
      );
    }

    this._pnPages.invoke(
      'update',
      page + '/' + this._pageCount
    );

    if ( page == this._pageCount ) {
      this._pnNext.invoke(
        'update',
        '&nbsp;'
      );
    }
    else {
      this._pnNext.each(
        function ( next ) {
          var a = new Element(
            'a',
            { href: 'javascript:void(0);' }
          );
          next.update( a );
          a.update( 'weiter &gt;&gt;' );
          a.observe(
            'click',
            function () {
              this.setPage( page + 1 );
            }.bind( this )
          );
        }.bind(this)
      );
    }
  }
});
