﻿$.fn.customerRequest = function (options) {
    var url = options.url;
    var _this = this;
    $('.header', _this).corner('5px top');
    var closeFunc = function (e) {
        $('.body', _this).slideUp('slow', function (e) {
            $('.body', _this).removeClass('body-expanded');
            $('.body', _this).addClass('body-collapsed');
            $('.request-text', _this).removeClass('text-expanded');
            $('.request-text', _this).addClass('text-collapsed');
            $('.request-text', _this).val('');
            $('.submit-link', _this).hide();
            $('.request-text', _this).hide();
            $('.close', _this).hide();
            $('.subject', _this).hide();
            $('.message-panel', _this).html('Ваше сообщение слишком короткое');
            $('.message-panel', _this).hide();
            $('.raise-request-block', _this).show();
            $('.success-message', _this).hide();
            $('.body', _this).show();
        });
    }
    $('.close', _this).click(function (e) {
        //$('.body', _this).fadeOut('slow', function (e) {
        closeFunc(e);

        //
        // });

    });
    $('.raise-request', _this).click(function (e) {
        $('.request-text', _this).corner('5px');
        $('.body', _this).hide();
        $('.body', _this).corner('5px');
        $('.body', _this).removeClass('body-collapsed');
        $('.body', _this).addClass('body-expanded');
        $('.body', _this).slideDown('slow');
        $('.request-text', _this).removeClass('text-collapsed');
        $('.request-text', _this).addClass('text-expanded');
        $('.request-text', _this).val('');
        $('.submit-link', _this).show();
        $('.request-text', _this).show();
        $('.close', _this).show();
        $('.subject', _this).show();
        $('.message-panel', _this).html('Ваше сообщение слишком короткое');
        $('.message-panel', _this).show();
        $('.raise-request-block', _this).hide();
    });
    $('.request-text', _this).keypress(function (e) {
        //alert($('.request-text', _this).valueOf().length);
        if ($('.request-text', _this).val().length > 3) {
            $('.message-panel', _this).html('');
            $('.submit-link', _this).addClass('active');
            $('.submit-link', _this).removeClass('inactive');
            $('.submit-link', _this).attr('data-isActive', 'true');
        }
        else {
            $('.message-panel', _this).html('Ваше сообщение слишком короткое');
            $('.submit-link', _this).addClass('inactive');
            $('.submit-link', _this).removeClass('active');
            $('.submit-link', _this).attr('data-isActive', 'false');
        }
    });
    $('.submit-link', _this).click(function (e) {
        if ($(this).attr('data-isActive') == 'false') {
            return false;
        }
        var requestText = $('.request-text', _this).val();
        $.ajax({
            type: "POST",
            url: url,
            data: { requestInput: requestText },
            dataType: "html",
            success: function (data) {

                $('.success-message', _this).show();
                setTimeout(
                closeFunc, 2500
                );
            }
        });

    });
}
