
var undefined;

//##########################################################
//##########################################################
//  Galery
//  Author: df
//  23.01.2009
//##########################################################
//##########################################################

function Galery(id) {
  this.id = undefined;
  this.imageMap = [];     
  this.startIdx = 0;
  this.listDrawElement = undefined; 
  this.curtainElement = undefined; 
  this.detailDrawElement = undefined;
  this._allowed = true;
  
  this._setID(id);
}

//##########################################################

Galery.prototype._setID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Galery->_setID: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Galery->_setID: Argument str ist nicht vom Typ String!");
  }
  this.id = str;
}   

//##########################################################

Galery.prototype.addImageMap = function (obj) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Galery->addImageMap: Falsche Anzahl von Argumenten!");
  }
  if (! (obj instanceof GaleryImage)) {
    focus();
    throw new Error('Galery->addImageMap: Argument obj ist keine Instance von GaleryImage!');
  }
  this.imageMap.push(obj);
} 

//##########################################################

Galery.prototype.setListDrawElement = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Galery->setListDrawElement: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Galery->setListDrawElement: Argument str ist nicht vom Typ String!");
  }
  var elem = document.getElementById(str);
  if (! elem) {
    focus();
    throw new Error("Galery->setListDrawElement: Es existiert kein HTML-Element mit id="+ str +"!");
  }
  this.listDrawElement = elem;
}       

//##########################################################

Galery.prototype.setDetailDrawElement = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Galery->setDetailDrawElement: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Galery->setDetailDrawElement: Argument str ist nicht vom Typ String!");
  }
  var elem = document.getElementById(str);
  if (! elem) {
    focus();
    throw new Error("Galery->setDetailDrawElement: Es existiert kein HTML-Element mit id="+ str +"!");
  }
  this.detailDrawElement = elem;
}               

//##########################################################

Galery.prototype.setCurtainElement = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("Galery->setCurtainElement: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("Galery->setCurtainElement: Argument str ist nicht vom Typ String!");
  }
  var elem = document.getElementById(str);
  if (! elem) {
    focus();
    throw new Error("Galery->setCurtainElement: Es existiert kein HTML-Element mit id="+ str +"!");
  }
  this.curtainElement = elem;
}             

//##########################################################

Galery.prototype.drawList = function () {
  if (! this.listDrawElement) {
    focus();
    throw new Error("Galery->drawList: Es wurd kein Element zum Zeichnen gesetzt!");
  }
  var html = '';
  for (var i=0; i<this.imageMap.length; i++) {
    var img = this.imageMap[i];
    if (! img.imgPreview) {
      continue;
    }
    var src = img.imgPreview.src;
    html += '<a href="javascript:void(0);" onclick="Galery.getInstance(\''+ this.id +'\').showDetail(\''+ i +'\');"><img src="'+ src+'" alt="Bild: '+ i + ' : ' + img.title +'" title="'+ img.title +'" class="galery-list-image" /></a>\n';
  } 
  this.listDrawElement.innerHTML = html;
}                   

//##########################################################

