﻿var cmsTestimonialArea;

$(document).ready(function() {
    //            if ($('#hiddenEnableCMS').val().toLowerCase() == 'true') {
    //                $('#divCMSTestimonial').show();
    //            }
    //            else {
    //                $('#divCMSTestimonial').hide();
    //            }

    $('#aOpenCMSTestimonialPopup').click(function() {
        editCMSTestimonialContent();
    });

    $('#CancelTestimonialContent').click(function() {
        $.modal.close();
    });

    $('#SaveTestimonialContent').click(function() {
        var dealerIdentification = IdentifyDealer();
        var newContent = nicEditors.findEditor("taTestimonialContent").getContent();

        if (newContent.length <= 0 || newContent == "<br>") {
            $("#divCMSMessageTestimonial").html('Content field is required.');
            $("#divCMSMessageTestimonial").show();
        }
        else {
            var CMSTestimonialObject = {};
            CMSTestimonialObject.TestimonialID = $('#hiddenTestimonialID').val();
            CMSTestimonialObject.Testimonial = newContent;
            CMSTestimonialObject.SubmittedBy = $('#txtTestimonialSubmittedBy').val();
            CMSTestimonialObject.DealerIdentification = dealerIdentification;
            var DTOCMSTestimonial = { "CMSTestimonialObject": CMSTestimonialObject };

            $.ajax({
                type: "POST",
                url: "/Services/CMS.asmx/UpdateTestimonial",
                data: JSON.stringify(DTOCMSTestimonial),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(res) {
                    $("#divCMSMessageTestimonial").hide();
                    $("#divTestimonialContent").html("<q>" + CMSTestimonialObject.Testimonial + " <span class=\"arrow\">&nbsp;</span></q>");
                    $("#divSubmittedByContent").html("<cite><span>- " + CMSTestimonialObject.SubmittedBy + " -</span></cite>");
                },
                error: function(res) {
                }
            });
            $.modal.close();
        }
    });
});

function editCMSTestimonialContent() {
    $("#cmsTestimonialPopup").modal({
        persist: true,
        containerCss: ({ width: "700px", height: "400px" }),
        onClose: modalOnCloseCMSTestimonial,
        onShow: function modalShow(dialog) {
            //Setup the NicEditor
            cmsTestimonialArea = new nicEditor({ maxHeight: 220, buttonList: ['bold', 'italic', 'underline'] }).panelInstance('taTestimonialContent');
        }
    })
}

function modalOnCloseCMSTestimonial(dialog) {
    $.modal.close();

    //Remove the NicEditor
    cmsTestimonialArea.removeInstance('taTestimonialContent');
    cmsTestimonialArea = null;
}             
