﻿var isSlided = false;
var isBasketSlided = false;
var isOpeningMenu = false;
var showWindow;
var isDelivMenuPositionCalculate = false;
var isDelivMenuShow = false;

$(document).ready(function () {
    var isSlided = $("#divTownSelector").css("display") != "none" ? false : true;
    $("body").click(
			  function () {
			      var townselectorblock = $("#divTownSelector");
			      townselectorblock.hide();
			      isSlided = true;
			  });
			  var townselectorblock = $("#divTownSelector");
    $("#imgts").click(function (event) {
        if (isSlided) {
            isSlided = false;
            townselectorblock.show();
        }
        else {
            isSlided = true;
            townselectorblock.hide();
        }
        return false;
    });

    $('a.images').openDOMWindow({
        borderColor: "#ca0045",
        overlayColor: "#FFF",
        overlayOpacity: "40",
        width: 600,
        height: 600,
        borderSize: "1",
        positionType: 'centered',
        positionTop: 20,
        eventType: 'click',
        windowSource: 'iframe',
        windowPadding: 0,
        shadow: true
    });
});

$.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;
function AddToCompare(cb) {
    if (cb.checked) {
        productToCompare += 1;
        var href = $("a.compare").attr("href");
        var pos = href.indexOf("=") + 1;
        if ($(cb).attr("value") != "") {
            href = href.substr(0, pos) + $(cb).attr("value") + "," + href.substr(pos);
            $("a.compare").attr("href", href);
        }
    }
    else {
        productToCompare -= 1;
        var href = $("a.compare").attr("href");
        href = href.replace($(cb).attr("value") + ",", "");
        $("a.compare").attr("href", href);
    }

    if (productToCompare > 0)
        $('div.compare').show("slow");
    else
        $('div.compare').hide("slow");
}


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 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;
    var left = aElem.position().left - 10;
    var top = aElem.position().top - 20;

    $.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 }, 0).animate({ opacity: 1 }, 0).show().delay(1000).fadeOut(100, function () {
                    $("#BasketTotalPrice").text(data);
                    var count = parseInt($("#BasketTotalCount").text()) + 1;
                    $("#BasketTotalCount").text(count);

                    $.openDOMWindow({
                        height: 90,
                        width: 250,
                        borderSize: "1",
                        windowPadding: 0,
                        windowSourceID: '#basketOk',
                        borderColor: "#ca0045",
                        windowSource: 'inline',
                        overlayColor: "#FFF",
                        overlayOpacity: "40",
                        positionType: 'centered',
                        positionLeft: 120,
                        positionTop: -100,
                        shadow: true,
                        anchoredSelector: aElem
                    });

                //    $("#DomDiv").hide();
             //   });
            }
            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");
            if (noveltysClass == "similar") {
                similar = "&similar=1&product.id=" + $("#productid").val();
            }
            else if (noveltysClass == "central") {
                similar = "&similar=3";
            }
            var groupID = "";
            var swgroupid = $("#swgroupid");
            if (swgroupid.length != 0) {
                groupID = "&group=" + swgroupid.val();
            }

            $.get("/ShowWindow/GetShowWindow.aspx?rowcount=10&columncount=10&rnd=" + Math.random() + similar + groupID,
            function (data) {
                showWindow.html('');
                showWindow.append(data);
                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) {
    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);
    });
};

