// URL prefix to use in all ajax calls
var url = '';


/**
* Removes a friend
* Used in the modal box
**/
function remove_friend(username) {  
  if (confirm('Are you sure about removing this friend?')) {
    $('#modal_friends_activity_loader').show();
    $.getJSON('/friends/remove/username/'+username,
      function(data) {
        $('#modal_friends_activity_loader').hide();
        if (data.status == 'success') {
          $('#friend_item_'+username+' .menu').empty();
          $('#friend_item_'+username+' .details_header').css('text-decoration','line-through').css('opacity','0.5');
          $('#friend_item_'+username+' .image').css('opacity','0.5');
          $('#friend_item_'+username+' .links').html('<div>You and this person are not friends anymore.</div>');
          if ($('#random_friend_item_'+username)) {
            $('#random_friend_item_'+username).css('opacity','0.5');
            $('#random_friend_item_'+username+' .random_friend_link').css('text-decoration','line-through');
          }
          var friends_count = parseInt($('#random_friends_count').html());
          if (friends_count >= 2) {
            var append = ((friends_count-1) > 1) ? ' friends' : ' friend';
            $('#random_friends_count').html((friends_count-1));$('#modal_friends_count').html((friends_count-1)+append);
          } else {
            $('#random_friends_see_all_link').hide();
            $('#modal_friends_count').html('0 friends');
          }
        } else {
          alert('There was a problem removing your friend.');
        }
      }
    );
  }
}


/**
* Delete one entry from the log
* 
* @author Vlad Mereuta
**/
function delete_log_entry(id,daymd5) {
  if (confirm('Are you sure about deleting this entry?')) {
    $('#activty_loader').show();
    $.getJSON(url+'/profile/deleteLogEntry/id/'+id,function(data) {
      $('#activty_loader').hide();
      if (data.status == 'success') {
        $('#log_entry_'+id).remove();
        // check to see if we need to remove the day as well
        if ($('.dayentry'+daymd5).length == 0) {
            $('.day'+daymd5).remove();
        };
        // check to see if we need to remove the whole activity window
        if ($('.dayentry').length == 0) {
          $('#activityul').append('<li class="top">You have no recent activities.</li>');
        }
      } else {
        alert('Can\'t delete entry.');
      }
    });
  }
}


/**
* Member profile related functions
* 
* @author Vlad Mereuta
**/

function update_status() {
  var new_status = $('#status_line').attr('value');
  var old_status = $('#status_text').html();
  var old_date = $('#status_update_time').html();
  if (new_status != '') {
    $('#status_text').text(new_status.substr(0,255));
    $('#status_update_time').html(time_ago_in_words(now_unixtime()));
    $.modal.close();
    var data = 'status='+new_status.substr(0,255);
    $.post(url+'/profile/updateStatus',data,function(data){
      $.modal.close();
      if (data.status != 'success') {
        $('#status_text').html(old_status);
        $('#status_update_time').html(old_date);
        alert('Can\'t update status. Please try again later.');
      }
    },'json');    
  } else {
    $.modal.close();
  }
}




/**
* Returns unix timestamp for now()
*
* @author Vlad Mereuta
*
* UNUSED
*/
function now_unixtime() {
  return(Math.round(new Date().getTime()/1000.0));
}

/**
*
* Returns time ago in words
*
* @author Vlad Mereuta
*
* UNUSED
*/
function time_ago_in_words(unixtime) {
  var elapsed_in_seconds = (now_unixtime()-unixtime);
  var s = elapsed_in_seconds;
  var m = parseInt(elapsed_in_seconds/60);
  var h = parseInt(elapsed_in_seconds/3600);
  var d = parseInt(elapsed_in_seconds/86400);
  if (d == 0) {
    if (h == 0) {
       if (m == 0) {
         if (s < 5) {
           return('less than 5 seconds');
         } else if (s < 30) {
           return('less than 30 seconds')
         } else if (s <= 60) {
           return('less than a minute')
         }
       } else {
         if (m < 2) {
           return('one minute');
         } else if (m < 5) {
           return('less than 5 minutes');
         } else if (m < 10) {
           return('less than 10 minutes');
         } else {
           return(m + ' minutes');
         }
       }
    } else {
      if (h == 1) {
        return('one hour');
      } else {
        return(h +' hours');
      }
    }
 } else {
   return(d + ' days');
 }
}
