function addToFavorites(favorite_id) {
	$('addToFavorites').value = 'adding... &nbsp;&nbsp;&nbsp;';
	$('addToFavorites').className = 'action-link-loading';
	
	JsHttpRequest.query(
		SITE_ADDR + 'public/actions/add_user_to_favorites.php',
		{
			method: 				'POST',
			favorite_id:		favorite_id,
			type: 				'user'
		},
		function (result, errors) {
			$('addToFavorites').className = 'action-link';
			if (!errors) {
				destroy($('addToFavorites'));
				jQuery.flash(tr['s_changes_were_saved']);
			} else {
				$('addToFavorites').innerHTML = tr['s_add_to_favorites'] + '&nbsp;&nbsp;&nbsp;';
			}
		},
		true
	);
}

function deleteFromFavorites(favorite_id) {		
	$('addToFavorites').innerHTML = tr['s_removing'] + '&nbsp;&nbsp;&nbsp;';
	$('addToFavorites').className = 'action-link-loading';
	
	JsHttpRequest.query(
		SITE_ADDR + 'public/actions/delete_user_from_favorites.php',
		{
			method: 			'POST',
			favorite_id:		favorite_id,
			type: 				'user'
		},
		function (result, errors) {
			$('addToFavorites').className = 'action-link';
			if (!errors) {
				jQuery.flash(tr['s_changes_were_saved']);
				$('addToFavorites').innerHTML = tr['s_add_to_favorites'] + '&nbsp;&nbsp;&nbsp;';
				$('addToFavorites').onclick = function() {
					addToFavorites();
					
					return false;
				};
			} else {
				$('addToFavorites').innerHTML = tr['s_delete_from_favorites'] + '&nbsp;&nbsp;&nbsp;';
			}
		},
		true
	);
}

function addToFriends(friend_id) {
	$('addToFriends').innerHTML = tr['s_adding'] + '&nbsp;&nbsp;&nbsp;';
	$('addToFriends').className = 'action-link-loading';
	
	JsHttpRequest.query(
		SITE_ADDR + 'public/actions/add_to_friends.php',
		{
			method: 			'POST',
			friend_id:		friend_id
		},
		function (result, errors) {
			$('addToFriends').className = 'action-link';
			if (!errors) {
				jQuery.flash(tr['s_changes_were_saved']);
				$('addToFriends').innerHTML = tr['s_delete_from_friends'] + '&nbsp;&nbsp;&nbsp;';
				$('addToFriends').onclick = function() {
					deleteFromFriends();
					
					return false;
				};
			} else {
				$('addToFriends').innerHTML = tr['s_add_to_friends'] + '&nbsp;&nbsp;&nbsp;';
			}
		},
		true
	);
}

function deleteFromFriends(friend_id) {		
	
	$('addToFriends').innerHTML = tr['s_removing'] + '&nbsp;&nbsp;&nbsp;';
	$('addToFriends').className = 'action-link-loading';
	
	JsHttpRequest.query(
		SITE_ADDR + 'public/actions/delete_from_friends.php',
		{
			method: 			'POST',
			friend_id:		friend_id
		},
		function (result, errors) {
			$('addToFriends').className = 'action-link';
			if (!errors) {
				jQuery.flash(tr['s_changes_were_saved']);
				$('addToFriends').innerHTML = tr['s_add_to_friends'] + '&nbsp;&nbsp;&nbsp;';
				$('addToFriends').onclick = function() {
					addToFriends();
					
					return false;
				};
			} else {
				$('addToFriends').innerHTML = tr['s_delete_from_friends'] + '&nbsp;&nbsp;&nbsp;';
			}
		},
		true
	);
}

function complainOfUser() {
	$('complainOfUser').innerHTML = tr['s_saving'] + '&nbsp;&nbsp;&nbsp;';
	$('complainOfUser').className = 'action-link-loading';
	
	var complain_of_id = parentElem($('complainOfUser')).id.substring(8);
	
	JsHttpRequest.query(
		SITE_ADDR + 'public/actions/complain_of_user.php',
		{
			complain_of_id: 	complain_of_id,
			method: 			'POST'
		},
		function (result, errors) {
			if (!errors) {
				jQuery.flash(tr['s_changes_were_saved']);
				$('complainOfUser').innerHTML = tr['s_complained'] + '&nbsp;&nbsp;&nbsp;';
				$('complainOfUser').className = 'action-link-deactive';
				$('complainOfUser').onclick = function(){ return false; };
			} else {
				$('complainOfUser').innerHTML = tr['s_complain'] + '&nbsp;&nbsp;&nbsp;';
				$('complainOfUser').className = 'action-link';
			}
		},
		true
	);
}

