// JavaScript Document
var dir = 'down';
var started = false;

function toggleBox(arrow,box)
{
	if(box.style.display == 'none' && dir == 'down' && !started)
	{
		started = true;
		arrow.src = 'img/arrow_u.jpg';
		new Effect.BlindDown(box, {afterFinish: function(){ started = false; dir = 'up'} } );
	}
	else if(dir == 'up' && !started)
	{
		started = true;	
		arrow.src = 'img/arrow_d.jpg';	
		new Effect.BlindUp(box, { afterFinish: function(){ started = false; dir = 'down'} });			
	}
}

function initMail()
{
	var string = '';
	for(var x=0;x<arguments.length;x++){
		string += Base64.decode(arguments[x]);
	}
		
	$('mail').appendChild(Builder.node('span',[ Builder.node('a', { href: 'mailto:'+string}, string ) ]));
}

function showMoreContent(e)
{
	var current;
	var counter = 0;
	current = e;
	while(current.className != 'content-hidden'){
		current = current.nextSibling;
		if(counter < 5)
			counter++;
		else
			break;
	}
	try{
		if(current.style.display == 'block')
			current.style.display = 'none';
		else
			current.style.display = 'block';	
	}catch(e)
	{}

}


/* test*/
var watchblanko = false;
var watchStates = new Array('Uhr_1.jpg','Uhr_2.jpg','Uhr_3.jpg','Uhr_4.jpg');
var watchPos    = 0;
var watchTimer;
var watchImgPath;
var watchForm;
var watchAjaxPage;
var watchAjaxDir;
var watchNextPage;
var watchSerializedForm = false;
var watchFormSent = false;
var watchAutoSubmit = false;

function navigateTo(ajax_directory,name,container)
{

	//possible 4th parameter for changing of navigation
	if(arguments.length >= 5){
		var navi_dir = arguments[3];
		var navi_id = arguments[4];
	}
	
	clearInterval(watchTimer);
	
	if(arguments.length == 6)
		var bgImage = arguments[5];	
		
	if(name.indexOf('.') == -1)
		var page = name+'.html';
	else
		var page = name;
	new Ajax.Request(ajax_directory+'/'+page,{
						  method: 'post',
						  onSuccess: function(e)
						  {
							  if(navi_dir)
							  {
								  //var cache = new Image();
								  //cache.src(navi_dir+'/'+name+'.jpg');
								  
								  if(bgImage){
	  								  $(navi_id).style.backgroundImage = 'url('+navi_dir+'/'+bgImage+')';
								  }
								  else{
									  $(navi_id).style.backgroundImage = 'url('+navi_dir+'/'+name+'.jpg)';
								  }
							  }
							  
							  $(container).update(e.responseText);
							  $(container).scrollTop = 0;
							  watchReset();
							  
						  },
						  onFailure: function(e)
						  {
							  alert("Es ist ein Fehler aufgetreten: Die Daten können nicht geladen werden!");
						  }
					 });
}

function watchReset()
{
	  clearInterval(watchTimer);			
	  watchFormSent = false;
	  watchSerializedForm = false;
	  $('watch').src = watchblanko;
	  watchPos = 0;	
}


function startWatch(secs,path,formname,nextPage,ajaxPage,ajaxDir)
{
	watchImgPath = path;
	watchForm = formname;
	watchTimer = setInterval(updateWatch, (secs*1000)/4);
	watchNextPage = nextPage;
	watchAjaxPage = ajaxPage;
	watchAjaxDir  = ajaxDir;
	
	if(arguments.length == 7)
		watchAutoSubmit = arguments[6];
	else
		watchAutoSubmit = false;

}

function updateWatch()
{
	$('watch').src = watchImgPath+watchStates[watchPos++];
	checkWatchEnd();
}

function checkWatchEnd()
{
	if(watchPos == 4)
	{
		clearInterval(watchTimer);		
		watchDisable();
	}
}

function watchDisable(){
	if(!watchSerializedForm)
		watchSerializedForm = $(watchForm).serialize();
		
	try{
		var f = document[watchForm];
		var len=f.elements.length;
		for(x=0;x<len;x++) {
			f.elements[x].disabled=true;
		}

		if(watchAutoSubmit) {		
			testNext();
		}
		else
			alert('Die Zeit ist leider abgelaufen, bitte fahre mit der nächsten Aufgabe fort.');
	}catch(e)
	{ }
}		



function checkForm(form, originalColor)
{
	
	//reset
	for(var x=0;x<form.elements.length;x++)
	{
		form.elements[x].style.borderColor = originalColor;
	
		if(form.elements[x].className == 'mustField' && form.elements[x].value == ''){
			alert('Bitte geben Sie Ihre(n) '+form.elements[x].name+' ein!');
			form.elements[x].style.borderColor = '#FF0000';
			form.elements[x].focus();
			
			return false;
		}
	}
	
}

