// imagebox

$(".profile_actions .icon").click(function(){
	alink = this;
	href = $(this).attr("href");
	cls = $(this).attr("class");


	// ************************************** buddy remove ***************
	if ($(this).is(".ibuddies_delete") && (href != "#noop")){
		$("#uidialog").html("Willst Du den Geier wirklich von Deiner Freundesliste entfernen?").dialog({
	    	resizable: false, overlay: { opacity: 0.5, background: "#77EE77" }, modal: true, show: "fade", hide: "fade",
	    	height: 100,
	    	title: "Aktion bitte bestätigen",
	    	buttons: {
	        	"Ja, will ich!": function(){
					$.post('/_ajax/ajax.profil.php', { buddy_delete : href.substring(1) }, function(ret){
						if (ret == 'true'){
							$(alink).removeClass("ibuddies_delete").addClass("ibuddies_add").html('<span>Zu Deinen Geierfreunden hinzufügen</span>');
							$("#uidialog").dialog("close");
						}else{
							$("#uidialog").dialog("close");
							display_error_dialog(ret, 100);
						}
					});
	        	},
	        	"Neee, lieber nicht!": function(){
	            	$(this).dialog("close");
	        	}
	    	}
		});


	// ************************************** buddy add ***************
	}else if ($(this).is(".ibuddies_add") && (href != "#noop")){
		$("#uidialog").html("Willst Du dem Geier jetzt eine Freundesanfrage senden?").dialog({
	    	resizable: false, overlay: { opacity: 0.5, background: "#77EE77" }, modal: true, show: "fade", hide: "fade",
	    	height: 100,
	    	title: "Aktion bitte bestätigen",
	    	buttons: {
	        	"Ja, bitte!": function(){
					$.post('/_ajax/ajax.profil.php', { buddy_add : href.substring(1) }, function(ret){
						if (ret == 'true'){
							$(alink).removeClass("ibuddies_add").addClass("ibuddies_delete").html('<span>Von Deinen Geierfreunden entfernen</span>');
							$("#uidialog").dialog("close");
						}else{
							$("#uidialog").dialog("close");
							display_error_dialog(ret, 100);
						}
					});
	        	},
	        	"Neee, doch nicht!": function(){
	            	$(this).dialog("close");
	        	}
	    	}
		});


	// ************************************** buddy block ***************
	}else if ($(this).is(".icross") && (href != "#noop")){
		$("#uidialog").html("Willst Du den Geier wirklich für Dein Profil blockieren?").dialog({
	    	resizable: false, overlay: { opacity: 0.5, background: "#77EE77" }, modal: true, show: "fade", hide: "fade",
	    	height: 100,
	    	title: "Aktion bitte bestätigen",
	    	buttons: {
	        	"Ja, bitte!": function(){
					$.post('/_ajax/ajax.profil.php', { block_add : href.substring(1) }, function(ret){
						if (ret == 'true'){
							$(alink).removeClass("icross").addClass("iuncross").html('<span>User für Dein Profil <b>nicht mehr</b> blockieren</span>');
							$("#uidialog").dialog("close");
						}else{
							$("#uidialog").dialog("close");
							display_error_dialog(ret, 100);
						}
					});
	        	},
	        	"Neee, doch nicht!": function(){
	            	$(this).dialog("close");
	        	}
	    	}
		});


	// ************************************** buddy unblock ***************
	}else if ($(this).is(".iuncross") && (href != "#noop")){
		$("#uidialog").html("Willst Du den Block für Dein Profil wieder aufheben?").dialog({
	    	resizable: false, overlay: { opacity: 0.5, background: "#77EE77" }, modal: true, show: "fade", hide: "fade",
	    	height: 100,
	    	title: "Aktion bitte bestätigen",
	    	buttons: {
	        	"Ja, bitte!": function(){
					$.post('/_ajax/ajax.profil.php', { block_delete : href.substring(1) }, function(ret){
						if (ret == 'true'){
							$(alink).removeClass("iuncross").addClass("icross").html('<span>User für Dein Profil blockieren</span>');
							$("#uidialog").dialog("close");
						}else{
							$("#uidialog").dialog("close");
							display_error_dialog(ret, 100);
						}
					});
	        	},
	        	"Neee, doch nicht!": function(){
	            	$(this).dialog("close");
	        	}
	    	}
		});

	// ************************************** popup write message dialog ***************
	}else if ($(this).is(".ibuddies_write") && (href != "#noop")){
		$("#uidialog").html('<div class="ajax_loader"><img src="_/style/images/ajax_loader.gif" /></div>').dialog({
	    	resizable: false, modal: true, show: "drop", hide: "drop",
	    	height: 300,
	    	width: 500,
	    	title: "Schreibe eine neue private Nachricht hier",
	    	buttons: {
	        	"Senden": function(){
					var text = $("#uidialog textarea").val();
					var len = (text) ? text.length : 0;

					if (len === 0){
						display_error_dialog('<p class="error_message">Du musst einen Text eingeben bevor Du ihn abschickst.</p>', 100);
					}else{
						$.post('/_ajax/ajax.profil.php', { write_message : href.substring(1), subject : $("#uidialog .hg_message_subject").val(), message : text }, function(ret){
							if (ret == 'true'){
								$("#uidialog").html('<p class="success_message">Nachricht wurde gesendet</p>');
								setTimeout('$("#uidialog").dialog("close");', 1500);
							}else{
								display_error_dialog(ret, 100);
							}
						});
					}
	        	},
	        	"Abbrechen": function(){
	            	$(this).dialog("close");
	        	}
	    	}
		});
		$.get('/_ajax/postbox.php', function(ret){
			$("#uidialog").html('<div class="width_gap_top"></div><b style="display: block; width: 45px; float: left;">Betreff: </b> <input type="text" name="hg_message_subject" class="hg_message_subject" value="" style="width: 435px; float: right;" /><div class="clear"><br /></div>' + ret);
		});

	// ************************************** buddy chat ***************
	}else if ($(this).is(".ibuddies_chat") && (href != "#noop")){
		/*hgchat_start(href.substring(1));*/
		chatWith(href.substring(1));
	}

	return false;
});



