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

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

function workspaceInit() {
	$("a#create-post").click(function (e) {
		e.preventDefault();
		$("div#paging-top").after('<div id="form-wrapper"><div class="post-wrapper post-wrapper-edit"><div id="loading"></div></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();
		$(this).fadeTo("normal", 0.1);		
		
		wrapperId = this.id.substring(5);

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

		//$("div#form-wrapper").hide();
		$("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(6);
		$(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() {
			$("div#form-wrapper button.highprio").click(function (e) {
				$(this).fadeTo("normal", 0.1); 
			});				
		}); 
	});
	$("a.edit-comment").click(function (e) {
		e.preventDefault();
		divId = this.id.substring(5);
		$(this).fadeTo("normal", 0.1);		
		$("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");										
				});	
			}
		}
	});	
	
	//
	$("a.favorite").click(function (e) {
		e.preventDefault();
		$(this).fadeTo("normal", 0);		
		idx = this.id.substring(9);
		
		var t=$(this);
	    var o={};
	  
		o.idx=idx;
        o.ajaxCall=true;
	  	$.getJSON(this.href,o,function (result) {
	  	    if(result.ok){
			  t.hide();
			  $("#unfavorite-"+idx).fadeTo("normal", 1);
			
		   }
		   
		});
	});
	$("a.unfavorite").click(function (e) {
		e.preventDefault();
		$(this).fadeTo("normal", 0);		
		idx = this.id.substring(11);
		
		var t=$(this);
	    var o={};
	  
		o.idx=idx;
        o.ajaxCall=true;
	  	$.getJSON(this.href,o,function (result) {
	  	    if(result.ok){
			    t.hide();
    			 $("#favorite-"+idx).fadeTo("normal", 1);
			  
		   }
		   
		});
	});
	//
	
	
}

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.delete").click(function (e) {
		e.preventDefault();
		if(confirm(this.title)) {
			$("div#div-"+this.id).load(this.href, { ajaxCall : true });
		}
	});	
	$("a").not("#form-wrapper a").click(function (e) {
		if(!confirm("Bearbeiten abbrechen?")) {
			e.stopPropagation()		
			e.preventDefault();
		}
	});
}
