$(document).ready(function () {
  
   
  
  
  $('textarea').live('focus', function () {
    $(this).autogrow();
  });
  
  $('.jsInput').input();
  $('.jsTextarea').input();
  
  
  
  $('#liveFeedPublisher').submit(function () {
    var status = $('#liveFeedPublisherTxtArea').val();
    var wallLink = $('#wallLink').val();
    var strForm = $(this).serialize();
    var errors = false;
    
    if (isEmpty(status) || status == $('#liveFeedPublisherTxtArea').attr('defaultValue'))
      errors = true;
    
    if (wallLink !== 'http://'){
    errors = false;
    }
    
    if (!errors) {
      $('#liveFeedSubmit').attr('disabled', 'disabled');
      $.ajax({
        type: 'POST',
        data: strForm,
        url: 'ajax/ajax.updateStatus.php', 
        cache: false,
        global: false, 
        success: function (html) {
          if (html == 2) {
            alert('Oops! System error! Please try again later');
          }
          else {
            $('#liveFeedPublisherTxtArea').val($('#liveFeedPublisherTxtArea').attr('defaultValue')).css({'height' : '25px'});
            $('#liveFeedContainer').after(html);
            $('#wallLink').val('');
            $('#wallLinkbox').hide();
          }
          $('#liveFeedSubmit').removeAttr('disabled');
          
        }
      });
    }
    return false;
  });
  
  $('#inboxSendPM').submit(function() {
    var errors = false;
    var strForm = $(this).serialize();
    var friendID = $('#inboxFriendSelect').val();
    var subject = $('#inboxSubject').val();
    var message = $('#inboxMessage').val();
    
    if (isEmpty(friendID))
      errors = true;
    if (isEmpty(subject) || subject == $('#inboxSubject').attr('defaultValue'))
      subject = '';
    if (isEmpty(message) || message == $('#inboxMessage').attr('defaultValue')) {
      errors = true;
    }
    if (!errors) {
      $('#inboxSendingMsg').text('Sending Message...');
      $('#inboxInputMessage').attr('disabled', 'disabled');
      
      $.ajax({
        type: 'POST',
        data: 'fid=' +friendID+ '&subject=' +subject+ '&message=' +message,
        url: 'ajax/ajax.sendPrivateMessage.php', 
        global: false, 
        success: function (html) {
          if (html == 1 || html == 2) {
            $('#inboxSendingMsg').text('Message failed, try again later.');
          }
          else {
            setTimeout(function () {$('#inboxSendingMsg').text('Message sent.')}, 800);
          }
          $('#inboxSubject').val($('#inboxSubject').attr('defaultValue'));
          $('#inboxMessage').val($('#inboxMessage').attr('defaultValue')).css({'height' : '16px'});
          setTimeout(function () {$('#inboxSendingMsg').fadeOut()}, 2000);
          $('#inboxInputMessage').removeAttr('disabled');
        }
      });
    }
    return false;
  });
  
  $('.ajaxLiveFeedReplyContainer').submit(function () {
    var id = this.id;
    var liveFeedStatusReply = $('#liveFeedReplyTxtArea-' + id).val();
    var strForm = $(this).serialize();
    var errors = false;
    
    if (isEmpty(liveFeedStatusReply))
      errors = true;
    
    if (!errors) {
      $('#liveFeedReplyTxtArea-' + id).attr('disabled', 'disabled');
      $('#liveFeedSubmit-' + id).attr('disabled', 'disabled');
      
      $.ajax({
        type: 'POST',
        data: strForm, 
        url: 'ajax/ajax.feedStatusReply.php',
        global: false, 
        success: function (html) {
          if (html == 1) {
            alert('Oops! System error. Please try again later.');
          }
          else {
            $('#liveFeedReplyTxtArea-' + id).val('');
            $('#liveFeedReplyTxtArea-' + id).css({'height' : '16px'});
            $('.liveFeedReplyContainer-' + id).before(html);
          }
          $('#liveFeedReplyTxtArea-' + id).removeAttr('disabled');
          $('#liveFeedSubmit-' + id).removeAttr('disabled');
        }
      });
    }
    return false;
  });
  
  $('#searchEmailAccount').submit(function () {
    var email = $('#searchEmail').val();
    var password = $('#searchEmailPassword').val();
    var provider = $('#searchEmailProvider').val();
    var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var strForm = $(this).serialize();
    var errors = false;
    $('#searchEmailtxt').css({'color' : '#FFFFFF'});
    $('#searchEmailPasswordtxt').css({'color' : '#FFFFFF'});
    $('#searchEmailProvidertxt').css({'color' : '#FFFFFF'});
    
    if (isEmpty(email)) {
      $('#searchEmailtxt').css({'color' : '#FF0000'});
      errors = true;
    }
    else if (isEmpty(password)) {
      $('#searchEmailPasswordtxt').css({'color' : '#FF0000'});
      errors = true;
    }
    else if (isEmpty(provider)) {
      $('#searchEmailProvidertxt').css({'color' : '#FF0000'});
      errors = true;
    }
    
    
    
    for(var i = 1; i <=3; i++)
      $('#emailAccountError' + i).addClass('globalHidden');
    
    if (!errors) {
      $('#findFriendEmailAccountLoader').show();
      $('#emailAccountResults').empty();
      
      $.ajax({
        type: 'POST',
        data: strForm,
        //data: 'email=' + email + '&password=' + password, 
        url: 'ajax/ajax.searchEmailAccount.php', 
        success: function (html) {
          if (html == 1)
            $('#emailAccountError1').removeClass('globalHidden');
          else if (html == 2)
            $('#emailAccountError2').removeClass('globalHidden');
          else if (html == 3)
            $('#emailAccountError3').removeClass('globalHidden');
          else {
            $('#emailAccountResults').removeClass('globalHidden');
            $('#emailAccountResults').html(html);
          }
          $('#findFriendEmailAccountLoader').hide();
        }
      });
    }
  
    return false;
  });
  
  $('.thButton').live('click', function () {
    var email = $('#ebox').val();
    var provider = $('#pbox').val();
    var session = $('#sbox').val();
    var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    $('#ebox1').val($('#ebox').val());
    $('#pbox1').val($('#pbox').val());
    $('#sbox1').val($('#sbox').val());
    var strForm = $('#openinviter').serialize();
    var errors = false;
    
    if (isEmpty(email) || isEmpty(provider) || !emailReg.test(email))
      errors = true;
    
    for(var i = 1; i <=3; i++)
      $('#emailAccountError' + i).addClass('globalHidden');
    
    if (!errors) {
      $('#findFriendEmailAccountLoader').show();
      $('#emailAccountResults').empty();
      
      $.ajax({
        type: 'POST',
        data: strForm,
        //data: 'email=' + email + '&password=' + password, 
        url: 'ajax/ajax.searchEmailAccount.php', 
        success: function (html) {
          if (html == 1)
            $('#emailAccountError1').removeClass('globalHidden');
          else if (html == 2)
            $('#emailAccountError2').removeClass('globalHidden');
          else if (html == 3)
            $('#emailAccountError3').removeClass('globalHidden');
          else {
            $('#emailAccountResults').removeClass('globalHidden');
            $('#emailAccountResults').html(html);
          }
          $('#findFriendEmailAccountLoader').hide();
        }
      });
    }
  
    return false;
  });
  

  
  
    $('#findMyFriends').submit(function () {
     $('#naNetworkResults').removeClass('globalHidden').show();
      var name = $('#searchName').val();
    //var city = $('#searchCity').val();
    var strForm = $(this).serialize();
    var errors = false;
    
    if (isEmpty(name))
      errors = true;
    
    if (!errors) {
      $('#naNetworkResults').show();
      
      $.ajax({
        type: 'POST',
        data: strForm, 
        url: 'ajax/ajax.findMyFriends.php',
        global: false, 
        success: function (html) {
          $('#naNetworkResults').html(html);
          $('#allmyfriends').hide();
        }
      });
    }
    return false;
  });
  
  
  
  $('#searchNA').submit(function () {
    var name = $('#searchName').val();
    var city = $('#searchCity').val();
    var strForm = $(this).serialize();
    var errors = false;
    
    if (isEmpty(name) && isEmpty(city))
      errors = true;
    
    if (!errors) {
      $('#findFriendNASearchLoader').show();
      
      $.ajax({
        type: 'POST',
        data: strForm, 
        url: 'ajax/ajax.searchNANetwork.php',
        global: false, 
        success: function (html) {
          $('#naNetworkResults').removeClass('globalHidden');
          $('#naNetworkResults').html(html);
          $('#findFriendNASearchLoader').hide();
        }
      });
    }
    return false;
  });
  
  $('#searchNAemail').submit(function () {
    var email = $('#searchEmail').val();
    var strForm = $(this).serialize();
    var errors = false;
    
    if (isEmpty(email))
      errors = true;
    
    if (!errors) {
      $('#findFriendNASearchLoader2').show();
      
      $.ajax({
        type: 'POST',
        data: strForm, 
        url: 'ajax/ajax.searchNANetworkEmail.php',
        global: false, 
        success: function (html) {
          $('#naNetworkResults').removeClass('globalHidden');
          $('#naNetworkResults').html(html);
          $('#findFriendNASearchLoader2').hide();
        }
      });
    }
    return false;
  });
  
});


  
function removeFromPlaylist(songid, playlistid) {

    $.ajax({
        type: 'POST',
        data: 'songID=' + songid + '&playlistID=' + playlistid, 
        url: 'ajax/ajax.removeFromPlaylist.php', 
        global: false, 
        success: function (html) {
          $.gritter.add({
            	title: 'Playlist Notice!',
            	text: 'Successfully removed song from playlist.',
              image: 'js/grumble/themes/smoke/success.png' //check image
            });
          $('#profilePlaylistSongContainer-' + songid).fadeOut();
        }
      });
  
}
function removeFromPlaylist_vid(videoid, playlistid) {
    $.ajax({
        type: 'POST',
        data: 'videoID=' + videoid + '&playlistID=' + playlistid, 
        url: 'ajax/ajax.removeFromPlaylist_vid.php', 
        global: false, 
        success: function (html) {
          $.gritter.add({
            	title: 'Playlist Notice!',
            	text: 'Successfully removed video from playlist.',
              image: 'js/grumble/themes/smoke/success.png' //check image
            });
          $('#profilePlaylistVideoContainer-' + videoid).fadeOut();
        }
      });
  
}

