/* File j_foto.js: Fotogalerie 'Am Riedteich' */

// opener-Variable:
var opW;				// Foto Fenster
var opID;				// Foto ID
var gpModus;				// PlayModus 0=Einzelbild, 1=mit Abspielfunktion
 
function FotoLaden(pID) {
  // Laderoutine im opener.Document aufrufen
  opID=pID;
  var para =	'width=512,height=384, resizable=no,left=50,top=50,dependent=yes,' +
		'location=no,status=yes,prefbar=no,menubar=yes,toolbar=no,scrollbars=auto,personalbar=no';
  var uri = location.href;					// !rel.Pfad aus rufender HTML-Seite ermitteln
  var sL = uri.indexOf('.htm');
  while ((sL > 0) && (uri.substr(sL,1) != '/')) sL--;
  var Dir = uri.substr(0,sL) + '/../';	// + '/';
  opW = window.open(Dir + 'basics/j_fotoladen.htm', 'Fotos', para);
}

function mkDiaListe(pVar, pPlatz) {
  // Object Diashow-Verwaltung
  this.Instanz = pVar;					// externe Instanz = new mkDiaListe()
  this.Platz =						// ?Dia 'HTML-Platzhalter'
	(pPlatz == void(0)) ? 'kShow' : pPlatz;
  this.kDias = new Array();  this.Idx = 0;
  this.idRun = false;  this.tcChange = 6500;		// ID setInterval, TC[msec] setInterval 6500
  this.tcWait = 3500;					// TC[msec] Startverzögerung 3500
  this.kRandom = false;					// ?Zufallswechsel
  
  this.kAdd = function(pID) {
    if (pID != void(0)) this.kDias.push(pID);
  } // end
  
  this.kStart = function() {
    var ht = this.Platz;
    if (document.getElementById(ht) == void(0)) return;		// ?Zielort unbekannt
    if (this.kDias.length < 1) return;				// ?zu wenig Einträge
    var f = this.Instanz + ".kPlay()";				// !Trick 17: bilde Instanz.kPlay()
    var s = this.Instanz +
	     ".idRun = setInterval('" + f + "', " + this.tcChange + ")";
    window.setTimeout(s, this.tcWait);				// !Run mit Startverzögerung[msec]
  } // end

  this.kStop = function() {
    if (!this.idRun) return;
    window.clearInterval(this.idRun);
    this.idRun = false;
  } // end

  this.kPlay = function() {
    var ht = this.Platz, Size = this.kDias.length, id, s; 

    with (this) { id = kDias[Idx]; }
    if (!document.getElementById(id)) {				// ?[Idx] ohne Daten
	this.Idx++;  if (this.Idx >= Size) this.Idx=0;
	return;
    }									// !folgende Abfrage so nur in IE/Opera
    if (document.getElementById(id).complete == true) {		// ?Bild geladen
	s = document.getElementById(id).src;
	    document.getElementById(ht).src = s;
	s = document.getElementById(id).title;
	    document.getElementById(ht).title = s;
	if (this.kxRandom == true) {
	  var r = Math.random() * Size;
	  this.Idx = Math.floor(r);
	} else {
	  this.Idx++;
	}
    } else {
	this.Idx++;
    }
    if (this.Idx >= Size) this.Idx=0;
  } // end

} // end mkDiaListe()


/* Fotogalerie extra Variable: */
var buPlayRun = new Image();				// !lade vorab PlayRun.img(besseres Timing)
    buPlayRun.src='bu_play-active.gif';
var tPlayStart='Abspielen', tPlayStop='Anhalten';


