function notice(action, msg){
	msg = msg || 'Bitte warten, während die Daten vom Server geladen werden …';
	switch(action){
		case 'show':
			loadingmsg = document.createElement("p");
			loadingmsg.className = 'smallnotice';
			loadingmsgText = document.createTextNode(msg);
			loadingmsg.appendChild(loadingmsgText);
			document.body.insertBefore(loadingmsg, document.body.firstChild);
			break;
		case 'hide':
			document.body.removeChild(loadingmsg);
	}
}
function toggle(id){
  var element = document.getElementById(id).style;
  if(element.display == 'none') {
    element.display = 'block';
  } else {
    element.display = 'none';
  }
}
function createbg() {
	allow_keyboard = false;

    var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	// für IE
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // alle außer IE für Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {  // für alle Anderen
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// für den IE im Quirks-Mode
		windowWidth = self.inne;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // für den IE im Strict-Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // alles außer IE
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
/*	
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
*/	
	
  var div = document.createElement("div");
  var Style = document.createAttribute("style");
  Style.nodeValue = "position: absolute; top: 0; left: 0; width: 100%; height: " + pageHeight + "px; background-color: black; -moz-opacity: 0.85; opacity: 0.85; z-index: 9";
  div.setAttributeNode(Style);
  var id = document.createAttribute("id");
  id.nodeValue = "background";
  div.setAttributeNode(id)
  var firstdiv = document.getElementsByTagName("div")[0];
  document.body.insertBefore(div,firstdiv);
}

function showremovevalue(element, action){
	if(action == 'show'){
		if(element.value == '')
		  element.value == element.defaultValue;
	}
	if(action == 'remove'){
	  if(element.value == element.defaultValue)	
	    element.value = '';
	}
}

