jQuery(document).ready(function(jQuery){
function validateForm(e){
var x, y, i, valid=true;
x=document.getElementById(e);
y=x.getElementsByTagName("input");
for (i=0; i < y.length; i++){
if(y[i].value==""){
y[i].className +=" invalid anim bounce";
y[i].style="animation-duration: 1s;";
valid=false;
}else{
y[i].classList.remove("invalid");
y[i].classList.remove("anim");
y[i].classList.remove("bounce");
}}
return valid;
}
function createReviewBox(){
jQuery.ajax({
type: 'POST',
url: ajax_unique.ajaxurl,
data: {
action: 'create_review_box',
data: jQuery('#selection').serialize(),
},
success: function (resp, textStatus, XMLHttpRequest){
jQuery("#create_review_box").html(resp);
jQuery(".rate-block").on('click', function(){
set_rating(this, false);
});
jQuery(".rate-block").on('mouseover', function(){
set_rating(this);
});
jQuery(".rate-block").on('mouseout', function(){
set_rating(this, true, true);
});
jQuery("#js_close_review_box").on('click', function(){
deleteReviewBox();
jQuery('body').removeClass("mobile_menu_open");
});
jQuery("#js_sendReview").on('click', function(){
if(validateForm("create_review_form")){
sendReview();
}});
jQuery('body').addClass("mobile_menu_open");
},
error: function (XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}});
}
function sendReview(){
jQuery.ajax({
type: 'POST',
url: ajax_unique.ajaxurl,
data: {
action: 'send_review',
data: jQuery('#create_review_form').serialize(),
},
success: function (resp, textStatus, XMLHttpRequest){
resp=jQuery.parseJSON(resp);
for (var error in resp){
if(resp[error].element!=""){
console.log(resp[error].element);
if(resp[error].ret > 0){
jQuery(resp[error].element).addClass(" invalid anim bounce");
}else{
jQuery('#create_review_form')[0].reset();
}
if(resp[error].message!=""){
jQuery(resp[error].element).closest(".input-element").find('.errortext').html(resp[error].message);
}}
}},
error: function (XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}});
}
function deleteReviewBox(){
jQuery("#create_review_box").html("");
}
function set_rating(e, hover, reset){
if(hover===undefined){
hover=true;
}
if(reset===undefined){
reset=false;
}
var current_rating=jQuery(e).closest('.ratingDistribution-rating');
var count=jQuery(e).data("rating");
for(var i=1; i <=10; i++){
current_rating.find('.rate-'+i).removeClass('rate-hover');
if(!hover){
current_rating.find('.rate-'+i).removeClass('rate-active');
}}
if(reset){
return;
}
if(count){
for(var i=1; i <=count; i++){
if(hover){
current_rating.find('.rate-'+i).addClass('rate-hover');
}else{
current_rating.find('.rate-'+i).addClass('rate-active');
}}
if(!hover){
current_rating.find('.js_rating_value').val(count);
current_rating.find('.js_selected_rating').html(count+".0");
}}
}
jQuery("#js_createReviewBox").on('click', function(){
createReviewBox();
});
jQuery("#selectVariant, #selectSort, .js-datepicker_selection").on('change', function(){
jQuery('#site_js').val(0);
dispProducts();
});
});
jQuery(window).scroll(function (){
jQuery('.productbox').each(function(i, el){
if(jQuery(this).isInViewport()){
if(jQuery(this).attr("data-viewed")==null){
var id=jQuery(this).attr("data-id");
insert_view(id);
}
jQuery(this).attr("data-viewed", "true");
}})
});
function insert_view(id){
jQuery.ajax({
type: 'POST',
url: ajax_unique.ajaxurl,
data: {
action: 'insert_product_view',
data: id
},
success: function (resp, textStatus, XMLHttpRequest){
},
error: function (XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}});
}
function dispProducts(){
jQuery.ajax({
type: 'POST',
url: ajax_unique.ajaxurl,
data: {
action: 'disp_product',
title: ajax_unique.title,
data: jQuery('#selection').serialize(),
},
success: function (data, textStatus, XMLHttpRequest){
var site=jQuery('#site_js').val();
if(site==0)
jQuery("#products").hide().html(data).fadeIn(1500);
else
jQuery("#products").hide().append(data).fadeIn(1500);
site++;
jQuery('#site_js').val(site);
jQuery(".img-gallery").each(function(){
var gallery=new Gallery(this, 'one');
});
},
error: function (XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}});
};