﻿var hideResponse = false;

$(document).ready(function () {

    if (g_searchText.length > 0) {
        $("#txtSearch").val(g_searchText);
        $("#txtSearch").removeClass("disabled");
        $("#txtSearch").addClass("enabled");
    }
    
    $("#txtSearch").keypress(function () {
        if (event.keyCode == 13) {
            $("#btnSearch").click();
            return false;
        }
    });

    $("#btnSearch").click(function () {
        if ($("#txtSearch").val().length < 2 || $("#txtSearch").val() == 'Найти...')
            return false;

        //document.location = g_sitePath + "/search/" + encodeURIComponent($("#txtSearch").val()) + ".aspx";
        document.location = g_sitePath + "/search.aspx?name=" + encodeURIComponent($("#txtSearch").val());
    });

    $("#txtSearch").focusin(function () {
        if ($("#txtSearch").val() == 'Найти...')
            $("#txtSearch").val('');

        $("#txtSearch").removeClass("disabled");
        $("#txtSearch").addClass("enabled");
    });

    $("#txtSearch").focusout(function () {
        if ($("#txtSearch").val().length == 0) {
            $("#txtSearch").val('Найти...');
            $("#txtSearch").addClass("disabled");
            $("#txtSearch").removeClass("enabled");
        }
        else {
            $("#txtSearch").removeClass("disabled");
            $("#txtSearch").addClass("enabled");
        }
    });

    $("#viewResponse").click(function () {
        $(".responseList").toggle();
    });

    $("#sendResponse").click(function () {
        if ($(".responseText").val().length < 10 || $(".responseText").val() == "Написать отзыв о товаре...") {
            $("#articleModal .modalTitle").text('Ошибка добавления. Отзыв содержит короткое соощение. ');
            $("#articleModal .modalTitle").css('color', 'red');
            $('#articleModal').modal();

            hideResponse = false;

            return false;
        }

        var articleId = $("#hdArticleId").val();

        $.post(g_sitePath + "/do.aspx", { ajax: "addResponse", id: articleId, text: escape($(".responseText").val()) },
            function (data) {
            }
        );

        hideResponse = true;
        $("#articleModal .modalTitle").text('Ваш отзыв на товар добавлен и будет опубликован после проверки администратором.');
        $("#articleModal .modalTitle").css('color', '#58b403');
        $('#articleModal').modal();

        return false;

    });

    $(".responseText").focusout(function () {
        if ($(".responseText").val().length == 0) {
            $(".responseText").val("Написать отзыв о товаре...");
        }

        $(".responseText").css("color", "#c5c5c5");
    });

    $(".responseText").focusin(function () {
        if ($(".responseText").val() == "Написать отзыв о товаре...")
            $(".responseText").val("");

        $(".responseText").css("color", "#5F574F");
    });

    $("#addResponse").click(function () {
        $("#writeResponse").show();
        $(".responseText").focus();
        $(".responseText").val("");

    });

    $(".btn_update").click(function () {
        var s = new String();
        var ids = "";
        var qs = "";

        $(".shortTextBox").each(function (i, item) {
            s = item.id;
            s = s.replace("quantity_", "");
            ids += (s + ",");
            qs += (item.value + ",");
        });

        $.getJSON("do.aspx", { ajax: "updateBasket", id: ids, count: qs },
            function (json) {
                $("#numItems").text(json.itemCount);
                $("#totalPrice").text(json.itemsSum);
                $("#span_basketSum").text("Сумма:" + json.itemsSum);
                $("#span_basketSale").text(json.sale);
                $("#span_basketSaleSum").text(json.saleSum);

                for (i = 0; i < json.ids.length; i++)
                    $("#rowPrice" + json.ids[i]).text(json.prices[i] + " руб.");
            });

    })

    $('.evBuy').click(function (e) {
        var id = this.id.replace('article_', '');

        if ($('#error_' + id).val().toLowerCase() == 'true') {
            $(".modalTitle").text(this.title);
            $(".modalMessageText").css('color', 'red');
            $(".modalMessageText").text('Товар временно отсутствует');
            $('#basic-modal-content').modal();
            return false;
        }

        $.getJSON(g_sitePath + "/do.aspx", { ajax: "buy", id: id, count: 1 },
                function (json) {
                    $("#numItems").text(json.itemCount);
                    $("#totalPrice").text(json.itemsSum);
                });

        $(".modalMessageText").css('color', '');
        $(".modalTitle").text(this.title);
        $('#basic-modal-content').modal();
        return false;
    });

    $('.evContinue').click(function (e) {

        $.modal.close();

        if (hideResponse)
            $("#writeResponse").hide();

        return false;
    });


    $('.checkKey').keypress(function (e) {

        if (e.keyCode == 13) {
            e.preventDefault();
            $(g_submitButton).click();
        }

    });

    $('#sortOrder').change(function (e) {
        document.location = $('#sortOrder').attr('value');
    });

});

