/*
=======================================================================
  JavaScript to show GoogleMaps 
=======================================================================
  Version:    V 1.2.0
  Date:       20.02.2009
  Created:    Jos Meens
-----------------------------------------------------------------------
  Arguments:  none
-----------------------------------------------------------------------
  Copyright:  J. Meens, Softwareentwicklung
              Lucerne, Switzerland
              www.meens.ch
=======================================================================
  Changes:    
  V1.1	03.03.09 Preload of a certain amount of images in cache
  V1.2  22.05.11 Loop of display the pictures
=======================================================================  
*/

//globale Variablen
	
	var seiten = images.length-1;  		//Anzahl der Seiten tourX.htm
	var stat = "stop";  				//Status der Animation: "stop" oder "start"
	var akt = -1;  						//aktuell angezeigte Seite tourX.htm
	var ID = 0;  						//ID des letzten Timeouts
	var error = "";						// Meldungen an User
	var nummer = 0;						// Hilfsfeld zum Bild laden

	loadData();
	

//Funktionen
function loadData() {
	var h = window.location.host;
	if ( h == "www.meens.ch" || h == "www.anica.ch" || h == "" || h == "127.0.0.1") {
		f_loadFile("../scripts/moduls/anica_fotoShow/texte.txt");
		f_loadFile("../scripts/moduls/anica_fotoShow/settings.ini");
	} else {
		f_loadFile("anica_fotoShow/texte.txt");
		f_loadFile("anica_fotoShow/settings.ini");
	}	
}

function f_loadFile(file) {
	document.write('<script type="text/javascript" src="'+file+'"></script>')
}

function start() {

	if (scrollToPosition != 999) {
		window.scrollTo(0, scrollToPosition);
	}

   	if (stat == "stop" && akt < seiten) {
      	stat = "start";
      	lade(akt+1);
   	}
}
function stop() {
   stat = "stop";
   clearTimeout(ID);
}
function vor() {
   clearTimeout(ID);
   if (akt < seiten) {
      lade(akt+1);
   } else {
   	  error = beginAgain;
   	  lade(0);
   }
}
function zurueck() {
clearTimeout(ID);
   if (akt > 0) {
     lade(akt-1);
   } else {
   	lade(images.length-1);
   } 
}
function verlassen() {
   clearTimeout(ID);
   top.location.href = goToByLeave;
}
function lade(nummer) {
	akt = nummer;
	if (akt == seiten) {
	    if (!loop) { error = lastPicture };
      	} 
	
	var n = nummer;
	// w = parent.window["Tour"];
	
	html = "<"+"table width="+tableWidth+" height=100%>"+"<"+"tr>"+"<"+"td><center>";
	
	// Navigation
	html += '<'+'table width='+tableWidth+'><'+'tr>';
	if (navigation == "text") {
		html += '<'+'td align="center">'+'<'+'a href="javascript:start();">'+str1+'<'+'/a>'+'<'+'/td>';
		html += '<'+'td align="center">'+'<'+'a href="javascript:stop();">'+stp1+'<'+'/a>'+'<'+'/td>';
		html += '<'+'td align="center">'+'<'+'a href="javascript:zurueck();">'+bwd1+'<'+'/a>'+'<'+'/td>';
		html += '<'+'td align="center">'+'<'+'a href="javascript:vor();">'+fwd1+'<'+'/a>'+'<'+'/td>';
		if (showLeaveButton) {
			html += '<'+'td align="center">'+'<'+'a href="javascript:verlassen();">'+lve1+'<'+'/a>'+'<'+'/td>';
		}
	
	}
	if (navigation == "button") {
	    html += '<input type="submit" value="Absenden" />';
	   	html += '<'+'td align="center">'+'<'+'input type="submit" onclick="start();" value='+str1+' />'+'<'+'/td>';
		html += '<'+'td align="center">'+'<'+'input type="submit" onclick="stop();" value='+stp1+' />'+'<'+'/td>';
		html += '<'+'td align="center">'+'<'+'input type="submit" onclick="zurueck();" value='+bwd1+' />'+'<'+'/td>';
		html += '<'+'td align="center">'+'<'+'input type="submit" onclick="vor();" value='+fwd1+' />'+'<'+'/td>';
		if (showLeaveButton) {
			html += '<'+'td align="center">'+'<'+'input type="submit" onclick="verlassen();" value='+lve1+' />'+'<'+'/td>';
		}
	}
	html += '<'+'/tr>'+'<'+'/table>';
		
	// Meldungen 
	html += (error != "") ? "<hr/><"+"font color="+errorTextColor +">"+ error + "<"+"/font>" : "";

	// Bildbeschreibung oben
	if (imgDescriptionPosition.toLowerCase() == "top") {
		html += (descriptions[nummer] == undefined) ? "" : trenner+"<"+"h4>"+descriptions[nummer]+"<"+"/h4>";
	}
	
	// Bild
	html += "<"+"img id=\""+images[nummer]+"\" src=\"" + imgpath + 	images[nummer] + "\" align=center valign=top/>";
	
	// Bilddaten 
	if (showImgData == true) {
		html += "<br/><"+"small>"+"Foto Nr. "+(nummer+1)+" / "+ images.length +" | "+ imgpath + 	images[nummer] + "<"+"/small>";
	} 
	
	// Bildbeschreibung unten
	if (imgDescriptionPosition.toLowerCase() == "bottom") {
		html += (descriptions[nummer] == undefined) ? "" : "<"+"hr/>"+"<"+"h4>"+descriptions[nummer]+"<"+"/h4>";
	}
	
	// Werbung
	html += "<br/><"+"small>"+"created with <a href=http://www.anica.ch>anica FotoShow </a> ("+ navigator.appName + ")<"+"/small>";
	html += "<"+"center>"+"<"+"/td>"+"<"+"/tr>"+"<"+"/table>";
	
	// Vorladen
	for (var m = 0; m < anzPreload; m++) {
		html += '<'+'img name="'+images[nummer+1+m]+'" id="'+images[(nummer+1+m)]+'" src="' + imgpath + images[nummer+1+m] + '" align=center width=2 height=10 border=0 />&nbsp;';
	}
		
	document.getElementById(divID).innerHTML = html;

    if (stat == "start" && akt < seiten) {
       ID = setTimeout("lade(akt+1)", delay);
   } else if (akt == seiten) {
   if (loop) {
       ID = setTimeout("lade(0)", delay);
   } else {
       stat = "stop";
   }
    }
    error = "";
}

