// ============================================================
// Globale Deklarationen
// ============================================================

// den undefinierten Wert deklarieren
var undefined;


// ============================================================
// Klasse EyecatcherItemList
// ============================================================

// ------------------------------------------------------------
// Konstruktor
// ------------------------------------------------------------

// ---------------------------------------
// EyecatcherItemList(string,string)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//

function EyecatcherItemList() {
  // Attribute
  this.id = undefined;
  this.idCounter = undefined;
  this.itemList = [];
  this.currentStartIndex;
  this.pageSize;
  this.idPrefixGlobalElement;
  this.idPrefixPicture;
  this.idPrefixAuthor;
  this.idPrefixTitle;
  this.idSuffixInnerElement;
  this.idSuffixLinkElement;
  // Initialisierungen
  this.id = EyecatcherItemList._getValideId(this);
  this.pageSize = 5;
  this.currentStartIndex = 0;
}

// ------------------------------------------------------------
// Zugriffsfunktionen
// ------------------------------------------------------------

// -----------
// addEyecatcherItemToItemList(obj)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
EyecatcherItemList.prototype.addEyecatcherItemToItemList = function(obj) {
  if (arguments.length) {
    if (! obj instanceof EyecatcherItem) {
      throw new Error("Argument in 'EyecatcherItemList.prototype.addEyecatcherItemToItemList' ist nicht aus der Klasse 'EyecatcherItem'!");
    }
    this.itemList.push(obj);
  }
  return this.itemList.length;
}


// ------------------------------------------------------------
// Private Instanzmethoden
// ------------------------------------------------------------

// -------------------------------------------
// EyecatcherItemList.prototype._refillPage()
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
EyecatcherItemList.prototype._refillPage = function(currentIndex) {

  var counter = 1;
  var newStartIndex = currentIndex;
  var itemList = [];

  itemList = this.itemList;

  while (counter <= this.pageSize) {

    var element = document.getElementById(this.idPrefixGlobalElement + counter);
    var pictureElement = document.getElementById(this.idPrefixPicture + counter);
    var pictureElementInner = document.getElementById(this.idPrefixPicture + this.idSuffixInnerElement + counter);
    var pictureElementLink = document.getElementById(this.idPrefixPicture + this.idSuffixLinkElement + counter);
    var authorElement = document.getElementById(this.idPrefixAuthor + counter);
    var authorElementInner = document.getElementById(this.idPrefixAuthor + this.idSuffixInnerElement + counter);
    var titleElement = document.getElementById(this.idPrefixTitle + counter);
    var titleElementInner = document.getElementById(this.idPrefixTitle + this.idSuffixInnerElement + counter);

    if (itemList[currentIndex]) {

      element.style.display = 'block';

      if (itemList[currentIndex].picture) {
        pictureElementInner.style.display = 'inline';
        pictureElementInner.src = itemList[currentIndex].picture;
        pictureElementLink.href = itemList[currentIndex].link;
      } else {
        pictureElementInner.style.display = 'none';
      }

      if (itemList[currentIndex].author) {
        authorElement.style.display = 'block';
        authorElementInner.innertHTML = itemList[currentIndex].author;
      } else {
        authorElement.style.display = 'none';
      }

      if (itemList[currentIndex].title) {
        titleElement.style.display = 'block';
        titleElementInner.href = itemList[currentIndex].link;
        titleElementInner.innerHTML = itemList[currentIndex].title;
      } else {
        titleElement.style.display = 'none';
      }

    } else {

      element.style.display = 'none';

    }

    counter += 1;
    currentIndex += 1;

  }

  this.currentStartIndex = newStartIndex;

}


// ------------------------------------------------------------
// Öffentliche Instanzmethoden
// ------------------------------------------------------------

// -------------------------------------------
// EyecatcherItemList.prototype.setCounter()
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
EyecatcherItemList.prototype.setCounter = function() {

  var element = document.getElementById(this.idCounter);
  var string = '';
  var startIndex = this.currentStartIndex + 1;
  var endIndex = this.currentStartIndex + this.pageSize;

  if (!element) {
    return false;
  }

  if (endIndex > this.itemList.length) {
    endIndex =  this.itemList.length;
  }

  if (startIndex <= 0) {
    start =  1;
  }

  string += startIndex + ' bis ' + endIndex + ' von ' + this.itemList.length;

  element.innerHTML = string;

  return string;

}

// -------------------------------------------
// EyecatcherItemList.prototype.nextPage()
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
EyecatcherItemList.prototype.previousPage = function() {

  var currentIndex = this.currentStartIndex - this.pageSize;

  if (currentIndex >= 0) {
    this._refillPage(currentIndex);
    this.setCounter();
  }

}

// -------------------------------------------
// EyecatcherItemList.prototype.nextPage()
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
EyecatcherItemList.prototype.nextPage = function() {

  var currentIndex = this.currentStartIndex + this.pageSize;

  if (currentIndex < this.itemList.length) {
    this._refillPage(currentIndex);
    this.setCounter();
  }

}


// ------------------------------------------------------------
// Private Klasseneigenschaften
// ------------------------------------------------------------

EyecatcherItemList._idCounter = 0;
EyecatcherItemList._elementsList = [];


// ------------------------------------------------------------
// Öffentliche Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// EyecatcherItemList.getListByID(string)
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
EyecatcherItemList.getListByID = function(id) {

  for (var i=0; i<EyecatcherItemList._elementsList.length; i++) {

    if (EyecatcherItemList._elementsList[i].id == id) {
      return EyecatcherItemList._elementsList[i];
    }

  }

  return undefined;

}


// ------------------------------------------------------------
// Private Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// EyecatcherItemList._getValideId()
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
EyecatcherItemList._getValideId = function(obj) {

  var newID = EyecatcherItemList._idCounter + 1;

  EyecatcherItemList._idCounter = newID;
  EyecatcherItemList._elementsList.push(obj);

  return newID;

}


// ------------------------------------------------------------
// toString()
// ------------------------------------------------------------

EyecatcherItemList.prototype.toString = function() {

  var string = '';

  string += 'ID: ' + this.id + '\n';
  string += 'ItemList:\n';
  for (var i=0; i<this.itemList.length; i++) {
    string += this.itemList[i];
    string += '\n';
  }

  return string;

  // zunaechst an Methode der Basisklasse weiterleiten
  // return Object.prototype.toString.apply(this);
}




// ============================================================
// Klasse EyecatcherItem
// ============================================================

// ------------------------------------------------------------
// Konstruktor
// ------------------------------------------------------------

// ---------------------------------------
// EyecatcherItem(string,string)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//

function EyecatcherItem(picture,author,link,title) {
  // Attribute
  this.picture = undefined;
  this.author = undefined;
  this.link = undefined;
  this.title = undefined;
  // Initialisierungen
  this.picture = picture;
  this.author = author;
  this.link = link;
  this.title = title;
}


// ------------------------------------------------------------
// toString()
// ------------------------------------------------------------

EyecatcherItem.prototype.toString = function() {

  var string = '';

  string += '  Picture: ' + this.picture + '\n';
  string += '  Author: ' + this.author + '\n';
  string += '  Link: ' + this.link + '\n';
  string += '  Title: ' + this.title + '\n';

  return string;

  // zunaechst an Methode der Basisklasse weiterleiten
  // return Object.prototype.toString.apply(this);
}