function confirmFriend(id, response) {
  $.ajax({
      type: 'POST',
      data: 'id=' + id + '&response=' + response, 
      url: 'ajax/ajax.confirmFriend.php', 
      global: false, 
      success: function (html) {
        $('#newFriend-' +id).fadeOut();
     }
    });
}
function confirmFriendProfile(id, response) {
  $.ajax({
      type: 'POST',
      data: 'id=' + id + '&response=' + response, 
      url: 'ajax/ajax.confirmFriend.php', 
      global: false, 
      success: function (html) {
        $('.profileNewFriendAction').fadeOut();
        $('#profileNotAFriendRequest').fadeOut();
        $('.profileNewFriendAdded').show();
        window.location.replace("/profile.php?id="+ id);
      }
    });
}


function removeFriend(id) {
  var msgtxt ='Are you sure you want to remove this friend?';
  jConfirm(msgtxt, 'Remove Friend', function(r) {
          if(r){
            $.ajax({
                type: 'POST',
                data: 'friendID=' + id, 
                url: 'ajax/ajax.removeFriend.php', 
                global: false, 
                success: function (html) {
                  $('#friendContainer-' + id).fadeOut();
                }
              });
          }
  });
}

function findMoreFriends() {
  $('.findFriendsContainer').slideToggle();
}

