    var idTarget;


    function showError(str) {
        $('#error').html(str);
        $('#error').dialog('open');
    }


    function showInfo(str) {
        $('#info').html(str);
        $('#info').dialog('open');
    }


    function pleaseWait() {
    	  $.blockUI({ message:"Please wait ..." });
    }


    function viewProfile(id) {
        $.blockUI({ message:"Please wait ..." });

        $.post("/ajax/users/contacts.php",
               {
                  "op": "view_profile",
                  "profile": id
               },
               function(data) {
                   $.unblockUI({fadeOut: 0});

                   var response = json_decode(data);
                   if (response.type == "ok") {
                       $('#view-profile').dialog('open');
                       $('#profileName').html(response.profile.name_full);
							  $('#profileAge').html(response.profile.age);
                       $('#profileAvatar').attr('src', response.profile.avatar);
                       $('#profileGender').html(response.profile.gender);
                       $('#profileOccupation').html(response.profile.occupation);
                       $('#profileWebsite').html(response.profile.website);
                       $('#profileBiography').html(response.profile.bio);
                       $('#profileFavoriteEquipment').html(response.profile.fav_equipment);
                       $('#profileHobbies').html(response.profile.hobbies);
                       $('#profileStats').html('Joined ' + response.profile.join_date + ', ' + response.profile.num_photos + ' photos, ' + response.profile.num_comments + ' comments');
                   } else if (response.type == "error") {
                       showError(response.message);
                   } else {
                       alert(data);
                   }
               }
        );
    }


    function sendMessage(id_user, name) {
        idTarget = id_user;
        $('#recipient').html(name);
        $('#message-form').dialog('open');
    }

	function addContact(id_user,name) {
		idTarget = id_user;
		$('#recipientContact').html(name);
		$('#contact-form').dialog('open');
	}

    function removeContact(id_user, name) {
        idTarget = id_user;
        $('#remove-contact-name').html(name);
        $('#confirmRemoveContact').dialog('open');
    }


    $(document).ready(function(){

            $("#view-profile").dialog({
          			autoOpen: false,
          			height: 400,
          			width: 550,
          			modal: true,
          			buttons: {
          				'Close': function() {
          					  $(this).dialog('close');
          				}
          			},
          			close: function() {
          				//
          			}
        		});


            $("#message-form").dialog({
          			autoOpen: false,
          			height: 400,
          			width: 450,
          			modal: true,
          			buttons: {
          				'Cancel': function() {
          					  $(this).dialog('close');
          				},
          				'Send': function() {
          						var subject = $('#txtSubject').val();
          						var body = $('#txtBody').val();

          						if (!subject) {
          						    showError("Subject is not entered");
          						    return;
          						}

          						if (!body) {
          						    showError("Body is not entered");
          						    return;
          						}

          						$(this).dialog('close');
                      pleaseWait();

                      $.post("/ajax/users/contacts.php",
                             {
                                "op": "send_message",
                                "to": idTarget,
                                "subject": subject,
                                "body": body
                             },
                             function(data) {
                                 $.unblockUI();
                                 if (data == "ok") {
                                     showInfo("Message was sent");
                                 } else {
                                     alert(data);
                                 }
                             }
                      );
          				}
          			},
          			close: function() {
          				$('#txtSubject').val('');
          				$('#txtBody').val('');
          			}
        		});

            $("#contact-form").dialog({
                  autoOpen: false,
                  height: 200,
                  width: 450,
                  modal: true,
                  buttons: {
                     'Cancel': function() {
                          $(this).dialog('close');
                     },
                     'Send Request': function() {
                           $(this).dialog('close');
                      pleaseWait();

                      $.post("/ajax/users/contacts.php",
                             {
                                "op": "friend_request",
                                "to": idTarget,
                             },
                             function(data) {
                                 $.unblockUI();
                                 if (data == "ok") {
                                     showInfo("Request was sent");
                                 } else {
                                     alert(data);
                                 }
                             }
                      );
                     }
                  },
                  close: function() {
                     $('#txtSubject').val('');
                     $('#txtBody').val('');
                  }
            });


            $("#error").dialog({
          			autoOpen: false,
          			height: 200,
          			width: 500,
          			modal: true,
          			buttons: {
          				Close: function() {
          					$(this).dialog('close');
          				}
          			}
        		});

            $("#info").dialog({
          			autoOpen: false,
          			height: 200,
          			width: 500,
          			modal: true,
          			buttons: {
          				Close: function() {
          					$(this).dialog('close');
          				}
          			}
        		});

            $("#confirmRemoveContact").dialog({
          			autoOpen: false,
          			height: 200,
          			width: 500,
          			modal: true,
          			buttons: {
          				No: function() {
          					  $(this).dialog('close');
          				},
          				Yes: function() {
          				    $(this).dialog('close');

                      $.post("/ajax/users/contacts.php",
                             {
                                "op": "remove_contact",
                                "id_contact": idTarget
                             },
                             function(data) {
                                 $.unblockUI();
                                 if (data == "ok") {
                                     //showInfo("Contact has been removed from your list");
                                     pleaseWait();
                                     document.location.href = "contacts.php";
                                 } else {
                                     alert(data);
                                 }
                             }
                      );
          				}
          			}
        		});
    });


