// ============================================================
// Globale Deklarationen
// ============================================================

// den undefinierten Wert deklarieren
var undefined;


// ============================================================
// Klasse Gallery
// ============================================================

// ------------------------------------------------------------
// Konstruktor
// ------------------------------------------------------------

// ---------------------------------------
// Gallery()
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
function Gallery() {
  // Attribute
  this.id = undefined;
  this.title = undefined;
  this.description = undefined;
  this.itemList = [];
  this.displayThumbLength = undefined;
  this.displayThumbFirstItem = undefined;
  this.displayThumbLinkIDPrefix = undefined;
  this.displayThumbPictureIDPrefix = undefined;
  this.displayDetailPictureID = undefined;
  this.displayDetailCurrentItem = undefined;
  this.displayDetailDescriptionID = undefined;
  this.popupWindowName = undefined;
  this.popupPreferences = undefined;
  this._popup = undefined;
  // Initialisierungen
  Gallery._registerGallery(this);
  this.setDisplayThumbLength(5);
  this.setDisplayThumbFirstItem(0);
  this.setDisplayThumbLinkIDPrefix('default-thumb-');
  this.setDisplayThumbPictureIDPrefix('default-detail-');
  this.setPopupWindowName('default');
}

// ------------------------------------------------------------
// Zugriffsfunktionen
// ------------------------------------------------------------

// -----------
// setID(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setID = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("Gallery.prototype.setID: Argument ist nicht vom Typ number!");
    }
    this.id = num;
  }
  return this.id;
}

// -----------
// title(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setTitle = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.title: Argument ist nicht vom Typ string!");
    }
    this.title = str;
  }
  return this.title;
}

// -----------
// setDescription(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDescription = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.setDescription: Argument ist nicht vom Typ string!");
    }
    this.description = str;
  }
  return this.description;
}

// ------------------
// addItemList(object)
// ------------------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.addItemList = function(obj) {
  if (arguments.length) {
    if (! obj instanceof GalleryItem) {
      throw new Error("Gallery.prototype.addItemList: Argument ist nicht Instanz von GalleryItem!");
    }
    this.itemList.push(obj);
  }
  return this.itemList;
}

// -----------
// setDisplayThumbLength(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDisplayThumbLength = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("Gallery.prototype.setDisplayThumbLength: Argument ist nicht vom Typ number!");
    }
    this.displayThumbLength = num;
  }
  return this.displayThumbLength;
}

// -----------
// setDisplayThumbFirstItem(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDisplayThumbFirstItem = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("Gallery.prototype.setDisplayThumbFirstItem: Argument ist nicht vom Typ number!");
    }
    this.displayThumbFirstItem = num;
  }
  return this.displayThumbFirstItem;
}

// -----------
// setDisplayThumbLinkIDPrefix(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDisplayThumbLinkIDPrefix = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.setDisplayThumbLinkIDPrefix: Argument ist nicht vom Typ string!");
    }
    this.displayThumbLinkIDPrefix = str;
  }
  return this.displayThumbLinkIDPrefix;
}

// -----------
// setDisplayThumbPictureIDPrefix(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDisplayThumbPictureIDPrefix = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.setDisplayThumbPictureIDPrefix: Argument ist nicht vom Typ string!");
    }
    this.displayThumbPictureIDPrefix = str;
  }
  return this.displayThumbPictureIDPrefix;
}

// -----------
// setDisplayDetailPictureID(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDisplayDetailPictureID = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.setDisplayDetailPictureID: Argument ist nicht vom Typ string!");
    }
    this.displayDetailPictureID = str;
  }
  return this.displayDetailPictureID;
}

// -----------
// setDisplayDetailCurrentItem(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDisplayDetailCurrentItem = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("Gallery.prototype.setDisplayDetailCurrentItem: Argument ist nicht vom Typ number!");
    }
    this.displayDetailCurrentItem = num;
  }
  return this.displayDetailCurrentItem;
}