// ************************************** delete guestbook entries ***************
$(".gbm_area .icon").click(function(){
	cls = $(this).attr("class");
	id = $(this).attr("rel");


	if ($(this).is(".itrash")){
		$("#uidialog").html("Willst Du den Gästebucheintrag wirklich löschen?").dialog({
	    	resizable: false, overlay: { opacity: 0.5, background: "#77EE77" }, modal: true, show: "fade", hide: "fade",
	    	height: 100,
	    	title: "Aktion bitte bestätigen",
	    	buttons: {
	        	"Ja, weg damit!": function(){
					$.post('/_ajax/ajax.profil.php', { delete_gb : id }, function(ret){
						if (ret == 'true'){
							$('.gbm' + '.' + id).slideUp('', function(){
								$('.gbm' + '.' + id).remove();

								if (!$('.gbm').length){
									if ($("ul .page_navigation_next a").attr("href")){
										reload_link = $("ul .page_navigation_info input").val();
									}else if (!(reload_link = $("ul .page_navigation_prev a").attr("href"))){
										$(".gbm_area").html('<p class="info_message">Du hast alle Gästebucheinträge gelöscht!</p>');
										return;
									}

									location.href = reload_link;
								}
							});
							$("#uidialog").dialog("close");
						}else{
							$("#uidialog").dialog("close");
							display_error_dialog(ret, 100);
						}
					});
	        	},
	        	"Neee, doch nicht!": function(){
	            	$(this).dialog("close");
	        	}
	    	}
		});

	// ************************** antworten
	}else if ($(this).is(".ibuddies_write.reply")){
		$("#uidialog").html('<div class="ajax_loader"><img src="_/style/images/ajax_loader.gif" /></div>').dialog({
	    	resizable: false, modal: true, show: "drop", hide: "drop",
	    	height: 300,
	    	width: 500,
	    	title: "Schreibe Deine Antwort hier",
	    	buttons: {
	        	"Senden": function(){
					var text = $("#uidialog textarea").val();
					var len = (text) ? text.length : 0;

					if (len === 0){
						display_error_dialog('<p class="error_message">Du musst einen Text eingeben bevor Du ihn abschickst.</p>', 100);
					}else{
						$.post('/_ajax/ajax.profil.php', {
							reply_gb : id,
							reply : text
						}, function(ret){
							if (ret !== 'false'){
								$(".gbm."+id+" .ibuddies_write").fadeOut();
								$(".gbm."+id+" .entry_content .clear:first").after(ret);
								$(".gbm."+id+" .entry_content .clear:first").next().slideDown('slow');
								$("#uidialog").dialog("close");
							}else{
								display_error_dialog('<p class="error_message">Fehler beim Schreiben der Antwort!</p>', 100);
							}
						});
					}
	        	},
	        	"Abbrechen": function(){
	            	$(this).dialog("close");
	        	}
	    	}
		});

		$.get('/_ajax/postbox.php', function(ret){
			$("#uidialog").html(ret);
		});
	}

	return false;
});