function voteForUser() {
	
	JsHttpRequest.query(
		SITE_ADDR + 'public/actions/vote_for_user.php',
		{
			vote_for_id: 	user_id,
			method: 		'POST'
		},
		function (result, errors) {
			if (!errors) {
				jQuery.flash(tr['s_changes_were_saved']);
				$('rating').innerHTML = result.rating;
				$('vote').className = 'deactive';
			} else {
				jQuery.flash(tr['v_already_voted']);
			}
		},
		true
	);
	
	return false;
}

function answer(parent_id) {
	if ($('comment_body').value.trim()=='') {
		return false;
	}
	
	$('commentStatus').className = 'status-loading';
			
	JsHttpRequest.query(
		SITE_ADDR + 'public/actions/add_comment_to_user.php',
		{
			method: 			'POST',
			comment_body:  		$('comment_body').value,
			comment_object_id: 	$('comment_object_id').value
		},
		function (result, errors) {
			if (!errors) {
				jQuery.flash(tr['s_changes_were_saved']);
				$('commentForm').reset();
				$('commentForm').style.display = 'none';
				$('commentFormSlider').innerHTML = tr['s_add_comment'] + '&nbsp;&nbsp;&nbsp;';
				$('commentStatus').className = 'status-ok';
				var comment = create('div');
				comment.className = 'comment';
				comment.innerHTML = result.content;
				if (first($('commentsList'))) {
					before(comment, first($('commentsList')));
				} else {
					append($('commentsList'), comment);
				}
				
			} else {
				$('commentStatus').className = 'status-errors';
			}
		},
		true
	);
}
		
function signupVote() {
	document.location.href = SITE_ADDR + 'signup/vote';
	
	return false;
}

