
function Dedon() {
	var breite = 1000;
	var hoehe = 700;
	var language = 'de' // possible values de,en
	var jump = "micro";
	var seite = (screen.availWidth - breite) / 2;
	var oben = (screen.availHeight - hoehe) / 2;
	var sStatus; sStatus =
	"resizable=no,scrollbars=yes,location=no,menubar=no,status=no,width=" + breite +
	",height=" + hoehe + ",left=" + seite + ",top=" + oben;
	window.open("http://www.dedon.de/main.php?myLan=" + language + "&directJump=" +
	jump,"dedon",sStatus);
}

// toggles a menu item (subtree)
function toggle_subtree(id) {
	$("#m" + id).toggleClass("hide show");
	var pm = $("#m" + id).prev().prev("img");
	if(pm.attr("src") == "gfx/plus.gif")
		pm.attr("src","gfx/minus.gif");
	else
		pm.attr("src","gfx/plus.gif");
}

// opens the edit page (step==1) / saves the edit page (step==2=
function ajax_edit(id,step) {
	$.post("ajax_edit.php", $("#edit_form").serialize() + "&menu_id=" + id + "&step=" + step, function(data) { 
		$("#page_content").html(data);
		if(step == 2) {
			notify("Die Seite wurde gespeichert.");
			$("#s" + id).attr("class",$("#active").attr("checked") ? "active" : "inactive");
		}
	});
}

// creates new (inactive) page (db and menu tree)
function ajax_create() {                  
	$.post("ajax_create.php", "a=b", function(data) {	
		$("#m0 li:first").before("<li><div id=\"s" + data + "\" class=\"inactive\"><a href=\"javascript:ajax_edit(" + data + ",1)\">Neuer Eintrag</a></div></li>");
		notify("Eine neue Seite wurde erstellt.");			
	});	
}

// sets all pages to active (db and menu tree)
function ajax_activate() {
	$.post("ajax_activate.php", function(data) {
		$("#m0 div").attr("class","active");
		notify("Alle Seiten wurden aktiviert.");
	});
}

// removes an item from the menu (db and tree)
function ajax_remove(id) {
	if(confirm("Diese Seite wirklich löschen?")) {
		$.post("ajax_remove.php", "id=" + id, function(data) {
			if(data == "true") {
				notify("Die Seite wurde erfolgreich gelöscht.");
				$("#page_content").text("");
				$("#s"+id).parent().remove();
				$("#m"+id).remove();
			} else {
				notify("Eine zu löschende Seite darf keine Unterseiten besitzen.",false);
			}
		});
		return true;
	}
	else	
		return false;
}

// saves the current thumbnail arrangement
function ajax_arrange_thumbs(place) {
	// pos => id
	var thumbs = {};
	
   // get the menu strucutre
	$("#thumbs_" + place).find("div").each(function f(pos) {
		thumbs[pos] = $(this).attr("id").substring(1); 
	});

	// save in db (by calling arrange via post)
	$.post("ajax_arrange_thumbs.php", thumbs, function(data) {
		notify("Die Bilder wurden umsortiert.");
	});
}

// saves the current menu arrangement
function ajax_arrange() {
	// pos => {parent_id, id, active}
	var menu = {};
	
   // get the menu strucutre
	$("#m0").find("div").each(function f(pos) {
		var parent_id = $(this).closest("ol").attr("id").substring(1);
		parent_id = parent_id != '' ? parent_id : $(this).closest("ol").prev("div").attr("id").substring(1);
		var div_id = $(this).attr("id").substring(1);
		var div_class = $(this).attr("class") == "active" ? 1 : 0; 
		menu[pos] = new Array(parent_id, div_id, div_class); 
	});

	// save in db (by calling arrange via post)
	$.post("ajax_arrange.php", menu, function(data) {
		notify("Das Menü wurde gespeichert.");
	});
}

// build a new li containing the uploaded image with the given id, and append it to #thumbs
function edit_add_thumb(id,filename,place) { 
	var elemString = '<li class="no-nest"><div ondblclick="edit_image_details(' + id + ')" id="i' + id + '"><img src="../pictures/' + id + '_' + filename + '.jpg" alt="" class="thumb" /></div></li>';
	$(elemString).appendTo($("#thumbs_" + place));
	notify("Ein oder mehrere Bilder wurden hochgeladen.");
}

// opens a dialog to change alt and title of given picture id
function edit_image_details(id) {
	$.post("ajax_image_details.php", "id="+id, function(data) {
		var details = jQuery.parseJSON(data);
		$("#pic_id").val(details["id"]);
		$("#pic_details").find("img").first().attr("src","../pictures/" + details["id"] + "_" + details["name"] + ".jpg");
		//alert($("img, #pic_id").first().attr("src").val());
		$("#pic_alt").val(details["alt"]);
		$("#pic_title").val(details["title"]); 
		$("#pic_details").parent().toggleClass("hide show");
	});
}

// if update is true, saves alt and title for the picture id in the form #pic_details
function save_image_details(update) {
	$("#pic_details").parent().toggleClass("hide show");
	if(update) {	
		$.post("ajax_image_details.php", $("#pic_details").serialize(), function(data) {
 			$("#i" + $("#pic_id").val()).find("img").first().attr("title",$("#pic_title").val());
			notify("Die Bildinformationen wurden gespeichert.");
		});
	} else {
		notify("Die Bildinformationen wurden nicht geändert.", false);
	}
}


function delete_image() {
	$("#pic_details").parent().toggleClass("hide show");
	var id = $("#pic_id").val();
	$.post("ajax_delete_image.php", "id="+id, function(data) {
 			$("#i" + $("#pic_id").val()).remove();
			notify("Das Bild wurde gelöscht.", true);
		});
}


function show_content_type(ctype) {
	$("#ccontent").hide();
	$("#clist").hide();
	$("#cbrands").hide();
	$("#" + ctype).show();
}

// adds leading zeros while str.length < len
function lz(str, len) {
	for(i = String(str).length; i < len; i++)
		str = '0' + str;
	return str;
}

// returns the current client time as hh:mm:ss
function timestamp() {
	var d = new Date();
	return lz(d.getHours(),2) + ":" + lz(d.getMinutes(),2) + ":" + lz(d.getSeconds(),2);
}

// prints a notifications message on the top left of the screen
function notify(msg, good) {
	// default to good
	var notify_class = good == null || good ? "good" : "bad";
	$("#notify").attr("class",notify_class);
	$("#notify").html(timestamp() + " : <span>" + msg + "</span>");
}
