/* Teil des Bildergalerie-Moduls des CMS */

/* Das Script ermöglicht einen Bildwechsel, ohne die Seite neu zu laden.
   Bild und Bildunterschrift werden ersetzt, danach werden die Steuerelemente gelöscht und die benötigten neu erzeugt. */

var gallery_href = new Array();
var gallery_current = new Array();
var gallery_imgs = new Array();
var gallery_txts = new Array();
var gallery_mode = "arrows";

function gallery_setmode(p_mode)
{
  gallery_mode = p_mode;
}

function gallery_init(p_url, p_id, p_current, p_imgs, p_txts)
{
  var txt;
  var t;

  gallery_href[p_id] = p_url + (p_url.search(/\?/) == -1 ? "?" : "&") + p_id + "_";
  gallery_current[p_id] = p_current;
  gallery_imgs[p_id] = p_imgs;
  gallery_txts[p_id] = p_txts;
  txt = document.getElementById("gallery" + p_id + "_text");
  if(txt && !txt.hastChildNodes)
  {
    t = document.createTextNode("");
    txt.appendChild(t);
  }
  gallery_refresh(p_id);
}

function gallery_refresh(p_id)
{
  var txt;
  var t;
  var a;
  var l;
  var controlpanel;
  var i;

  document.getElementById("gallery" + p_id + "_img").src = gallery_imgs[p_id][gallery_current[p_id]];
  txt = document.getElementById("gallery" + p_id + "_text");
  if(txt) txt.firstChild.nodeValue = gallery_txts[p_id][gallery_current[p_id]];

  controlpanel = document.getElementById("gallery" + p_id + "_control");
  while(controlpanel.firstChild) controlpanel.removeChild(controlpanel.firstChild);

  if(gallery_mode == "arrows")
  {
    if(gallery_current[p_id] > 0)
    {
      t = document.createTextNode("|<");
      a = document.createElement("abbr");
      a.appendChild(t);
      a.title = "erstes Bild";
      l = document.createElement("a");
      l.appendChild(a);
      l.className = "gallery_first";
      l.href = gallery_href[p_id] + "0" + "#gallery" + p_id;
      l.onclick = function() { return gallery_first(p_id) };
      controlpanel.appendChild(l);

      t = document.createTextNode("<<");
      a = document.createElement("abbr");
      a.appendChild(t);
      a.title = "vorheriges Bild";
      l = document.createElement("a");
      l.appendChild(a);
      l.className = "gallery_previous";
      l.href = gallery_href[p_id] + (gallery_current[p_id] - 1) + "#gallery" + p_id;
      l.onclick = function() { return gallery_previous(p_id) };
      controlpanel.appendChild(l);
    }

    if(gallery_current[p_id] < gallery_imgs[p_id].length - 1)
    {
      t = document.createTextNode(">|");
      a = document.createElement("abbr");
      a.appendChild(t);
      a.title = "letztes Bild";
      l = document.createElement("a");
      l.appendChild(a);
      l.className = "gallery_last";
      l.href = gallery_href[p_id] + (gallery_imgs[p_id].length - 1) + "#gallery" + p_id;
      l.onclick = function() { return gallery_last(p_id) };
      controlpanel.appendChild(l);

      t = document.createTextNode(">>");
      a = document.createElement("abbr");
      a.appendChild(t);
      a.title = "nächstes Bild";
      l = document.createElement("a");
      l.appendChild(a);
      l.className = "gallery_next";
      l.href = gallery_href[p_id] + (gallery_current[p_id] + 1) + "#gallery" + p_id;
      l.onclick = function() { return gallery_next(p_id) };
      controlpanel.appendChild(l);
    }
  }
  else
  {
    if(gallery_current[p_id] > 0)
    {
      t = document.createTextNode("<");
      a = document.createElement("abbr");
      a.appendChild(t);
      a.title = "vorheriges Bild";
      l = document.createElement("a");
      l.appendChild(a);
      l.href = gallery_href[p_id] + (gallery_current[p_id] - 1) + "#gallery" + p_id;
      l.onclick = function() { return gallery_previous(p_id) };
      controlpanel.appendChild(l);
    }
    t = document.createTextNode(" \u00A0 ");
    controlpanel.appendChild(t);
    for(i = 0; i < gallery_imgs[p_id].length; i++)
    {
      t = document.createTextNode(i + 1);
      if(gallery_current[p_id] == i)
      {
	l = document.createElement("strong");
      }
      else
      {
	l = document.createElement("a");
	l.href = gallery_href[p_id] + i + "#gallery" + p_id;
	l.title = i + 1;
	l.onclick = function() { return gallery_jump(p_id, this.title - 1) };
      }
      l.appendChild(t);
      controlpanel.appendChild(l);
      t = document.createTextNode(" \u00A0 ");
      controlpanel.appendChild(t);
    }
    if(gallery_current[p_id] < gallery_imgs[p_id].length - 1)
    {
      t = document.createTextNode(">");
      a = document.createElement("abbr");
      a.appendChild(t);
      a.title = "nächstes Bild";
      l = document.createElement("a");
      l.appendChild(a);
      l.href = gallery_href[p_id] + (gallery_current[p_id] + 1) + "#gallery" + p_id;
      l.onclick = function() { return gallery_next(p_id) };
      controlpanel.appendChild(l);
    }
  }

}

function gallery_first(p_id)
{
  gallery_current[p_id] = 0;
  gallery_refresh(p_id);
  return false;
}

function gallery_previous(p_id)
{
  gallery_current[p_id] -= 1;
  gallery_refresh(p_id);
  return false;
}

function gallery_next(p_id)
{
  gallery_current[p_id] += 1;
  gallery_refresh(p_id);
  return false;
}

function gallery_last(p_id)
{
  gallery_current[p_id] = gallery_imgs[p_id].length - 1;
  gallery_refresh(p_id);
  return false;
}

function gallery_jump(p_id, p_img)
{
  gallery_current[p_id] = p_img;
  gallery_refresh(p_id);
  return false;
}