function showloginform() {
  createbg();
  
  var formdiv = document.createElement("div");
  var styleFormdiv = document.createAttribute("style");
  styleFormdiv.nodeValue = "position: absolute; border: 3px solid black; background-color: white; width: 240px; top: 50%; height: 170px; left: 50%; padding: 35px; text-align: center;z-index: 10; margin-left:-138px; margin-top: -80px;";
  formdiv.setAttributeNode(styleFormdiv);
  formdiv.id = "loginform";

  var firstdiv = document.getElementsByTagName("div")[0];
  document.body.insertBefore(formdiv,firstdiv);
  
  loadingGraphic = new Image();
  loadingGraphic.src = path+"/assets/ajax-load.gif";
  
  var formContainer = document.createElement("form");
  formContainer.method = "post";
  formContainer.action = "anmelden.php";
  formContainer.onsubmit = function(e){
    notice('show', 'Bitte warte, während deine Daten an den Server geschickt werden ...');
    $('input.submit[name=submit_login]').val('Bitte warten ...').attr('disabled',true);
    $.post(path+"/ajax.php?action=login", { name: inputUsername.value, passwort: inputPassword.value, check: inputCheck.value}, function(response){
        notice('hide');
      	if(response == 1){
      	  alert("Du hast dich erfolgreich angemeldet!");
      	  $("#loginform").html("<p style='font-size:20px;'>Bitte warten …<br /><img src='"+path+"/assets/ajax-load.gif' alt='loading ...' /></p>");
      	  if(window.location.pathname=="/neueintrag") window.location.pathname="/";
      	  else window.location.reload();
      	} else {
      	  alert("Bitte überprüfe deine Eingaben auf Tippfehler, der Loginversuch schlug fehl.")
		  $('input.submit[name=submit_login]').val('Abschicken').attr('disabled',false);
	   	  return false;
      	}
      }
	);
	return false;
  }
  formdiv.appendChild(formContainer);
  
  var inputUsername = document.createElement("input");
  inputUsername.type = "text";
  inputUsername.name = "username";
  inputUsername.defaultValue = "Dein Vor- und Nachname...";
  inputUsername.addEventListener('focus', function() { if(this.value == this.defaultValue) this.value = '';	}, false);
  inputUsername.addEventListener('blur', function() { if(this.value=='') this.value = this.defaultValue; }, false);
  formContainer.appendChild(inputUsername);

  inputPassword = document.createElement("input");
  inputPassword.type = "password";
  inputPassword.name = "password";
  inputPassword.defaultValue = "Passwort";
  inputPassword.addEventListener('focus', function() { if(this.value == this.defaultValue) this.value = ''; }, false);
  inputPassword.addEventListener('blur', function() { if(this.value=='') this.value = this.defaultValue; }, false);
  formContainer.appendChild(inputPassword);
  
  inputSubmit = document.createElement("input");
  inputSubmit.type = "submit";
  inputSubmit.name = "submit_login";
  inputSubmit.value = "Abschicken!";
  inputSubmit.className = "submit";
  formContainer.appendChild(inputSubmit);
  document.forms[0].submit_login.focus();
  
  labelCheck = document.createElement("label");
  labelCheck.for = "check";
  text = document.createTextNode("Automatisch einloggen?");
  labelCheck.appendChild(text); 
  labelCheck.title = "Empfehlenswert bsplsw. zu Hause, aber keinesfalls in Internetcafés o.Ä. aktivieren."
  formContainer.appendChild(labelCheck);

  inputCheck = document.createElement("input");
  inputCheck.type = "checkbox";
  inputCheck.name = "check";
  inputCheck.id = "check";
  inputCheck.value = "1";
  inputCheck.className = "check";
  labelCheck.appendChild(inputCheck);

  /* Zum Ausblenden des BG */

  var hideForm = document.createElement("p");
  formdiv.appendChild(hideForm);
  
  var hidelink = document.createElement("a");
  hidelink.href = "javascript: hidebg('loginform')";
  hideForm.appendChild(hidelink);
  var textLink = document.createTextNode("Login-Formular ausblenden");
  hidelink.appendChild(textLink); 

  var pw_forgotten = document.createElement("p");
  formdiv.appendChild(pw_forgotten);

  var forgotten_link = document.createElement("a");
  forgotten_link.href = path+"/passwortvergessen";
  var forgotten_text = document.createTextNode("Passwort vergessen?");
  pw_forgotten.appendChild(forgotten_link);
  forgotten_link.appendChild(forgotten_text);

}
function hidebg(addId) {
  allow_keyboard = true;
  document.getElementsByTagName("body")[0].removeChild(document.getElementById("background"));
  document.getElementsByTagName("body")[0].removeChild(document.getElementById(addId));
}
function makehigher(rows,id){
  element = document.getElementById(id);
  element.setAttribute("rows", element.rows + rows);
}
function showpollmsg(){
  createbg();
  
  var div = document.createElement("div");
  var styleDiv = document.createAttribute("style");
  styleDiv.nodeValue = "position: absolute; border: 3px solid black; background-color: white; width: 300px; top: 45%; height: 150px; left: 35%; padding: 5px; text-align: center;z-index: 10";
  div.setAttributeNode(styleDiv);
  var idDiv = document.createAttribute("id");
  idDiv.nodeValue = "messageform";
  div.setAttributeNode(idDiv);
  var firstdiv = document.getElementsByTagName("div")[0];
  document.body.insertBefore(div,firstdiv);
  
  var headline = document.createElement("h2");
  var text = document.createTextNode("Aktuelle Umfrage!");
  headline.appendChild(text); 
  div.appendChild(headline);

  var message = document.createElement("p");
  var text = document.createTextNode("Es gibt ein wichtiges, aktuelles Thema auf der Aktuell-Seite: ein Abschiedsgeschenk fǬr Leia. Bitte stimme bei der Umfrage mit ab! ");
  message.appendChild(text); 
  div.appendChild(message);
  
  var linkto = document.createElement("a");
  var text = document.createTextNode("Gehe zur Umfrage");
  linkto.appendChild(text); 
  var linktarget = document.createAttribute("href");
  linktarget.nodeValue = "aktuell.php";
  linkto.setAttributeNode(linktarget);
  message.appendChild(linkto);
}

