﻿
function ResizeSpeakUpAvatars() {

    $('.speakUpAvatarImgWrap').each(function (index) {
        var imgContainHeight = $(this).height();

        var imgContainWidth = $(this).width();
        var imgInnerHeight = $(this).children('img').height();
        var imgInnerWidth = $(this).children('img').width();

        if (imgInnerHeight > imgInnerWidth) { //Orientation == 'portrait'

            $(this).children('.postersAvatarImage').css('width', imgContainWidth + 'px');

            //get new width/height
            imgInnerHeight = $(this).children('img').height();
            imgInnerWidth = $(this).children('img').width();

            //offset image
            var imageOffset = (imgInnerHeight - imgContainHeight) / 2;
            var imageOffsetString = '-' + imageOffset + 'px';
            $(this).children('img').css('margin-top', imageOffsetString);

        } else { //Orientation == 'landscape';

            $(this).children('img').css('height', imgContainHeight + 'px');

            //get new width/height
            imgInnerHeight = $(this).children('img').height();
            imgInnerWidth = $(this).children('img').width();

            //offset image
            var imageOffset = (imgInnerWidth - imgContainWidth) / 2;
            var imageOffsetString = '-' + imageOffset + 'px';
            $(this).children('img').css('margin-left', imageOffsetString);
        }


    });
}



/* ////////////////////////////////// DELETE SPEAKUP POST ///////////////////////////// */

function DeleteSpeakUpPost(postID) {

    $.ajax({
        url: 'http://' + window.location.hostname + '/SpeakUp/DeleteSpeakUpPostAjax',
        type: 'POST',
        data: 'publicAdminPostID=' + postID,
        dataType: 'json',
        cache: false,
        success: function (result) {
            if (result.success == true) {
                // reload the blog page
                navigateToSpeakUp();
            } else {
                alert(result.message);
            }
        },
        error: function () {
            alert('Sorry there was an AJAX error.')
        }
    });
}

function OpenTextAreaSpeakUp(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);
                    }
                );

            } else {
                displayError(''); //Before you post, we need to know who you are?
            }
        },
        error: function () {
            alert('Sorry there was an AJAX error.')
        }
    });
}

function SubmitSpeakUpComment(TextAreaTargetID, publicAdminPostID, userCommentsWrapID) {
    
    var textAreaID = '#' + TextAreaTargetID;

    var commentPostTemp = $(textAreaID).val();
    var commentPost = "";

    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 + '/SpeakUp/SubmitSpeakUpUserCommentsToSQLAjax',
            type: 'POST',
            data: 'commentPost=' + commentPost + '&publicAdminPostID=' + publicAdminPostID,
            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.')
            }
        });
    }
}


/* /////////////////////// OPEN POST PANEL ////////////////////////////////// */

function OpenSpeakUpPanel() {
    //so really I'm just going to open the login panel
    displayError(''); //this displays login popup
}

















//SPEAKUP PAGING FUNCTIONS ..

function RecentSpeakUpPosts() {

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/SpeakUp/RecentSpeakUpPosts",
        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 + "/SpeakUp .ajaxContent", function (response, status, xhr) {
                if (status == "error") {
                    alert("Sorry but there was an error: ");
                }
            });
        }
    });
}

function OlderSpeakUpPosts() {

    $.ajax({
        type: "POST",
        url: "http://" + window.location.hostname + "/SpeakUp/OlderSpeakUpPosts",
        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 + "/SpeakUp .ajaxContent", function (response, status, xhr) {
                if (status == "error") {
                    alert("Sorry but there was an error: ");
                }
            });
        }
    });
}



function SpeakUpREBIND() {

    /* for now I'm just going to delay the resize untill I can listen for complete */
    setTimeout('ResizeSpeakUpAvatars()', 1000);

    
    /* STRIP NON-ALPHANUMERIC CHARACTORS */
    $('.stripHtml').alphanumeric({ ichars: '<>' });
    $('.stripHtmlTags').alphanumeric({ ichars: '<>' });
};