Galery.prototype.showDetail = function (idx) {
  if (! this._allowed) {
    return;
  }
  if (! this.detailDrawElement) {
    focus();
    throw new Error("Galery->showDetail: Es wurd kein Element zum Zeichnen gesetzt!");
  }
  if (this.curtainElement) {
    this.curtainElement.style.display = 'block';
  }
  if (! this.imageMap.length) {
    return;
  }
  var img = this.imageMap[idx];
  this.startIdx = idx;
  if (! img) {
    img = this.imageMap[0];
    this.startIdx = 0;
  }
  var html = '';  

html += '<div id="galery-detail-inner" class="galery-detail-navigation">';
html += '<table border="0" cellpadding="0" cellspacing="0" class="galery-detail-navigation">';
html += '<tr>';
html += '<td class="galery-detail-navigation-backw">';
html += '<a href="javascript:void(0);" onclick="Galery.getInstance(\''+ this.id +'\').back();" class="galery-detail-navigation-backw"><img src="/kiosk/grafik/trans.gif" alt="Button: zurück" title="zurück" /></a>';
html += '</td>';
html += '<td class="galery-detail-navigation-info">';
html += '<h5 id="__Galery_Detail_Info__">'+ this.imageMap.length +' Bilder</h5>';
html += '<div class="galery-detail-image">';
html += '<img id="__Galery_Detail_Image__" src="'+ img.imgStandard.src +'" class="galery-detail-image" alt="Bild: '+ (this.startIdx+1) + ' : ' + img.title +'" title="' + img.title +'" />';
html += '</div>';
html += '<div class="galery-detail-details">';
var title = '';
if (img.title && img.title.length) {
  title = img.title;
}                                                             
var description = '';
if (img.description && img.description.length) {
  description = img.description;
}
html += '<div id="__Galery_Detail_Details__" class="galery-detail-text">';  
html += '<h6 id="__Galery_Detail_Title__">'+ title +'</h6>'; 
html += '<p id="__Galery_Detail_Description__">' + description + '</p>';  
html += '</div>';  
html += '</div>';
html += '</td>';
html += '<td class="galery-detail-navigation-forw">';
html += '<a href="javascript:void(0);" onclick="Galery.getInstance(\''+ this.id +'\').next();" class="galery-detail-navigation-forw"><img src="/kiosk/grafik/trans.gif" alt="Button: weiter" title="weiter" /></a>';
html += '</td>';
html += '</tr>';
html += '<tr>';
html += '<td class="galery-detail-navigation-backw">';
html += '<br />';
html += '</td>';
html += '<td class="galery-detail-navigation-close-info">';
html += '<div class="galery-detail-settings">';
html += '<a href="javascript:void(0);" onclick="Galery.getInstance(\''+ this.id +'\').hideDetail();">Fenster schließen</a>';
html += '</div>';
html += '</td>';
html += '<td class="galery-detail-navigation-close">';
html += '<a href="javascript:void(0);" onclick="Galery.getInstance(\''+ this.id +'\').hideDetail();" class="galery-detail-navigation-close"><img src="/kiosk/grafik/trans.gif" alt="Button: Fenster schließen" title="Fenster schließen" /></a>';
html += '</td>';
html += '</tr>';
html += '</table>';
html += '</div>';

  var instance = Galery.getInstance(this.id);
  var id = this.id;
  var resizeFunc = function() {
      Galery.getInstance(id).positioningDetail();
  }
  var loadFunc = function () {
    instance.detailDrawElement.innerHTML = html;
    instance.detailDrawElement.style.width = img.imgStandard.width + 'px';
    instance.positioningDetail();
    instance.detailDrawElement.style.display = 'block';
    this._allowed = true;
    Galery.addEvent('resize', window, resizeFunc);
  }
  if (! img.imgStandard.complete) {
    img.imgStandard.onload = loadFunc;
    return;
  }
  loadFunc();
}                

//##########################################################

Galery.prototype.hideDetail = function (idx) {
  if (! this.detailDrawElement) {
    focus();
    throw new Error("Galery->showDetail: Es wurd kein Element zum Zeichnen gesetzt!");
  }
  if (this.curtainElement) {
    this.curtainElement.style.display = 'none';
  }
  var html = '';
  this.detailDrawElement.style.display = 'none';
  this.detailDrawElement.innerHTML = html;
  this._allowed = true;
}                          

//##########################################################

Galery.prototype.next = function () {
  if (! this._allowed) {
    return;
  }
  this.startIdx++;
  if (this.startIdx >= this.imageMap.length) {
    this.startIdx = 0;
  }
  this.update();
}                             

//##########################################################

Galery.prototype.back = function () {
  if (! this._allowed) {
    return;
  }
  this.startIdx--;
  if (this.startIdx < 0) {
    this.startIdx = (this.imageMap.length - 1);
  }
  this.update();
}                             

//##########################################################

