﻿$(document).ready(function () {
    $("div[type=feedbackform]").each(function () {

        var width = $(this).width();
        if (width > 250)
            width = 250;

        var title = $(this).attr("title");
        if (title == "") 
            title = "Задайте свой вопрос";
        
        var subject = $(this).attr("subject");

        $(this).css('display', 'inline-block');
        $(this).html("\
<div id=\"FeedBackForm\" class=\"FeedBackFormGadget\" style=\"width: " + (width - 8) + "px; height: 200px;\">\
    <table style=\"width: 100%; height: 100%; text-align: center;  \">\
        <tbody>\
            <tr valign=\"top;\">\
                <td colspan=\"2\">\
                    " + title + "\
                </td>\
            </tr>\
            <tr>\
                <td colspan=\"2\" style=\"height:50%\">\
                <textarea id=\"FeedbackTextArea\" class=\"FeedBackFormGadget_TextArea\"></textarea>\
                </td>\
            </tr>\
            <tr>\
                <td width=\"50%\">\
                    <img src=\"#\" id=\"CaptureImage\" width=\"90px\" height=\"40px\">\
                </td>\
                <td width=\"50%\">\
                    <span id=\"ReloadCapture\" style=\"cursor:pointer;\">Загрузить другую картинку.</span>\
                </td>\
            </tr>\
            <tr>\
                <td>\
                    <input id=\"FeedBackCaptureText\" type=\"text\" class=\"FeedBackFormGadget_CaptureText\" >\
                </td>\
                <td>\
                    <input id=\"SendFeedback\" value=\"Отправить\" type=\"button\" class=\"FeedBackFormGadget_SendButton\">\
                </td>\
            </tr>\
        </tbody>\
    </table>\
</div>");

        // LOAD CAPTURE IMAGE
        this.loadCapture = function () {
            var loadCaptureService = new OS.TC.Portal.WebSite.Modules.Core.WebServices.Core();

            loadCaptureService.set_defaultSucceededCallback(Function.createDelegate(this,
            function (result) {
                this.captureCode = result.captureCode;
                $("#CaptureImage", this).attr("src", result.captureUrl);
            }));
            loadCaptureService.set_defaultFailedCallback(function (error) { });

            loadCaptureService.GetFeedbackCaptureImage();
        };
        this.loadCapture();

        // SEND FEEDBACK HANDLER
        $("#SendFeedback", this).click(Function.createDelegate(this,
            function () {
                var sendFeedbackService = new OS.TC.Portal.WebSite.Modules.Core.WebServices.Core();

                sendFeedbackService.set_defaultSucceededCallback(Function.createDelegate(this,
                    function () {
                        alert("Сообщение успешно отправлено!");

                        $("#FeedbackTextArea", this).val("");
                        $("#FeedBackCaptureText", this).val("")
                        this.loadCapture();
                    })
                );
                sendFeedbackService.set_defaultFailedCallback(function (error) { alert(error.get_message()); });

                sendFeedbackService.SendFeedback(subject, $("#FeedbackTextArea", this).val(), this.captureCode, $("#FeedBackCaptureText", this).val());
            })
        );

        // RELOAD CAPTURE HANDLER
        $("#ReloadCapture", this).click(Function.createDelegate(this,
            function () {
                this.loadCapture();
            })
        );
    });
});