/*
 *  Naam      : R. Wacanno  Skip Harks  Chris Bras  Bodi Boelé
 *  UvAnetID  : 11741163    12002038    11917318    11904089
 *  Studie    : BSc Informatica
 *
 *  homeTextfield.js
 *  -This file handles the manipulation of the text on the 
 *   home page by admins.
 */

/* Pulls the home page text from the database and displays it 
   on the home page, including markdown conversion. */
function pullHomeText()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        var xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            $('#homeText').html(this.responseText);
        }
    };
    xmlhttp.open("POST", "hometext.php");
    xmlhttp.send();
}

/* Pulls the home page text from the database and inserts it 
   into the textaea (mean for editing), excluding markdown conversion. */
function pullHomeTextP()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        var xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // code for IE6, IE5
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            $('#homeTextfield').val(this.responseText);
        }
    };
    xmlhttp.open("POST", "homeTextP.php");
    xmlhttp.send();
}

/* Inserst contents of the textarea into the database. */
function pushHomeText()
{
    var text = $('#homeTextfield').val();
    var dataString = "text=" + text;

    $.ajax({
        type: "POST",
        url: "changetext.php",
        data: dataString,
        cache: false
    })
    .done(function(data){
        showHomeText()
    })
    .fail(function() {
        alert( "Er ging iets mis bij het opslaan." );
    });
}

/* Shows the div with the home page text and hides the textarea, 
   Also changes the 'annuleren' button into the 'veranderen' button*/
function showHomeText()
{
    pullHomeText();
    pullHomeTextP();
    $('#homeText').css('display', 'inline');
    $('#homeTextWrapper').css('display', 'none');
    $('#homeSaveButton').css('display', 'none');
    $('#homeTextButton').html('Veranderen');
    $('#homeTextButton').attr('onclick', 'showHomeField()');
    $('#homeTextButton').attr('class', 'formButton');
}

/* Shows the textarea and hides the div with the home page text, 
   Also changes the 'veranderen' button into the 'annuleren' button*/
function showHomeField()
{
    pullHomeText();
    pullHomeTextP();
    $('#homeText').css('display', 'none');
    $('#homeTextWrapper').css('display', 'inline');
    $('#homeSaveButton').css('display', 'inline');
    $('#homeTextfield').focus();
    $('#homeTextButton').html('Annuleren');
    $('#homeTextButton').attr('onclick', 'showHomeText()');
    $('#homeTextButton').attr('class', 'formButtonRed');
}

/* Saves the contents of the textarea. */
function saveHomeText()
{
    pushHomeText();
}

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

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