var change_sub_counter = 0;
var clear_cache_counter = 0;

$(document).ready(function () {
	postInit();
	settingsInit();	
});

function settingsInit() {
	$("#warning-email").hide();
	$("#warning-usergroup").hide();

	if($("#change-group").val() != 3) {
		$("#matrikel").hide();
	}
	if($("#change-group").val() == 11) {
		$("#alumnizusatz").show();
	}
	
	$("a.folder-toggle").click(function (e) {
		e.preventDefault();
		//console.log("#settings-"+this.id);
		$("div#settings-"+this.id).toggle();
	});	

	$("#change-mail").focus(function() {
		$("#warning-email").show("normal");		
		
	});	
	
	$("#change-mail").blur(function (e) {
		adress = $("#change-mail").val();
		$.postJSON(baseUrl + 'action/check-profile/email', {
			mail : adress
		}, function (data) {
			if(data.mail_okay) {
				$("#change-mail").css('background-color', '#be8');
			} else {
				$("#change-mail").css('background-color', '#fba');			
			}
		});
	});
	
	
	$("#change-group").change(function() {
		if(this.value < 3) {
			$("#warning-usergroup").show("normal");		
		} else {
			$("#warning-usergroup").hide("normal");				
		}
		if(this.value == 3) {
			$("#matrikel").slideDown();			
		} else {
			$("#matrikel").slideUp();					
		}
		if(this.value == 11) {
			$("#alumnizusatz").slideDown();			
		} else {
			$("#alumnizusatz").slideUp();					
		}
		
		
	});
	
	$("a.change-subscription").click(function (e) {
		e.preventDefault();
		var link = e.target;
		$.postJSON(this.href, {
			ajaxCall : true
		}, function(data) {
			if(data.success == true) {
				link.href = data.url;
				if(data.subscription == true) {
					$(link).addClass('selected');
				} else {
					$(link).removeClass('selected');
				}
			}
		});
	});	
	//
	$("a.favorite").click(function (e) {
		e.preventDefault();
		$(this).fadeTo("normal", 0);		
		var 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);		
		var 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);
			  
			}
		   
		});
	});
	$(".leave").click(function (e) {
		e.preventDefault();
		if(confirm(this.title)) {
			var link = this;
			$.postJSON(this.href, {
				ajaxCall : true
			}, function (data) {
				if(data.success) {
					$(link).parents('tr').fadeOut("slow");

				}
			});
		} else {
			return false;
		}
	});
//
	
}

function postInit() {
	$("a.edit-post").click(function (e) {
		e.preventDefault();
		$(this).fadeTo("normal", 0.1);		
		
		var wrapperId = this.id.substring(5);

		$("div#"+wrapperId +" .post").after('<div id="form-wrapper"><div id="loading"></div></div>');
		$("div#"+wrapperId +" .post").hide("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("normal");
			
		}); 
	});
	$("a.add-comment").click(function (e) {
		if($("#form-wrapper").size() > 0) {
			$("a.add-comment").fadeTo("fast", 1);
			$("#form-wrapper").remove();	
		}
		e.preventDefault();
		var 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();
		var divId = this.id.substring(5);
		$(this).fadeTo("normal", 0.1);		
		$("div#"+divId).append('<div id="loading"></div>');
		$("div#"+divId).load(this.href, {
			ajaxCall : true
		});
	});
	$(".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");
				});
			}
		}
		
	});
	$(".del-project").click(function (e) {
		e.preventDefault();
		if(confirm(this.title)) {
			window.location = this.href;
		}
	});
}

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();
		}
	});
}
