var gGeklappt = "";

function getSicherSendMail() {
	if (confirm("Sicher verschicken? Letzte Warnung!")) {
		document.getElementById("serialmail").submit();
	}
}

function sendSerialMail() {
	document.getElementById("serialmail").submit();
}

function popOut(elem) {
	if (gGeklappt != "") {
		new Effect.SlideUpAndDown(gGeklappt);
	}
	if (elem == gGeklappt) {
		gGeklappt = "";
	} else {
		gGeklappt = elem;
		new Effect.SlideUpAndDown(elem);
	}
}

Effect.SlideUpAndDown = function(element) {
 		element = $(element);
 		if(Element.visible(element)) new Effect.Fade(element, {duration:0.3});
 		else new Effect.Appear(element, {duration:0.3});
}


function ajaxDisp(site, obj) {
	var zufall = Math.random();
	var param = "operation=open&site="+site+"&ran="+zufall;
	new Ajax.Updater(obj, "editor_ajax.php", {asynchronous:true, postBody:param, evalScripts: true});

}

function save(site, obj, saveObj) {
	var zufall = Math.random();
	var param = "operation=save&postcontent="+escape($(saveObj).value)+"&site="+site+"&ran="+zufall;
	new Ajax.Updater(obj, "editor_ajax.php", {asynchronous:true, postBody:param, evalScripts: true});
}

function cancel(site, obj) {
	var zufall = Math.random();
	var param = "operation=cancel&site="+site+"&ran="+zufall;
	new Ajax.Updater(obj, "editor_ajax.php", {asynchronous:true, postBody:param, evalScripts: true});
}

function createSite() {
	
	var zufall = Math.random();
	var param = "operation=createSite&site="+$('siteID').value+"&postcontent="+escape($(content).value)+"&ran="+zufall;

	new Ajax.Updater("raum", "editor_ajax.php", {asynchronous:true, postBody:param, evalScripts:true});
}

function deleteSite(site) {
	if (confirm('Sicher Site '+site+' entfernen? Wiederrufen nicht moeglich!')) {
		var zufall = Math.random();
		var param = "operation=deleteSite&site="+site+"&ran="+zufall;
		new Ajax.Updater("raum", "editor_ajax.php", {asynchronous:true, postBody:param, evalScripts:true});
	}
}

function editornew() {
	var site = prompt("Bitte geben Sie eine Site-Nummer ein:","");
	if (site) {
		var idvar = 'site'+site;
		tr = Builder.node('tr');
		td1 = Builder.node('td',[ site ]);
		td2 = Builder.node('td', [
			Builder.node('div', {id:idvar,className:"smallFont"}, [
				Builder.node('div',{onclick:"ajaxDisp("+site+",'site"+site+"');"},'Bitte bearbeiten...')
			]),
		]);
		tr.appendChild(td1);
		tr.appendChild(td2);
		$('editor_table').appendChild(tr);

		var zufall = Math.random();
		var param = "operation=editornew&site="+site+"&ran="+zufall;

		new Ajax.Request("editor_ajax.php", {asynchronous:true, postBody:param, evalScripts:true});
	}
}


// ------------- File-Functions ---------------- //

function file_delete(path) {
	if (confirm("Sind Sie sicher, dass Sie diese Datei dauerhaft entfernen wollen?\nAchtung: Nur leere Ordner können gelöscht werden!")) {
		var url = "_file_functions.php?op=delete&filePath=" + path;
		file_doAjax(url);
	}
}

function file_createFolder(path) {
	var title = prompt("Wie wollen Sie den neuen Ordner nennen?");
	if (title) {
		var url = "_file_functions.php?op=createFolder&path=" + path + "/"+ title;
		file_doAjax(url);
	}
}

function file_memeForCopy(path, name) {
	var url = "_file_functions.php?op=memeForCopy&path=" + path + "&name=" + name;
	alert("Datei zum verschieben markiert.");
	file_doAjax(url);
}

function file_moveFile(path) {
	var url = "_file_functions.php?op=moveFile&path=" + path;
	file_doAjax(url);
}

function file_rename(path, oldName) {
	newName = prompt("Welchen neuen Namen wollen Sie der Datei geben?\nBitte Endungen beachten!", oldName);
	if (newName){
		
		var url = "_file_functions.php?op=rename&path=" + path + "&oldName=" + oldName + "&newName=" + newName;
		file_doAjax(url);
	}
}

function set_start_dir(set) {

	var url = "_file_functions.php?op=start_dir_set&set="+set;
	file_doAjax(url);
	
}

function file_doAjax(url) {
	new Ajax.Request(url, {
	  method: 'get',
	  onSuccess: function(t) {
	    window.location.reload(true);
	  }
	});
}

// ------------- OOP - Diashow ---------------- //


// Die Eingabe der Daten muss als Array erfolgen. Relative URLs möglich.
var gDia;
function Diashow(pix) {
	// Geschwindigkeit:
	this.changeSpeed = 4000;
	
	
	this.ChangePic = ChangePic;
	this.FadePic = FadePic;
	this.nextPic = 0;
	this.picSer = 0;
	this.DelPic = DelPic;
	this.minus = 0;
	this.ChangePic();
}

function ChangePic() {
	element = Builder.node('div', {className:'diashowPic', id:'pic'+this.picSer, style: "display:none;"},[
		Builder.node('img', {src: pix[this.nextPic]})
	]);
	$('diashow').appendChild(element);
	//this.FadePic();
	gDia = this;
	if (this.picSer == 0) {
		$('pic'+this.picSer).style.display="block";
		this.nextPic += 1;
		this.picSer += 1;
		this.ChangePic();
	} else {
		setTimeout("gDia.FadePic()", this.changeSpeed);
	}
}

function FadePic() {
	new Effect.Appear('pic'+this.picSer, {afterFinish: function(){gDia.DelPic();} });
	this.nextPic += 1;
	this.picSer += 1;
	if (this.nextPic == pix.length) {
		this.nextPic = 0;
	}
	this.ChangePic();
}
function DelPic() {
	if (this.minus >= 0) Element.remove('pic'+this.minus);
	this.minus +=1;
}
