﻿
function JHGalleryREBIND() {
    $('a[rel*=facebox]').facebox();

    /* STRIP NON-ALPHANUMERIC CHARACTORS */
    $('#facebox .content .stripHtml').alphanumeric({ ichars: '<>' });
    $('#facebox .content .stripHtmlTags').alphanumeric({ ichars: '<>' });

    /* THIS WILL VERTICALLY CENTER ALL THE THUMBNAILS */
    $('.imageContainer').each(function (index) {
        var imgContainHeight = $(this).height();
        var imgInnerHeight = $(this).children('.highslide').children('img').height();

        var imageOffset = (imgInnerHeight - imgContainHeight) / 2;
        var imageOffsetString = '-' + imageOffset + 'px';
        $(this).children('.highslide').children('img').css('margin-top', imageOffsetString);
    }); 
}

function TextArea_LoginCheck() {

    $.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') {
                //do nothing the user is logged in
            } else {
                displayError(''); //Before you post, we need to know who you are?
            }
        },
        error: function () {
            alert('Sorry an AJAX error ocurred :(')
        }
    });
}

function SubmitAdminGalleryComment(a, asocAdminImageID, c, textAreaGalleryCommentID, commentRePopTargetID) {

    var textAreaID = '#facebox #' + textAreaGalleryCommentID;
    
    //var userComment = $(textAreaID).val().replace("'", "ˈ");

    //var userCommentTemp = $(textAreaID).val();

    var userCommentTemp = $("#facebox textarea[name='commentTextArea']").val();
    var userComment = "";

    for (var i = 0; i < userCommentTemp.length; i++) {
        if (userCommentTemp.charAt(i) == "'") {
            userComment = userComment + "ˈ";
        } else {
            userComment = userComment + userCommentTemp.charAt(i);
        }
    }

    $.ajax({
        url: 'http://' + window.location.hostname + '/JHGallery/SubmitJHGalleryComentsAJAX_SQL',
        type: 'POST',
        data: 'imageComment=' + userComment + '&postersName=' + 'frankNothing' + '&asocAdminImageID=' + asocAdminImageID,
        dataType: 'json',
        cache: false,
        success: function loggedInReturn(result) {

            if (result.success == true) {

                //alert(loggedin.rePopedComments);

                var commentWrap = "#facebox #" + commentRePopTargetID;
                $(commentWrap).html(result.rePopedComments);

                //$(textAreaID).val(''); /* clear comment box */
                $("#facebox textarea[name='commentTextArea']").val(''); /* clear comment box */

                //do something

            } else {
                displayError(''); //Before you post, we need to know who you are?
            }
        },
        error: function () {
            alert('Sorry there was an AJAX error.')
        }
    });
}

/* ////////////////////////////////////////////////////////////////////////////////////////////////////// */
/* ////////////////////////// SetJHGalleryImageCategory //////////////////////////////////////////////// */
/* ///////////////////////////////////////////////////////////////////////////////////////////////////// */

function SetJHGalleryImageCategory(category) {

    $.ajax({
        url: 'http://' + window.location.hostname + '/JHGallery/SetJHGalleryImageCategory',
        type: 'POST',
        data: 'category=' + category,
        dataType: 'json',
        cache: false,
        success: function (result) {
            if (result.success == true) {

                $("#ajaxContainer").load("/JHGallery .ajaxContent", function (response, status, xhr) {
                    hideAjaxLoader();
                    if (status == "error") {
                        alert("Sorry but there was an error: ");
                    } else {
                        JHGalleryREBIND();
                    }
                });

            } else {
                alert('Sorry there was an AJAX error.');
            }
        },
        error: function () {
            alert('Sorry there was an AJAX error.');
        }
    });
}

/* ////////////////////////////////////////////////////////////////////////////////////////////////////////// */
/* ////////////////////////////////// DeleteJHGalleryPic //////////////////////////////////////////////////// */
/* ////////////////////////////////////////////////////////////////////////////////////////////////////////// */

function DeleteJHGalleryPic(adminImageID, adminImageName) {

    $.ajax({
        url: 'http://' + window.location.hostname + '/JHGallery/DeleteJHGalleryPic',
        type: 'POST',
        data: 'adminImageID=' + adminImageID + '&adminImageName=' + adminImageName,
        dataType: 'json',
        cache: false,
        success: function (result) {
            if (result.success == true) {

                //RELOAD PAGE ON SUCCESS
                jQuery.facebox.close();
                window.navigateToJHGallery();

            } else {
                alert(result.message);
            }
        },
        error: function () {
            alert(result.message);
        }
    });
}

//SubmitJHGalleryComentsAJAX_SQL(string imageComment, string postersName, string asocAdminImageID

function RecentjhGalleryPosts() {

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/jhGallery/RecentjhGalleryPosts",
        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 + "/jhGallery .ajaxContent", function (response, status, xhr) {
                if (status == "error") {
                    alert("Sorry but there was an error: ");
                } else {
                    JHGalleryREBIND();
                }
            });
        }
    });
}

function OlderjhGalleryPosts() {

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/jhGallery/OlderjhGalleryPosts",
        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 + "/jhGallery .ajaxContent", function (response, status, xhr) {
                if (status == "error") {
                    alert("Sorry but there was an error: ");
                } else {
                    JHGalleryREBIND();
                }
            });
        }
    });
}

/* //////////////////////////////////////////////////////////////////   GALLERY VIRAL ////////////////////////////////////////////////////////////// */

function ShareJHImageOnFacebook(postTitle, picture) {

    FB.ui({

        method: 'stream.publish',
        message: 'just seen this pic on johnnyhollow.com ...',
        attachment: {
            name: postTitle,
            description: (postTitle),
            media: [{ type: 'image',
                src: 'http://www.johnnyhollow.com/adminImages/' + picture,
                href: 'http://www.johnnyhollow.com/'
            }],
            href: 'http://www.johnnyhollow.com/'
        }
    },

    function (response) {
        if (response && response.post_id) {
            //success action

        } else {
            //failure action
        }

    });
}
