(function( $ ) {
'use strict';
$(function() {
var seasons = {};
$('#twf_wpr_season>option').each( function() {
seasons[$(this).val()] = $(this).text();
});
if ( tyresaddict_woo_finder === null ) {
return;
}
/* tabs */
$('.js-finder-tabs a').on( 'click', function() { // common tabs
$(this).parent().find($(this).data('tabs')).removeClass('active');
$(this).addClass('active');
$(this).closest('.twf-finder').find($(this).data('blocks')).hide();
$(this).closest('.twf-finder').find($(this).data('block')).show();
});
$('.js-finder-selector a').on( 'click', function() { // common selector
$($(this).data('tabs')).removeClass('active');
$(this).addClass('active');
$($(this).data('blocks')).hide();
$($(this).data('block')).show();
});
/* selectors */
$('.js-twf-auto-make').on( 'change', function(){ tfp.mmy_make( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-auto-model').on( 'change', function(){ tfp.mmy_model( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-auto-year').on( 'change', function(){ tfp.mmy_year( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-ymm-year').on( 'change', function(){ tfp.ymm_year( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-ymm-make').on( 'change', function(){ tfp.ymm_make( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-ymm-model').on( 'change', function(){ tfp.ymm_model( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-auto-width').on( 'change', function(){ rstfp.wpr_width( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-auto-profile').on( 'change', function(){ rstfp.wpr_profile( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-auto-diameter').on( 'change', function(){ rstfp.wpr_diameter( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
// $('.js-twf-auto-season').on( 'change', function(){ rstfp.wpr_season( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
$('.js-twf-auto-tyre-brand').on( 'change', function(){ rstfp.wpr_tyre_brand( $(this).data('idprefix'), $(this).closest('.twf-finder') ); });
/* integration widget */
$('.js-tf-integration').on( 'click', function(){
tfp.tyres_popup( '', false, $(this).data('make'), $(this).data('model'), $(this).data('year'), $(this).data('mod') );
return false;
});
$('.js-tfw-integration').on( 'click', function(){
tfp.wheels_popup( '', false, $(this).data('make'), $(this).data('model'), $(this).data('year'), $(this).data('mod') );
return false;
});
// mod | mmy
$('.js-twf-auto-mod').on( 'change', function() {
var widget = '#' + $(this).data('idprefix') + '_';
var finder = $(this).closest('.twf-finder');
tfp.tyres_popup( widget, finder,
finder.find(widget + 'auto_make').val(), finder.find(widget + 'auto_model').val(),
finder.find(widget + 'auto_year').val(), finder.find(widget + 'auto_mod').val(), !tyresaddict_woo_finder.popup_only_button );
});
$('.js-twf-w-auto-mod').on( 'change', function() {
var widget = '#' + $(this).data('idprefix') + '_';
var finder = $(this).closest('.twf-finder');
tfp.wheels_popup( widget, finder,
finder.find(widget + 'auto_make').val(), finder.find(widget + 'auto_model').val(),
finder.find(widget + 'auto_year').val(), finder.find(widget + 'auto_mod').val(), !tyresaddict_woo_finder.popup_only_button );
});
// mod | ymm
$('.js-twf-ymm-mod').on( 'change', function() {
var widget = '#' + $(this).data('idprefix') + '_';
var finder = $(this).closest('.twf-finder');
tfp.tyres_popup( widget, finder,
finder.find(widget + 'ymm_make').val(), finder.find(widget + 'ymm_model').val(),
finder.find(widget + 'ymm_year').val(), finder.find(widget + 'ymm_mod').val(), !tyresaddict_woo_finder.popup_only_button );
});
$('.js-twf-w-ymm-mod').on( 'change', function() {
var widget = '#' + $(this).data('idprefix') + '_';
var finder = $(this).closest('.twf-finder');
tfp.wheels_popup( widget, finder,
finder.find(widget + 'ymm_make').val(), finder.find(widget + 'ymm_model').val(),
finder.find(widget + 'ymm_year').val(), finder.find(widget + 'ymm_mod').val(), !tyresaddict_woo_finder.popup_only_button );
});
// button
$('.js-tf-vehicle-button').on( 'click', function(){
tfp.tyres_popup_call( $(this).data('idprefix'), $(this).closest('.twf-finder'), $(this).data('fieldprefix'), true );
return false;
});
$('.js-tfw-vehicle-button').on( 'click', function(){
tfp.wheels_popup_call( $(this).data('idprefix'), $(this).closest('.twf-finder'), $(this).data('fieldprefix'), true );
return false;
});
var tfp = {
mmy_make : function ( idprefix, finder )
{
var widget = '#' + idprefix + '_';
finder.find(widget + 'auto_model').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_models',
'make' : finder.find(widget + 'auto_make').val()
}, function(data)
{
finder.find(widget + 'auto_model').removeClass('load');
if (!data.result) {
alert(data.message);
return;
}
finder.find(widget + 'auto_model').html('').removeAttr('disabled');
finder.find(widget + 'auto_year').html( '').attr('disabled', '');
finder.find(widget + 'auto_mod').html( '').attr('disabled', '');
if ( finder.find(widget + 'auto_make').val() == '' )
finder.find(widget + 'auto_model').attr('disabled', '');
finder.find(widget + 'auto_button').attr('disabled', '');
$.each(data.models, function()
{
//$(sel_model).append('');
finder.find(widget + 'auto_model').append('');
});
}, "json");
},
ymm_year : function ( idprefix, finder )
{
var widget = '#' + idprefix + '_';
finder.find(widget + 'ymm_make').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_ymm_makes',
'year' : finder.find(widget + 'ymm_year').val()
}, function(data)
{
finder.find(widget + 'ymm_make').removeClass('load');
if (!data.result) {
alert(data.message);
return;
}
finder.find(widget + 'ymm_make').html( '').removeAttr('disabled');
finder.find(widget + 'ymm_model').html('').attr('disabled', '');
finder.find(widget + 'auto_mod').html( '').attr('disabled', '');
if ( finder.find(widget + 'ymm_year').val() == '' )
finder.find(widget + 'ymm_make').attr('disabled', '');
finder.find(widget + 'auto_button').attr('disabled', '');
$.each(data.makes, function()
{
finder.find(widget + 'ymm_make').append('');
});
}, "json");
},
mmy_model : function ( idprefix, finder )
{
var widget = '#' + idprefix + '_';
finder.find(widget + 'auto_year').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_years',
'make' : finder.find(widget + 'auto_make').val(),
'model' : finder.find(widget + 'auto_model').val()
}, function(data)
{
finder.find(widget + 'auto_year').removeClass('load');
if (!data.result) {
alert(data.message);
return;
}
finder.find(widget + 'auto_year').html( '').removeAttr('disabled');
finder.find(widget + 'auto_mod').html( '').attr('disabled', '');
if ( finder.find(widget + 'auto_model').val() == '' )
finder.find(widget + 'auto_year').attr('disabled', '');
finder.find(widget + 'auto_button').attr('disabled', '');
$.each(data.years, function()
{
finder.find(widget + 'auto_year').append('');
});
}, "json");
},
ymm_make : function ( idprefix, finder )
{
var widget = '#' + idprefix + '_';
finder.find(widget + 'ymm_model').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_ymm_models',
'year' : finder.find(widget + 'ymm_year').val(),
'make' : finder.find(widget + 'ymm_make').val()
}, function(data)
{
finder.find(widget + 'ymm_model').removeClass('load');
if (!data.result) {
alert(data.message);
return;
}
finder.find(widget + 'ymm_model').html( '').removeAttr('disabled');
finder.find(widget + 'ymm_mod').html( '').attr('disabled', '');
if ( finder.find(widget + 'ymm_make').val() == '' )
finder.find(widget + 'ymm_model').attr('disabled', '');
finder.find(widget + 'auto_button').attr('disabled', '');
$.each(data.models, function()
{
finder.find(widget + 'ymm_model').append('');
});
}, "json");
},
mmy_year : function( idprefix, finder )
{
var widget = '#' + idprefix + '_';
finder.find(widget + 'auto_mod').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_mods',
'make' : finder.find(widget + 'auto_make').val(),
'model' : finder.find(widget + 'auto_model').val(),
'year' : finder.find(widget + 'auto_year').val()
}, function(data){
finder.find(widget + 'auto_mod').removeClass('load');
if (!data.result) {
alert(data.message);
return;
}
finder.find(widget + 'auto_mod').html( '' ).removeAttr('disabled');
if ( finder.find(widget + 'auto_year').val() == '' )
finder.find(widget + 'auto_mod').attr('disabled', '');
finder.find(widget + 'auto_button').attr('disabled', '');
$.each(data.mods, function()
{
finder.find(widget + 'auto_mod').append('');
});
}, "json");
},
ymm_model : function ( idprefix, finder )
{
var widget = '#' + idprefix + '_';
finder.find(widget + 'ymm_mod').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_mods',
'year' : finder.find(widget + 'ymm_year').val(),
'make' : finder.find(widget + 'ymm_make').val(),
'model' : finder.find(widget + 'ymm_model').val()
}, function(data)
{
finder.find(widget + 'ymm_mod').removeClass('load');
if (!data.result) {
alert(data.message);
return;
}
finder.find(widget + 'ymm_mod').html( '' ).removeAttr('disabled');
if ( finder.find(widget + 'ymm_year').val() == '' )
finder.find(widget + 'ymm_mod').attr('disabled', '');
finder.find(widget + 'auto_button').attr('disabled', '');
$.each(data.mods, function()
{
finder.find(widget + 'ymm_mod').append('');
});
}, "json");
},
tyres_popup_call : function( widget_prefix, finder, fields_prefix, is_popup_open )
{
var widget = '#' + widget_prefix + '_';
tfp.tyres_popup( widget, finder,
finder.find(widget + fields_prefix + '_make').val(),
finder.find(widget + fields_prefix + '_model').val(),
finder.find(widget + fields_prefix + '_year').val(),
finder.find(widget + fields_prefix + '_mod').val(),
is_popup_open
);
},
tyres_popup : function( widget, finder, auto_make, auto_model, auto_year, auto_mod, is_popup_open = true )
{
var data = {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_trim',
'make' : auto_make,
'model' : auto_model,
'year' : auto_year,
'mod' : auto_mod
};
if ( finder != false )
finder.find(widget + 'auto_mod').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, data, function(data){
if ( finder != false )
finder.find(widget + 'auto_mod').removeClass('load');
if (!data.result)
alert(data.message);
else
{
$('.js-twf-tyre-sizes').html(data.html).show().slideDown();
$('.js-twf-wheel-sizes').html('').hide();
$('.js-tfw-vehicle-name').html(data.car);
if (is_popup_open)
tfp.popup();
if ( finder != false )
finder.find(widget + 'auto_button').removeAttr('disabled');
}
}, "json");
},
wheels_popup_call : function( widget_prefix, finder, fields_prefix, is_popup_open )
{
var widget = '#' + widget_prefix + '_';
tfp.wheels_popup( widget, finder,
finder.find(widget + fields_prefix + '_make').val(),
finder.find(widget + fields_prefix + '_model').val(),
finder.find(widget + fields_prefix + '_year').val(),
finder.find(widget + fields_prefix + '_mod').val(),
is_popup_open
);
},
wheels_popup : function ( widget, finder, auto_make, auto_model, auto_year, auto_mod, is_popup_open = true )
{
var data = {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_trim_wheels',
'make' : auto_make,
'model' : auto_model,
'year' : auto_year,
'mod' : auto_mod
};
if ( finder != false )
finder.find(widget + 'auto_mod').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, data, function(data){
if ( finder != false )
finder.find(widget + 'auto_mod').removeClass('load');
if (!data.result)
alert(data.message);
else
{
$('.js-twf-tyre-sizes').html('').hide();
$('.js-twf-wheel-sizes').html(data.html).show().slideDown();
$('.js-tfw-vehicle-name').html(data.car);
if ( data.trim.multisearch_url !== 'undefined' )
{
// finder.find(widget + 'auto_button').data('filter_redirect', data.trim.multisearch_url);
}
if (is_popup_open)
tfp.popup();
if ( finder != false )
finder.find(widget + 'auto_button').removeAttr('disabled');
}
}, "json");
},
popup : function()
{
if ( tyresaddict_woo_finder.popup_type == 'default')
jQuery("#twf-finder-results").iziModal("open");
else
PUM.open(tyresaddict_woo_finder.popup_id);
return false;
}
}
var rstfp = {
types : [
'width', 'profile', 'diameter',
'season',
'tyre_brand'
],
wpr_width : function ( idprefix, finder )
{
this.rs( idprefix, finder, 'width', this.types );
},
wpr_profile : function ( idprefix, finder )
{
this.rs( idprefix, finder, 'profile', this.types );
},
wpr_diameter : function ( idprefix, finder )
{
this.rs( idprefix, finder, 'diameter', this.types );
},
wpr_tyre_brand : function ( idprefix, finder )
{
this.rs( idprefix, finder, 'tyre_brand', this.types );
},
rs : function ( idprefix, finder, name, selTypes )
{
var type = selTypes[0];
selTypes.splice(0, 1);
if(type == name){
type = selTypes[1];
selTypes.splice(0, 1);
}
var widget = '#' + idprefix + '_';
var selector_type = finder.find(widget + `wpr_${type}`);
var val_type = selector_type.val();
var val_type2 = '';
var selector_name = finder.find(widget + `wpr_${name}`);
var option_val = (selector_type.val() == '' && name == 'tyre_brand') ? 'all' : selector_type.val();
var ajax_data = {
'action': 'tyresaddict_woo_finder',
'method': `tyredb_rs`,
'type' : type,
[name] : selector_name.val()
};
this.types.forEach( gtype => {
if( gtype != name && gtype != type ){
val_type2 = finder.find(widget + `wpr_${gtype}`).val();
if(!( val_type2 == '' || (val_type2 == 'all' && gtype == 'tyre_brand' ) || (val_type2 == 'all' && gtype == 'season' )))
ajax_data[gtype] = val_type2;
}
});
// console.log(ajax_data);
selector_type.addClass('load');
selector_type.html('');
$.get( tyresaddict_woo_finder.ajax_url, ajax_data, function(data)
{
selector_type.removeClass('load');
if (!data.result) {
alert(data.message);
return;
}
selector_type.html(`').removeAttr('disabled');
if ( selector_name.val() == '' )
selector_type.attr('disabled', '');
finder.find(widget + 'wpr_button').attr('disabled', '');
$.each(data[type], function()
{
(type == 'season' && seasons[this]) ? console.log(seasons[this]):'';
var node = document.createElement('option');
node.value = this;
node.textContent = (type == 'season' && seasons[this]) ? seasons[this] : this;
node.selected = ( this == val_type );
selector_type.append(node);
});
this.rs( idprefix, finder, name, selTypes );
}, "json");
},
tyres_popup_call : function( widget_prefix, finder, fields_prefix, is_popup_open )
{
var widget = '#' + widget_prefix + '_';
tfp.tyres_popup( widget, finder,
finder.find(widget + fields_prefix + '_make').val(),
finder.find(widget + fields_prefix + '_model').val(),
finder.find(widget + fields_prefix + '_year').val(),
finder.find(widget + fields_prefix + '_mod').val(),
is_popup_open
);
},
tyres_popup : function( widget, finder, auto_make, auto_model, auto_year, auto_mod, is_popup_open = true )
{
var data = {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_trim',
'make' : auto_make,
'model' : auto_model,
'year' : auto_year,
'mod' : auto_mod
};
if ( finder != false )
finder.find(widget + 'auto_mod').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, data, function(data){
if ( finder != false )
finder.find(widget + 'auto_mod').removeClass('load');
if (!data.result)
alert(data.message);
else
{
$('.js-twf-tyre-sizes').html(data.html).show().slideDown();
$('.js-twf-wheel-sizes').html('').hide();
$('.js-tfw-vehicle-name').html(data.car);
if (is_popup_open)
tfp.popup();
if ( finder != false )
finder.find(widget + 'auto_button').removeAttr('disabled');
}
}, "json");
},
wheels_popup_call : function( widget_prefix, finder, fields_prefix, is_popup_open )
{
var widget = '#' + widget_prefix + '_';
tfp.wheels_popup( widget, finder,
finder.find(widget + fields_prefix + '_make').val(),
finder.find(widget + fields_prefix + '_model').val(),
finder.find(widget + fields_prefix + '_year').val(),
finder.find(widget + fields_prefix + '_mod').val(),
is_popup_open
);
},
wheels_popup : function ( widget, finder, auto_make, auto_model, auto_year, auto_mod, is_popup_open = true )
{
var data = {
'action': 'tyresaddict_woo_finder',
'method': 'vehicledb_trim_wheels',
'make' : auto_make,
'model' : auto_model,
'year' : auto_year,
'mod' : auto_mod
};
if ( finder != false )
finder.find(widget + 'auto_mod').addClass('load');
$.get( tyresaddict_woo_finder.ajax_url, data, function(data){
if ( finder != false )
finder.find(widget + 'auto_mod').removeClass('load');
if (!data.result)
alert(data.message);
else
{
$('.js-twf-tyre-sizes').html('').hide();
$('.js-twf-wheel-sizes').html(data.html).show().slideDown();
$('.js-tfw-vehicle-name').html(data.car);
if ( data.trim.multisearch_url !== 'undefined' )
{
// finder.find(widget + 'auto_button').data('filter_redirect', data.trim.multisearch_url);
}
if (is_popup_open)
tfp.popup();
if ( finder != false )
finder.find(widget + 'auto_button').removeAttr('disabled');
}
}, "json");
},
popup : function()
{
if ( tyresaddict_woo_finder.popup_type == 'default')
jQuery("#twf-finder-results").iziModal("open");
else
PUM.open(tyresaddict_woo_finder.popup_id);
return false;
}
}
$('.js-finder-tyre-front').on( 'click', function(){
$('.js-finder-tyre-front').hide();
$('.js-finder-tyre-rear').show();
$('.js-tyre-size-rear').slideToggle({duration:300});
return false;
});
$('.js-finder-tyre-rear').on( 'click', function(){
$('.js-finder-tyre-front').show();
$('.js-finder-tyre-rear').hide();
$('.js-tyre-size-rear select').prop('selectedIndex',0);
$('.js-tyre-size-rear').slideToggle({duration:300});
return false;
});
$('.js-finder-wheel-front').on( 'click', function(){
$('.js-finder-wheel-front').hide();
$('.js-finder-wheel-rear').show();
$('.js-wheel-size-rear').slideToggle({duration:300});
return false;
});
$('.js-finder-wheel-rear').on( 'click', function(){
$('.js-finder-wheel-front').show();
$('.js-finder-wheel-rear').hide();
$('.js-wheel-size-rear select').prop('selectedIndex',0);
$('.js-wheel-size-rear').slideToggle({duration:300});
return false;
});
if ( tyresaddict_woo_finder.fix_url_params )
{
$(window).on('pageshow', function() {
$('.js-tf-size-button').closest('form').find(':input').attr('disabled', false);
});
}
$('.js-tf-size-button').on( 'click', function(){
var form = $(this).closest('form');
var a = $('', {
href: $(this).closest('form').attr('action')
});
if ( tyresaddict_woo_finder.fix_url_params )
{
form.find(':input').filter(function(){
if ( $(this).hasClass('js-tf-size-button'))
return false;
return this.value == "" || this.value == "all";
}).attr('disabled', true);
}
if ( a.prop('search') == '' )
return true;
a.prop('search').substr(1).split("&").forEach(function(item) {
var key = item.split("=")[0];
var value = item.split("=")[1];
var input = document.createElement('input');
input.setAttribute('name', key);
input.setAttribute('value', value);
input.setAttribute('type', 'hidden');
form[0].appendChild(input);
});
form.submit();
return false;
});
$('.js-garage-clear').on( 'click', function()
{
var data = {
'action': 'tyresaddict_woo_finder',
'method': 'garage_clear'
};
$.post( tyresaddict_woo_finder.ajax_url, data, function(data){
if (!data.result)
{
alert(data.message);
return false;
}
$('.css-cars').html('').hide();
$('.css-cars-empty').show();
}, "json");
return false;
});
$('.js-garage-car-rm').on( 'click', function()
{
var data = {
'action': 'tyresaddict_woo_finder',
'method': 'garage_car_rm',
'make' : $(this).data('make'),
'model' : $(this).data('model'),
'year' : $(this).data('year'),
'mod' : $(this).data('mod')
};
$(this).parent().slideUp();
$.post( tyresaddict_woo_finder.ajax_url, data, function(data){
if (!data.result)
{
alert(data.message);
return false;
}
}, "json");
return false;
});
});
})( jQuery );