//------------------------------
//Mathematische Hilfsfunktionen
//------------------------------

function Mod(a, b) {
    return a - Math.floor(a / b) * b;
}

function Div(a, b) {
    return Math.floor(a / b);
}

function MakeArray(n){
	this.length=n;
	for(var i=1; i<=n; i++) this[i]=i-1;
	return this
}

hex=new MakeArray(16);
hex[11]="A"; hex[12]="B"; hex[13]="C"; hex[14]="D"; 
hex[15]="E"; hex[16]="F";

function ToHex(x){      // Changes a int to hex (in the range 0 to 255)
	var high=x/16;
    var s=high+"";       
    s=s.substring(0,2);  
    high=parseInt(s,10);  
    var left=hex[high+1]; 
    var low=x-high*16;    
    s=low+"";             
    s=s.substring(0,2);   
    low=parseInt(s,10);   
    var right=hex[low+1]; 
    var string=left+""+right; 
    return string;
  }

//------------------------------
//Form-Funktionen
//------------------------------

function getInputText(element_ID) {
	return document.getElementById(element_ID).value;
}

//------------------------------
//Allg. Grafik-Funktionen
//------------------------------
function clearAllJg() {
	jg_info.clear();
	jg_aufg.clear();
	//document.getElementById('div_info').innerHTML='';
	//document.getElementById('div_aufgaben').innerHTML='';
	//document.getElementById('div_aufgaben_buttons').innerHTML='';
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

//------------------------------
//Timer-Funktionen
//------------------------------

timer_value=0;
max_time=10;
timer_enabled = null;

function startTimer() {
	stopTimer();
	timer_value=0;
	updateTimer();
	timer_enabled = window.setInterval('updateTimer()',1000);
}

function stopTimer() {
	window.clearInterval(timer_enabled);
	timer_value=0;
	jg_info.clear();
}

function updateTimer() {	
	//Wichtig: Gezeichnete Objekte loeschen (Perfermance!)
	jg_info.clear();
	
	//Kuchen zeichnen
	if (timer_value < max_time) {
		r = ToHex(100+(timer_value*( (255-100)/ (max_time+1) )));
		//alert(r);
		jg_info.setColor("#"+r+"2929"); //Originalfarbe: #c42929
		jg_info.fillArc(0,0,120,70,0.0,Math.round(360 - ((360/max_time) * timer_value)));
	} 
	
	//Rand der Ellipse zeichnen
	jg_info.setColor("#000000");
	jg_info.setStroke(2);  
	jg_info.drawEllipse(0,0,119,69);
	
	//Zeichnen!
	jg_info.paint();

	//Vergangene Zeit aktualisieren
	if (timer_value > 0 && document.getElementById('aufgabe_sekunden'))
		document.getElementById('aufgabe_sekunden').innerHTML = Math.round(document.getElementById('aufgabe_sekunden').innerHTML) + 1;
	
	//Timer hochzaehlen
	timer_value += 1;
	
	if (timer_value > max_time) {
		if (getInputText('ergebnis') == '')
		{
			button = document.getElementById('ergebnis');
			button.value = '---'; //Ein leeres Feld wird nicht abgeschickt
		}
		
		Ergebnis();
	}
}

//------------------------------
//Funktionen fuer Aufgaben
//------------------------------

function startAufgaben() {
	clearAllJg();
	xajax_AufgabeButtons('');
	xajax_startGame('');
}

//Auf Eingabe den Fokus setzen und alles markieren
function selectEingabe() {
	if (document.getElementById('ergebnis')) 
	{
		button = document.getElementById('ergebnis');
		button.focus();
		button.select();
	}
}

//Ergebnis der Aufgabe an Server schicken
function Ergebnis(){

	//Nur Ergebnis pruefen, wenn Eingabe nicht leer ist
	//Sonst kann man den Request aus versehen ausloesen
	if (getInputText('ergebnis'))
	{
	
		button = document.getElementById('ergebnis');
		
		tmp = button.value;	
		button.value = ''; //Eingabefeld leern
		
		stopTimer();
		xajax_checkErgebnis(tmp);
	}
}

function stopAufgaben() {
	if (timer_enabled) 
	{
		clearAllJg();
		stopTimer();
		xajax_AnzeigeLoeschen('');
	}
}

//Nur waehrend dem Spiel soll das Div angezeigt werden, da es sonst bei der Bestenliste etc. stoert
function ButtonDivSetVisibility(yes_or_not)
{
	if (yes_or_not)
		showdiv('div_aufgaben_buttons');
	else
		hidediv('div_aufgaben_buttons');
}

//------------------------------
//Funktionen fuer Statistik
//------------------------------
function drawStatistik(draw_actions) {
	jg_aufg.clear();
	eval(draw_actions);
	jg_aufg.paint();
}