function checkFormLehrerinformationen(form, originalColor) {
	if(checkForm(form, originalColor) != false) {
		var bezugsquelle = form.elements['bezugsquelle'];
		var kontaktanfrage = form.elements['kontaktanfrage'];
		
		if(bezugsquelle[0].checked && bezugsquelle[1].checked) {
			alert('Bitte geben Sie an, ob Sie das Unterrichtsmaterial per Post erhalten oder downloaden wollen! Sie können nur eine der beiden Optionen wählen!');
			return false;
		}
		
		if(!(bezugsquelle[0].checked || bezugsquelle[1].checked || kontaktanfrage.checked)) {
			alert('Bitte geben Sie an, ob Sie das Unterrichtsmaterial per Post erhalten oder downloaden wollen!');
			return false;
		}
		return true;
	}
	return false;
}

function testNext()
{
	if(!watchblanko)
		watchblanko   = $('watch').src;
		
	if(arguments.length >= 6){
		watchForm 	  = arguments[0];
		watchAjaxDir  = arguments[1];
		watchNextPage = arguments[2];			
		watchAjaxPage = arguments[3];		
		
		var imgPath = arguments[4];
		var id		= arguments[5];
	}	
	
	if(arguments.length == 7){
		var bgImage = arguments[6];
	}
	

	if(!watchSerializedForm)
		watchSerializedForm = $(watchForm).serialize();
		


	if(!watchFormSent)
	{
		
		new Ajax.Request(watchAjaxPage,{
						  method: 'POST',
						  parameters:watchSerializedForm,							  						  
						  onSuccess: function(e)
						  {

							  watchFormSent = true;
							  if(e.responseText.length > 30)
							  {
								  $('container').update(e.responseText);
								  $('container').scrollTop = 0;								  								  
								  watchReset();
							  }
							  else
							  {
								  
								  if(id && imgPath){
									  if(bgImage)
										  navigateTo(watchAjaxDir,watchNextPage,'container',imgPath,id,bgImage);
									  else							  
										  navigateTo(watchAjaxDir,watchNextPage,'container',imgPath,id);							  							  
								  }
								  else
									  navigateTo(watchAjaxDir,watchNextPage,'container');							  							  
							  }

							  //console.log(e.responseText);
						  },
						  onFailure: function(e)
						  {
							  alert("Es ist ein Fehler aufgetreten: Die Daten können nicht geladen werden!");
						  }
					 });
	}
	
	return false;			
}

Array.prototype.in_array = function(needle) {
for(var i=0; i < this.length; i++) if(this[ i] === needle) return true;
return false;
}

var correctAnswersA = [44,52,56,80,90,12,16,64,38,88,64,60];

function testTableA(id)
{
	if(correctAnswersA.in_array(parseInt($(id).innerHTML)))
	{
		if($(id).className == 'activeTdTest')
		{
			$(id).className = '';
			$('cor').value = (parseInt($('cor').value) - 1);			
		}
		else
		{			
			$(id).className = 'activeTdTest';		
			$('cor').value = (parseInt($('cor').value) + 1);
		}
	}
	else
	{
		if($(id).className == 'activeTdTest')
		{
			$(id).className = '';
			$('cor').value = (parseInt($('cor').value) + 1);			
		}
		else
		{			
			$(id).className = 'activeTdTest';		
			$('cor').value = (parseInt($('cor').value) - 1);
		}		
	}
}


var correctAnswersB = [52,56,90,12,16,38];

function testTableB(id)
{
	if(correctAnswersB.in_array(parseInt($(id).innerHTML)))
	{
		if($(id).className == 'activeTdTest')
		{
			$(id).className = '';
			$('cor').value = (parseInt($('cor').value) - 1);			
		}
		else
		{			
			$(id).className = 'activeTdTest';		
			$('cor').value = (parseInt($('cor').value) + 1);
		}
	}
	else
	{
		if($(id).className == 'activeTdTest')
		{
			$(id).className = '';
			$('cor').value = (parseInt($('cor').value) + 1);			
		}
		else
		{			
			$(id).className = 'activeTdTest';		
			$('cor').value = (parseInt($('cor').value) - 1);
		}		
	}
}





var defaultWorld = false;

function worldOver(image)
{
	if(!defaultWorld)
		defaultWorld = $('world').src;

	$('world').src = image;
}

function worldOut()
{
	$('world').src = defaultWorld;
}



var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },
  // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}



