(function(a,m){var e={session:function(){return a("html").attr("data-session")},idStore:function(){return a("html").attr("data-store")},removeProduct:function(b){var d=parseInt(a(b).attr("data-id")),c="/"+a(b).attr("data-variant"),h=""==a(b).attr("data-add")?"":"/?additional_information="+a(b).attr("data-add").replace(/br>/g,"br/>");b=""!==a(b).attr("data-together")?"/"+a(b).attr("data-together"):"";a.ajax({method:"DELETE",url:"/web_api/carts/"+e.session()+"/"+d+c+b+h}).success(function(k){e.listProduct()}).fail(function(k){e.listProduct()})}, listProduct:function(){a.ajax({method:"GET",url:"/checkout/cart/api?session_id="+e.session()+"&store_id="+e.idStore()+"&nocache=0."+(new Date).getTime(),success:function(b){var d=[];b.data.cart.products.forEach(function(c){d.push({Cart:{email:"",variants_kit:c.variants_kit||"",additional_info_kit:c.additional_info_kit||"",price_itens_kit:c.price_itens_kit||"",product_id:c.id,product_name:c.name,quantity:c.quantity,price:c.price,variant_id:c.variant_id||"0",additional_information:c.additional_information, product_url:c.url,bought_together_id:c.bought_together_id||"",product_image:c.images}})});e.forProduct(d)},error:function(){e.forProduct([])}})},number:function(b){a(".cart-header .number").text(b)},total:function(b){a(".cart-sidebar .total .value").text(toReal(parseFloat(b),"R$"))},forProduct:function(b){var d=a(".cart-sidebar .content-cart .list");d.find("*").remove();d.parent().removeClass("empty");a(".modal-theme").removeClass("active");a(".cart-header").addClass("active");var c=a(".botao-commerce.buy"), h=c.attr("href")+"/checkout?session_id="+e.session()+"&store_id="+e.idStore();c.attr("href",h);var k=0,l=0,f=[];b.length?(b.forEach(function(g){g=g.Cart;var n=g.additional_information.replace(/\//g,"");prices=g;d.append(e.templateProduct(g.product_id,g.variant_id,g.product_name,g.product_image.medium,g.quantity,g.price,g.product_url.https,n,g.additional_info_kit,g.bought_together_id));k+=parseInt(g.quantity);l+=parseFloat(g.price)*parseInt(g.quantity);f.push(parseInt(g.product_id))}),e.number(k), e.total(l)):(d.append('
Carrinho Vazio
'),d.parent().addClass("empty"),e.number(0),a("body").find(".add-cart .buy-product").each(function(){a(this).hasClass("active")&&a(this).removeClass("active").find(".text").text("Comprar")}))},startCart:function(){a(".cart-header .area").click(function(){a(this).parent().hasClass("active")?a(".cart-header").removeClass("active"):e.showCart()});a(".shadow-cart, .shadow-cart-header, .box-prev, .close-nav,.box-fixed .close-box,.close-modal,.close-icon,.modal-theme .shadow").click(function(b){a(".cart-sidebar, .nav-mobile,.box-fixed,.modal-theme,.cart-header").removeClass("active")}); this.initAdd();a(".product-submit").submit(function(b){b.preventDefault();b=a(this).find(".select").val();var d=a(this).find(".quantity").val(),c=a(this).find(".quantity").attr("data-id");b&&e.addVariantComplete(b,d,c)});a(".remove-items").click(function(){e.removeCart()})},removeCart:function(){a.ajax({method:"DELETE",url:"/web_api/carts/"+e.session()}).success(function(b){e.listProduct()}).fail(function(b){e.listProduct()})},showCart:function(){e.listProduct();a(".header .menu").is(":visible")&& a("html, body").animate({scrollTop:0})},templateProduct:function(b,d,c,h,k,l,f,g,n,p){f='
Qtd: {length}
{price}
'.replace(/{url}/g, f);f=f.replace(/{image}/g,h);f=f.replace(/{name}/g,c);f=f.replace(/{id}/g,b);f=f.replace(/{variant}/g,d);f=f.replace(/{length}/g,k);f=f.replace(/{addMsg}/g,g);f=f.replace(/{together}/g,p);l=toReal(parseFloat(l),"R$");return f=f.replace(/{price}/g,l)},addVariantComplete:function(b,d,c){a.ajax({method:"POST",url:"/web_api/cart/",contentType:"application/json; charset=utf-8",data:'{"Cart":{"session_id":"'+e.session()+'","product_id":"'+c+'","quantity":"'+d+'","variant_id":"'+b+'"}}'}).success(function(h, k,l){e.showCart()}).fail(function(h,k,l){m.location.href=a(".modal-product").find(".name a").attr("href")})},filterVariant:function(b,d){var c=0,h=d.eq(0).val();if(h)for(var k=d.eq(1).val();c'+h+'
'+k+"
");Number(d.stock)>=c?a(b).removeClass("dont-stock"):a(b).addClass("dont-stock")},initAdd:function(){a("body").on("change",".add-cart input",function(){var b=Number(a(this).val());a(this).val(0 a").attr("href"),h=a(this).data("variants").length? a(this).find("input").attr("data-variant"):0;e.addToCart(b,d,h,c)})},submitAdd:function(){a(".add-cart-modal form").submit(function(b){b.preventDefault();b=a(this).find("#product_modal").val();var d=a(this).find("#quant_modal").val(),c=a(this).find("#variant_modal");c.hasClass("required")?a("#alert-modal-add").removeClass("tray-hide"):(a(".action-add .add-cart").attr("disabled"),e.addVariantComplete(c.val(),d,b))})},addToCart:function(b,d,c,h){a.ajax({method:"POST",url:"/web_api/cart/",contentType:"application/json; charset=utf-8", data:'{"Cart":{"session_id":"'+e.session()+'","product_id":"'+b+'","quantity":"'+d+'","variant_id":"'+c+'"}}'}).success(function(){e.showCart()}).fail(function(){m.location.href=h})},ajaxGet:function(b,d){a.ajax({method:"GET",url:b}).success(function(c){d(c)}).fail(function(c,h,k){d({error:!0});c=$.parseJSON(c.responseText);console.error(c)})}};e.startCart();m.cart=e;a(".cart-sidebar").on("click",".remove",function(){e.removeProduct(this)});a(".compreJunto form").bind("submit.botao-compre-junto", function(){if(!a(this).find(".blocoAlerta").is(":visible"))var b=setInterval(function(){a(".cart-preview-loading-modal").hasClass("tray-hide")&&(e.showCart(),clearInterval(b))},50)})})(jQuery,window);