var Relationship = {
	in_home : {
		allow_friendship : function() {

			var self = $(this);
			var user_id = self.parents('.plate').attr('id').split('_').pop();

			$.ajax({
				type : 'POST',
				url : '/relationship/add_friend_request/',
				data : {
					to_user : user_id
				},
				dataType : 'xml',
				success : function(xml) {
					if ($(xml).find('ok').length) {
						$.ajax({
							data : {},
							url : '/ajax_awaiting_friendship/',
							type : 'GET',
							dataType : 'html',
							cache : false,
							success : function(html) {
								$('#ajax_friends').html(html);

								var next_plate = self.parents('.plate').next();

								if (next_plate.length) {

									self.parents('.plate').remove();

									if (next_plate.prev().attr('class') != 'plate') {
										next_plate
												.find('.arrowleft')
												.before('<span class="dark_grey">&#8592;</span')
												.remove();
									}

									next_plate.show();

								} else {

									var prev_plate = self.parents('.plate')
											.prev();

									self.parents('.plate').remove();

									if (prev_plate.next().attr('class') != 'plate') {
										prev_plate
												.find('.arrowright')
												.before('<span class="dark_grey">&#8594;</span')
												.remove();
									}

									prev_plate.show();

								}

								$('#gadget_relationship .plate .heading a span')
										.html(parseInt(
												$('.plate .heading a span')
														.html(), 10)
												- 1);

								if ($('#gadget_relationship .plate .heading a span')
										.html() == 1) {
									$('#gadget_relationship .plate .heading a')
											.html('СО МНОЙ ХОЧЕТ ДРУЖИТЬ');
								}

							}
						});

					}

				}
			});

			return false;
		},
		deny_friendship : function() {
			var self = $(this);
			var user_id = self.parents('.plate').attr('id').split('_').pop();

			$.ajax({
				type : 'POST',
				url : '/relationship/deny_friendship/',
				data : {
					to_user : user_id
				},
				dataType : 'xml',
				success : function(xml) {
					if ($(xml).find('ok').length) {
						$.ajax({
							data : {},
							url : '/ajax_awaiting_friendship/',
							type : 'GET',
							dataType : 'html',
							cache : false,
							success : function(html) {
								$('#ajax_friends').html(html);

								var next_plate = self.parents('.plate').next();

								if (next_plate.length) {

									self.parents('.plate').remove();

									if (next_plate.prev().attr('class') != 'plate') {
										next_plate
												.find('.arrowleft')
												.before('<span class="dark_grey">&#8592;</span')
												.remove();
									}

									next_plate.show();

								} else {

									var prev_plate = self.parents('.plate')
											.prev();

									self.parents('.plate').remove();

									if (prev_plate.next().attr('class') != 'plate') {
										prev_plate
												.find('.arrowright')
												.before('<span class="dark_grey">&#8594;</span')
												.remove();
									}

									prev_plate.show();

								}

								$('#gadget_relationship .plate .heading a span')
										.html(parseInt(
												$('.plate .heading a span')
														.html(), 10)
												- 1);

								if ($('#gadget_relationship .plate .heading a span')
										.html() == 1) {
									$('#gadget_relationship .plate .heading a')
											.html('СО МНОЙ ХОЧЕТ ДРУЖИТЬ');
								}

							}
						});

					}

				}

			});
			return false;
		}

	},
	not_in_home : {
		// Если находимся у кого-нибудь на главной
		add_to_friends : function() {
			$('#friend_window_form').ajaxSubmit({
						dataType : 'xml',
						cache : false,
						success : function(xml) {
							if ($(xml).find('ok').length) {
								if ($(xml).find('mutual').length) {
									// $('#friends_status').html('<span
									// class="green">Мы с ним дружим</span>');
									$('#friends_status span').hide();
									$('#friends_status_friends').show();
								} else {
									$('#friends_status span').hide();
									$('#friends_status_i_want').show();
									// $('#friends_status')
									// .html('<span class="green">Я хочу с ним
									// дружить</span>');
								}

								$('#add_to_friends').hide();
								$('#remove_from_friends').show();
								$('#remove_from_ignore').hide();
								$('#add_to_ignore').show();

								$('#add_to_enemies').show();
								$('#remove_from_enemies').hide();

								$('#relations_menu_link').click();

								tb_remove();
							}
						}
					});
		},
		ban_set : function(user_id) {
			$.ajax({
						type : 'POST',
						url : '/relationship/ban_set/',
						data : {
							user_id : user_id
						},
						dataType : 'xml',
						success : function(xml) {
							if ($(xml).find('ok').length) {
								// tb_remove();
								$('#ban_set').hide();
								$('#unban').show();
							}
						}
					});
		},
		unban : function(user_id) {
			$.ajax({
						type : 'POST',
						url : '/relationship/unban_set/',
						data : {
							user_id : user_id
						},
						dataType : 'xml',
						success : function(xml) {
							if ($(xml).find('ok').length) {
								// tb_remove();
								$('#unban').hide();
								$('#ban_set').show();
							}
						}
					});
		},
		deny_friendship : function() {
			$.ajax({
						type : 'POST',
						url : '/relationship/deny_friendship/',
						data : {
							to_user : $('#to_user_friend').val()
						},
						dataType : 'xml',
						success : function(xml) {
							if ($(xml).find('ok').length) {
								tb_remove();
								$('#add_to_friends_link').hide();
								$('#relationship_divider').hide();
							}
						}
					});
		},
		add_to_ignore : function(user_id) {
			$.ajax({
						type : 'POST',
						url : '/relationship/add_to_ignore/',
						data : {
							user_id : user_id
						},
						dataType : 'xml',
						success : function(xml) {
							if ($(xml).find('ok').length) {
								$('#friends_status span').hide();
								$('#friends_status_ignore').show();

								// $('#friends_status').html('<span ="red"> Я не
								// его слышу </span>');
								$('#relationship_gadget_friend_'
										+ REGISTRY.user_id).hide();
								$('#relationship_gadget_friends_count')
										.text($('#relationship_gadget_friends_count')
												.text()
												- 1);
								$('#add_to_friends').show();
								$('#add_to_ignore').hide();
								$('#remove_from_ignore').show();
								$('#remove_from_friends').hide();

								$('#relations_menu_link').click();

								tb_remove();

							}
						}
					});
		},
		unignore : function(user_id) {
			$.ajax({
						type : 'POST',
						url : '/relationship/unignore/',
						data : {
							user_id : user_id
						},
						dataType : 'xml',
						success : function(xml) {
							if ($(xml).find('ok').length) {

								// $('#friends_status').html('<span
								// class="grey">Мы в нейтральных
								// отношениях</span>');
								$('#friends_status span').hide();
								$('#friends_status_neutral').show();

								$('#add_to_friends').show();
								$('#add_to_ignore').show();
								$('#remove_from_ignore').hide();
								$('#remove_from_friends').hide();

								$('#relations_menu_link').click();

							}
						}
					});
		},
		remove_from_friends : function(user_id) {
			$.ajax({
						data : {
							friend_id : user_id
						},
						url : '/relationship/remove_from_friends/',
						type : 'GET',
						datatype : 'xml',
						cache : false,
						success : function(xml) {
							if ($(xml).find('ok').length) {
								$('#friends_status span').hide();
								$('#friends_status_neutral').show();

								// $('#friends_status').html('<span
								// class="grey">Мы в нейтральных
								// отношениях</span>');

								$('#add_to_friends').show();
								$('#add_to_ignore').show();
								$('#remove_from_ignore').hide();
								$('#remove_from_friends').hide();

								$('#relations_menu_link').click();
							}
						}
					});
		},

		add_to_enemies : function(user_id) {
			$.ajax({
						type : 'POST',
						url : '/relationship/add_to_enemies/',
						data : {
							enemy_id : user_id
						},
						dataType : 'xml',
						success : function(xml) {
							if ($(xml).find('ok').length) {
								$('#friends_status span').hide();
								$('#friends_status_enemy').show();

								$('#add_to_friends').show();
								$('#remove_from_friends').hide();

								$('#add_to_enemies').hide();
								$('#remove_from_enemies').show();

								$('#relations_menu_link').click();

								tb_remove();

							}
						}
					});
		},
		remove_from_enemies : function(user_id) {
			$.ajax({
						type : 'POST',
						url : '/relationship/remove_from_enemies/',
						data : {
							enemy_id : user_id
						},
						dataType : 'xml',
						success : function(xml) {
							if ($(xml).find('ok').length) {

								$('#friends_status span').hide();
								$('#friends_status_enemy').hide();
								$('#friends_status_neutral').show();

								$('#add_to_friends').show();

								$('#remove_from_friends').hide();

								$('#add_to_enemies').show();
								$('#remove_from_enemies').hide();

								$('#relations_menu_link').click();

							}
						}
					});
		}

	},
	relationship : {
		// Если находимся на странице отношений
		listing : {
			// Всяческие выборки и сортировки друзей
			bind_userslist : function() {
				// вешает на клик по подменю "отобрать/сортировать" нужные для
				// выборок события

				try {
					$('#u_all_friends_sort_by_date').bind('click', {
						order_by : 'date',
						filter_by : REGISTRY.settings['#u_all_friends']['filter'],
						url : '/relationship/ajax/friends/',
						node_id : '#u_all_friends'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_all_friends_sort_by_nick').bind('click', {
						order_by : 'nick',
						filter_by : REGISTRY.settings['#u_all_friends']['filter'],
						url : '/relationship/ajax/friends/',
						node_id : '#u_all_friends'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_all_friends_filter_by_online').bind('click', {
						order_by : REGISTRY.settings['#u_all_friends']['order'],
						filter_by : 'online',
						url : '/relationship/ajax/friends/',
						node_id : '#u_all_friends'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_all_friends_filter_by_all').bind('click', {
						order_by : REGISTRY.settings['#u_all_friends']['order'],
						filter_by : 'all',
						url : '/relationship/ajax/friends/',
						node_id : '#u_all_friends'
					}, Relationship.relationship.listing.ajax_get_users);
				} catch (e) {
				};

				try {
					$('#u_not_mutual_friends_sort_by_date').bind('click', {
						order_by : 'date',
						filter_by : REGISTRY.settings['#u_not_mutual_friends']['filter'],
						url : '/relationship/want/ajax/users/',
						node_id : '#u_not_mutual_friends'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_not_mutual_friends_sort_by_nick').bind('click', {
						order_by : 'nick',
						filter_by : REGISTRY.settings['#u_not_mutual_friends']['filter'],
						url : '/relationship/want/ajax/users/',
						node_id : '#u_not_mutual_friends'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_not_mutual_friends_filter_by_online').bind('click', {
						order_by : REGISTRY.settings['#u_not_mutual_friends']['order'],
						filter_by : 'online',
						url : '/relationship/want/ajax/users/',
						node_id : '#u_not_mutual_friends'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_not_mutual_friends_filter_by_all').bind('click', {
						order_by : REGISTRY.settings['#u_not_mutual_friends']['order'],
						filter_by : 'all',
						url : '/relationship/want/ajax/users/',
						node_id : '#u_not_mutual_friends'
					}, Relationship.relationship.listing.ajax_get_users);
				} catch (e) {
				};

				try {
					$('#u_ignore_list_filter_by_online').bind('click', {
						order_by : REGISTRY.settings['#u_ignore_list']['order'],
						filter_by : 'online',
						url : '/relationship/ignore/ajax/users/',
						node_id : '#u_ignore_list'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_ignore_list_filter_by_all').bind('click', {
						order_by : REGISTRY.settings['#u_ignore_list']['order'],
						filter_by : 'all',
						url : '/relationship/ignore/ajax/users/',
						node_id : '#u_ignore_list'
					}, Relationship.relationship.listing.ajax_get_users);

					$('#u_ignore_list_sort_by_date').bind('click', {
						order_by : 'date',
						filter_by : REGISTRY.settings['#u_ignore_list']['filter'],
						url : '/relationship/ignore/ajax/users/',
						node_id : '#u_ignore_list'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_ignore_list_sort_by_nick').bind('click', {
						order_by : 'nick',
						filter_by : REGISTRY.settings['#u_ignore_list']['filter'],
						url : '/relationship/ignore/ajax/users/',
						node_id : '#u_ignore_list'
					}, Relationship.relationship.listing.ajax_get_users);
				} catch (e) {
				};

				try {
					$('#u_enemies_filter_by_online').bind('click', {
						order_by : REGISTRY.settings['#u_enemies']['order'],
						filter_by : 'online',
						url : '/relationship/enemies/ajax/users/',
						node_id : '#u_enemies'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_enemies_filter_by_all').bind('click', {
						order_by : REGISTRY.settings['#u_enemies']['order'],
						filter_by : 'all',
						url : '/relationship/enemies/ajax/users/',
						node_id : '#u_enemies'
					}, Relationship.relationship.listing.ajax_get_users);

					$('#u_enemies_sort_by_date').bind('click', {
						order_by : 'date',
						filter_by : REGISTRY.settings['#u_enemies']['filter'],
						url : '/relationship/enemies/ajax/users/',
						node_id : '#u_enemies'
					}, Relationship.relationship.listing.ajax_get_users);
					$('#u_enemies_sort_by_nick').bind('click', {
						order_by : 'nick',
						filter_by : REGISTRY.settings['#u_enemies']['filter'],
						url : '/relationship/enemies/ajax/users/',
						node_id : '#u_enemies'
					}, Relationship.relationship.listing.ajax_get_users);
				} catch (e) {
				};				
				
//				$('#friend_of_sort_by_date').bind('click', {
//							order_by : 'date',
//							filter_by : REGISTRY.filter_friends_in_relationship,
//							url : '/relationship/friend/ajax/users/'
//						}, Relationship.relationship.listing.ajax_get_users);
//				$('#friend_of_sort_by_nick').bind('click', {
//							order_by : 'nick',
//							filter_by : REGISTRY.filter_friends_in_relationship,
//							url : '/relationship/friend/ajax/users/'
//						}, Relationship.relationship.listing.ajax_get_users);
//				$('#friend_of_filter_by_online').bind('click', {
//							order_by : REGISTRY.sort_friends_in_relationship,
//							filter_by : 'online',
//							url : '/relationship/friend/ajax/users/'
//						}, Relationship.relationship.listing.ajax_get_users);
//				$('#friend_of_filter_by_all').bind('click', {
//							order_by : REGISTRY.sort_friends_in_relationship,
//							filter_by : 'all',
//							url : '/relationship/friend/ajax/users/'
//						}, Relationship.relationship.listing.ajax_get_users);
//
//				$('#all_friends_filter_by_of_both').bind('click', {
//							order_by : REGISTRY.sort_friends_in_relationship,
//							filter_by : '',
//							url : '/relationship/both/ajax/users/'
//						}, Relationship.relationship.listing.ajax_get_users);
//				$('#friend_of_both_sort_by_date').bind('click', {
//							order_by : 'date',
//							filter_by : REGISTRY.filter_friends_in_relationship,
//							url : '/relationship/both/ajax/users/'
//						}, Relationship.relationship.listing.ajax_get_users);
//				$('#friend_of_both_sort_by_nick').bind('click', {
//							order_by : 'nick',
//							filter_by : REGISTRY.filter_friends_in_relationship,
//							url : '/relationship/both/ajax/users/'
//						}, Relationship.relationship.listing.ajax_get_users);

			},
			// делает выборки по клику по подменю "отобрать/сортировать"
			ajax_get_users : function(event) {
				$.ajax({
					data : {
						order_by : event.data.order_by,
						filter_by : event.data.filter_by
					},
					url : event.data.url,
					type : 'GET',
					datatype : 'html',
					cache : false,
					success : function(html, status) {
						// console.log(html);
						console.log(event.data.node_id);
						$(event.data.node_id).html(html);

						console.log(event);

						// REGISTRY.filter_friends_in_relationship =
						// event.data.filter_by;
						// REGISTRY.sort_friends_in_relationship =
						// event.data.order_by;

						$(event.data.node_id + '_filter a')
								.removeClass('active');
						$(event.data.node_id + '_sort a').removeClass('active');

						$(event.data.node_id + '_filter a').addClass('dashed');
						$(event.data.node_id + '_sort a').addClass('dashed');

						console.log(event.data.node_id + '_sort_by_'
								+ event.data.order_by);
						console.log(event.data.node_id + '_filter_by_'
								+ event.data.order_by);

						$(event.data.node_id + '_sort_by_'
								+ event.data.order_by).removeClass('dashed');
						$(event.data.node_id + '_sort_by_'
								+ event.data.order_by).addClass('active');

						$(event.data.node_id + '_filter_by_'
								+ event.data.filter_by).removeClass('dashed');
						$(event.data.node_id + '_filter_by_'
								+ event.data.filter_by).addClass('active');

						// Relationship.relationship.listing.bind_userslist();
					}
				});
				// event.preventDefault();
				return false;
			}
		},
		management : {
			// Управление друзьми
			remove_from_friends : function() {

				if ($(this).is('.clicked')) {
					return false;
				}
				$(this).addClass('clicked');

				friend_id = $(this).attr('id').split('_').pop();

				if (confirm("Вы уверены, что не хотите больше дружить?")) {
					$.ajax({
								data : {
									friend_id : friend_id
								},
								url : '/relationship/remove_from_friends/',
								type : 'GET',
								datatype : 'xml',
								cache : false,
								success : function(xml) {
									if ($(xml).find('ok').length) {
										$('#u_all_friends_buttonset_'
												+ friend_id)
												.html('Вы только что лишись друга');
									}
								}
							});
				}
				return false;
			},
			approve_friendship : function() {

				if ($(this).is('.clicked')) {
					return false;
				}
				$(this).addClass('clicked');

				friend_id = $(this).attr('id').split('_').pop();

				$.ajax({
							data : {
								to_user : friend_id
							},
							url : '/relationship/add_friend_request/',
							type : 'POST',
							datatype : 'xml',
							cache : false,
							success : function(xml) {
								if ($(xml).find('ok').length) {
									$('#u_friend_of_buttonset_' + friend_id)
											.html('Вы только что подружились');
									$('#u_enemies_buttonset_' + friend_id)
											.html('Запрос на дружбу отправлен');
									$('#u_enemy_of_buttonset_' + friend_id)
											.html('Запрос на дружбу отправлен');
								}
							}
						});

				return false;
			},
			deny_friendship : function() {
				if (confirm("Вы уверены, что не хотите дружить с этим человеком?")) {

					if ($(this).is('.clicked')) {
						return false;
					}
					$(this).addClass('clicked');

					user_id = $(this).attr('id').split('_').pop();

					$.ajax({
								type : 'POST',
								url : '/relationship/deny_friendship/',
								data : {
									to_user : user_id
								},
								dataType : 'xml',
								success : function(xml) {
									$('#u_friend_of_buttonset_' + user_id)
											.html('Вы не стали дружить');
									$('#u_not_mutual_friends_buttonset_'
											+ user_id)
											.html('Вы передумали дружить');
								}
							});
				}
				return false;
			},
			unignore : function() {

				if ($(this).is('.clicked')) {
					return false;
				}
				$(this).addClass('clicked');

				user_id = $(this).attr('id').split('_').pop();

				$.ajax({
							type : 'POST',
							url : '/relationship/unignore/',
							data : {
								user_id : user_id
							},
							dataType : 'xml',
							success : function(xml) {
								if ($(xml).find('ok').length) {
									$('#u_ignore_list_buttonset_'
											+ user_id)
											.html('Вы удалили из игнора');
								}
							}
						});
				return false;
			},
			remove_from_enemies : function() {

				if ($(this).is('.clicked')) {
					return false;
				}
				$(this).addClass('clicked');
				
				user_id = $(this).attr('id').split('_').pop();
				
				$.ajax({
							type : 'POST',
							url : '/relationship/remove_from_enemies/',
							data : {
								enemy_id : user_id
							},
							dataType : 'xml',
							success : function(xml) {
								if ($(xml).find('ok').length) {
									$('#u_enemies_buttonset_'
											+ user_id)
											.html('Больше не враг');
								}
							}
						});
						return false;
			},
			add_to_enemies : function() {

				if (confirm("Вы уверены, что хотите нового врага?")) {

					if ($(this).is('.clicked')) {
						return false;
					}
					$(this).addClass('clicked');

					user_id = $(this).attr('id').split('_').pop();

					$.ajax({
								type : 'POST',
								url : '/relationship/add_to_enemies/',
								data : {
									enemy_id : user_id
								},
								dataType : 'xml',
								success : function(xml) {
									if ($(xml).find('ok').length) {
										$('#u_all_friends_buttonset_' + user_id)
												.html('Вы только что стали врагами');
										$('#u_all_friends_buttonset_' + user_id)
												.html('Вы только что стали врагами');
										$('#u_enemy_of_buttonset_' + user_id)
												.html('Вы только что стали врагами');												

									}
								}
							});
				}
				return false;
			}
		}
	}
}

