var CarClick = {
mergeObjects: function(obj_a, obj_b){
var object = {};
var prop;
for(prop in obj_a){
object[prop] = obj_a[prop];
}
for(prop in obj_b){
object[prop] = obj_b[prop];
}
return object;
},
getPathnameObj: function(pathname, basePath) {
pathname = pathname.replace(basePath, '');
var path = pathname.split('/');
var model = path[1];
var action = path[2] || 'busca';
var query = path[3] || '*';
return {
page: basePath,
model: model,
action: action,
query: query
};
},
renderCarUrl: function(car) {
var link = jQuery('a[data-car-link-id="'+car+'"]');
if(CarClick.AdvancedResult._config.options.friendlyUrl) {
var path = CarClick.getPathnameObj(document.location.pathname, CarClick.AdvancedResult._config.options.pageName);
var manufacturer = link.data('car-manufacturer');
link.attr('href', path.page + '/' + link.data('car-slug') + '/detalhes' + '?view=' + car);
} else {
link.attr('href', '?view=' + car);
}
},
getRequest: function(url, callback){
var callback_function = '_' + (new String(Math.random())).replace('.','_');
window[callback_function] = callback;
var s = document.createElement('script');
s.src = 'https://www.carclick.com.br/plugin_v2/jsonp/?u=' + encodeURIComponent(url) + '&c=' + callback_function;
document.getElementsByTagName('head')[0].appendChild(s);
},
AdvancedForm: {
_config: { },
init: function(container_id, url_advanced){
CarClick.AdvancedForm._config.container_id = container_id;
if(arguments[3]){
CarClick.AdvancedForm._config.title = arguments[3];
}
var web_site_custom = 'form' ;
//Verificamos se foi passado um quato parametro, que seria de busca custom (buscas custom são buscas avançadas e personalizadas a pedido do cliente)
if(arguments[4]) {
web_site_custom = 'custom/form-' + arguments[4] ;
}
CarClick.AdvancedForm._config.web_site_custom = web_site_custom;
CarClick.AdvancedForm.preloader();
CarClick.getRequest('https://www.carclick.com.br/plugin_v2/66e870035ffc94882e8b45bb/config', function(data){
var config = jQuery.parseJSON(data);
CarClick.AdvancedForm._config = CarClick.mergeObjects(config,CarClick.AdvancedForm._config);
CarClick.AdvancedForm._config.show_new = parseInt(0);
CarClick.AdvancedForm._config.show_all = parseInt(0);
CarClick.AdvancedForm._config.container_id = container_id;
CarClick.AdvancedForm._config.url_advanced = url_advanced;
CarClick.AdvancedForm._config.filter_dealers = parseInt(0);
CarClick.AdvancedForm._config.show_dealers = parseInt(0);
CarClick.AdvancedForm._config.show_cms_dealers = parseInt(0);
CarClick.AdvancedForm._config.assetsUrl = '//www.carclick.com.br/assets/';
CarClick.AdvancedForm._config.cdnUrl = 'https://assets.disaltecnologia.com.br';
if (web_site_custom != 'custom/form-white-label') {
CarClick.AdvancedForm.renderCss();
}
CarClick.AdvancedForm.renderForm();
});
},
preloader: function(){
var _container = jQuery(CarClick.AdvancedForm._config.container_id);
if(_container.css('position') == 'static'){
_container.css('position','relative');
}
if (CarClick.AdvancedForm._config.web_site_custom != 'custom/form-white-label') {
_container.css({width: 'auto', minHeight: '300px'});
}
var html_preloader = '
Carregando ';
_container.append(html_preloader);
},
renderCss: function(){
jQuery('head').append('');
jQuery('head').append('');
},
renderForm: function(){
CarClick.getRequest('https://www.carclick.com.br/plugin_v2/66e870035ffc94882e8b45bb/advanced/form?show_new=' + CarClick.AdvancedForm._config.show_new + '&' + 'show_all=' + CarClick.AdvancedForm._config.show_all + '&url_advanced=' + CarClick.AdvancedForm._config.url_advanced + '&web_site_custom=' + CarClick.AdvancedForm._config.web_site_custom + '&title=' + CarClick.AdvancedForm._config.title , function(html){
jQuery(CarClick.AdvancedForm._config.container_id).html(html);
});
},
toggleAccessoriesBox: function(){
jQuery('#cc-plugin-advanced-box-accessories-selection').toggle();
}
},
AdvancedResult: {
_config: {
mobile:false,
suggestions_title: 'Outras sugestões de veículos'
},
init: function(container_id, url_advanced){
//varivel que define se oo pluguin renderiza apenas uma busca avancada ou uma busca avancada custom
var web_site_custom = 'default';
CarClick.AdvancedResult._config.container_id = container_id;
if(arguments[2]){
CarClick.AdvancedResult._config.suggestions_title = arguments[2];
}
if(arguments[3]){
CarClick.AdvancedResult._config.page_title = arguments[3];
} else {
CarClick.AdvancedResult._config.page_title = 'Navegue por nosso estoque';
}
//Verificamos se foi passado um quarto parametro, que siguinifica que é uma busca advanced custom
if (arguments[4]) {
web_site_custom = arguments[4];
}
var options = arguments[5] || {};
CarClick.AdvancedResult.preloader();
CarClick.AdvancedResult._config.show_state = options.show_state || 0;
CarClick.AdvancedResult._config.show_city = options.show_city || 0;
CarClick.getRequest('https://www.carclick.com.br/plugin_v2/66e870035ffc94882e8b45bb/config', function(data){
var config = jQuery.parseJSON(data);
CarClick.AdvancedResult._config = CarClick.mergeObjects(config,CarClick.AdvancedResult._config);
CarClick.AdvancedResult._config.show_new = parseInt(0);
CarClick.AdvancedResult._config.show_all = parseInt(0);
CarClick.AdvancedResult._config.container_id = container_id;
CarClick.AdvancedResult._config.dealer = 'all';
CarClick.AdvancedResult._config.url_advanced = url_advanced;
CarClick.AdvancedResult._config.web_site_custom = web_site_custom ;
CarClick.AdvancedResult._config.filter_dealers = parseInt(0);
CarClick.AdvancedResult._config.show_dealers = parseInt(0);
CarClick.AdvancedResult._config.show_featured = parseInt(0);
CarClick.AdvancedResult._config.show_premium = parseInt(0);
CarClick.AdvancedResult._config.show_cms_dealers = parseInt(0);
CarClick.AdvancedResult._config.assetsUrl = '//www.carclick.com.br/assets/';
CarClick.AdvancedResult._config.cdnUrl = 'https://assets.disaltecnologia.com.br';
CarClick.AdvancedResult._config.options = options;
CarClick.AdvancedResult.renderCss();
CarClick.AdvancedResult.injectJs();
var queryString = CarClick.AdvancedResult.getQueryString();
var action;
if (typeof CarClick.AdvancedResult._config.options.friendlyUrl != 'undefined' && CarClick.AdvancedResult._config.options.friendlyUrl) {
var pathname = document.location.pathname;
var pathnameObj = CarClick.getPathnameObj(pathname, CarClick.AdvancedResult._config.url_advanced);
if(pathnameObj.model) {
//queryString = 'q=' + pathnameObj.model + '&' + queryString;
}
action = pathnameObj.action;
} else {
if(CarClick.AdvancedResult.getQueryStringParam('view')) {
action = 'detalhes';
} else {
action = 'busca';
}
}
if(action == 'busca'){
CarClick.AdvancedResult.renderResultPage(queryString);
} else if(action == 'detalhes') {
CarClick.AdvancedResult.renderCarPage(queryString);
} else {
console.log('invalid action: ' + action);
}
});
},
// TODO all the stuff is duplicated, so we need to remove all the code replication
injectJs: function() {
var injector = function(path, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = CarClick.AdvancedResult._config.cdnUrl + '/assets' + path;
script.onload = callback || function(){};
document.body.appendChild( script );
}
injector('/cidades-estados.js/cidades-estados.js');
if(!jQuery().mask) {
injector('/jquery-mask-plugin/dist/jquery.mask.min.js', function() {
injector('/carclick/dist/default.min.js');
});
} else {
injector('/carclick/dist/default.min.js');
}
},
preloader: function(){
var _container = jQuery(CarClick.AdvancedResult._config.container_id);
if(_container.css('position') == 'static'){
_container.css('position','relative');
}
_container.css({width: 'auto', minHeight: '300px'});
var html_preloader = '
Carregando ';
_container.append(html_preloader);
},
renderCss: function(){
jQuery('head').append('');
jQuery('head').append('');
},
getQueryStringParam: function(name){
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
},
getQueryString: function(){
var query_string = location.search;
return query_string.replace('?','');
},
renderResultPage: function(queryString){
var url = 'https://www.carclick.com.br/plugin_v2/66e870035ffc94882e8b45bb/advanced/result?' +
queryString +
'&show_new=' + CarClick.AdvancedResult._config.show_new +
'&show_all=' + CarClick.AdvancedResult._config.show_all +
'&show_featured=' + CarClick.AdvancedResult._config.show_featured +
'&url_advanced=' + CarClick.AdvancedResult._config.url_advanced +
'&show_premium=' + CarClick.AdvancedResult._config.show_premium +
'&web_site_custom=' + CarClick.AdvancedResult._config.web_site_custom +
'&show_dealers=' + CarClick.AdvancedResult._config.show_dealers +
'&page_title=' + CarClick.AdvancedResult._config.page_title +
'&show_cms_dealers=' + CarClick.AdvancedResult._config.show_cms_dealers +
'&filter_dealers=' + CarClick.AdvancedResult._config.filter_dealers;
if(queryString.indexOf('dealer=') == -1) {
url += '&dealer=' + CarClick.AdvancedResult._config.dealer;
}
CarClick.getRequest(url, function(html){
jQuery(CarClick.AdvancedResult._config.container_id).html(html);
CarClick.AdvancedResult.renderSuggestions();
CarClick.AdvancedResult.onResultPageComplete();
});
},
toggleAccessoriesBox: function(){
jQuery('#cc-plugin-advanced-box-accessories-selection').toggle();
},
setFilter: function(param, value){
if(CarClick.AdvancedResult.getQueryStringParam(param)){
document.location.href = document.location.href.replace(param + '=' + CarClick.AdvancedResult.getQueryStringParam(param), param + '=' + value);
} else {
if(document.location.href.indexOf('?') != -1){
document.location.href = document.location.href + '&' + param + '=' + value;
} else {
document.location.href = document.location.href + '?' + param + '=' + value;
}
}
},
viewCar: function(id){
//console.log(CarClick.AdvancedResult._config.url_advanced + '?view=' + id + '&' + CarClick.AdvancedResult.getQueryString());
document.location.href = CarClick.AdvancedResult._config.url_advanced + '?view=' + id + '&' + CarClick.AdvancedResult.getQueryString();
},
setPage: function(page_number){
if(CarClick.AdvancedResult.getQueryStringParam('p')){
document.location.href = document.location.href.replace('p=' + CarClick.AdvancedResult.getQueryStringParam('p'), 'p=' + page_number);
} else {
if(document.location.href.indexOf('?') != -1){
document.location.href = document.location.href + '&p=' + page_number;
} else {
document.location.href = document.location.href + '?p=' + page_number;
}
}
},
renderCarPage: function(){
CarClick.getRequest('https://www.carclick.com.br/plugin_v2/66e870035ffc94882e8b45bb/advanced/car/' +
CarClick.AdvancedResult.getQueryStringParam('view') + '?' +
CarClick.AdvancedResult.getQueryString() +
'&url_advanced=' + CarClick.AdvancedResult._config.url_advanced +
'&show_dealers=' + CarClick.AdvancedResult._config.show_dealers +
'&show_cms_dealers=' + CarClick.AdvancedResult._config.show_cms_dealers +
'&mobile=' + CarClick.AdvancedResult._config.mobile +
'&show_state=' + CarClick.AdvancedResult._config.show_state +
'&show_city=' + CarClick.AdvancedResult._config.show_city, function(html){
jQuery(CarClick.AdvancedResult._config.container_id).html(html);
CarClick.AdvancedResult.initCarMediaGallery();
jQuery(document).trigger('carclick.loaded'); //Listen on
});
},
backToMain: function(){
document.location.href = CarClick.AdvancedResult._config.url_advanced + '?' + CarClick.AdvancedResult.getQueryString().replace('view=' + CarClick.AdvancedResult.getQueryStringParam('view') + '&', '');
},
initCarMediaGallery: function(){
if(jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails').length){
var items_total = jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails .container-carousel ol li').length;
var item_width = (jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails .container-carousel ol li:first-child').width() + 10);
var slider = jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails .container-carousel ol');
var viewport_width = jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails .container-carousel').width();
// Seta o tamanho do container do slider
slider.css({width: items_total * item_width});
var slider_width = slider.width();
if(viewport_width > slider_width){
jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails .control-nav').fadeTo(0,0.3);
}
// Controles de navegação
jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails .control-nav-right').click(function(){
var step = parseInt(slider.css('left')) - item_width;
if(viewport_width < slider_width){
if((viewport_width - slider_width) <= step){
slider.animate({left: step}, 250);
} else {
step = (viewport_width - slider_width + 10);
slider.animate({left: step}, 50);
}
}
});
jQuery('#cc-plugin-loader .view-car .col-set .media .thumbnails .control-nav-left').click(function(){
var step = parseInt(slider.css('left')) + item_width;
if(viewport_width < slider_width){
if(parseInt(slider.css('left')) < -(step)){
slider.animate({left: step}, 250);
} else {
slider.animate({left: 0}, 50);
}
}
});
}
},
showImage: function(image_url){
jQuery('#cc-plugin-loader .view-car .col-set .media img.zoom-image').fadeTo(0,0.5);
jQuery('#cc-plugin-loader #player-container').hide();
var image = new Image();
image.src = image_url;
image.onload = function(){
jQuery('#cc-plugin-loader .view-car .col-set .media img.zoom-image').fadeTo(0,1);
};
jQuery('#cc-plugin-loader .view-car .col-set .media img.zoom-image').attr('src',image_url);
},
sendContact: function(target){
var form = target;
var errors = 0;
jQuery(form).find('input.required, select.required, textarea.required').each(function(){
if(jQuery(this).val() === '' || !jQuery(this).val()){
alert(jQuery(this).attr('data-required-message'));
jQuery(this).focus();
errors++;
return false;
}
});
if(errors) return false;
var button_html = jQuery(form).find('button').html();
jQuery(form).find('button').attr('disabled','disabled').html('Enviando...');
jQuery.post("https://www.carclick.com.br/plugin_v2/66e870035ffc94882e8b45bb/contact", jQuery(form).serialize(), function(json){
if(json.success){
$('body').append('');
jQuery(form).trigger("reset");
jQuery(document).trigger('carclick.form-success');
alert('Seu contato foi enviado com sucesso!');
} else {
alert('Ocorreu um erro ao tentar enviar o seu contato. Por favor, tente novamente mais tarde.');
}
jQuery(form).find('button').html(button_html).removeAttr('disabled');
});
},
setSuggestionsTitle: function(title){
CarClick.AdvancedResult._config.suggestions_title = title;
},
renderSuggestions: function(){
var SUGGESTION_MIN_PRICE = SUGGESTION_MIN_PRICE | null;
var SUGGESTION_MAX_PRICE = SUGGESTION_MAX_PRICE | null;
if(SUGGESTION_MIN_PRICE && SUGGESTION_MAX_PRICE){
var price_from = SUGGESTION_MIN_PRICE;
var price_to = SUGGESTION_MAX_PRICE;
var exclude = EXCLUDE.join(',');
var url = 'https://www.carclick.com.br/plugin_v2/66e870035ffc94882e8b45bb/advanced/suggestions?price_from=' + price_from + '&price_to=' + price_to + '&exclude=' + exclude;
CarClick.getRequest(url, function(html){
jQuery('#suggestions-container').html(html);
jQuery('#suggestions-container h4.title').html(CarClick.AdvancedResult._config.suggestions_title);
CarClick.AdvancedResult.initSuggestionsMediaGallery();
});
}
},
initSuggestionsMediaGallery: function(){
if(jQuery('#suggestions-container .product-list .carousel').length){
var items_total = jQuery('#suggestions-container .product-list .carousel li').length;
var slider = jQuery('#suggestions-container .product-list .carousel');
var viewport_width = jQuery('#suggestions-container .product-list .carousel').width();
jQuery('#suggestions-container .product-list .carousel li').css('width',parseInt(viewport_width/3));
var item_width = jQuery('#suggestions-container .product-list .carousel li:first-child').width();
// Seta o tamanho do container do slider
slider.css({width: items_total * item_width});
var slider_width = slider.width();
if(viewport_width > slider_width){
jQuery('#suggestions-container .product-list .control-nav').fadeTo(0,0.3);
}
// Controles de navegação
jQuery('#suggestions-container .product-list .control-nav-right').click(function(){
var step = parseInt(slider.css('left')) - viewport_width;
if(viewport_width < slider_width){
if((viewport_width - slider_width) <= step){
slider.animate({left: step}, 250);
} else {
step = (viewport_width - slider_width);
slider.animate({left: step}, 50);
}
}
});
jQuery('#suggestions-container .product-list .control-nav-left').click(function(){
var step = parseInt(slider.css('left')) + viewport_width;
if(viewport_width < slider_width){
if(parseInt(slider.css('left')) < -(step)){
slider.animate({left: step}, 250);
} else {
slider.animate({left: 0}, 50);
}
}
});
}
},
onResultPageComplete: function(){}
},
Plugin :{
sendContact: function(obj) {
CarClick.AdvancedResult.sendContact(obj);
},
showImage: function(image) {
CarClick.AdvancedResult.showImage(image);
},
submitSearch: function(target){
if(CarClick.AdvancedResult._config.options.friendlyUrl) {
var values = jQuery(target).serializeArray();
var queryString = jQuery.param(values);
var path = CarClick.getPathnameObj(window.location.pathname, CarClick.AdvancedResult._config.options.pageName);
window.location.replace(path.page + '/' + values[0].value + '/busca?' + queryString);
return false;
} else {
return true;
}
},
showVideo: function(video_id) {
jQuery('#cc-plugin-loader .view-car .col-set .media img.zoom-image').hide();
jQuery('#cc-plugin-loader #player-container').show();
var player;
player = new YT.Player('player-container', {
height: '450',
width: '100%',
videoId: video_id,
events: {
onReady: function(){
player.stopVideo();
},
onStateChange: function(event){
console.log('change');
}
}
});
},
show360: function(url) {
jQuery('#cc-plugin-loader .view-car .col-set .media img.zoom-image').hide();
var container = jQuery('#cc-plugin-loader #player-container');
var html_preloader = '
Carregando ';
container.html(html_preloader);
container.show();
var iFrame = $('