﻿/// <reference path="/Custom/Ui/Common/js/jquery-1.4.4-vsdoc.js" />

var valtech = valtech || {};

valtech.comments = (function () {

    var AddComment = function () {
        var commentContainer = $(this).closest(".comment-container");
        var commentForm = commentContainer.find(".comment-form");
        commentForm.appendTo(commentContainer);
        $(this).hide();
        commentForm.show();
        commentForm.find("#ParentCommentId").val('');
        commentForm.find("#ReportComment").val('false');
        $("#AddCommentFormHeading").show();
        $("#ReportCommentFormHeading").hide();
    };

    var CheckAddComment = function () {
        if (window.location.hash === '#addcomment') {
            AddComment.apply($("#comment-add")[0]);
        }
    };

    var init = function () {

        var validationElements = $(".comment-validation > ul > li");
        if (validationElements && validationElements.size() > 0) {
            var commentContainer = $("#comment-add").closest(".comment-container");
            var commentForm = commentContainer.find(".comment-form");
            commentForm.appendTo(commentContainer);
            $("#comment-add").hide();
            commentForm.show();
            commentForm.find("#ParentCommentId").val('');
            commentForm.find("#ReportComment").val('false');
            $("#AddCommentFormHeading").show();
            $("#ReportCommentFormHeading").hide();
        }

        $("#comment-add").click(AddComment);

        $("#comment-cancel").click(function () {
            var commentContainer = $(this).closest(".comment-container");
            var commentForm = commentContainer.find(".comment-form")
            commentForm.hide();
            commentForm.find("#ParentCommentId").val('');
            commentForm.find("#ReportComment").val('false');
            commentContainer.find("#comment-add").show();
        });

        $(".comment-reply").click(function () {
            showCommentForm(this, false);
        });

        $(".comment-report").click(function () {
            showCommentForm(this, true);
        });

        $(window).bind('hashchange', function (e) {
            CheckAddComment();
        });

    };

    var showCommentForm = function (element, isReport) {
        var parentCommentID = $(element).closest('li').find("#commentid").val();
        var commentContainer = $(element).closest(".comment-container");
        var commentForm = commentContainer.find(".comment-form");
        commentForm.appendTo($(element).closest("li"));
        commentForm.find("#ParentCommentId").val(parentCommentID);
        commentForm.find("#ReportComment").val(isReport);
        commentForm.show();
        commentContainer.find("#comment-add").hide();

        if (isReport) {
            $("#AddCommentFormHeading").hide();
            $("#ReportCommentFormHeading").show();
        }
        else {
            $("#AddCommentFormHeading").show();
            $("#ReportCommentFormHeading").hide();
        }
    };

    return {
        init: init,
        checkaddcomment: CheckAddComment
    }
})();

$(document).ready(function () {
    $(valtech.comments.init);

    valtech.comments.checkaddcomment();
});

