$(document).ready(function () {
  $('textarea').autogrow();
  
   $('#wallLinkbox').hide();
    $('#addLink').click(function () {
      $('#wallLinkbox').toggle();
    });
  
  $('#profilePublisher').submit(function () {
  var wallMessage = $('#wallMessage').val();
  var wallLink = $('#wallLink').val();
  var strForm = $(this).serialize();
  var errors = false;

  if (isEmpty(wallMessage) || wallMessage == $('#wallMessage').attr('defaultValue'))
    errors = true;
  if (wallLink !== 'http://'){
    errors = false;
  }

  if (!errors) {
    $('#wallMessage').attr('disabled', 'disabled');
    $('#profilePublisherSubmit').attr('disabled', 'disabled');

    $.ajax({
    type: 'POST',
    data: strForm,
    url: 'ajax/ajax.sendWallPost.php',
    cache: false,
    global: false,
    success: function (html) {
      if (html != 1) {
        $('#wallMessage').val('');
        $('#wallLink').val('');
        $('#newWallPost').fadeIn();
        $('#profilePublisher').after(html);
        $('#wallMessage').removeAttr('disabled');
        $('#wallLinkbox').hide();
        $('#wallMessage').css({'height' : '30px'});
        $('#profilePublisherSubmit').removeAttr('disabled');
      }
      
    }
    });
  }
  return false;
  });

  $('.ajaxProfileFeedReply').submit(function () {
    var id = this.id;
    var wMessage = $('#profileReplyTxtArea-' + id).val();
    var strForm = $(this).serialize();
    var errors = false;

    if (isEmpty(wMessage))
      errors = true;

    if (!errors) {
      $('#profileReplySubmit-' + id).attr('disabled', 'disabled');
      $('#profileReplyTxtArea-' + id).attr('disabled', 'disabled');

      $.ajax({
        type: 'POST',
        data: strForm,
        url: 'ajax/ajax.profileReplyWallPost.php',
        global: false,
        success: function (html) {
          if (html == 1)
            alert('Error');
          else {
            $('.profileFeedReply-' + id).before(html);
            $('#profileReplyTxtArea-' + id).val('');
          }
          $('#profileReplySubmit-' + id).removeAttr('disabled');
          $('#profileReplyTxtArea-' + id).removeAttr('disabled');
        }
      });
    }
    return false;
  });
    
   
    $(function() {
  		$("ul#listcontainer").sortable({ opacity: 0.6, cursor: 'move', update: function() {
  			var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
  			$.post("ajax/ajax.updateMusicOrder.php", order, function(theResponse){
  			$.gritter.add({
            	title: 'Music Display Order',
            	text: 'Successfully changed song display position',
              image: 'js/grumble/themes/smoke/success.png' //check image
            });
        });
  		}
  		});
    });

  
});


function reportPhoto(div, photoID) {
  $.ajax({
    type: 'POST',
    data: 'photoID=' + photoID,
    url: 'ajax/ajax.reportPhoto.php',
    global: false,
    success: function (html) {
      $('#' + div).html(html);
    }
  });
}

function makeDefault(div, photoID) {
  $.ajax({
    type: 'POST',
    data: 'photoID=' + photoID,
    url: 'ajax/ajax.makeDefault.php',
    global: false,
    success: function (html) {
      $('#' + div).html(html);
    }
  });
}

function deletePhoto(div, photoID) {
  $.ajax({
    type: 'POST',
    data: 'photoID=' + photoID,
    url: 'ajax/ajax.deletePhoto.php',
    global: false,
    success: function (html) {
      $('#' + div).html(html);
    }
  });
}

function submitPhotoComment(photoID) {
  var comment = $('#photoCommentArea').val();
  var errors = false;

  if (isEmpty(comment)) {
    errors = true;
  }

  if (!errors) {
    $.ajax({
      type: 'POST',
      data: 'comment=' + comment + '&photoID=' + photoID,
      url: 'ajax/ajax.photoComment.php',
      global: false,
      success: function (html) {
        $('#photoCommentBox').before(html);
        $('#photoCommentArea').val('');
      }
    });
  }
}