Galery.prototype.update = function () {
  this._allowed = false;
  var img = this.imageMap[this.startIdx];
  if (! img) {
    this._allowed = true;
    return;
  }
  var info = document.getElementById('__Galery_Detail_Info__');
  var title = document.getElementById('__Galery_Detail_Title__');  
  var description = document.getElementById('__Galery_Detail_Description__');
  var image = document.getElementById('__Galery_Detail_Image__');
  var instance = Galery.getInstance(this.id);
  var loadFunc = function() {
    var titleTxt = '';
    if (img.title && img.title.length) {
      titleTxt = img.title;
    }                                                             
    var descriptionTxt = '';
    if (img.description && img.description.length) {
      descriptionTxt = img.description;
    }
    title.innerHTML = titleTxt;    
    description.innerHTML = descriptionTxt;
    image.src = img.imgStandard.src;
    image.title = img.title;
    image.alt = 'Bild ' + (this.startIdx + 1) + ': ' + img.title;
    instance.detailDrawElement.style.width = img.imgStandard.width + 'px';
    instance.positioningDetail();
    instance._allowed = true;
  }
  if (! img.imgStandard.complete) {
    img.imgStandard.onload = loadFunc;
    return;
  }
  loadFunc();
}                                  

//##########################################################

Galery.prototype.positioningDetail = function (idx) {
  if (! this.detailDrawElement) {
    return;
  }
  var img = this.imageMap[this.startIdx];
  if (! img) {
    return;
  }
  var w = img.imgStandard.width + 24;
  var h1 = img.imgStandard.height;   
  var h2 = 0;
  if (document.getElementById('__Galery_Detail_Details__') && document.getElementById('__Galery_Detail_Details__').offsetHeight){
    h2 = document.getElementById('__Galery_Detail_Details__').offsetHeight;
  }
  var h = parseFloat(h1) + parseFloat(h2);
  if (typeof w != 'number') {
    return;
  }
  if (typeof h != 'number') {
    return;
  }
  var screenW = Galery.getScreenWidth();
  var screenH = Galery.getScreenHeight();
  screenW /= 2;
  screenH /= 2;
  var left = screenW - (w / 2)  - 24;
  var top = (screenH - (h / 2)) - 30;
  this.detailDrawElement.style.left = left + 'px';            
  this.detailDrawElement.style.top = top + 'px';
}       

//##########################################################

Galery._increment = [];
Galery._registerInstance = {};
Galery._registerInstanceLength = [];

//##########################################################

Galery.getInstance = function(id) {
  if (arguments.length!=1) {
    focus();
    throw new Error("Galery.getInstance: Falsche Anzahl von Argumenten!");
  }
  if (! (Galery._registerInstance[id])){
    focus();
    throw new Error("Galery.getInstance: Es ist keine Galery.Instance mit id=" + id + " registriert!");
  }
  return Galery._registerInstance[id];
}

//##########################################################

Galery.createInstance = function(id) {
  if (!arguments.length) {
    id = 'Galery' + Galery._increment.length;
    Galery._increment.push(1);
  }
  if (! (Galery._registerInstance[id])){
    Galery._registerInstance[id] = new Galery(id);
    Galery._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error("Galery.createInstance: id schon vorhanden!");
  }
  return Galery.getInstance(id);
}                    

//##########################################################

Galery.getScreenWidth = function () {
    var x = document['body'].offsetWidth;
    return x;
}                       

//##########################################################

Galery.getScreenHeight = function () {
    var y = document['body'].offsetHeight;
    return y;
}           

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Galery.addEvent = function(oneEvent, obj, handler, bol, ieWindow){
  if (arguments.length < 3) {
    focus();
    throw new Error('Galery.addEvent: Falsche Anzahl von Argumenten!');
  }
  if(window.addEventListener){
    // Other
    obj.addEventListener(oneEvent, handler, bol);
  } else if(obj.attachEvent){
    // IE
    var win = window;
    if (ieWindow) {
      win = ieWindow;
    }
    obj['e' + oneEvent + handler] = handler;
    obj[oneEvent + handler] = function() { obj['e' + oneEvent + handler] ( win.event ); win.event.cancelBubble = true;};
    obj.attachEvent('on' + oneEvent, obj[oneEvent + handler]);
  }
  return true;
}

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Galery.removeEvent = function(oneEvent, obj, handler, bol){
  if (arguments.length < 3) {
    focus();
    throw new Error('Galery.removeEvent: Falsche Anzahl von Argumenten!');
  }
  if(window.removeEventListener){
    // Other
    obj.removeEventListener(oneEvent, handler, bol);
  } else if(obj.detachEvent){
    // IE
    obj.detachEvent( "on"+ oneEvent, obj[oneEvent + handler] );
    obj[oneEvent + handler] = undefined;
    obj["e" + oneEvent + handler] = undefined;
  }
  return true;
}     