function editha(id){
	element = $('#el_' + id);
	childs = element.childNodes;
	bold = childs[0].firstChild.data;
	text = childs[1].data;
	pos = bold.lastIndexOf(":");
	if(pos != -1)	bold = bold.slice(0, pos);
/*	alert(bold);
	alert(text);*/
	createbg();
	$('#background').onclick = function() { hidebg('editha'); };

	var formdiv = document.createElement("div");
  var styleFormdiv = document.createAttribute("style");
  styleFormdiv.nodeValue = "position: absolute; border: 3px solid black; background-color: white; width: 400px; top: 50%; height: 240px; left: 50%; padding: 5px; text-align: center;z-index: 10; margin-left:-200px; margin-top: -120px;";
  formdiv.setAttributeNode(styleFormdiv);
  formdiv.id = "editha";
  var firstdiv = document.getElementsByTagName("div")[0];
  document.body.insertBefore(formdiv,firstdiv);

  
  var formContainer = document.createElement("form");
  formContainer.method = "post";
  formContainer.action = "ajax.php";
  formdiv.appendChild(formContainer);


  input = document.createElement("input");
  input.type = "text";
  input.value = bold;
  formdiv.appendChild(input);
  
  textarea = document.createElement("textarea");
  textarea.value = text;
  formdiv.appendChild(textarea);
  
  submit = document.createElement("input");
  submit.type = "button";
  submit.className = "submit";
  submit.value = "Abschicken";
  
  submit.onclick = function (e) { 
    if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    load = document.createElement("img");
    load.src = "assets/lightbox/loading.gif";
    load.alt = "Daten werden gesendet ..."
    formdiv.appendChild(load);
    xhr.open("POST", "ajax.php?action=editha", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("fach=" + input.value + "&inhalt="+textarea.value+"&id="+id);
    xhr.onreadystatechange = function(){
      if (xhr.readyState == 4) {
      	hidebg("editha");
        element.innerHTML = '<b>'+input.value+':</b> '+textarea.value+' <button onclick="editha('+id+')"><img src="assets/edit.png" alt="Bearbeiten" /></button><button onclick="delha('+id+')"><img src="assets/delete.png" alt="Löschen" /></button>';
        text = xhr.responseText;
        alert(text);
      }
    }
  }
  formdiv.appendChild(submit);
  
  var hideForm = document.createElement("p");
  formdiv.appendChild(hideForm);
  
  var hidelink = document.createElement("a");
  var targetlink = document.createAttribute("href");
  targetlink.nodeValue = "javascript: hidebg('editha')";
  hidelink.setAttributeNode(targetlink);
  hideForm.appendChild(hidelink);
  var textlink = document.createTextNode("Overlay ausblenden");
  hidelink.appendChild(textlink); 
}
function delha(id){
  if(confirm("Möchtest du diese Hausaufgabe wirklich löschen?")){
    if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("POST", "ajax.php?action=delha", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("id="+id);
    xhr.onreadystatechange = function(){
      if (xhr.readyState == 4) {
        text = xhr.responseText;
//        alert(text);
        $('#el_'+id).parentNode.removeChild($('#el_'+id));
      }
    }
  
  }
}
function is_IE() {
  if(navigator.appName == "Microsoft Internet Explorer") return true;
  else return false;  
}

function yesno(category, col, id, element, confirm) {
  notice('show');
  $.post('/ajax.php?action=switchYesNo', {
    id: id 	, cat: category, col: col }, function(ans){
      notice('hide');
      if(ans.responseText != 0) {
        response = ans.responseText;
        text = document.createTextNode(response);
        if(col != 'delete') {
          element.removeChild(element.firstChild);
          element.appendChild(text);
        } else element.parentNode.removeChild(element);
      }
      else alert("Konnte nicht gespeichert werden …");
    }
  );
}

function switchCourseMemberStatus(courseid){
	notice('show', 'Bitte warte, während die Daten an den Server geschickt werden.');
	$.post(path+'/ajax.php?action=switchCourseMemberStatus', { courseid: courseid }, function(response){
		$.post(path+'/ajax.php?action=getCourseMembers', {courseid:courseid}, function(response2){
			if(response2 == '') response2 = '<em>Keine Kursteilnehmer</em>';
			$("span.members").html(response2);
			if(response == '1') $("button").html("Bin kein Mitglied mehr!");
			if(response == '0') $("button").html("Bin Mitglied dieses Kurses!");
		});
		notice('hide');
	});
}
function swapString(string){
	var erg = '';
	for (i=1; i<string.length+1; i++){
		erg+=string.substring(string.length-i,string.length-i+1);
	}
	return erg;
}
$(document).ready(function(){
	$(".email").each(function(a){
		value = $(this).html();
		atpos = value.search(/@/);
		$(this).html(swapString(value.substr(0,atpos)) + '@' + swapString(value.substr(atpos+1, value.lastIndexOf('.') - atpos - 1)) + value.substr(value.lastIndexOf('.')));  
	});
	$("button.upload").click(function(){
		$(this).html("Bitte warten …");
		$(this).attr("disabled",true);
	});
});

function setUploadType(element){
	val = $(element).val();
	fieldset = $(element).closest("fieldset");
	$("#label-"+val).slideDown("normal");
	$(element).closest("label").fadeOut();
	
}