// Define globally immediately function useProofNotification() { if (window.showNextOrder) { window.showNextOrder(); } else { console.error("showNextOrder function is not defined yet."); } } $(document).ready(function() { var currentIndex = 0; function timeSince(date) { var now = new Date(); var datePast = new Date(date); var seconds = Math.floor((now - datePast) / 1000); var interval = seconds / 86400; // Days if (interval >= 1) { return Math.floor(interval) + " day" + (Math.floor(interval) !== 1 ? "s" : "") + " ago"; } interval = seconds / 3600; // Hours if (interval >= 1) { return Math.floor(interval) + " hour" + (Math.floor(interval) !== 1 ? "s" : "") + " ago"; } interval = Math.max(Math.floor(seconds / 60), 1); // Minutes return interval + " minute" + (interval !== 1 ? "s" : "") + " ago"; } function toTitleCase(str) { return str.toLowerCase().replace(/\b\w/g, function(char) { return char.toUpperCase(); }); } var currentIndex = 0; // Initialize currentIndex outside the function function updateContent() { if (currentIndex >= orders.length) currentIndex = 0; // Reset index if over var order = orders[currentIndex++]; var productInfo = productData[order.lineItemData.itemNo]; var city = order.customerContactInfo.find(info => info.field === 'city')?.value; var state = order.customerContactInfo.find(info => info.field === 'shippingState')?.value; var country = order.customerContactInfo.find(info => info.field === 'shippingCountry')?.value.toUpperCase() || 'CA'; // Ensure state is always in uppercase when it is displayed state = state ? state.toUpperCase() : null; // Construct location string, handling city, state, and country appropriately var location; if (city && state) { location = toTitleCase(city) + ', ' + state; // State is always uppercase } else if (city) { location = toTitleCase(city) + ', ' + country; // City and country } else if (state) { location = state; // Only state in uppercase } else { location = country; // Only country code when both city and state are missing } var html = '
' + 'Product Image' + '
' + '
' + '' + toTitleCase(order.firstName) + ' from ' + location + '' + 'Just purchased ' + productInfo.title + '' + '' + timeSince(new Date(order.transactionTime)) + '' + '
'; $('#notification-widget').html(html).slideDown(300).delay(7000).slideUp(300, function() { setTimeout(showNextOrder, 20000); // Wait 20 seconds before starting the next slide down }); } window.showNextOrder = function() { updateContent(); }; window.useProofNotification = function() { showNextOrder(); }; $(document).on('click', '.useproof-close', function() { $('#notification-widget').stop(true).hide(); // Instantly hides the widget when closed }); });