function mkFotoListe(pThema) {
  // Object Bildlisten-Verwaltung
  this.Thema = pThema;                                  // Foto-Thema setzen => <TITLE>
  this.myFoto = new Array();                            // Img-Liste (ID,src,Titel,Quelle)
  this.myCnt = this.myIdx = 0;                          // Img Anzahl, lfd.Index
var uri = location.href;					// !rel.Pfad aus rufender HTML-Seite ermitteln
var sL = uri.indexOf('.htm');
while ((sL > 0) && (uri.substr(sL,1) != '/')) sL--;
var Dir = uri.substr(0,sL) + '/../';
  
  // Object-interne Funktionen:
  function _gItem(pID, pSrc, pTitle, pQuelle) {
    // liefert Object Grossfoto:  ID, img.src, Titel, Quelle|CopyRight
    this.ID=pID;  this.title=pTitle;  this.quelle=pQuelle;
    var p = pSrc.indexOf('.');			  		// Img: Src,Typ[.jpg]
    if (p > 0)  { this.src=pSrc.substr(0,p);  this.typ=pSrc.substr(p); }
	    else { this.src=pSrc;  this.typ='.jpg'; }
  }
    
  // Object-globale Funktionen:
  this.kTag = function(pSpan, Info) {
    // liefert TD-String '<td><img..': !!!bei Bedarf Layout anpassen
    var span = (pSpan > 0) ? ' colspan=' + pSpan : '';
    if (this.myCnt < 1) return('<td' + span + '></td>');

    var xF = this.myFoto[this.myIdx];
    var kImg = Dir + xF.src + '_k.jpg';
    var s =	'<td' + span + '><div class="bk"><div class="code">' +
		'<a href="javascript: FotoLaden(\'' + xF.ID + '\')"><div class="shadow"><img id="' + xF.ID + '" ' +
			'src="' + kImg + '" title="' + xF.title + '"></div></a>';
    if (Info != void(0)) s += '<pre>' +Info+ '</pre>';
    s += '</div></div></td>';
    return(s);
  } // end
          
  this.Add = function(pID, pSrc, pTitle, pQuelle) {
    var xF = new _gItem(pID, pSrc, pTitle, pQuelle);
    with (this) {
      myFoto.push(xF);
      myCnt = myFoto.length;  myIdx = (myCnt < 1) ? 0 : myCnt-1;
    }  
  } // end

  this.Find = function(pID) {
    // Sucht pID in myFoto[], Abgleich myIdx
    with (this) {
	for (var r=false,x=0;  x < myFoto.length;  x++)
		if (myFoto[x].ID == pID) { myIdx = r = x;  break; }
    }
    return(r);
  } // end

  this.Step = function(direction) {
    direction = (direction == void(0)) ? +1 : direction;
    var cntEmpty = 0;
    with (this) do {
      myIdx += direction;
      if (myIdx >= myCnt) myIdx = 0;
			     else if (myIdx < 0) myIdx = myCnt-1;
      if (myFoto[myIdx].src.length > 0) break;
    } while (++cntEmpty < this.myCnt);
  }
   
  this.Sort = function() {
    // Sortiert myFoto[].records aufsteigend nach ID-Nummer
    var item, x,x2, c, s1,n1,s2,n2;
    for (x=0;  x < this.myCnt;  x++) {
      x2 = x + 1;  if (x2 >= this.myCnt) break;
      id1 = this.myFoto[x].ID;
      for (c=0;  c < id1.length;  c++)
	  if ((id1.charAt(c) >= '0') && (id1.charAt(c) <= '9')) { s1=id1.substr(0,c);  n1=id1.substr(c);  break; }
      id2 = this.myFoto[x2].ID;
      for (c=0;  c < id2.length;  c++)
	  if ((id2.charAt(c) >= '0') && (id2.charAt(c) <= '9')) { s2=id2.substr(0,c);  n2=id2.substr(c);  break; }
      while (s1.length < s2.length) s1 += ' ';
      while (s2.length < s1.length) s2 += ' ';
      if (s2 > s1) continue;
      c = n2 - n1;
      if ((s2 < s1) || (c < 0)) {	// document.writeln('n2(' +n2+ ') - n1(' + n1+ ') = ' + c + '<br>');
	 item = this.myFoto[x];
	 this.myFoto[x] = this.myFoto[x2];
	 this.myFoto[x2] = item;
	 x = -1; // restart from 0
      }
    } // end for()
  } // end

  this.sbImg = function() {
    with (this) { var s = Dir + myFoto[myIdx].src + myFoto[myIdx].typ;  return(s); }  
  } // end

  this.sbTitle = function() {
    with (this) { var s = myFoto[myIdx].title;  return(s); }  
  } // end

  this.sbQuelle = function() {
    with (this) { var s = myFoto[myIdx].quelle;  return(s); }  
  } // end
       
  this.sbNummer = function() {
    // liefert 'Bild x/Max'
    var n = this.myIdx + 1;
    var s = 'Bild ' + n + '/' + this.myCnt;
    return(s);
  } // end
    
} // end mkFotoListe()


