/*
* Diktat
* 
*/
function Dictation()
{
	this.firstStep = true;
	this.pause = "       ..       ";
	this.exercise = 'dictation';
	
	
	this.init = function(content, auto, lang) {
		if (auto) {
			// Dictat soll mit dem Linguatec Player vorgelesen werden
			eval("this.lang = new " + lang.toUpperCase()+"();");
			
			this.text = content;
			this.player = new LinguatecPlayer("linguatecplayer", lang);
			this.player.text = this.toDictation();
			this.player.speed = 75;
			this.player.write();
		}
		else {
			// MP3 File
			if (content) {
				this.file = content;
			}
			if (this.file && document.getElementById("player")) {
				var s3 = new SWFObject('/trainer/img/mp3player.swf', 'line', '240', '20', '7');
				s3.addVariable('file', this.file);
				s3.addVariable('repeat','false');
				s3.addVariable('showdigits','true');
				s3.addVariable('showdownload','false');
				s3.write('player');
			}
			
			if (lang) {
				this.summeryfile = lang;
			}
			if (this.summeryfile && document.getElementById("summeryplayer")) {
				var s3 = new SWFObject('/trainer/img/mp3player.swf', 'line', '240', '20', '7');
				s3.addVariable('file', this.summeryfile);
				s3.addVariable('repeat','false');
				s3.addVariable('showdigits','true');
				s3.addVariable('showdownload','false');
				s3.write('summeryplayer');
			}
		}
	}
	
	/*
	* Gibt einen Text als Diktat wieder
	*/
	this.toDictation = function() {
		this.pause = this.lang.pause;
		var parts = this.lang.splitText(this.text);
		
		// Ergebniss wird zusammengebaut
		var result = "";
		result += this.lang.dictationIntro + "     . " + this.text + "   .   .   ";
		result += this.lang.dictationStart + "         . ";
		for (var i = 0; i < parts.length; i++) {
			if (parts[i].length > 3 || parts[i].weight == 1) {
				// Part ist nicht zu kurz oder wird mit einem Punkt abgeschlossen
				if (parts[i].length <= 10) {
					// "Richtige" Länge wird hinzugefügt
					result += parts[i].toPunctuationString(this.lang)
							+ this.pause
							+ parts[i].toPause(this.pause)
							+ parts[i].toPunctuationString(this.lang)
							+ parts[i].toPause(this.pause);
				}
				else {
					// Zu lang, Part wird geteilt
					var n = 0;
					while (parts[i].length > n) {
						if ( parts[i].length >= (n + 8)) {
							var part = new Part(parts[i].substr(n, n + 6));
							result += part.toPunctuationString(this.lang)
									+ this.pause
									+ part.toPause(this.pause)
									+ part.toPunctuationString(this.lang)
									+ part.toPause(this.pause);
							n += 6;
						}
						else {
							var part = new Part(parts[i].substr(n));
							result += part.toPunctuationString(this.lang)
									+ this.pause
									+ part.toPause(this.pause)
									+ part.toPunctuationString(this.lang)
									+ part.toPause(this.pause);
							break;
						}
					}
				}
			}
			else {
				// Es wird überprüft ob der Part mit den nächsten zusammengeführt werden kann
				var len = parts[i].length;
				var text = parts[i].toPunctuationString(this.lang);
				var pause = parts[i].toPause(this.pause);
				i++
				while (i < parts.length && len + parts[i].length <= 8) {
					len += parts[i].length;
					text += parts[i].toPunctuationString(this.lang);
					pause += parts[i].toPause(this.pause);
					i++;
				}

				// Parts wereden hinzugefügt
				result += text
						+ this.pause
						+ pause 
						+ text
						+ pause;
				i--;
			}
		}
		result += this.lang.dictationRead + "     ." + this.text;
		return result;
	}
	
	/*
	* Autokorrektur für ein Diktat
	*/
	this.autocorrect = function(solution, event) {
		this.url = ajaxGetPopUp.url.replace(/form/,"info");
		if (solution)
		{
			this.solution = solution;
		}
		if (!this.solution)
		{
			// Lösung wird per AJAX-Request geladen
			this.startPostRequest(postBackString(null, 'viewdictation') + "&object=exercise&action=load");
		}
		else
		{
			// Autokorrektur wird durchgeführt
			var correction = document.createElement("span");
			correction.innerHTML = WDiffString(document.getElementById("value["+exercise.exercise+"][0][text][0]").value, this.solution);
			
			
			if (this.firstStep)
			{
				// Möglich zur Verbesserung wird geprüft
				this.firstStep = false;
				if (correction.getElementsByTagName("del").length > 0 || correction.getElementsByTagName("ins").length > 0) {
					// Der User darf sein Diktat nochmal verbessern
					document.getElementById("exerciseContent").appendChild(document.createTextNode("Ihre Loesung enthaelt leider noch Fehler. Sie koennen diese nun noch verbessern."));
					document.getElementById("correctionButton").value = "Loesung anzeigen";
				}
				else {
					// Alles richtig
					document.getElementById("exerciseContent").innerHTML = correction.innerHTML;
					document.getElementById("correctionButton").disabled = "disabled";
				}
			}
			else {
				// Ergebniss der Autokorrektur wird angezeigt
				document.getElementById("exerciseContent").innerHTML = correction.innerHTML;
				document.getElementById("correctionButton").disabled = "disabled";
			}
		}
	}
	
	/**
	* Schickt die Lösung ein
	*/
	this.solve = function(event) {
		ajaxGetPopUp.startPostRequest(postBackString(event, 'viewdictation'));
	}
}
Dictation.prototype = new Exercise();

/*
* Teil eines Satzes
*/
function Part(str, weight) {
	this.str = str;
	this.weight = weight;
	this.words = splitText(this.str.trim());
	// Anzahl der Wörter
	this.length = this.words.length
	
	this.toString = function() {
		return this.str;
	}
	
	/*
	* Gibt eine passend Lange Pause für den Part zurück
	*/
	this.toPause = function(pause) {
		// Länge Pause wird bestimmt
		var str = "";
		if (this.str.length > this.length * 7) {
			// Pausen werden anhand der Wortlängen eingefügt
			for (var i = 0; i < Math.ceil(this.str.length / 12); i++) {
				str += pause;
			}
		}
		else {
			for (var i = 0; i < Math.ceil(this.length / 3); i++) {
				str += pause;
			}
		}
		return str;
	}
	
	/*
	* Gibt eine String zurück in dem die Satzzeichen ausgeschrieben sind
	* und Pausen eingefügt worden sind
	*/
	this.toPunctuationString = function(lang) {
		return lang.toPunctuationString(this.str);
		
	}
	
	// Gibt einen Teil der Wörter zurück
	this.substr = function(n, len) {
		var str = "";
		if (len) {
			for (var i = n; i < len && i < this.length ; i++) {
				str += this.words[i] + " ";
			}
		}
		else {
			for (i = n; i < this.length; i++) {
				str += this.words[i] + "  ";
			}
		}
		return str;
	}
}