﻿var isSlided = false;
var isBasketSlided = false;
var isOpeningMenu = false;
var showWindow;
var isDelivMenuPositionCalculate = false;
var isDelivMenuShow = false;

$.keypress = function(key, callback, args) {
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null
        if(e.keyCode == key) {
            callback.apply(this, args);
            return false;
        }
    });
};

function trim(string) {
    return string.replace(/(^\s+)|(\s+$)/g, "");
};

var productToCompare = 0;
var MaxProductToComapre = 11;
function AddToCompare(cb) {
    if (cb.checked) {
        productToCompare += 1;
        if (productToCompare <= 30) {
            var href = $("#compare").attr("href");
            var pos = href.indexOf("=") + 1;
            if ($(cb).attr("value") != "") {
                href = href.substr(0, pos) + $(cb).attr("value") + "," + href.substr(pos);
                $("#compare").attr("href", href);
            }
        }
        else {
            alert('Максимальное количество товаров для сравнениия - 30');
            cb.checked = false;
        }
    }
    else {
        productToCompare -= 1;
        var href = $("a.compare").attr("href");
        href = href.replace($(cb).attr("value") + ",", "");
        $("a.compare").attr("href", href);
    }
    if (productToCompare > 0)
        $('#compare').show();
    else
        $('#compare').hide();
}

function AddToBasket(elem, goodcount, e, isModifiProductPage) {
    var aElem = $(elem);
    var productId = aElem.attr("product");
    var productPrice = parseInt(aElem.attr("price")) * goodcount;
    var buyimg = aElem.children('img.buyimg');  
    var left = aElem.position().left-30;
    var top  = aElem.position().top -50;
    var isHaveImage = false;
    if (buyimg.length > 0) {
        isHaveImage = true;
    }
    var IsUHT = aElem.attr("uht") == undefined ? false : true;
    var IsSale = aElem.attr("sale") == undefined ? false : true;
    var IsDiscount = aElem.attr("discount") == undefined ? false : true;
    var urlModifers = '', aText;

    $.get("/basket/AddToBasket.aspx?product=" + productId + "&count=" + goodcount + "&rnd=" + Math.random() + urlModifers,
        function (data) {
            if (data != "0") {
                $("#DomDiv")
                    .animate({
                        opacity: 0,
                        left: left,
                        top: top,
                        marginLeft: "0px"
                    }, 0, function () { aElem.hide(); })
                    .show()
                    .animate({
                        opacity: 1
                    }, 50)
                    .animate({
                        marginLeft: "+=200px",
                        opacity: 0
                    }, 700)
                    .animate({
                        marginLeft: "0px"
                    }, 1)
                    .fadeOut(100, function () {

                        var prCount = $("#BasketProductCount");
                        var prCountNew = parseInt(prCount.text()) + 1;
                        $("#BasketProductCount").text(prCountNew);
                        $("#BasketTotalPrice").text(data);

                        aElem.show();
                        if (IsUHT && isHaveImage) {
                            buyimg.attr("src", "/images/icons/button_kupit.jpg");
                        }
                        else if (IsDiscount && isHaveImage) {
                            buyimg.attr("src", "/images/icons/button_kupit.jpg");
                        }
                        else if (isHaveImage) {
                            buyimg.attr("src", "/images/icons/button_kupit.jpg");
                        }
                        else {
                            aElem.html(aText);
                        }

                    });
            }
            else {
                alert("Ошибка при добавлении в корзину. Попробуйте снова через несколько минут.");
            }
        });
    };

function Pather() {
    $("a.showPath").click(
		  function(event) {
		      var a = $(this);
		      var popUp = a.prev().prev();
		      if (popUp.css("display") != "none") {
		          popUp.hide();
		          a.children("img").attr("src", "/images/path.png");
		      }
		      else {
		          var position = a.position();
		          popUp.css("left", position.left + "px");
		          popUp.show();
		          a.children("img").attr("src", "/images/path_or.png");
		      }
		      event.stopPropagation();
		  });

    $("a.showPath").hover(
      function() {
          var a = $(this);
          var popUp = a.prev().prev();
          if (popUp.css("display") == "none") {
              a.children("img").attr("src", "/images/path_or.png");
          }
      },
      function() {
          var a = $(this);
          var popUp = a.prev().prev();
          if (popUp.css("display") == "none") {
              a.children("img").attr("src", "/images/path.png");
          }
      });
};

