﻿

function RecentBlogPosts() {

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/Blog/RecentBlogPosts",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function () {

            //SUCCESS updating seesion ..
            //RELOAD THE BLOG PAGE ..

            $("#ajaxContainer").load("http://" + window.location.hostname + "/Blog .ajaxContent", function (response, status, xhr) {
                if (status == "error") {
                    alert("Sorry but there was an error: ");
                }
            });
        }
    });
}

function OlderBlogPosts(){

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/Blog/OlderBlogPosts",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function () {

            //SUCCESS updating seesion ..
            //RELOAD THE BLOG PAGE ..

            $("#ajaxContainer").load("http://" + window.location.hostname + "/Blog .ajaxContent", function (response, status, xhr) {
                if (status == "error") {
                    alert("Sorry but there was an error: ");
                }
            });
        }
    });
}

function OpenTextAreaBlog(TextAreaTargetID) {

    var textAreaID = "#" + TextAreaTargetID;

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/home/UserLoggedIn",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function loggedInReturn(loggedin) {
            if (loggedin.status == 'true') {

            

                //first close all open textareas
                $('.textAreaWrap').animate({ 'height': '0' }, 1000, function() {
                        // Close all open textareas Animation complete.
                        $('.textAreaWrap').css('display', 'none');

                        //if user is logged in show text area
                        $(textAreaID).css('display', 'block');
                        $(textAreaID).animate({ 'height': '150' }, 1000 );
                       
                    }
                );


       
                
                //if user is logged in show text area
                $(textAreaID).css('display', 'block');
                $(textAreaID).animate({
                    'height': '150'
                }, 1000);

            } else {
                displayError(''); //Before you post, we need to know who you are?
            }
        },
        error: function () {
            alert('Sorry there was an AJAX error.')
        }
    });
}

function SubmitBlogComment(TextAreaTargetID, adminPostID, userCommentsWrapID) {

    var textAreaID = '#' + TextAreaTargetID;
    //var commentPost = $(textAreaID).val().replace("'", "ˈ");
    var commentPostTemp = $(textAreaID).val();
    var commentPost = "";

    //make sure comment is not empty
    if ((commentPostTemp != '') && (commentPostTemp != 'Type comment here ...')) {

        for (var i = 0; i < commentPostTemp.length; i++) {
            if (commentPostTemp.charAt(i) == "'") {
                commentPost = commentPost + "ˈ";
            } else {
                commentPost = commentPost + commentPostTemp.charAt(i);
            }
        }

        var userCommentsWrap = '#' + userCommentsWrapID;

        $.ajax({
            url: 'http://' + window.location.hostname + '/blog/SubmitBlogCommentToSQLAjax',
            type: 'POST',
            data: 'commentPost=' + commentPost + '&adminPostID=' + adminPostID,
            dataType: 'json',
            cache: false,
            success: function (result) {
                if (result.success == true) {

                    //Close comment box after comment was submitted ..
                    $(textAreaID).animate({
                        'height': '0'
                    }, 600,
                    function () {
                        //hide the textbox
                        $('.textAreaWrap').css('display', 'none');
                        //repopulate the comments with new one
                        $(userCommentsWrap).html(result.rePopString);
                    });
                } else {
                    displayError(''); //Before you post, we need to know who you are?
                }
            },
            error: function () {
                alert('Sorry there was an AJAX error.')
            }
        });
    }
}


/* ////////////////////////////////// DELETE BLOG POST ///////////////////////////// */

function DeleteBlogPost(blogID, imageName) {

    $.ajax({
        url: 'http://' + window.location.hostname + '/blog/DeleteBlogPostAjax',
        type: 'POST',
        data: 'adminPostID=' + blogID + '&imageName=' + imageName,
        dataType: 'json',
        cache: false,
        success: function (result) {
            if (result.success == true) {
                // reload the blog page
                navigateToBlog();
            } else {
                alert(result.message);
            }
        },
        error: function () {
            alert('Sorry there was an AJAX error.')
        }
    });
}



