(function($) {
  var userAgent = navigator.userAgent.toLowerCase();
  var ie_version = -1;
  if (navigator.appName == 'Microsoft Internet Explorer') {
   var re = new RegExp("msie ([0-9]{1,}[\.0-9]{0,})");
   if (re.exec(userAgent) != null)
   ie_version = parseFloat(RegExp.$1);
  }
  $.browser = {
    version: (ie_version>-1)?ie_version:(userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
    safari: /webkit/.test( userAgent ),
    opera: /opera/.test( userAgent ),
    msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
    mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
  };
})(jQuery);

function changeGoodCount(id, count, price, redirect) {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ajax.php",
        data: { fact: "send", type: "changeGoodCount", id: id , count: count, price: price},
        success: function(data){

            if(redirect) document.location.href = '/checkout/';

            if(data.count<=0) $("#row" + id).remove();

            $("#good" + id).text(data.count);
            $("#totalGoodPrice" + id).text(data.totalGoodPrice);
            $(".totalCount").text(data.totalCount);
            $(".totalCount2").text('(' + data.totalCount + ')');
            $(".totalPrice").text(data.totalPrice);
            $(".shopping-cart a").attr("title", "Всего " + data.totalCount + " наклеек на общую сумму " + data.totalPrice + " рублей");

            $(".item" + id).addClass('active');
            $(".item" + id + " .sticker-buy a").text("купить еще (уже " + data.count + ")");

            if(data.totalPrice<=0) {
                $(".hasPositions").hide();
                $(".noPositions").show();
                $(".totalCount2").empty();
            } else {
                $(".hasPositions").show();
                $(".noPositions").hide();
            }

            return false;

        }
    });

}

function removeVariant(id) {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ajax.php",
        data: { fact: "send", type: "removeVariant", id: id},
        success: function(data){

            $("#row" + id).remove();

            $("#good" + id).text(data.count);
            $("#totalGoodPrice" + id).text(data.totalGoodPrice);
            $(".totalCount").text(data.totalCount);
            $(".totalCount2").text('(' + data.totalCount + ')');
            $(".totalPrice").text(data.totalPrice);
            $(".shopping-cart a").attr("title", "Всего " + data.totalCount + " наклеек на общую сумму " + data.totalPrice + " рублей");

            if(data.totalPrice<=0) {
                $(".hasPositions").hide();
                $(".noPositions").show();
                $(".totalCount2").empty();
            } else {
                $(".hasPositions").show();
                $(".noPositions").hide();
            }


        }
    });
}

function sendMessage() {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ajax.php",
        data: { fact: "send", type: "sendMessage", name: $(".page41 .form .email").val(), message: $(".page41 .form .message").val()},
        success: function(data) {

            $(".page41 .form").fadeOut("slow", function() {
                $(".page41 .message-result").fadeIn("slow");
            });

        }
    });
}