function findNANetwork() {
  var name = $('#searchName').val();
  var city = $('#searchCity').val();
  
  $.ajax({
      type: 'POST',
      data: 'name=' + name + '&city=' + city, 
      url: 'ajax/ajax.searchNANetwork.php',
      global: false, 
      success: function (html) {
        $('#findFriendsContainer').after(html);
      }
    });
}

function addFriend(id) {
  $.ajax({
      type: 'POST',
      data: 'id=' + id, 
      url: 'ajax/ajax.addFriend.php',
      global: false, 
      success: function () {
        $('#newFriend-' + id).fadeOut();
      }
    });
}

function deleteStatus(statusID) {
  var msgtxt ='Are you sure you would like to Delete this comment?';
  jConfirm(msgtxt, 'Delete Comment', function(r) {
          if(r){
              $.ajax({
                  type: 'POST',
                  data: 'sid=' + statusID, 
                  url: 'ajax/ajax.deleteStatus.php',
                  global: false, 
                  success: function () {
                    $('#feedStatusBox-' + statusID).fadeOut();
                  }
                });
          }
  });
}

function deleteFeedComment(commentID) {
  var msgtxt ='Are you sure you would like to Delete this comment?';
  jConfirm(msgtxt, 'Delete Comment', function(r) {
          if(r){
            $.ajax({
              type: 'POST',
              data: 'cid=' + commentID, 
              url: 'ajax/ajax.deleteWallReplyComment.php',
              global: false, 
              success: function (response) {
                if (response == 1)
                  $('#feedReply-' + commentID).fadeOut().remove();
              }
            });
          }
  });
}