// -----------
// setDisplayDetailDescriptionID(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setDisplayDetailDescriptionID = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.setDisplayDetailDescriptionID: Argument ist nicht vom Typ string!");
    }
    this.displayDetailDescriptionID = str;
  }
  return this.displayDetailDescriptionID;
}

// -----------
// setPopupWindowName(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setPopupWindowName = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.setPopupWindowName: Argument ist nicht vom Typ string!");
    }
    this.popupWindowName = str;
  }
  return this.popupWindowName;
}

// -----------
// setPopupPreferences(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
Gallery.prototype.setPopupPreferences = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("Gallery.prototype.setPopupPreferences: Argument ist nicht vom Typ string!");
    }
    this.popupPreferences = str;
  }
  return this.popupPreferences;
}

// ------------------------------------------------------------
// Öffentliche Instanzmethoden
// ------------------------------------------------------------

// ---------------------------------------
// getGalleryThumbHTML()
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype.getGalleryThumbHTML = function() {
  // Im Quelltext direkt erstellen
}

// ---------------------------------------
// getGalleryDetailHTML()
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype.getGalleryDetailHTML = function() {
  // Im Quelltext direkt erstellen
}

// ---------------------------------------
// drawGalleryThumb(obj)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype.drawGalleryThumb = function(windowObj) {

  if (!arguments.length) {
    document.write(this.getGalleryThumbHTML());
  } else {
    windowObj.document.write(this.getGalleryThumbHTML());
  }

}

// ---------------------------------------
// drawGalleryDetail(obj)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype.drawGalleryDetail = function(windowObj) {

  if (!arguments.length) {
    document.write(this.getGalleryDetailHTML());
  } else {
    windowObj.document.write(this.getGalleryDetailHTML());
  }

}

// ---------------------------------------
// displayThumbMove(string)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype.displayThumbMove = function(direction) {

  var newDisplayThumbFirstItem;
  var linkElement;
  var linkElementValue;
  var regExp = /^(.+pictureid\=)(\d+)(.*)$/;
  var regExpReplace;
  var pictureElement;
  var currentItem;

  if ((direction == 'backward' && this.displayThumbFirstItem > 0) || (direction == 'forward' && (this.displayThumbLength + this.displayThumbFirstItem < this.itemList.length))) {

    if (direction == 'backward') {
      newDisplayThumbFirstItem = this.displayThumbFirstItem - 1;
    } else {
      newDisplayThumbFirstItem = this.displayThumbFirstItem + 1;
    }

    for (var i=0; i<this.displayThumbLength; i++) {

      currentItemID = newDisplayThumbFirstItem + i;
      linkElement = document.getElementById(this.displayThumbLinkIDPrefix + this.id + i);
      pictureElement = document.getElementById(this.displayThumbPictureIDPrefix + this.id + i);

      if (linkElement) {
        linkElementValue = linkElement.href;
        regExpReplace = '$1' + currentItemID + '$3';
        linkElement.href = linkElementValue.replace(regExp,regExpReplace);
      }

      if (pictureElement) {
        pictureElement.src = this.itemList[currentItemID].sourcePathThumb;
      }

    }

    this.displayThumbFirstItem = newDisplayThumbFirstItem;

  }

}

// ---------------------------------------
// displayDetailMove(string,object)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype.displayDetailMove = function(direction,windowObj) {

  var currentCounter;
  var element;
  var description;

  if (!windowObj) {
    element = document.getElementById(this.displayDetailPictureID);
    description = document.getElementById(this.displayDetailDescriptionID);
  } else {
    element = windowObj.document.getElementById(this.displayDetailPictureID);
    description = windowObj.document.getElementById(this.displayDetailDescriptionID);
  }

  if (element) {

    if (direction == 'backward') {

      currentCounter = this.displayDetailCurrentItem - 1;

      if (currentCounter < 0) {
        currentCounter = this.itemList.length - 1;
      }

    } else {

      currentCounter = this.displayDetailCurrentItem + 1;

      if (currentCounter >= this.itemList.length) {
        currentCounter = 0;
      }

    }

    element.src = this.itemList[currentCounter].sourcePathDetail;

    this.displayDetailCurrentItem = currentCounter;

  }

  if (description && this.itemList[currentCounter].description) {
    description.innerHTML = this.itemList[currentCounter].description;
  }

}

