var add_admin_count = 0;
var add_folder_count = 0;
var UrlKeyTimer;

$(document).ready(function () {
	workspaceInit();
});

function workspaceInit() {
	$("a.ws-addfile").click(function(e) {
		e.preventDefault();
		if($("#form-ws-addfile").size() > 0) {
			$("#form-ws-addfile").remove();	
			$(".side-list-footer a").show();
		}
		$("#liste-"+ this.id).append('<div id="form-ws-addfile"></div>');
		$("#form-ws-addfile").hide();
		$("#form-ws-addfile").load(this.href, { ajaxCall : true }, function() {
			$("#form-ws-addfile").slideDown("normal");
		});
		$("#liste-"+ this.id +" a").fadeOut("normal");
	});
	$("a#workspace-edit").click(function (e) {
		e.preventDefault();
		if($("#form-wrapper").size() == 0) {
			$("div#page-wrapper").prepend('<div id="form-ws-edit"></div>');
			$("div#form-ws-edit").hide();
			$("div#form-ws-edit").load(this.href, { ajaxCall : true }, function() {
				$("div#form-ws-edit").fadeIn("normal");
			});
		}
	});
	$("a#create-post").click(function (e) {
		e.preventDefault();
		if($("#form-wrapper").size() == 0) {
			$("div#paging-top").after('<div id="form-wrapper"><div class="post-wrapper post-wrapper-edit"></div></div>');
			$("div#form-wrapper").hide();
			$("div#form-wrapper .post-wrapper-edit").load(this.href, { ajaxCall : true }, function() {
				$('#post-text').markItUp(mySettings);
				$('#markdown-info').hide();
				$('#show-markdown-info').click(function (e) {
					e.preventDefault();
					$('#markdown-info').slideDown();
				});
				$("div#form-wrapper").show("normal");
			});
		}
	});
	$("a.edit-post").click(function (e) {
		e.preventDefault();
		wrapperId = this.id.substring(5);
		$(this).fadeTo("normal", 0.1);

		$("div#"+wrapperId +" .post").after('<div id="form-wrapper"><div id="loading"></div></div>');
		$("div#"+wrapperId +" .post").slideUp("normal");

		$("div#form-wrapper").load(this.href, { ajaxCall : true }, function() {
			//MarkItUp Editor
			$('#post-text').markItUp(mySettings);
			$('#markdown-info').hide();
			$('#show-markdown-info').click(function (e) {
				e.preventDefault();
				$('#markdown-info').slideDown();
			});
		
			//$("div#form-wrapper").show();
			
		}); 
	});
	$("a.add-comment").click(function (e) {
		if($("#form-wrapper").size() > 0) {
			$("a.add-comment").fadeTo("fast", 1);
			$("#form-wrapper").remove();	
		}
		e.preventDefault();
		wrapperId = this.id.substring(5);
		$(this).fadeTo("normal", 0.1);

		$("div#"+wrapperId).append('<div id="form-wrapper"><div id="loading"></div></div>');

		$("div#form-wrapper").load(this.href, { ajaxCall : true }, function() {
			$.scrollTo($("#form-comment-add"), 1000, {easing:'swing', offset: -200, axis: 'y' } );
			$("div#form-wrapper button.highprio").click(function (e) {
				$(this).fadeTo("normal", 0.1); 
			});
		}); 
	});
	$("a.edit-comment").click(function (e) {
		e.preventDefault();
		$(this).fadeTo("normal", 0.1);		
		divId = this.id.substring(5);
		$("div#"+divId).append('<div id="loading"></div>');
		$("div#"+divId).load(this.href, { ajaxCall : true }); 
	});
	$("a.folder-toggle").click(function (e) {
		e.preventDefault();
		$("#list-"+this.id +" li").toggle();
		$("#list-"+this.id +" .side-list-head").toggleClass('collapsed');
	});
	$(".meta-functions a.delete").click(function (e) {
		e.preventDefault();
		if(confirm(this.title)) {
			if(this.title == '') {
				window.location = this.href;
			} else {
				delId = this.id.substring(4);
				$("div#"+ delId).load(this.href, { ajaxCall : true }, function() {
					$("div#"+ delId).fadeTo("normal", 0.1);				
					$("div#"+ delId).slideUp("slow");										
				});	
			}
		}
	});	
	$(".side-list a.delete").click(function (e) {
		e.preventDefault();
		if(confirm(this.title)) {
			delId = this.id.substring(4);
			if($("#form-wrapper-"+delId).size() > 0) {
				$("#form-wrapper-"+delId).hide();			
			}		
			if(this.title == '') {
				window.location = this.href;
			} else {
				delId = this.id.substring(4);
				$("li#"+ delId).load(this.href, { ajaxCall : true }, function() {
					$("li#"+ delId).fadeTo("normal", 0.1);				
					$("li#"+ delId).slideUp("slow");										
				});	
			}
		}
	});		
	$(".side-list a.edit").click(function (e) {
		e.preventDefault();
		editId = this.id.substring(5);
		if($("#form-wrapper-"+editId).size() == 0) {
			$("li#"+ editId).after('<div id="form-wrapper-'+ editId +'"></div>');
			$("#form-wrapper-"+editId).hide();
			$("#form-wrapper-"+editId).load(this.href, { ajaxCall : true }, function() {
				$(this).slideDown("slow");										
			});
		}
	});		
	
}

