/*
 *  Naam      : R. Wacanno  Skip Harks  Chris Bras  Bodi Boelé
 *  UvAnetID  : 11741163    12002038    11917318    11904089
 *  Studie    : BSc Informatica
 *
 *  changeReplies.js
 *  - This file makes it possible for an user to edit his own replies.
 */
 /* Get the text from the database */
function pullText()
{
    var replyId = <?php echo json_encode($reactieId); ?>;

    $.get("replyContent.php",
    {
        id: replyId
    },
    function(data)
    {
        $('#replyMessage').html(data);
    }).fail(function()
    {
        alert( "Er ging iets mis bij het laden." );
    });
}

/* get the text from the database without parsedown */
function pullTextP()
{
    var replyId = <?php echo json_encode($reactieId); ?>;

    $.post("replyContentP.php",
    {
        id: threadId
    },
    function(data)
    {
        $('#textfield').val(data);
    }).fail(function()
    {
        alert( "Er ging iets mis bij het laden." );
    });
}

/* push the text into the database */
function pushText()
{
    var text = $('#textfield').val();
    var replyId = <?php echo json_encode($reactieId); ?>;

    $.post("changeReplies.php",
    {
        reactie_id: replyId,
        text: text
    },
    function(data)
    {
        showText();
    }).fail(function()
    {
        alert( "Er ging iets mis bij het opslaan." );
    });
}

/* shows the text */
function showText()
{
    pullText();
    pullTextP();
    $('#textWrapper').css('display', 'none');
    $('#saveButton').css('display', 'none');
    $('#textButton').html('Veranderen');
    $('#textButton').attr('onclick', 'showField()');
    $('#textButton').attr('class', 'formButton');
}

/* shows the textfield */
function showField()
{
    pullText();
    pullTextP();
    $('#textWrapper').css('display', 'inline');
    $('#saveButton').css('display', 'inline');
    $('#textfield').focus();
    $('#textButton').html('Annuleren');
    $('#textButton').attr('onclick', 'showText()');
    $('#textButton').attr('class', 'formButtonRed');
}

/* saves the text */
function saveText()
{
    pushText();
}

$('body').ready(showText());

/* Adapt textarea height based on its contents */
$('#textWrapper').on( 'focus change keyup keydown paste cut', 'textarea', function (){
    $(this).height(0).height(this.scrollHeight);
}).find( 'textarea' ).change();