var currheight = 0;
var slideHeight = 0;
var slideWidth = 0;
var resizeOnWork = 0;
function onResize() {
    if (((currheight != document.documentElement.clientHeight && jQuery.browser.msie) || !jQuery.browser.msie) && resizeOnWork == 0) {
        resizeOnWork = 1; 

        if (showWindow == undefined) {
            showWindow = $("#noveltys");
        }

        if ($("#swv").val() == "1") {
            var etalon = $("#maincatalog");
            var width = 0;

            if (etalon.length == 0) {
                etalon = $("#pather");
                if (etalon.length == 0) {
                    etalon = $("#tags");
                    width = etalon.width() + 28;
                }
                else {
                    width = etalon.width();
                }
            }
            else {
                width = etalon.width() / 2 - 20;
            }
            if (jQuery.browser.msie && ($.browser.version == "7.0" || $.browser.version == "6.0")) {
                width = width - 20;
            }
            var blocks = parseInt(width / 150);
            var blockWidth = parseInt(width / blocks);
            var remainigWidth = width - (blockWidth * (blocks - 1)) - (blockWidth);
            if (remainigWidth > 0) {
                blockWidth += parseInt(remainigWidth / blocks);
            }
            showWindow.html("<img class='loader' src='/images/ajax-loader.gif' alt='загружаю' />");
            var similar = "";
            var noveltysClass = showWindow.attr("class");
            var rowCount = 5;
            if (noveltysClass == "similar") {
                similar = "&similar=1&product.id=" + $("#productid").val();
                rowCount = 2;
            }
            else if (noveltysClass == "themebooks") {
                similar = "&similar=2&product.id=" + $("#productid").val();
                rowCount = 2;
            }
            else if (noveltysClass == "central") {
                similar = "&similar=3";
                rowCount = 5;
            }
            var groupID = "";
            var swgroupid = $("#swgroupid");
            if (swgroupid.length != 0) {
                groupID = "&group=" + swgroupid.val();
            }

  $.get("/ShowWindow/GetShowWindow.aspx?rowcount=10&columncount=4&rnd=" + Math.random() + similar + groupID,
            function (data) {
                showWindow.html('');
               // showWindow.append('<table cellpadding=0 cellspacing=0 border=0>');
                showWindow.append(data);
               // showWindow.append('</table>');
                var novblocks = $("div.novblock");
                if (novblocks.length == 0 && noveltysClass == "similar") {
                    $("h1.pohogie").hide();
                    $("#noveltys").next().hide();
                }
                resizeOnWork = 0;
            });
        }
        else {
            var noveltys = $("#filter");
            if (noveltys.length == 0) {
                noveltys = $("#searchblock");
            }
            if (noveltys.length == 0) {
                noveltys = $("#productlist");
            }
            resizeOnWork = 0;
        }
    }
    currheight = document.documentElement.clientHeight;
};

var curentBlock = $("#group1");
function pageselectCallback(page_index, jq) {
    curentBlock.hide();
    curentBlock = $("#group" + (page_index + 1).toString());
    curentBlock.show();
    return false;
};

function ChangeCategory(lvl, selec) {
    $("#searchLoader").show();
    $("#divFilterSelectors").hide();

    var selected = $("#" + selec + " option:selected");
    var isChildren = selected.attr("isChildren");
    var sel_value = selected.val();
    if (lvl == 0 && sel_value != 0) {
        $(".trCategorys").show();
    }
    if (lvl == 0 && sel_value == 0) {
        $(".trCategorys").hide();
    }
    if (sel_value != 0 && isChildren == 1) {
        var url = "/GetCategorys.aspx?category.id=";
        $("#sclvl" + lvl).load(url + sel_value);
    }
    else {
        if (isChildren == 1) {
            sel_value = selected.attr("all");
        }
        $("#sclvl" + lvl).text("");
    }
    if (sel_value == 0) {
        sel_value = selected.attr("all");
    }
    $(".hiddenCat").val(sel_value);


    $.get("/GetCategorys.aspx?manufac.id=1&category.id=" + sel_value + "&rnd=" + Math.random(),
        function (data){
        $("#manufacturerSelector").html(data);
         $("#searchLoader").hide();
        $("#divFilterSelectors").slideDown(500);
    });
};