function editPostInit() {
	$("div#form-wrapper button.highprio").click(function (e) {
		$(this).fadeTo("normal", 0.1); 
	});		
	$("a#action-post-preview").click(function (e) {
		e.preventDefault();
		$(this).fadeTo("normal", 0.1);		
		$("input#post-publish").val('N');
		$("form#form-post-edit").submit();
	});
	$("a").not("#form-wrapper a").not("#create-post").not("a.delete").click(function (e) {
		if(!confirm("Bearbeiten abbrechen?")) {
			e.stopPropagation()		
			e.preventDefault();
		}
	});
}

function editMaterialInit() {
	$(".form-add-link").hide();
	$(".form-add-file").hide();
	$("a.show-add-link").click(function (e) {
		e.preventDefault();
		$(".form-add-file").hide();
		$(".form-add-link").show();		
	});
	$("a.show-add-file").click(function (e) {
		e.preventDefault();
		$(".form-add-file").show();
		$(".form-add-link").hide();		
	});
	$("a.hide-add-link").click(function (e) {
		e.preventDefault();
		$(".form-add-link").hide();		
	});
	$("a.hide-add-file").click(function (e) {
		e.preventDefault();
		$(".form-add-file").hide();
	});
	
	
}

function editWorkspaceInit() {

	$("#add-admin").hide();
	$("#add-folder").hide();
	$("#btn-add-admin").click(function(e) {
		e.preventDefault();
		add_admin_count++;
		$("#add-admin-wrapper").append('<input type="text" name="add-admin[]" value="" id="add-admin-' + add_admin_count +'" class="text" />');
		$("#add-admin-"+add_admin_count).autocomplete(baseUrl + "action/search-user", {
			width: 230,
			matchContains: true,
			minChars: 2
		});		
	});
	$("#btn-add-folder").click(function(e) {
		e.preventDefault();
		add_folder_count++;
		$("#add-folder-wrapper").append('<input type="text" name="add-folder[]" value="" id="add-folder-' + add_folder_count +'" class="text" />');
	});
	$("a.delete").click(function (e) {
		e.preventDefault();
		if(confirm(this.title)) {
			$("div#div-"+this.id).load(this.href, { ajaxCall : true });
		}
	});	
	$("#workspace-titel").keyup(function(e) {
		urlkeyDelay();
	});
	if($("#workspace-fach").val() == 0) {
		$("#fieldset-exam").hide();
	}
	$("#workspace-fach").change(function () {
		if($(this).val() != 0) {
			$("#fieldset-exam").slideDown("normal");			
		} else {
			$("#fieldset-exam").slideUp("normal");						
		}
	});
	
}

function urlkeyDelay() {
	window.clearTimeout(UrlKeyTimer);
	UrlKeyTimer = window.setTimeout("loadUrlkey()", 500);
}

function loadUrlkey() {
	$("#workspace-urlkey").load(baseUrl + "action/get-urlkey", { ajaxCall : true, q : $("#workspace-titel").val() }, function() {
		$("#workspace-urlkey").prepend("URL Key: ");
	});
}