// ---------------------------------------
// openPopup(string)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype.openPopup = function(url) {

  var popup;

  popup = window.open(url,this.popupWindowName,this.popupPreferences);
  popup.focus();

  this._popup = popup;

}

// ------------------------------------------------------------
// Private Instanzmethoden
// ------------------------------------------------------------

// ----------------------------------------
// _xxx(VariablenTypXxx1, VariablenTypXxx2)
// ----------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.prototype._xxx = function(VariablenNameXxx1, VariablenNameXxx2) {
  // ...
}


// ------------------------------------------------------------
// Öffentliche Klasseneigenschaften
// ------------------------------------------------------------

Gallery.elementList = [];


// ------------------------------------------------------------
// Private Klasseneigenschaften
// ------------------------------------------------------------

Gallery._xxx = undefined;


// ------------------------------------------------------------
// Öffentliche Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// Gallery.xxx(VariablenTypXxx1, VariablenTypXxx2)
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery.xxx = function(variablenNameXxx1, variablenNameXxx2) {

}


// ------------------------------------------------------------
// Private Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// GalleryItem._registerGallery(obj)
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
Gallery._registerGallery = function(obj) {

  var newID = Gallery.elementList.length;

  obj.setID(newID);
  Gallery.elementList[Gallery.elementList.length] = obj;

  return newID;

}


// ------------------------------------------------------------
// toString()
// ------------------------------------------------------------

Gallery.prototype.toString = function() {

  var string = '';

  string += 'Title: ' + this.title + '\n';
  string += 'Description: ' + this.description + '\n';
  string += 'ItemList:\n';
  string += '\n';

  for (var i=0; i<this.itemList.length; i++) {
    string += this.itemList[i];
    string += '\n';
  }

  return string;

}


// -----------------------------------------------------------------------
// -----------------------------------------------------------------------


// ============================================================
// Klasse GalleryItem
// ============================================================

// ------------------------------------------------------------
// Konstruktor
// ------------------------------------------------------------

// ---------------------------------------
// GalleryItem(VariablenTypXxx1, VariablenTypXxx2)
// ---------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
function GalleryItem(title) {
  // Attribute
  this.id = undefined;
  this.title = undefined;
  this.description = undefined;
  this.sourcePathThumb = undefined;
  this.sourcePathDetail = undefined;
  this.thumb = new Image();
  this.detail = new Image();
  this.thumbWidth = undefined;
  this.thumbHeight = undefined;
  this.detailWidth = undefined;
  this.detailHeight = undefined;
  // Initialisierungen
  this.id = GalleryItem._getValidID();
}

// ------------------------------------------------------------
// Zugriffsfunktionen
// ------------------------------------------------------------

// -----------
// setTitle(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setTitle = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("GalleryItem.prototype.setTitle: Argument ist nicht vom Typ string!");
    }
    this.title = str;
  }
  return this.title;
}

// -----------
// setDescription(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setDescription = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("GalleryItem.prototype.setDescription: Argument ist nicht vom Typ string!");
    }
    this.description = str;
  }
  return this.description;
}

// -----------
// setSourcePathThumb(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setSourcePathThumb = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("GalleryItem.prototype.setSourcePathThumb: Argument ist nicht vom Typ string!");
    }
    this.sourcePathThumb = str;
  }
  return this.sourcePathThumb;
}

// -----------
// setSourcePathDetail(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setSourcePathDetail = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("GalleryItem.prototype.setSourcePathDetail: Argument ist nicht vom Typ string!");
    }
    this.sourcePathDetail = str;
  }
  return this.sourcePathDetail;
}

// -----------
// setThumb(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setThumb = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("GalleryItem.prototype.setThumb: Argument ist nicht vom Typ string!");
    }
    this.thumb.src = str;
  }
  return this.thumb;
}