function isEmpty(str) {
  var strText = String(str).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  
  if (strText == '')
    return true;
  else
    return false;
}

function showAllFeedReplies(id) {
  $('.feedHiddenReply-' + id).show();
  $('#feedShowAllReplies-' + id).hide();
  $('#feedReplyContainer-' + id).fadeIn();
  $('#feedReplyTxtArea-' + id).focus();
}

function showFeedReplyStatusBox(id) {
  $('#liveFeedReplyContainer-' + id).fadeIn();
  $('#liveFeedReplyTxtArea-' + id).focus();
}

//Inbox function
function readPrivateMessage(pmID) {
  var errors = false;
  var stage = '';
  
  if (isNaN(pmID))
    errors = true;
  
  $('#pmRead-' + pmID + '-1').attr('id', function () {
    stage = this.id;
  });
  
  if (!errors && stage == 'pmRead-' + pmID + '-1') {
    $('#pmLoading-' + pmID).show();
    
    $.ajax({
        type: 'POST',
        data: 'pmID=' + pmID,
        url: 'ajax/ajax.getPrivateMessage.php',
        global: false, 
        success: function (html) {
          if (html == 2) {
            alert('Oops! System error. Please try again later.');
          }
          else if (html == 1) {
            alert('Message not found.');
          }
          else {
            setTimeout(function () { $('#pm-' + pmID).html(html) }, 500);
            $('#pmRead-' + pmID + '-1').text('Close');
            $('#pmRead-' + pmID + '-1').attr('id', 'pmRead-' + pmID + '-2');
          }
          $('#pmLoading-' + pmID).fadeOut();
        }
      });
  }
  else {
    $('#pm-' + pmID).empty();
    $('#pmRead-' + pmID + '-2').text('Read');
    $('#pmRead-' + pmID + '-2').attr('id', 'pmRead-' + pmID + '-1');
  }
}

function readSentPrivateMessage(pmID) {
  var errors = false;
  var stage = '';
  
  if (isNaN(pmID))
    errors = true;
  
  $('#pmRead-' + pmID + '-1').attr('id', function () {
    stage = this.id;
  });
  
  if (!errors && stage == 'pmRead-' + pmID + '-1') {
    $('#pmLoading-' + pmID).show();
    
    
    $.ajax({
        type: 'POST',
        data: 'pmID=' + pmID,
        url: 'ajax/ajax.getSentPrivateMessage.php',
        global: false, 
        success: function (html) {
          if (html == 2) {
            alert('Oops! System error. Please try again later.');
          }
          else if (html == 1) {
            alert('Message not found.');
          }
          else {
            setTimeout(function () { $('#pm-' + pmID).html(html) }, 500);
            $('#pmRead-' + pmID + '-1').text('Close');
            $('#pmRead-' + pmID + '-1').attr('id', 'pmRead-' + pmID + '-2');
          }
          $('#pmLoading-' + pmID).fadeOut();
        }
      });
  }
  else {
    $('#pm-' + pmID).empty();
    $('#pmRead-' + pmID + '-2').text('Read');
    $('#pmRead-' + pmID + '-2').attr('id', 'pmRead-' + pmID + '-1');
  }
}

function replyPrivateMessage(id) {
  $('#inboxFriendSelect option').each(function () {
    if (this.value == id) {
      $(this).parent().val(id);
    }
  });
  $('#inboxSubject').focus();
}

function deletePrivateMessage(pmID, from) {
  var errors = false;
  
  if (isNaN(pmID))
    errors = true;
  
  if (!errors) {
    $.ajax({
        type: 'POST',
        data: 'pmID=' + pmID + '&from=' + from,
        url: 'ajax/ajax.deletePrivateMessage.php',
        global: false, 
        success: function (html) {
          if (html == 2) {
            alert('Oops! System error. Please try again later.');
          }
          else if (html == 1) {
            alert('Message not found.');
          }
          else {
            setTimeout(function () { $('#pmContainer-' + pmID).fadeOut() }, 500);
          }
          $('#pmLoading-' + pmID).fadeOut();
        }
      });
  }
}