function deletePhotoComment(commentID) {
  $.ajax({
      type: 'POST',
      data: 'commentID=' + commentID,
      url: 'ajax/ajax.deletePhotoComment.php',
      global: false,
      success: function (html) {
        if (html == 1)
          $('#photoComment-' + commentID).fadeOut();
      }
    });
}

function uploadPic() {
  var errors = false;
  var picHolder = $('#uploadInputFile').val();

  if (isEmpty(picHolder))
    errors = true;
  if ($('#certifyBox').is(':checked')) {
    $('#certifyTxt').css({'color' : '#808184'});
    errors = false;
  }
  else {
    $('#certifyTxt').css({'color' : '#FF0000'});
    errors = true;
  }

  if (!errors) {
    parent.document.uploadform.submit();
    $('#singleUploadForm').hide();
    $('#singleUploadMessage').removeClass('globalHidden');
  }
}

function isEmpty(str) {
  var strText = String(str).replace(/^\s\s*/, '').replace(/\s\s*$/, '');

  if (strText == '')
    return true;
  else
    return false;
}
function updatePlayCount_vid(videoID) {
  errors = false;

  if (!errors) {
    $.ajax({
      type: 'POST',
      data: 'videoID=' + videoID,
      url: 'ajax/ajax.updatePlayCount_vid.php',
      global: false,
      success: function (html) {
        $('#photoCommentBox').before(html);
        $('#photoCommentArea').val('');
      }
    });
  }
}
function uploadSong() {
  var errors = false;
  var mp3Holder = $('#uploadInputFile').val();
  var songName = $('#songNameField').val();

  if (isEmpty(mp3Holder) || isEmpty(songName))
    errors = true;
  if ($('#certifyBox').is(':checked')) {
    $('#certifyTxt').css({'color' : '#808184'});
    erorrs = false;
  }
  else {
    $('#certifyTxt').css({'color' : '#FF0000'});
    errors = true;
  }

  if (!errors) {
    document.uploadform.submit();
    $('#singleUploadForm').hide();
    $('#singleUploadMessage').removeClass('globalHidden');
  }
}
function uploadVideo() {
  var errors = false;
  var videoHolder = $('#uploadInputFile').val();
  var videoName = $('#videoNameField').val();

  if (isEmpty(videoHolder) || isEmpty(videoName))
    errors = true;
  if ($('#certifyBox').is(':checked')) {
    $('#certifyTxt').css({'color' : '#808184'});
    erorrs = false;
  }
  else {
    $('#certifyTxt').css({'color' : '#FF0000'});
    errors = true;
  }

  if (!errors) {
    document.uploadform.submit();
    $('#singleUploadForm').hide();
    $('#singleUploadMessage').removeClass('globalHidden');
  }
}

function showProfileReplyContainer(id) {
  $('#profileReplyContainer-' + id).fadeIn();
  $('#profileReplyTxtArea-' + id).focus();
}

function showAllWallReplies(id) {
  $('.profileHiddenFeedReply-' + id).show();
  $('#profileShowAllReplies-' + id).hide();
  $('#profileReplyContainer-' + id).fadeIn();
  $('#profileReplyTxtArea-' + id).focus();
}

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

function deleteProfileReply(rid) {
  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=' + rid,
                url: 'ajax/ajax.deleteWallReplyComment.php',
                global: false,
                success: function (response) {
                  if (response == 1)
                    $('#profileFeedReply-' +rid).fadeOut();
                }
              });
          }
  });
}

function profileNotAFriendRequest(id) {
  $.ajax({
    type: 'POST',
    data: 'id=' + id,
    url: 'ajax/ajax.addFriend.php',
    global: false,
    success: function () {
      $('#profileNotAFriendLink').html('Friend requested');
      $('#profileNotAFriendLink2').html('Friend requested');
    }
  });
}

function setVisibility(intUserID, intSectionID, boolValue)
{
	$.ajax({
		type: 'POST',
		data: ({intUserID:intUserID, intSectionID:intSectionID, boolValue:boolValue}),
		url: 'ajax/ajax.profileVisibility.php',
		global: false,
		success: function (js) {
		  	eval(js);
		}
	});
}