var complete = 0;

function preLoad() {
	
    var preHtml = "";
    
    // Anzahl Elemente im Balken
    for (var k = 0; k < anzPreload; k++) {
    	if (k < complete) {
    		preHtml += '<'+'img id="Laufbalken'+k+'" src="../scripts/moduls/anica_fotoShow/icons/green.png" width="20" height="10" />&nbsp;'
    	} else {
    		preHtml += '<'+'img id="Laufbalken'+k+'" src="../scripts/moduls/anica_fotoShow/icons/red.png" width="20" height="10" />&nbsp;'
    	}
	}
    
    // Anzahl Bilder
	preHtml += '<'+'br />';
	for (var i = 0; i < anzPreload; i++) {
		preHtml += '<'+'img name="'+images[i]+'" id="'+images[i]+'" src="'+ imgpath + 	images[i] +'" width="20" height="80" />&nbsp;';
	}
			
	document.getElementById(divID).innerHTML = preHtml;
		
	setGreen();
}

function setGreen() {
	// Wieviele sind komplett ?
	complete = 0;
	for (var l = 0; l < anzPreload; l++) {
	// geht nicht mit IE mejmej
		if (document.getElementById(images[l]).complete) { 
			complete++;
		} 
	}
	
// Loop, wenn noch nicht alle vorgeladen sind. 	(Bei IE geht das nicht)
	if (navigator.appName == "Microsoft Internet Explorer") {
		start();
	}
	else if (complete == anzPreload) {
		start();
	} else {
		setTimeout("preLoad()", 500);
	}
}

// Warten bis Settings verfügbar sind, dann start FotoShow
function load() {
	
	try {						
		if (tableWidth) {					
			preLoad();
		}
	} catch (ex) {
		setTimeout("load()", 500);
	}
}

load();