var show_relations_menu = function() {
	$('#relations .arrow-on').hide();
	$('#relations .arrow-off').show();
	$('#relations .action').show();
	$('#relations').attr('class', 'relations_down');
}

var hide_relations_menu = function() {
	$('#relations .arrow-on').show();
	$('#relations .arrow-off').hide();
	$('#relations .action').hide();
	$('#relations').attr('class', 'relations');
}

$(function() {
			// $('#friend_window').find('a.share').bind('click',
			// Relationship.not_in_home.add_to_friends);
			// $('#deny_friendship').bind('click',
			// Relationship.not_in_home.deny_friendship);

			Relationship.relationship.listing.bind_userslist();

			$('.all_friends_remove_from_friends').live('click',
					Relationship.relationship.management.remove_from_friends);
			$('.all_friends_add_to_enemies').live('click',
					Relationship.relationship.management.add_to_enemies);

			$('.friend_of_approve_friendship').live('click',
					Relationship.relationship.management.approve_friendship);
			$('.friend_of_deny_friendship').live('click',
					Relationship.relationship.management.deny_friendship);

			$('.not_mutual_friends_deny_friendship').live('click',
					Relationship.relationship.management.deny_friendship);

			$('.ignore_list_unignore').live('click',
					Relationship.relationship.management.unignore);

			$('.enemies_add_to_friends').live('click',
					Relationship.relationship.management.approve_friendship);

			$('.enemies_enemies').live('click', 
					Relationship.relationship.management.remove_from_enemies);
					
			$('.enemy_of_add_to_friends').live('click', 
					Relationship.relationship.management.approve_friendship);
			$('.enemy_of_enemies').live('click', 
					Relationship.relationship.management.add_to_enemies);					

			$('#gadget_relationship .arrowleft').click(function() {
						$(this).parents('.plate').hide();
						$(this).parents('.plate').prev().show();
						return false;
					});

			$('#gadget_relationship .arrowright').click(function() {
						$(this).parents('.plate').hide();
						$(this).parents('.plate').next().show();
						return false;
					});

			$('#gadget_relationship .create')
					.click(Relationship.in_home.allow_friendship);
			$('#gadget_relationship .cancel')
					.click(Relationship.in_home.deny_friendship);

			$('#relations_menu_link').toggle(show_relations_menu,
					hide_relations_menu);

		});