// ************************************** manage your buddy requests ***************
$(".buddy_requests a.icon").click(function(){
	id = $(this).attr("rel");

	// ************************** deny / abort buddy request
	if ($(this).is(".idelete")){
		$.post('/_ajax/ajax.profil.php', { buddy_deny : id }, function(ret){
			if (ret == 'true'){
				$('.buddy_requests '+'.'+id).fadeOut('', function(){
					$('.buddy_requests '+'.'+id).remove();
				});
			}else{
				display_error_dialog(ret, 100);
			}
		});
	}else if ($(this).is(".isuccess")){
		$.post('/_ajax/ajax.profil.php', { buddy_accept : id }, function(ret){
			if (ret == 'true'){
				display_success_dialog('Anfrage erfolgreich akzeptiert.', 100);
				$('.buddy_requests '+'.'+id).fadeOut('', function(){
					$('.buddy_requests '+'.'+id).remove();
				});
			}else{
				display_error_dialog(ret, 100);
			}
		});
	}

	return false;
});


// ************************************** write guestbook message functions ***************
$("#guestbook_write_trigger").click(function(){
	$(this).fadeOut('fast', function(){
		$("#guestbook_write").slideDown();
	});
	return false;
});

$("#guestbook_write button[type='reset']").click(function(){
	$("#guestbook_write").slideUp('', function(){
		$("#guestbook_write_trigger").fadeIn('fast');
	});
	return false;
});

$("#guestbook_write button[type='submit']").click(function(){
	var text = $("#guestbook_write textarea").val();
	var len = (text) ? text.length : 0;

	if (len === 0){
		display_error_dialog('<p class="error_message">Du musst einen Text eingeben bevor Du ihn abschickst.</p>', 100);
		return false;
	}

	return true;
});



// ************************************** friend list filter ***************
$("#friend_list_filter").keyup(function(){
	online_only = $("#friend_list_filter_online").hasClass("idelete");
	s = $(this).val();

	if (s == ''){
		$(".friend_list .buddy_box").fadeIn(300);
		return;
	}

	$(".friend_list .buddy_box").each(function(){
		if (!online_only || (online_only && $("img", this).hasClass("geieruser_userbox_on"))){
			if ($("a:first", this).html().search(s, 'i') !== -1){
				$(this).fadeIn(300);
			}else{
				$(this).fadeOut(200);
			}
		}
	});
});

$("#friend_list_filter_online").click(function(){
	if ($(this).hasClass("isuccess")){
		$(".friend_list .geieruser_userbox_on").parent().parent().fadeIn(300);
		$(".friend_list .geieruser_userbox_off").parent().parent().fadeOut(300);
		$(this).removeClass("isuccess").addClass("idelete").html('<span>Online Filter entfernen</span>');
	}else{
		$(".friend_list .geieruser_userbox_off").parent().parent().fadeIn(300);
		$(this).removeClass("idelete").addClass("isuccess").html('<span>nur online anzeigen</span>');
	}

	if ($("#friend_list_filter").val()){
		$("#friend_list_filter").keyup();
	}
	return false;
});
