﻿var searchBox;
var searchSlide;
var searchContainer;

$(function () {
    searchBox = $("input[id$=tbGlobalSearch]");
    searchSlide = $("#divSearchSlide");
    searchContainer = $("#tSearchSlide");
    
    $("#aspnetForm")[0].setAttribute('autocomplete', 'off');
    $(document).click(function () {
        searchSlide.hide();
    });

    $(searchSlide).click(function (e) {
        e.stopPropagation();
    });
    $(searchBox).click(function (e) {
        e.stopPropagation();
    });

        $("input[id$=tbGlobalSearch]").focus(function() {
            $(".search-box").addClass('active');
            if ($(this).val() == "Поиск") {
                $(this).val("");
            }
            GetSearchSlide();
        }).blur(function() {
            $(".search-box").removeClass('active');
            if ($(this).val() == "") {
                $(this).val("Поиск");
            }
        });

        $(".more").unbind("click").click(function () {
            $("#pMoreInfo").removeClass("more-info");
            $(this).remove();
        });
});

function AddCallMe() {
    var name = $("#divCallMe #tbName").val();
    var phone = $("#divCallMe #tbPhone").val();
    var comment = $("#divCallMe #tbComment").val();
    if (name != '' && phone != '') {
        $.ajax({
            type: "POST",
            url: "/jQueryAjaxRequest.aspx/AddCallMe",
            data: "{name: '" + name + "', phone: '" + phone + "', comment: '" + comment + "', url: '" + top.location + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
            $('#divCallMe').hide(); 
            $("#lock").hide(); $.notifier("Ваш номер добавлен.", "success"); },
            error: function() { $('#divCallMe').fadeOut(); $("#lock").hide(); $.notifier("Ошибка. Попробуйте еще раз.", "error"); }
        });
    }
    else {
        $.notifier("Ошибка. Поля заполнены неверно", "error");
    }
}
function CloseCallMe() {
    $("#lock").hide();
    $('#divCallMe').hide();
    //e.stopPropagation();
}

function ShowCallMe() {
    $("#lock").show();
    $('#divCallMe').show();
}
/* -------------------NOTIFIER -----------------*/
(function($) {    
    $.notifier = function(message, type) { notify(message, type); }

    function notify(message, type) {
        var instance = $(document.getElementById('divNoticeContainer'));
        var html = jQuery.notifier.settings.noticeTemplate;
        html = html.replace('%Message%', message);

        if (type == "success")
            html = html.replace('%CssClass%', 'b-notif-success');
        else if (type == "error")
            html = html.replace('%CssClass%', 'b-notif-error');

        var notice = $(html).hide().fadeIn(jQuery.notifier.settings.noticeFadeTimeout);

        $.notifier.settings.noticeDisplay(notice);
        instance.append(notice);

        if ($.notifier.settings.displayTimeout > 0) {
            setTimeout(function() {
                jQuery.notifier.settings.noticeRemove(notice, function() {
                    notice.remove();
                });
            }, jQuery.notifier.settings.displayTimeout);
        }
    };

    $.notifier.settings = {
        noticeTemplate: '<div class="%CssClass%">%Message%</div>',
        noticeDisplay: function(notice) {
            notice.fadeIn(jQuery.notifier.settings.noticeFadeTimeout);
        },
        noticeRemove: function(notice, callback) {
            notice.animate({ opacity: '0', height: '0px' }, { duration: jQuery.notifier.settings.noticeFadeTimeout, complete: callback });
        },
        noticeFadeTimeout: 'slow',
        displayTimeout: 5000
    };
})(jQuery);

//function pageLoad(sender, args) {
//    if (args.get_isPartialLoad()) {
//        completeNotify();
//    }
//}

function completeNotify() {
    var inputs = $("input[id*=hCommandStatusMessage]");
    for (var i = 0; i < inputs.length; i++) {
        var message = inputs[i].value;
        if (message) {
            $.notifier(message);
            inputs[i].value = '';
        }
    }
}

function GlobalSearch() {
    var tb = searchBox[0];
    var reg = /[^0-9a-zA-Zа-яА-Я.,()\-\/ ]/;
    if (tb.value.match(reg)) {
        var replaced = tb.value.split(reg).join("");
        tb.value = replaced;
    }
    GetSearchSlide();
}

function GetSearchSlide() {
    var request = searchBox[0].value;
    if (request.replaceAll(" ", "").length > 1) {
        execText("getSearch_" + request, $("#tSearchSlide"));
    }
    else {
        $("#divSearchSlide").hide();
    }
}

function execText(cmd, container) {
    $.ajax({
        type: "POST",
        url: "/AjaxHandler.ashx",
        data: cmd,
        contentType: "application/x-www-form-urlencoded",
        dataType: "text",
        timeout: 30000,
        success: function (response) { confirmTextCommand(response, container); },
        error: function (response) { /*$.notifier("Произошла ошибка. Попробуйте снова.", "error");*/ }
    });
}

function confirmTextCommand(response, container) {
    var request = searchBox[0].value;

    if (response != "" && request.replaceAll(" ", "").length > 1) {
        searchContainer.html(response);
        $("#divSearchSlide").fadeIn();
    }
    else {
        $("#divSearchSlide").hide();
    }
}
