/* *****************************************************************************************
 *	Das Modul stellt den Worker für das Overlay der automatischen Berechnungen dar.
 *  Es wird lediglich die Datums- und Zeitangabe im Overlay angezeigt. Dabei
 *  kann vom Haupthtread aus die Geschwindigkeit (DELAY) geändert werden.
 *  Dazu muss lediglich der Wert im Geschwindigkeits Eingabefeld geändert werden.
 *	-------------------------------------------------------------------------------------------------------------
 * The module represents the worker for the overlay of the automatic calculations.
 * Only the date and time indication is displayed in the overlay. In doing so
 * the speed (DELAY) can be changed from the main tread.
 * All you have to do is change the value in the speed input field.
 ***************************************************************************************** */
 
 
var DELAY = 500;											// Initial Wartezeit für die Zeitermittlung.
																		// Initial Waiting time for the time calculation.
var d = null;													// Nimmt das Objekt des aktuelle Datum auf.
																		// Pick up the object of the current date.
var channel = new BroadcastChannel ("com"); // Neuen Nachrichtenkanal zum Hauptprogramm erstellen.
																		// Create a new message channel to the main program.
// Die Message Queue des Workers für Messages über den Broadcast Kanal.
//	The worker’s message queue for messages via the broadcast channel.
channel.onmessage = function (e){
	var splitData = e.data.split("=");
	// Delay beim Senden vom Hauptthread, wenn die Geschwindigkeit
	// im Overlay geändert wird.
	// Delay when sending from the main thread, if the speed
	// in the overlay is changed.
	if(splitData[0] == "velosity"){
		DELAY = splitData[1];
		// An den Hauptthread die verwendete Delay melden, damit
		// sie im Overlay unter Aktiv eingetragen wird.
		// Report the used delay to the main thread so that
		// it is entered in the Overlay under Active.
		channel.postMessage("delay=" + DELAY);
	}
	
	
}// End of Message Queue



/* ******************************************************
	Die Funktion füllt eine Zeichenketten mit Zahl vorne
	mit n-Zeichen auf.	Z.B. 1 -> 01.
	
	Übergabewert:
	--------------------
		izahl 				= 	Die Zahl ohne führende Nullen
		ianzStellen		= 	Die gewünschte Anzahl von Stellen
		sFuelzeichen	=	Das gewünschet führende Zeichen
	
	Rückgabewert:
	---------------------
									Z.B. "01" oder "0001"
	----------------------------------------------------------------
	The function fills a string with number in front
	with n-signs.	E.g. 1 -> 01.
	
	Transfer value:
	--------------------
		izahl    =  The number without leading zeros
		ianzPlaces  =  The desired number of posts
		sFuelmark = The desired leading character
	
	Return value:
	---------------------
									E.g. “01” or “0001”
   *************************************************** */

function LPad( iZahl, iAnzStellen, sFuellzeichen ){
    var sZahl = iZahl + "";
	// Durchlaufe die Zeichenkette solange bis die gewünschte
	// Anzahl von Zeichen erreicht ist ...
	// Run the string until the desired one
	// Number of characters reached ...
    while( sZahl.length < iAnzStellen )
		// ... und fülle den Ergebnisstring sZahl links vor der Zahl
		// mit dem Füllzeichen auf ...
		// ... and fill in the result string sNumber left before the number
		// with the fill mark on ...
        sZahl = sFuellzeichen + sZahl;
	// ... und dann zurück zum Aufrufer.
	// ... and then back to the caller.
    return sZahl;
	
}// End of Function LPad()




// Ermittelt Datum und Uhrzeit für das DIV-Overlay im Hauptprogramm..
// Die aktuellen Daten werden an die Message Que des Worker "controlAutomatic.js" 
// gesendet, die in der Funktion startWorker() liegt. Danach werden sie im Overlay
// unter dem Punkt "Mit aktuellem Datum" angezeigt.
//
// Determines the date and time for the DIV overlay in the main program..
// The current data is sent to the message Que of the worker “controlAutomatic.js” 
// sent, which is in the function startWorker (). After that, they are displayed in the overlay
// displayed under the item “With current date.”
function getTime() {
	// Aktuelles Datum holen ...
	// Get current date ...
	d = new Date();
	var hhmm = "";
	// ... und das komplette Datum senden ...
	// ... and send the complete date ...
	postMessage("date=" + d);
	// .. sowie hh:mm als formatierter String ...
	// .. as well as hh:mm as formatted string ...
	hhmm = LPad( d.getHours(), 2, "0" ) + ":" + LPad( d.getMinutes(), 2, "0" )
	postMessage("StundeMinute=" + hhmm);
	// ... danach die Sekunde ...
	// ... then the second ...
	postMessage("Second=" + d.getSeconds());
	// ... und die Millisekunde ...
	// ... and the millisecond ...
	postMessage("MSecond=" + d.getMilliseconds());
  
	// Ruft die Funktion nach der Wartezeit DELAY wieder auf, bis gestoppt wird.
	// Calls the function again after the DELAY wait until it is stopped.
	setTimeout("getTime()", DELAY);
  
}// End of getTime()



// An den Hauptthread und alle beteiligten Programmteile, bei Start die verwendete Delay melden, damit
// sie z. B. im Overlay unter Aktiv eingetragen wird.
//
// To the main thread and all participating program parts, report the used delay at startup, so that
// it is entered e.g. in the Overlay under Active.
channel.postMessage("delay=" + DELAY);


// Startet die Funktion als Thread im Background.
// Starts the function as a thread in the background.
//
// Holt das aktuelle Datum und sendet es an das Overlay für automatische Berechnungen.
// Get the current date and send it to the overlay for automatic calculations.
getTime();