﻿$(function () {

    if (typeof (SVG) == 'undefined') { SVG = {}; }
    if (typeof (SVG.Wall) == 'undefined') { SVG.Wall = {}; }
    else { return; }

    SVG.Wall.MainContainer = null;
    SVG.Wall.WallPostsContainer = null;
    SVG.Wall.LastCommentForm = null;
    SVG.Wall.WallPostInputArea = null;
    SVG.Wall.WallPostForm = null;
    SVG.Wall.WallPostCommentForms = null;
    SVG.Wall.WallPostMoreLink = null;
    SVG.Wall.CallBackInit = null;

    SVG.Wall.AsyncInit = function (callBackInit) {
        SVG.Wall.CallBackInit = callBackInit;
        callBackInit();
    };



    SVG.Wall.Init = function (opts) {
        $.extend(SVG.Wall, opts);

        SVG.Wall.InitPosts();
        SVG.Wall.InitComments();
        SVG.Wall.InitMorePostLinks();
        SVG.Wall.InitMoreCommentsLinks();
        //SVG.Wall.InitDeleteCommentsLinks();
        SVG.Wall.InitDeletePostLinks();
        SVG.Wall.InitCommunityDirectoryLinks();
    };

    SVG.Wall.InitCommunityDirectoryLinks = function () {
        SVG.Wall.MainContainer.delegate('.modal', 'click', function () {
            $(this).colorbox({
                initialHeight: 300,
                initialWidth: 500,
                opacity: 0.4,
                width: 500,
                onOpen: function () {
                    $('#cboxClose').appendTo($('#cboxWrapper'));
                }
            });
        });
    };

    SVG.Wall.InitMoreCommentsLinks = function () {
        if (!SVG.Wall.WallPostCommentForms) return;

        $(SVG.Wall.WallPostCommentForms).each(function (index, form) {

            var container = $(form).parents(".wallPostCommentContainer");
            var el = container.find(".moreCommentsLink");
            var href = $(el).attr("href");

            $(el).click(function () {
                var commentCount = container.find(".wallComment").length;
                $.get(href, { "ShowCommentCount": commentCount }, function (result) {
                    var el = $("<div />").html(result);
                    var comments = el.find(".WallComments").html();
                    var div = $("<div />").html(comments);
                    $(div).find(".moreCommentsLink").remove();
                    $(div).hide();
                    $(container).find(".moreCommentsLink").remove();
                    $(container).find(".WallComments").prepend(div);

                    $(div).slideDown("slow");
                    $(div).find(".moreCommentsLink").css({ "color": "red" }).slideUp();
                    var commentForm = $(container).find("form");
                    SVG.Wall.InitCommentForm(commentForm);
                    SVG.Wall.InitDeleteCommentsLinks(commentForm);
                });
                return false;
            });
        });
    };

    SVG.Wall.InitDeletePostLinks = function (postContainer) {
        if (!SVG.Wall.WallPostsContainer) return;

        var posts = SVG.Wall.WallPostsContainer.find(".wallPost");
        posts.each(function (index, post) {
            var deleteForm = $(post).find(".deletePostForm");
            var deletePostLink = $(post).find(".deletePostLink");
            var postContainer = $(deleteForm).parents(".wallPost");
            var onSuccess = function () { postContainer.remove(); };
            $(deletePostLink).click(function (e) {
                e.preventDefault();
                $(deleteForm).submit();
            });
            $(deleteForm).ajaxForm({ success: onSuccess });
        });

    };


    SVG.Wall.InitDeleteCommentsLinks = function (commentForm) {
        if (!SVG.Wall.WallPostCommentForms) return;

        var forms = commentForm || SVG.Wall.WallPostCommentForms;

        $(forms).each(function (index, form) {

            var postContainer = $(form).parents(".wallPostCommentContainer");
            var commentsContainer = postContainer.find(".wallComment");
            var deleteForms = commentsContainer.find(".deleteCommentForm");

            deleteForms.each(function (index, deleteForm) {
                var commentContainer = $(deleteForm).parents(".wallComment");
                var deleteCommentLink = $(deleteForm).find(".deleteCommentLink");
                var onSuccess = function () { commentContainer.remove(); };
                $(deleteCommentLink).unbind('click');
                $(deleteCommentLink).click(function (e) {
                    e.preventDefault();
                    $(deleteForm).submit();
                });
                $(deleteForm).ajaxForm({ success: onSuccess });
            });
        });

    };

    SVG.Wall.InitMorePostLinks = function () {
        if (!SVG.Wall.WallPostMoreLink) return;

        SVG.Wall.WallPostMoreLink.click(function () {
            var el = SVG.Wall.WallPostMoreLink;
            var href = el.attr("href");

            $(SVG.Wall.MainContainer).load(href, function () {
                SVG.Wall.CallBackInit();
                var url = window.location.href;
                if (!url.match(new RegExp("#More", 'gi')))
                    window.location.href = url + "#" + el.attr("name");
                else
                    window.location.href = url;

                //      SVG.Wall.InitDeletePostLinks(commentForm);

            });

            return false;
        });

    };

    SVG.Wall.InitPosts = function () {

        var wallInputArea = SVG.Wall.WallPostInputArea;
        if (wallInputArea) {
            $(wallInputArea).attr("value", "Type here...");
            $(wallInputArea).focus(function () {
                if ($(wallInputArea).attr("value") == "Type here...") {
                    $(wallInputArea).attr("value", "");
                }
                $(wallInputArea).addClass("focused");
            });
            $(wallInputArea).blur(function () {
                if ($.trim($(wallInputArea).attr("value")) == "") {
                    $(wallInputArea).attr("value", "Type here...");
                }
                $(wallInputArea).removeClass("focused");
            });
        }

        var wallPostForm = SVG.Wall.WallPostForm;

        var postSubmitButton = $(wallPostForm).find("input:submit");
        var postOnSubmit = function () {
            var textArea = SVG.Wall.WallPostInputArea;
            postSubmitButton.attr("disabled", "true");
            if ($(textArea).attr("value") == "Type here...") {
                $(postSubmitButton).removeAttr("disabled");
                return false;
            }

        }; //if(!SVG.ValidateForm(form)) return false;    };
        var postSuccess = function (result) {
            var post = $('<div />').html(result).hide();
            SVG.Wall.WallPostsContainer.prepend(post);
            post.show().effect("highlight", { color: "#fff9ca" }, 1000);
            var postContainer = $(SVG.Wall.WallPostsContainer).find(".wallPost:first");
            SVG.Wall.WallPostInputArea.attr("value", "Type here...");
            $(postSubmitButton).removeAttr("disabled");
            SVG.Wall.InitComments(postContainer);
            SVG.Wall.Init(SVG.Wall.WallPostsContainer);
        };

        if (wallPostForm) {
            wallPostForm.ajaxForm({
                success: postSuccess,
                beforeSubmit: postOnSubmit
            });
        }
    };

    SVG.Wall.InitComments = function (postContainer) {
        if (postContainer) $(postContainer).find(".wallPostCommentEntry").hide();
        else $(".wallPostCommentEntry").hide();

        var wallPostCommentForms;
        if (postContainer) wallPostCommentForms = $(postContainer).find("form.CreateWallPostCommentForm:first");
        else wallPostCommentForms = SVG.Wall.WallPostCommentForms;

        wallPostCommentForms.each(function (index, form) {
            SVG.Wall.InitCommentForm(form);
            SVG.Wall.InitDeleteCommentsLinks(form);
        });
    };

    SVG.Wall.InitCommentForm = function (commentForm) {

        var commentTextArea = $(commentForm).find("textarea");
        $(commentTextArea).addClass("unfocused");
        var commentSubmitButton = $(commentForm).find("input:submit");
        $(commentTextArea).attr("value", "Type here...");


        $(commentTextArea).focus(function () {

            if ($(commentTextArea).attr("value") == "Type here...") {
                $(commentTextArea).removeClass("unfocused");
                $(commentTextArea).addClass("focused");
                $(commentTextArea).attr("value", "");
            }
        });

        var wallPostTextContainer = $(commentForm).parents(".wallPost-text");
        var commentFormContainer = $(commentForm).parents(".wallPostCommentEntry");
        var link = $("<a />")
                    .attr("href", "javascript:")
                    .text("Comment")
                    .addClass("CommentLink")
                    .click(function () {
                        commentFormContainer.fadeIn();
                        commentTextArea.focus();
                        $(this).remove();
                    });


        if (wallPostTextContainer.find(".CommentLink").length == 0 && $(commentForm).find("textarea:visible").length == 0)
            wallPostTextContainer.find(".newCommentLink").append(link);

        var commentOnSubmit = function (arr, $form) {
            commentSubmitButton.attr("disabled", "true");
            SVG.Wall.LastCommentForm = $form;
            var textArea = $form.find("textarea");
            var value = $.trim(textArea.attr("value"));
            if (value == "Type here..." || value == "") {
                $(commentSubmitButton).removeAttr("disabled");
                return false;
            }
        }
        var commentSuccess = function (result) {
            var el = $(wallPostTextContainer).find(".WallCommentPosts")
            //            el.append(result);
            var comment = $('<div />').html(result).hide();
            el.append(comment);
            comment.fadeIn();
            $(commentTextArea).removeClass("focused");
            $(commentSubmitButton).removeClass("focused");
            $(commentSubmitButton).removeAttr("disabled");
            SVG.Wall.InitCommentForm(commentForm);
            SVG.Wall.InitDeleteCommentsLinks(commentForm);
        };

        if (commentForm) {
            $(commentForm).ajaxForm({
                success: commentSuccess,
                beforeSubmit: commentOnSubmit
            });
        }
    }
});