// -----------
// setDetail(string)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setDetail = function(str) {
  if (arguments.length) {
    if (typeof str != "string") {
      throw new Error("GalleryItem.prototype.setDetail: Argument ist nicht vom Typ string!");
    }
    this.detail.src = str;
  }
  return this.detail;
}

// -----------
// setThumbWidth(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setThumbWidth = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("GalleryItem.prototype.setThumbWidth: Argument ist nicht vom Typ number!");
    }
    this.thumbWidth = num;
  }
  return this.thumbWidth;
}

// -----------
// setThumbHeight(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setThumbHeight = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("GalleryItem.prototype.setThumbHeight: Argument ist nicht vom Typ number!");
    }
    this.thumbHeight = num;
  }
  return this.thumbHeight;
}

// -----------
// setDetailWidth(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setDetailWidth = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("GalleryItem.prototype.setDetailWidth: Argument ist nicht vom Typ number!");
    }
    this.detailWidth = num;
  }
  return this.detailWidth;
}

// -----------
// setDetailHeight(number)
// -----------
//
// Beschreibung:
// -------------
//   ...
//
GalleryItem.prototype.setDetailHeight = function(num) {
  if (arguments.length) {
    if (typeof num != "number") {
      throw new Error("GalleryItem.prototype.setDetailHeight: Argument ist nicht vom Typ number!");
    }
    this.detailHeight = num;
  }
  return this.detailHeight;
}

// ------------------------------------------------------------
// Öffentliche Instanzmethoden
// ------------------------------------------------------------

// ---------------------------------------
// loadThumb()
// ---------------------------------------
//
// Beschreibung:
// -------------
//   Laedt das Thumbnail-Bild in den Cache
//
// Beispiel:
// ---------
//   ...
//
GalleryItem.prototype.loadThumb = function() {

  if (this.sourcePathThumb) {
    this.thumb.src = this.sourcePathThumb;
  }

}

// ------------------------------------------------------------
// Private Instanzmethoden
// ------------------------------------------------------------

// ----------------------------------------
// _xxx(VariablenTypXxx1, VariablenTypXxx2)
// ----------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
GalleryItem.prototype._xxx = function(VariablenNameXxx1, VariablenNameXxx2) {
  // ...
}


// ------------------------------------------------------------
// Öffentliche Klasseneigenschaften
// ------------------------------------------------------------

GalleryItem.xxx = undefined;


// ------------------------------------------------------------
// Private Klasseneigenschaften
// ------------------------------------------------------------

GalleryItem._idList = [];


// ------------------------------------------------------------
// Öffentliche Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// GalleryItem.xxx(VariablenTypXxx1, VariablenTypXxx2)
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
GalleryItem.xxx = function(variablenNameXxx1, variablenNameXxx2) {

}


// ------------------------------------------------------------
// Private Klassenmethoden
// ------------------------------------------------------------

// -------------------------------------------
// GalleryItem._getValidID()
// -------------------------------------------
//
// Beschreibung:
// -------------
//   ...
//
// Beispiel:
// ---------
//   ...
//
GalleryItem._getValidID = function() {

  var newID = GalleryItem._idList.length;

  GalleryItem._idList[GalleryItem._idList.length] = newID;

  return newID;

}


// ------------------------------------------------------------
// toString()
// ------------------------------------------------------------

GalleryItem.prototype.toString = function() {

  var string = '';

  string += 'Title: ' + this.title + '\n';
  string += 'Description: ' + this.description + '\n';
  string += 'SourcePathThumb: ' + this.sourcePathThumb + '\n';
  string += 'SourcePathDetail: ' + this.sourcePathDetail + '\n';
  string += 'ThumbWidth: ' + this.thumbWidth + '\n';
  string += 'ThumbHeight: ' + this.thumbHeight + '\n';
  string += 'DetailWidth: ' + this.detailWidth + '\n';
  string += 'DetailHeight: ' + this.detailHeight + '\n';

  return string;

}