function mkPlayer(pVar, pFotos) {
  // Object Fotoplayer
  var s = navigator.appName;				// Browser-KZ ermitteln
  this.Navi = (s.indexOf("Internet Explorer") >= 0) ? 'ie' : '?';
  this.Instanz = pVar;					// externe Instanz = new mkPlayer()
  this.Fotos = pFotos;					// externe Fotoliste
  this.imgLaden = false;					// STAT: Bild wird gerade geladen[n,y]
  this.autoPlay = false;					// STAT: Abspielen[n/y]
  this.idPlay;						// ID setInterval
  this.tcChange = 5000;					// TC[msec] setInterval: 8000, 7000 evtl.zu kurz(Modem)!

  if (document.getElementById('DOCTITLE') != void(0)) {	// ?Fotofenster Dokumenten-Titel
	if (this.Navi == 'ie') document.getElementById('DOCTITLE').text = this.Fotos.Thema;
  }
  if (document.getElementById('buPlay') != void(0)) {		// ?Bullet AutoPlay(Text)
	var s = (this.autoPlay) ? tPlayStop : tPlayStart;
	document.getElementById('buPlay').title = s;
  }

  this.tcStep = function(wie) {
    var t = this.tcChange;
    switch(wie) {
	case '>0': if ((t + 500) < 16000) t+=500;		// !MAX 16sec
		    break;
	case '<0': if ((t - 500) > 1500)  t-=500;		// !MIN 1sec
		    break;
	default:   t=8000;					// !default
    }
    this.tcChange = t;
  } // end
  
  this.FotoSelect = function(pID) {
    if (pID != void(0)) this.Fotos.Find(pID);      
    var gImg = this.Fotos.sbImg();
    if (document.getElementById('ONLOAD') != void(0)) {	// !Set Lade-Text
	if (this.Navi != 'ie') // ?Unklar IE: Text bleibt manchmal (trotz FotoResize) stehen
		document.getElementById('ONLOAD').innerHTML = 'Foto wird geladen ..';
    }
    this.imgLaden = true;                               // !SET: Img-laden
    if (document.getElementById('ZFoto')) {
	document.getElementById('ZFoto').src = gImg;
    }	
  } // end

  this.FotoStep = function(direction) {
    if (this.imgLaden ||
	(this.Fotos.myCnt < 2)) return;                  // ?TEST: Img-laden | zu wenig Fotos
    this.Fotos.Step(direction);
    this.FotoSelect();
  } // end

  this.FotoResize = function() {
    var bw,bh, w1,h1, n,s;
    this.imgLaden = false;                              // !RES Img-laden
    bw=document.images['ZFoto'].width;			// !Breite,Höhe neues Bild
    bh=document.images['ZFoto'].height;			// IE: !Statusleiste nur offline aus	
    if (this.Navi == 'ie') { w1=bw+38;  h1=bh+135; }	// fuer IEx.x:  +38,+115
			 else { w1=bw+38;  h1=bh+145; }	// fuer Andere: +38,+125
    self.resizeTo(w1,h1);
    if (document.getElementById('ONLOAD') != void(0)) {		// !Clear Lade-Text
	document.getElementById('ONLOAD').innerHTML = '';
    }
    if (document.getElementById('FOTOTEXT') != void(0)) {	// ?Fototext
	s = this.Fotos.sbTitle();
	s = (s != void(0)) ? s : 'Titel?';
	document.getElementById('FOTOTEXT').innerHTML = s;
    }
    if (document.getElementById('FOTONR') != void(0)) {		// ?Fotonr.
	document.getElementById('FOTONR').innerHTML = this.Fotos.sbNummer();
    }
    if (document.getElementById('QUELLE') != void(0)) {		// ?[Quelle|Copyright] oder
	s = this.Fotos.sbQuelle();					// ?['BxH = ' + bw + 'x' + bh]
	s = (s != void(0)) ? s : 'BxH = ' + bw + 'x' + bh;
	document.getElementById('QUELLE').innerHTML = s;
    }
  } // end

  this.FotoPlay = function() {
    if (this.Fotos.myCnt < 1) return;
    this.autoPlay = !this.autoPlay;			// !Swap Status
    if (this.autoPlay == true) {
	var f = this.Instanz + '.FotoStep(+1)';		// !Trick 17: bilde Instanz.FotoStep()
	this.idPlay = self.setInterval(f, this.tcChange);
	document.getElementById('buPlay').src = buPlayRun.src;
	document.getElementById('buPlay').title = tPlayStop;
    } else {
	self.clearInterval(this.idPlay);
	document.getElementById('buPlay').src = 'bu_play.gif';
	document.getElementById('buPlay').title = tPlayStart;
    }
  } // end

  this.FotoStop = function() {
    if (this.autoPlay) self.clearInterval(this.idPlay);
    self.close();  opener.focus();
  } // end  
    
} // end mkPlayer()