onLoad(function() {

	var
		answerToComment = css('answer'),
		markCommentAsSpam = css('mark-comment-as-spam'),
		deleteComment = css('delete-comment'),
		deactive = css('action-link-deactive'),
		complainOfComment = css('complain'),
		answerForm = $('answerForm');
		
	var $answerForm = jQuery('#answerForm');
		
	var commentDiv;

	/*
	if (is_logged_in) {
		for (answer in answerToComment) {
			
			jQuery(answerToComment[answer]).click(function() {
				$self = jQuery(this);
				
				alert($self.parents('.comment').length);
				$self.parents('.comment').append($('qweqweqwe'));
				$answerForm.show();
				
				return false;
			});
			
			/*answerToComment[answer].onclick = function() {
				alert('wow');
				commentDiv = parentElem(this, 2);
				
				before(answerForm, next(commentDiv));
				show(answerForm);
				
				return false;
		}
	}
	*/
	
	var vote = $('vote');
	
	if (vote) {
		vote.onclick = voteForUser;
	}
	
	if (complainOfComment.length>0) {
		
		for (var i=0; i<complainOfComment.length; i++) {
			
			complainOfComment[i].onclick = function() {
				
				var t = this;
				var comment_id = parentElem(t, 3).id.substring(8);
				
				t.innerHTML = tr['s_saving'] + '&nbsp;&nbsp;&nbsp;';
				t.className = 'action-link-loading';
				t.onclick = function () { return false; }
				
				JsHttpRequest.query(
					SITE_ADDR + 'public/actions/complain_of_comment.php',
					{
						comment_id: 	comment_id,
						method: 			'POST'
					},
					function (result, errors) {
						if (!errors) {
							jQuery.flash(tr['s_changes_were_saved']);
							t.innerHTML = tr['s_complained'] + '&nbsp;&nbsp;&nbsp;';
							t.className = 'action-link-deactive';
							t.onclick = function(){ return false; };
						} else {
							t.innerHTML = tr['s_complain'] + '&nbsp;&nbsp;&nbsp;';
							t.className = 'action-link';
						}
					},
					true
				);
				
				return false;
				
			};
			
		}
		
	}
		
	if (deactive) {
		for (var i=0; i<deactive.length; i++) {
			deactive[i].onclick = function() { return false; }
		}
	}
		
	if (markCommentAsSpam) {
		
		for (var i=0; i<markCommentAsSpam.length; i++) {
			
			markCommentAsSpam[i].onclick = function() {
				
				var t = this;
				var comment_id = parentElem(t, 3).id.substring(8);
				
				t.innerHTML = tr['s_saving'] + '&nbsp;&nbsp;&nbsp;';
				t.className = 'action-link-loading';
				t.onclick = function () { return false; }
				
				JsHttpRequest.query(
					SITE_ADDR + 'public/actions/mark_comment_as_spam.php',
					{
						comment_id: 	comment_id,
						method: 			'POST'
					},
					function (result, errors) {
						if (!errors) {
							jQuery.flash(tr['s_changes_were_saved']);
							t.innerHTML = tr['s_marked_comment_as_spam'] + '&nbsp;&nbsp;&nbsp;';
							t.className = 'action-link-deactive';
							t.onclick = function(){ return false; };
						} else {
							t.innerHTML = tr['s_spam'];
							t.className = 'action-link-loading';
						}
					},
					true
				);
				
				return false;
			};
			
		}
		
		
	}
		
	if (deleteComment) {
		
		for (var i=0; i<deleteComment.length; i++) {
			
			deleteComment[i].onclick = function() {
				var t = this;
				var comment_id = parentElem(t, 3).id.substring(8);
				
				t.innerHTML = tr['s_deleting'] + '&nbsp;&nbsp;&nbsp;';
				t.onclick = function () { return false; }
				
				JsHttpRequest.query(
					SITE_ADDR + 'public/actions/delete_comment.php',
					{
						comment_id: 	comment_id,
						comment_type:  'user',
						method: 			'POST'
					},
					function (result, errors) {
						if (!errors) {
							jQuery.flash(tr['s_changes_were_saved']);
							jQuery(t).parent().parent().parent().fadeOut(500);
						} else {
							t.className = 'action-link';
							t.innerHTML = tr['s_delete'] + '&nbsp;&nbsp;&nbsp;';
						}
					},
					true
				);
				
				return false;
			};
			
		}
		
		
	}
	
	if (is_logged_in && !is_it_me && can_act) {
		
		$('addToFavorites').onclick = function() {
			var favorite_id = (parentElem(this).id).substring(8);
			
			if (is_favorite) {
				deleteFromFavorites(favorite_id);
			} else {
				addToFavorites(favorite_id);
			}
			
			return false;
		};
		
		if (!is_deactive_friend) {
			
			$('addToFriends').onclick = function() {
				var friend_id = (parentElem(this).id).substring(8);
				
				if (is_friend) {
					deleteFromFriends(friend_id);
				} else	{
					addToFriends(friend_id);
				};
				
				return false;
			};
			
		}
		
		var vFU = $('voteForUser');
		if (vFU) {
			vFU.onclick = function() {
				voteForUser();
				return false;
			};
		}
		
		var cOU = $('complainOfUser');
		if (cOU) {
			cOU.onclick = function() {
				complainOfUser();
				return false;
			};
		}
		
		$('commentFormSlider').onclick = function() {
			if ($('commentForm').style.display == 'inline') {
				$('commentForm').style.display = 'none';
				$('commentFormSlider').innerHTML = tr['s_add_comment'] + '&nbsp;&nbsp;&nbsp;';
			} else {
				$('commentForm').style.display = 'inline';
				$('commentFormSlider').innerHTML = tr['s_hide_form'] + '&nbsp;&nbsp;&nbsp;';
			}
			
			return false;
		};
		
		$('commentForm').onsubmit = function() {
			
			if ($('comment_body').value.trim()=='') {
				return false;
			}
			
			$('commentStatus').className = 'status-loading';
					
			JsHttpRequest.query(
				SITE_ADDR + 'public/actions/add_comment_to_user.php',
				{
					method: 			'POST',
					comment_body:  		$('comment_body').value,
					comment_object_id: 	$('comment_object_id').value
				},
				function (result, errors) {
					if (!errors) {
						jQuery.flash(tr['s_changes_were_saved']);
						$('commentForm').reset();
						$('commentForm').style.display = 'none';
						$('commentFormSlider').innerHTML = tr['s_add_comment'] + '&nbsp;&nbsp;&nbsp;';
						$('commentStatus').className = 'status-ok';
						var comment = create('div');
						comment.className = 'comment';
						comment.innerHTML = result.content;
						if (first($('commentsList'))) {
							before(comment, first($('commentsList')));
						} else {
							append($('commentsList'), comment);
						}
						
					} else {
						$('commentStatus').className = 'status-errors';
					}
				},
				true
			);
			
			return false;
		};
	
	}
	
});
