jQuery(document).ready(function($){
'use strict';
$(document.body).on('wc_fragment_refresh',block_cart);
$(document.body).on('wc_fragments_refreshed wc_fragments_loaded',function(){
content_height();
});
if(!divilife_wsc_localize.added_to_cart){
$(document.body).trigger('wc_fragment_refresh');
}
function update_cartChk(){
if(window.wc_checkout_params&&wc_checkout_params.is_checkout==="1"){
if($('form.checkout').length===0){
location.reload();
return;
}
$(document.body).trigger("update_checkout");
}
if(window.wc_add_to_cart_params&&window.wc_add_to_cart_params.is_cart&&wc_add_to_cart_params.is_cart==="1"){
$(document.body).trigger("wc_update_cart");
}}
$(document.body).on('divilife_wsc_cart_updated',update_cartChk);
function toggle_sidecart(toggle_type){
var toggle_element=$('.divilife-wsc-modal , body, html'),
toggle_class='divilife-wsc-active';
if(toggle_type=='show'){
toggle_element.addClass(toggle_class);
}
else if(toggle_type=='hide'){
toggle_element.removeClass(toggle_class);
}else{
toggle_element.toggleClass('divilife-wsc-active');
}
unblock_cart();
}
$('.divilife-wsc-basket').on('click',toggle_sidecart);
if(divilife_wsc_localize.trigger_class){
$('.'+divilife_wsc_localize.trigger_class).on('click',toggle_sidecart);
}
function content_height(){
var header=$('.divilife-wsc-header').outerHeight(),
footer=$('.divilife-wsc-footer').outerHeight(),
screen=$(window).height();
if(divilife_wsc_localize.cont_height=="auto_adjust"){
var body_height='calc(100% - '+(header+footer)+'px)';
if($('.divilife-wsc-container').outerHeight() > screen){
$('.divilife-wsc-container').css({"top": "0", "bottom": "0"});
}}else{
var body_height=screen-(header+footer);
}
$('.divilife-wsc-body').css('height',body_height);
};
content_height();
$(window).resize(function(){
content_height();
});
function reset_cart(atc_btn){
$('.divilife-wsc-icon-atc',atc_btn).remove();
var qty_elem=atc_btn.parents('form.cart').find('.qty');
if(qty_elem.length > 0) qty_elem.val(qty_elem.attr('min')||1);
$('.added_to_cart').remove();
}
(function(){
if(divilife_wsc_localize.added_to_cart){
var toggled=false;
$(document).on('wc_fragments_refreshed',function(){
if(!toggled){
setTimeout(toggle_sidecart,1,'show');
toggled=true;
}})
}}());
$(document).on('added_to_cart',function(event,fragments,cart_hash,atc_btn){
if(divilife_wsc_localize.auto_open_cart==1){
setTimeout(toggle_sidecart,1,'show');
}
if(divilife_wsc_localize.atc_reset==1){
reset_cart(atc_btn);
}
update_cartChk();
});
function block_cart(){
$('.divilife-wsc-updating').show();
}
function unblock_cart(){
$('.divilife-wsc-updating').hide();
}
function close_sidecart(e){
$.each(e.target.classList,function(key,value){
if(value!='divilife-wsc-container'&&(value=='divilife-wsc-close'||value=='divilife-wsc-opac'||value=='divilife-wsc-basket'||value=='divilife-wsc-cont')){
$('.divilife-wsc-modal , body, html').removeClass('divilife-wsc-active');
}})
}
$('body').on('click','.divilife-wsc-close , .divilife-wsc-opac',function(e){
e.preventDefault();
close_sidecart(e);
});
$('body').on('click','.divilife-wsc-cont',function(e){
var link=$.trim($(this).attr('href'));
if(link=="#"||!link){
e.preventDefault();
close_sidecart(e);
}});
function add_to_cart(atc_btn,product_data){
$(document.body).trigger('adding_to_cart', [ atc_btn, product_data ]);
$.ajax({
url: divilife_wsc_localize.wc_ajax_url.toString().replace('%%endpoint%%', 'divilife_wsc_add_to_cart'),
type: 'POST',
data: $.param(product_data),
success: function(response){
add_to_cart_button_check_icon(atc_btn);
if(response.fragments){
$(document.body).trigger('added_to_cart', [ response.fragments, response.cart_hash, atc_btn ]);
}
else if(response.error){
show_notice('error',response.error);
toggle_sidecart('show');
}else{
console.log(response);
}}
})
}
function update_cart(cart_key,new_qty){
$.ajax({
url: divilife_wsc_localize.wc_ajax_url.toString().replace('%%endpoint%%', 'divilife_wsc_update_cart'),
type: 'POST',
data: {
cart_key: cart_key,
new_qty: new_qty
},
success: function(response){
if(response.fragments){
var fragments=response.fragments,
cart_hash=response.cart_hash;
$.each(response.fragments, function(key, value){
$(key).replaceWith(value);
$(key).stop(true).css('opacity', '1').unblock();
});
if(wc_cart_fragments_params){
var cart_hash_key=wc_cart_fragments_params.ajax_url.toString() + '-wc_cart_hash';
sessionStorage.setItem(wc_cart_fragments_params.fragment_name, JSON.stringify(fragments) );
localStorage.setItem(cart_hash_key, cart_hash);
sessionStorage.setItem(cart_hash_key, cart_hash);
}
$(document.body).trigger('wc_fragments_loaded');
$(document.body).trigger('divilife_wsc_cart_updated');
}else{
show_notice('error',response.error);
}}
})
}
$(document).on('click','.divilife-wsc-remove',function(e){
e.preventDefault();
block_cart();
var product_row=$(this).parents('.divilife-wsc-product');
var cart_key=product_row.data('divilife_wsc');
update_cart(cart_key,0);
})
$(document).on('submit','form.cart',function(e){
if(divilife_wsc_localize.ajax_atc!=1) return;
e.preventDefault();
block_cart();
var form=$(this);
var atc_btn=form.find('button[type="submit"]');
add_to_cart_button_loading_icon(atc_btn);
var product_data=form.serializeArray();
var has_product_id=false;
$.each(product_data,function(key,form_item){
if(form_item.name==='product_id'||form_item.name==='add-to-cart'){
if(form_item.value){
has_product_id=true;
return false;
}}
})
if(!has_product_id){
var is_url=form.attr('action').match(/add-to-cart=([0-9]+)/);
var product_id=is_url ? is_url[1]:false;
}
if(atc_btn.attr('name')&&atc_btn.attr('name')=='add-to-cart'&&atc_btn.attr('value')){
var product_id=atc_btn.attr('value');
}
if(product_id){
product_data.push({name: 'add-to-cart', value: product_id});
}
product_data.push({name: 'action', value: 'divilife_wsc_add_to_cart'});
add_to_cart(atc_btn,product_data);
})
function show_notice(notice_type,notice){
$('.divilife-wsc-notice').html(notice).attr('class','divilife-wsc-notice').addClass('divilife-wsc-nt-'+notice_type);
$('.divilife-wsc-notice-box').fadeIn('fast');
clearTimeout(fadenotice);
var fadenotice=setTimeout(function(){
$('.divilife-wsc-notice-box').fadeOut('slow');
},2000);
};
function add_to_cart_button_loading_icon(atc_btn){
if(divilife_wsc_localize.atc_icons!=1) return;
if(atc_btn.find('.divilife-wsc-icon-atc').length!==0){
atc_btn.find('.divilife-wsc-icon-atc').attr('class','divilife-wsc-icon-spinner2 divilife-wsc-icon-atc divilife-wsc-active');
}else{
atc_btn.append('<span class="divilife-wsc-icon-spinner2 divilife-wsc-icon-atc divilife-wsc-active"></span>');
}}
function add_to_cart_button_check_icon(atc_btn){
if(divilife_wsc_localize.atc_icons!=1) return;
atc_btn.find('.divilife-wsc-icon-atc').attr('class','divilife-wsc-icon-checkmark divilife-wsc-icon-atc');
}});