document.addEventListener("DOMContentLoaded", function() { // Get accent color from CSS variable or fallback to default var accentColor = getComputedStyle(document.documentElement).getPropertyValue('--aisista-accent-color').trim(); if (!accentColor) accentColor = "linear-gradient(90deg, #b9f6ca 0%, #a5d6a7 100%)"; var accentTextColor = "#1b5e20"; // SVG icons var iconClose = ` `; var iconMaximize = ` `; // Create chat button var chatWidget = document.createElement('div'); chatWidget.id = "chatWidget"; chatWidget.style.position = "fixed"; chatWidget.style.bottom = "20px"; chatWidget.style.right = "20px"; chatWidget.style.padding = "15px 25px"; chatWidget.style.background = accentColor; chatWidget.style.color = accentTextColor; chatWidget.style.borderRadius = "30px"; chatWidget.style.zIndex = "1000"; chatWidget.style.cursor = "pointer"; chatWidget.style.display = "flex"; chatWidget.style.alignItems = "center"; chatWidget.style.justifyContent = "center"; chatWidget.style.boxShadow = "0 4px 24px rgba(0,0,0,0.15)"; chatWidget.innerHTML = ` شروع گفتگو با پشتیبانی`; chatWidget.onclick = showChat; document.body.appendChild(chatWidget); // Create chat iframe container var chatIframe = document.createElement('div'); chatIframe.id = "chatIframe"; chatIframe.style.display = "none"; chatIframe.style.position = "fixed"; chatIframe.style.zIndex = "1001"; chatIframe.style.background = "#fff"; chatIframe.style.boxShadow = "0 8px 32px rgba(0,0,0,0.2)"; chatIframe.style.borderRadius = "18px 18px 0 0"; chatIframe.style.overflow = "hidden"; chatIframe.style.resize = "both"; chatIframe.style.transition = "all 0.3s cubic-bezier(.4,0,.2,1)"; chatIframe.style.minWidth = "280px"; chatIframe.style.minHeight = "320px"; setIframeStyle(); chatIframe.innerHTML = `
تماس با پشتیبانی
`; document.body.appendChild(chatIframe); // Animation helpers function animateOpen() { setIframeStyle(); chatIframe.style.display = "block"; chatWidget.style.display = "none"; if (window.innerWidth <= 600) { setTimeout(function() { chatIframe.style.transform="translateX(0)" ; chatIframe.style.opacity="1" ; }, 10); } else { chatIframe.style.opacity="0" ; chatIframe.style.transform="scale(0.8)" ; setTimeout(function() { chatIframe.style.transition="all 0.3s cubic-bezier(.4,0,.2,1)" ; chatIframe.style.opacity="1" ; chatIframe.style.transform="scale(1)" ; }, 10); } } function animateClose() { if (window.innerWidth <=600) { chatIframe.style.transform="translateX(100%)" ; chatIframe.style.opacity="0" ; setTimeout(function() { chatIframe.style.display="none" ; chatWidget.style.display="flex" ; }, 300); } else { chatIframe.style.opacity="0" ; chatIframe.style.transform="scale(0.8)" ; setTimeout(function() { chatIframe.style.display="none" ; chatWidget.style.display="flex" ; chatIframe.style.transform="scale(1)" ; }, 300); } } // Show chat function function showChat() { animateOpen(); } // Minimize chat function function minimizeChat() { animateClose(); } // Maximize chat function function maximizeChat() { window.open("https://aisista.com/c/2Bi_aad1a6f19ec3c29", "_blank" ); } // Attach events document.getElementById('minimizeChatBtn').onclick=minimizeChat; document.getElementById('maximizeChatBtn').onclick=maximizeChat; // Responsive style update window.addEventListener('resize', setIframeStyle); function setIframeStyle() { // Always show chatIframe if it was open before resize var wasOpen = chatIframe.style.display === "block"; if (window.innerWidth <= 600) { // Mobile: full-screen drawer from right chatIframe.style.top = "0"; chatIframe.style.right = "0"; chatIframe.style.bottom = "0"; chatIframe.style.left = "auto"; chatIframe.style.width = "100vw"; chatIframe.style.height = (window.CSS && CSS.supports('height: 100dvh')) ? "100dvh" : "100vh"; chatIframe.style.maxWidth = "100vw"; chatIframe.style.maxHeight = (window.CSS && CSS.supports('height: 100dvh')) ? "100dvh" : "100vh"; chatIframe.style.borderRadius = "0"; chatIframe.style.transform = wasOpen ? "translateX(0)" : "translateX(100%)"; chatIframe.style.opacity = wasOpen ? "1" : "0"; chatIframe.style.transition = "transform 0.3s cubic-bezier(.4,0,.2,1),opacity 0.3s cubic-bezier(.4,0,.2,1)"; chatIframe.style.resize = "none"; // Remove border radius from header var header = chatIframe.querySelector("#chatHeader"); if (header) header.style.borderRadius = "0"; // Always update iframe height on mobile var inner = document.getElementById('chatIframeInner'); if (inner) { var headerHeight = chatIframe.querySelector("#chatHeader").offsetHeight || 44; if (window.CSS && CSS.supports('height: 100dvh')) { inner.style.height = "calc(100dvh - " + headerHeight + "px)"; } else { inner.style.height = "calc(100vh - " + headerHeight + "px)"; } } } else { // Desktop: bottom right popup chatIframe.style.top = "auto"; chatIframe.style.right = "18px"; chatIframe.style.bottom = "18px"; chatIframe.style.left = "auto"; chatIframe.style.width = "50vw"; chatIframe.style.height = "80vh"; chatIframe.style.maxWidth = "600px"; chatIframe.style.borderRadius = "18px 18px 18px 18px"; chatIframe.style.transform = "scale(1)"; chatIframe.style.opacity = "1"; chatIframe.style.transition = "all 0.3s cubic-bezier(.4,0,.2,1)"; chatIframe.style.resize = "both"; var header = chatIframe.querySelector("#chatHeader"); if (header) header.style.borderRadius = "18px 18px 0px 0px"; var inner = document.getElementById('chatIframeInner'); if (inner) { inner.style.height = "calc(100% - 44px)"; } } // Restore display if chat was open before resize if (wasOpen) { chatIframe.style.display = "block"; chatWidget.style.display = "none"; } } window.addEventListener('resize', setIframeStyle); if (window.visualViewport) { window.visualViewport.addEventListener('resize', setIframeStyle); window.visualViewport.addEventListener('scroll', setIframeStyle); } window.addEventListener('orientationchange', setIframeStyle); // Also update on orientation change window.addEventListener('orientationchange', setIframeStyle); // Hide chatWidget when chatIframe is open (especially on mobile) function hideWidgetIfOpen() { if (chatIframe.style.display==="block" ) { chatWidget.style.display="none" ; } } window.addEventListener('resize', hideWidgetIfOpen); });