//##########################################################           

//##########################################################
//##########################################################
//  GaleryImage
//##########################################################
//##########################################################

function GaleryImage(id) {
  this.id = undefined;
  this.imgPreview = undefined; 
  this.imgStandard = undefined;  
  this.title = undefined;  
  this.description = undefined; 
  
  this._setID(id);
}

//##########################################################

GaleryImage.prototype._setID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GaleryImage->_setID: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GaleryImage->_setID: Argument str ist nicht vom Typ String!");
  }
  this.id = str;
}             

//##########################################################

GaleryImage.prototype.setImgPreview = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GaleryImage->setImgPreview: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GaleryImage->setImgPreview: Argument str ist nicht vom Typ String!");
  }
  if (! GaleryImage.validateImageExtension(str)) {
    focus();
    throw new Error("GaleryImage->setImg: Argument str hat keine valide Image-Extension!");
  }
  var img = new Image();
  img.src = str;
  this.imgPreview = img;
}      

//##########################################################

GaleryImage.prototype.setImgStandard = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GaleryImage->setImgStandard: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GaleryImage->setImgStandard: Argument str ist nicht vom Typ String!");
  }
  if (! GaleryImage.validateImageExtension(str)) {
    focus();
    throw new Error("GaleryImage->setImg: Argument str hat keine valide Image-Extension!");
  }
  var img = new Image();
  img.src = str;
  this.imgStandard = img;
}          

//##########################################################

GaleryImage.prototype.setTitle = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GaleryImage->setTitle: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GaleryImage->setTitle: Argument str ist nicht vom Typ String!");
  }
  this.title = str;
}                 

//##########################################################

GaleryImage.prototype.setDescription = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error("GaleryImage->setDescription: Falsche Anzahl von Argumenten!");
  }
  if (typeof str != "string") {
    focus();
    throw new Error("GaleryImage->setDescription: Argument str ist nicht vom Typ String!");
  }
  this.description = str;
}           

//##########################################################

GaleryImage._increment = [];
GaleryImage._registerInstance = {};
GaleryImage._registerInstanceLength = [];

//##########################################################

GaleryImage.getInstance = function(id) {
  if (arguments.length!=1) {
    focus();
    throw new Error("GaleryImage.getInstance: Falsche Anzahl von Argumenten!");
  }
  if (! (GaleryImage._registerInstance[id])){
    focus();
    throw new Error("GaleryImage.getInstance: Es ist keine GaleryImage.Instance mit id=" + id + " registriert!");
  }
  return GaleryImage._registerInstance[id];
}

//##########################################################

GaleryImage.createInstance = function(id) {
  if (!arguments.length) {
    id = 'GaleryImage' + GaleryImage._increment.length;
    GaleryImage._increment.push(1);
  }
  if (! (GaleryImage._registerInstance[id])){
    GaleryImage._registerInstance[id] = new GaleryImage(id);
    GaleryImage._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error("GaleryImage.createInstance: id schon vorhanden!");
  }
  return GaleryImage.getInstance(id);
}

//##########################################################

GaleryImage.validateURL = function(url){
  if (arguments.length!=1){
    focus();
    throw new Error("GaleryImage:validateURL: Falsche Anzahl von Argumenten!");
  }
  if (typeof url != 'string') {
    focus();
    throw new Error('GaleryImage.validateURL: Argument url ist nicht vom Typ String!');
  }
  if (url.match(/^https?:\/\//)){
    return true;
  }
  if (url.match(/^\//)){
    return true;
  }
  return false;
}

//##########################################################

GaleryImage.validateImageExtension = function(src){
  if (arguments.length!=1){
    focus();
    throw new Error("GaleryImage:validateImageExtension: Falsche Anzahl von Argumenten!");
  }
  if (typeof src != 'string') {
    focus();
    throw new Error('GaleryImage.validateImageExtension: Argument src ist nicht vom Typ String!');
  }
  if (src.match(/\.gif$/)){
    return true;
  }
  if (src.match(/\.jpg$/)){
    return true;
  }
  if (src.match(/\.jpeg$/)){
    return true;
  }
  if (src.match(/\.png$/)){
    return true;
  }
  return false;
}

