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(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() {
			$.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.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);
			  
		   }
		   
		});
	});
	
	$("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)) {
			var link = this;
			$.postJSON(this.href, {
				ajaxCall : true
			}, function (data) {
				if(data.success) {
					$(link).parents('li').html('<span>Auf Wiedersehen</span>').slideUp("slow");
				}
			});
		} else {
			return false;
		}
	});		
	$(".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");										
			});
		}
	});
	$("a#workspace-invite").click(function(e) {
		e.preventDefault();
		if($("#overlay-invite").size() == 0) {
			$("body").append('<div id="overlay-invite"></div>');
		}
		$("#overlay-invite").hide().css("top", ($(this).offset().top-89) + "px");
		$("#overlay-invite").load(this.href, {
			ajaxCall : true
		}, function() {
			$("#overlay-invite-receiver").autocomplete(baseUrl + "action/search-user", {
				width: 330,
				matchContains: true,
				minChars: 2
			});
			$("#overlay-invite").fadeIn("fast");
			$("#overlay-invite-message").focus();
			$("#overlay-invite .go-back").click(function(ev) {
				ev.preventDefault();
				$("#overlay-invite").hide();
			});
			$("#overlay-invite form").submit(function(ev) {
				ev.preventDefault();
				var post = {
					ajaxCall : true
				};
				var postdata = $("#overlay-invite :input").serializeArray();
				jQuery.each(postdata, function(i, field){
					post[field.name] = field.value;
				});
				$.post($("#overlay-invite form").attr("action"), post, function(result){
					$("#overlay-invite .form-wrapper").slideUp("fast");
					$("#overlay-invite").append('<div id="sendmsg-result">' + result +'</div>').animate({
						opacity: 1.0
					}, 1000); //.fadeOut("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();
	});
	$(".media-wrapper a.delete").click(function (e) {
		e.preventDefault();
		if(confirm(this.title)) {
			if(this.title == '') {
				window.location = this.href;
			} else {
				delId = "div-" + this.id;
				$("div#"+ delId).load(this.href,
				{
					ajaxCall : true
				});
			}
		}
	});
	$("a").not("#form-wrapper a").not("#create-post").not("a.delete").click(function (e) {
		if(!confirm("Bearbeiten abbrechen?")) {
			e.stopPropagation()		
			e.preventDefault();
		}
	});
	
	$("#post-title").focus(function (e) {
		if(e.target.value == "…") {
			e.target.value = "";
		}
	});
}

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() {
    $.ui.autocomplete.prototype._renderItem = function( ul, item ) {

     var t = item.label.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(this.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<span style='font-weight:bold;'>$1</span>");

      return $( '<li></li>' )
      .data( 'item.autocomplete', item )
      .append( '<a> <img src="'+item.icon+'"> <span  style="vertical-align:top;">' + t + '</span> </a>' )
      .appendTo( ul );
    };
   
	$("#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" /><input type="hidden" value="" name="add-admin-id[]" id="add-admin-' + add_admin_count +'-id" >');
	    $("#add-admin-"+add_admin_count).autocomplete( {
	        	source: baseUrl + "action/search-user", 
        		minLength: 2,
        		select: function(event, ui) {
                			$("#add-admin-"+add_admin_count).val( ui.item.label );
                			$("#add-admin-"+add_admin_count+"-id").val( ui.item.id );
                			return false;
                		}
	    	
	    });		
	});
	$("#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)) {
			if(this.id != "") {
				$("div#div-"+this.id).load(this.href, {
					ajaxCall : true
				});
			} else {
				window.location.href = this.href;
			}
		}
	});
	$("#workspace-titel").focus(function(e) {
		if (e.target.value == "Neuer Workspace") {
			e.target.value = "";
		}
	});
	$("#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");						
		}
	});
	$("#workspace-open-on").change(function () {
		if($(this).attr('checked') == true) {
			$("#workspace-open").attr('checked',false);
		}
	});
	$("#workspace-open").change(function () {
		if($(this).attr('checked') == true) {
			$("#workspace-open-on").attr('checked',false);
		}
	});
	
}

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: ");
	});
}

