var base = "http://www.bodenseecoaching.de";
var adminpanel = 'on';
$(document).ready(function(){
	toggleAdminPanel();
	
	$('#content h4').css('color','#465862');
	$('#content h4').css('text-decoration','underline');
	$('#content h4').css('cursor','pointer');
	
	$('h4').each(function(){
		hideAll(this);
	});
	
	$('h4').click(function(i){
		//console.clear();
		myToggle(this);
	});
	
	function hideAll(obj){
		if ($(obj).next().get(0)) {
			var name = $(obj).next().get(0).tagName;
			if (name == "H3" || name == "P" || name == "UL"  || name == "OL" || name == "IMG") {
				$(obj).next().hide();
				//console.info(name);
				hideAll($(obj).next());
			}
			else {
				//console.info("--------------");
				return null;
			}
		}
		else {
			return null;
		}
	}
	
	function myToggle(obj){
		if ($(obj).next().get(0)) {
			var name = $(obj).next().get(0).tagName;
			if (name == "H3" || name == "P" || name == "UL"  || name == "OL" || name == "IMG") {
				$(obj).next().slideToggle();
				//console.info(name);
				myToggle($(obj).next());
			}
			else {
				return null;
			}
		}
		else {
			return null;
		}
	}

});

function toggleAdminPanel(){
	if (adminpanel=="off") {
		$('.admin-panel').each(function(){
			$(this).hide();
		});
		$('.admin').each(function(){
			$(this).attr('class','admin-off');
		});
		$('#admin-menu-toggle').html('Bedienelemente anschalten');
		$('#subpage_of-edit-p').hide();
		$('#item_of-edit-p').hide();
		$('#redirect_to-edit-p').hide();
		$('#photo_sidebar-edit-p').hide();
		$('#order-edit-p').hide();
		$('#titel_navi-edit-p').hide();
		$('#status-edit-p').hide();
		$('#delete-page').hide();
		adminpanel ="on";
		return false;
	}else{
		$('.admin-panel').each(function(){
			$(this).show();
		});
		$('.admin-panel.hidden').each(function(){
			$(this).hide();
		});
		$('.admin-off').each(function(){
			$(this).attr('class','admin');
		});
		$('#admin-menu-toggle').html('Bedienelemente ausschalten');
		$('#subpage_of-edit-p').show();
		$('#item_of-edit-p').show();
		$('#redirect_to-edit-p').show();
		$('#photo_sidebar-edit-p').show();
		$('#order-edit-p').show();
		$('#titel_navi-edit-p').show();
		$('#status-edit-p').show();
		$('#delete-page').show();
		adminpanel ="off";
		return false;
	}
}

function edit(elem){
	$("#"+elem+"-edit-ap").hide();
	$("#"+elem+"-cancel-ap").show();
	$("#"+elem).fadeOut('fast');
	window.setTimeout(function(){
		$("#"+elem+"-edit").fadeIn('fast');
	},200);
	return false;
}

function cancel(elem){
	$("#"+elem+"-edit").fadeOut('fast');
	$("#"+elem+"-edit-ap").show();
	$("#"+elem+"-cancel-ap").hide();
	window.setTimeout(function(){
		$("#"+elem).fadeIn('fast');
	},200);
	return false;
}

function save(id,elem){
	var val = $("#"+elem+"-edit").val();
	
	$.ajax({
	   	type: "POST",
		url: base+"/edit/"+id+"/"+elem+"/"+val,
	   	success: function(msg){
	     	$("#"+elem+"-edit").fadeOut('fast');
			window.setTimeout(function(){
				if (elem == "photo_1" || elem == "photo_2" || elem == "photo_3" || elem == "photo_4") {
					$("#" + elem).html('<img src="' + base + '/photos/' + msg + '" />');
				}
				else {
					$("#" + elem).html(msg);
				}
				$("#"+elem).fadeIn('fast');
			},200);
			$("#"+elem+"-edit-ap").show();
			$("#"+elem+"-cancel-ap").hide();
	   	}
 	});
	return false;
}

function savePost(id,elem){
	var value = $("#"+elem+"-edit").val();
	$.post(base+"/edit/"+id+"/"+elem, { value: value }, function(msg){
   		$("#"+elem+"-edit").fadeOut('fast');
			window.setTimeout(function(){
				if (elem == "photo_1" || elem == "photo_2" || elem == "photo_3" || elem == "photo_4") {
					$("#" + elem).html('<img src="' + base + '/photos/' + msg + '" />');
				}
				else {
					$("#" + elem).html(msg);
				}
				$("#"+elem).fadeIn('fast');
			},200);
			$("#"+elem+"-edit-ap").show();
			$("#"+elem+"-cancel-ap").hide();
	});
	return false;
}
