jQuery(document).ready(function($){
'use strict';
const $widget=$('#soak-contact-widget');
const $trigger=$('#soak-widget-trigger');
const $expanded=$('#soak-widget-expanded');
const $closeBtn=$('#soak-widget-close');
const $form=$('#soak-contact-form');
const $successMessage=$('#soak-success-message');
let currentLang='da';
initializePageContent();
function initializePageContent(){
const currentUrl=window.location.href.toLowerCase();
const pathname=window.location.pathname.toLowerCase();
let pageType='default';
if(currentUrl.includes('dynamics')||pathname.includes('dynamics')){
pageType='dynamics';
}else if(currentUrl.includes('power-platform')||pathname.includes('power-platform')||currentUrl.includes('powerplatform')){
pageType='powerplatform';
}else if(currentUrl.includes('fabric')||currentUrl.includes('ai')||pathname.includes('fabric')){
pageType='fabric';
}else if(currentUrl.includes('extension')||pathname.includes('extension')||currentUrl.includes('business-central')){
pageType='extension';
}
updateDescriptionForPage(pageType);
}
function updateDescriptionForPage(pageType){
const $descriptionText=$('#soak-description-text');
if(pageType!=='default'){
const daText=$descriptionText.data(pageType + '-da');
const enText=$descriptionText.data(pageType + '-en');
if(daText&&enText){
$descriptionText.data('da', daText);
$descriptionText.data('en', enText);
const currentText=$descriptionText.data(currentLang);
if(currentText){
$descriptionText.text(currentText);
}}
}}
$('.soak-lang-flag').on('click', function(e){
e.preventDefault();
const lang=$(this).data('lang');
if(lang!==currentLang){
switchLanguage(lang);
}});
function switchLanguage(lang){
currentLang=lang;
$('.soak-lang-flag').removeClass('active');
$(`.soak-lang-flag[data-lang="${lang}"]`).addClass('active');
$('[data-da][data-en]').each(function(){
const $el=$(this);
const text=$el.data(lang);
if(text){
$el.text(text);
}});
updatePlaceholders(lang);
}
function updatePlaceholders(lang){
if(lang==='da'){
$('#soak-firstname').attr('placeholder', 'Fornavn');
$('#soak-lastname').attr('placeholder', 'Efternavn');
$('#soak-phone').attr('placeholder', 'Telefonnummer (inkl. landekode)');
$('#soak-email').attr('placeholder', 'Email');
$('#soak-message').attr('placeholder', 'Andet vedrørende din henvendelse');
$('.soak-widget-text').text('Bliv ringet op');
}else{
$('#soak-firstname').attr('placeholder', 'First name');
$('#soak-lastname').attr('placeholder', 'Last name');
$('#soak-phone').attr('placeholder', 'Phone number (incl. country code)');
$('#soak-email').attr('placeholder', 'Email');
$('#soak-message').attr('placeholder', 'Other regarding your inquiry');
$('.soak-widget-text').text('Get a call back');
}}
$trigger.on('click', function(e){
e.preventDefault();
openWidget();
});
$closeBtn.on('click', function(e){
e.preventDefault();
closeWidget();
});
$(document).on('click', function(e){
if(!$widget.is(e.target)&&$widget.has(e.target).length===0){
if($expanded.hasClass('show')){
closeWidget();
}}
});
$form.on('submit', function(e){
e.preventDefault();
submitForm();
});
$(document).on('keydown', function(e){
if(e.keyCode===27&&$expanded.hasClass('show')){
closeWidget();
}});
function openWidget(){
$trigger.fadeOut(200, function(){
$expanded.addClass('show').show();
setTimeout(function(){
$('#soak-firstname').focus();
}, 100);
});
}
function closeWidget(){
$expanded.css('animation', 'slideOutDown 0.3s ease-in');
setTimeout(function(){
$expanded.removeClass('show').hide().css('animation', '');
$trigger.fadeIn(200);
if($successMessage.is(':visible')){
resetForm();
}}, 300);
}
function submitForm(){
const $submitBtn=$('.soak-submit-btn');
const formData={
action: 'soak_contact_submit',
nonce: soak_ajax.nonce,
firstname: $('#soak-firstname').val().trim(),
lastname: $('#soak-lastname').val().trim(),
phone: $('#soak-phone').val().trim(),
email: $('#soak-email').val().trim(),
message: $('#soak-message').val().trim(),
current_page: window.location.href
};
if(!formData.firstname||!formData.lastname||!formData.phone||!formData.email){
const errorMsg=currentLang==='da' ? 'Udfyld venligst alle felter':'Please fill in all fields';
showError(errorMsg);
return;
}
const emailRegex=/^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if(!emailRegex.test(formData.email)){
const errorMsg=currentLang==='da' ? 'Indtast venligst en gyldig email adresse':'Please enter a valid email address';
showError(errorMsg);
return;
}
$submitBtn.addClass('loading').prop('disabled', true);
$.ajax({
url: soak_ajax.ajax_url,
type: 'POST',
data: formData,
success: function(response){
if(response.success){
showSuccess();
}else{
const errorMsg=currentLang==='da' ?
'Der opstod en fejl. Prøv igen eller ring til os på +45 7174 1100' :
'An error occurred. Please try again or call us at +45 7174 1100';
showError(errorMsg);
}},
error: function(){
const errorMsg=currentLang==='da' ?
'Der opstod en fejl. Prøv igen eller ring til os på +45 7174 1100' :
'An error occurred. Please try again or call us at +45 7174 1100';
showError(errorMsg);
},
complete: function(){
$submitBtn.removeClass('loading').prop('disabled', false);
}});
}
function showSuccess(){
$form.fadeOut(300, function(){
$successMessage.fadeIn(300);
setTimeout(function(){
if($expanded.hasClass('show')){
closeWidget();
}}, 3000);
});
}
function showError(message){
$('.soak-error-message').remove();
const $errorMessage=$('<div class="soak-error-message" style="background-color: #f8d7da; color: #721c24; padding: 10px; border-radius: 5px; margin-bottom: 15px; font-size: 14px; text-align: center;">' + message + '</div>');
$form.prepend($errorMessage);
setTimeout(function(){
$errorMessage.fadeOut(300, function(){
$(this).remove();
});
}, 5000);
}
function resetForm(){
$form[0].reset();
$form.show();
$successMessage.hide();
$('.soak-error-message').remove();
}
$('.soak-team-member img').on('mouseenter', function(){
$(this).css({
'transform': 'scale(1.15)',
'z-index': '10',
'border-color': '#95BDC7'
});
}).on('mouseleave', function(){
$(this).css({
'transform': 'scale(1)',
'z-index': '1',
'border-color': 'white'
});
});
$('.soak-input-group input').on('focus', function(){
$(this).closest('.soak-input-group').addClass('focused');
}).on('blur', function(){
$(this).closest('.soak-input-group').removeClass('focused');
});
setTimeout(function(){
$trigger.css({
transform: 'scale(0)',
opacity: '0'
}).animate({
opacity: 1
}, 500).css({
transform: 'scale(1)',
transition: 'transform 0.3s ease'
});
}, 1000);
});