
( function () {
	'use strict';

	document.addEventListener( 'DOMContentLoaded', function () {
		initHamburgerMenu();
		initMobileSubMenus();
		initThemeToggle();
		initStickyHeader();
		initRevealAnimations();
		initCustomCursor();
		initSubMenuAccessibility();

		if ( document.getElementById( 'psSlider' ) ) {
			initPortfolioSlider();
		}

	} );

	function initHamburgerMenu() {

		var toggle  = document.getElementById( 'menuToggle' );
		var panel   = document.getElementById( 'mobile-nav-panel' );
		var overlay = document.getElementById( 'mobileNavOverlay' );
		var close   = document.getElementById( 'mobileNavClose' );

		if ( ! toggle || ! panel ) return;

		function openMenu() {
			toggle.classList.add( 'is-active' );
			panel.classList.add( 'is-active' );
			panel.setAttribute( 'aria-hidden', 'false' );
			if ( overlay ) {
				overlay.classList.add( 'is-active' );
				overlay.removeAttribute( 'aria-hidden' );
			}
			toggle.setAttribute( 'aria-expanded', 'true' );
			document.body.classList.add( 'menu-open' );

			var firstLink = panel.querySelector( 'a, button' );
			if ( firstLink ) setTimeout( function () { firstLink.focus(); }, 300 );
		}

		function closeMenu() {
			toggle.classList.remove( 'is-active' );
			panel.classList.remove( 'is-active' );
			panel.setAttribute( 'aria-hidden', 'true' );
			if ( overlay ) {
				overlay.classList.remove( 'is-active' );
				overlay.setAttribute( 'aria-hidden', 'true' );
			}
			toggle.setAttribute( 'aria-expanded', 'false' );
			document.body.classList.remove( 'menu-open' );
			toggle.focus();
		}

		toggle.addEventListener( 'click', function () {
			toggle.classList.contains( 'is-active' ) ? closeMenu() : openMenu();
		} );

		if ( overlay ) overlay.addEventListener( 'click', closeMenu );
		if ( close )   close.addEventListener( 'click', closeMenu );

		document.addEventListener( 'keydown', function ( e ) {
			if ( e.key === 'Escape' && panel.classList.contains( 'is-active' ) ) closeMenu();
		} );
	}

	function initMobileSubMenus() {

		var panel = document.getElementById( 'mobile-nav-panel' );
		if ( ! panel ) return;

		var toggleBtns = panel.querySelectorAll( '.mobile-sub-toggle' );

		toggleBtns.forEach( function ( btn ) {
			var li      = btn.closest( 'li' );
			var subMenu = li ? li.querySelector( '.mobile-sub-menu' ) : null;
			if ( ! subMenu ) return;

			subMenu.style.height    = '0';
			subMenu.style.overflow  = 'hidden';
			subMenu.style.transition = 'height 0.3s ease';

			btn.addEventListener( 'click', function ( e ) {
				e.stopPropagation();
				var isOpen = btn.getAttribute( 'aria-expanded' ) === 'true';

				var parentUl = li.closest( 'ul' );
				if ( parentUl ) {
					parentUl.querySelectorAll( '.mobile-sub-toggle[aria-expanded="true"]' ).forEach( function ( otherBtn ) {
						if ( otherBtn === btn ) return;
						var otherLi  = otherBtn.closest( 'li' );
						var otherSub = otherLi ? otherLi.querySelector( '.mobile-sub-menu' ) : null;
						if ( otherSub ) {
							otherSub.style.height = '0';
						}
						otherBtn.setAttribute( 'aria-expanded', 'false' );
						var otherIcon = otherBtn.querySelector( 'i' );
						if ( otherIcon ) otherIcon.style.transform = 'rotate(0deg)';
					} );
				}

				var icon = btn.querySelector( 'i' );
				if ( isOpen ) {
					subMenu.style.height = '0';
					btn.setAttribute( 'aria-expanded', 'false' );
					if ( icon ) icon.style.transform = 'rotate(0deg)';
				} else {
					subMenu.style.height = subMenu.scrollHeight + 'px';
					btn.setAttribute( 'aria-expanded', 'true' );
					if ( icon ) icon.style.transform = 'rotate(180deg)';
				}
			} );
		} );
	}

	function initThemeToggle() {

		var btn  = document.getElementById( 'themeToggleBtn' );
		var icon = document.getElementById( 'themeIcon' );
		var html = document.documentElement;

		if ( ! btn ) return;

		function applyTheme( isDark ) {
			html.setAttribute( 'data-theme', isDark ? 'dark' : 'light' );
			localStorage.setItem( 'vistaProTheme', isDark ? 'dark' : 'light' );
			if ( icon ) icon.className = isDark ? 'fa-solid fa-sun' : 'fa-solid fa-moon';
			btn.setAttribute( 'aria-label', isDark ? 'تغییر به حالت روشن' : 'تغییر به حالت تاریک' );
		}

		var adminDefault = html.getAttribute( 'data-admin-theme' ) || 'auto';
		var saved        = localStorage.getItem( 'vistaProTheme' );
		var adminVersion = html.getAttribute( 'data-admin-theme-version' ) || '0';
		var storedVersion = localStorage.getItem( 'vistaProThemeVersion' ) || '0';

		if ( adminVersion !== storedVersion ) {
			localStorage.removeItem( 'vistaProTheme' );
			localStorage.setItem( 'vistaProThemeVersion', adminVersion );
			saved = null;
		}

		var sysDark = window.matchMedia( '(prefers-color-scheme: dark)' ).matches;

		if ( saved ) {
			applyTheme( saved === 'dark' );
		} else if ( adminDefault === 'dark' ) {
			applyTheme( true );
		} else if ( adminDefault === 'light' ) {
			applyTheme( false );
		} else {
			applyTheme( sysDark );
		}

		btn.addEventListener( 'click', function () {
			applyTheme( html.getAttribute( 'data-theme' ) !== 'dark' );
		} );

		window.matchMedia( '(prefers-color-scheme: dark)' ).addEventListener( 'change', function ( e ) {
			if ( ! localStorage.getItem( 'vistaProTheme' ) ) applyTheme( e.matches );
		} );
	}

	function initStickyHeader() {
		var header = document.getElementById( 'masthead' );
		if ( ! header ) return;

		function update() {
			header.classList.toggle( 'scrolled', window.scrollY > 20 );
		}
		window.addEventListener( 'scroll', update, { passive: true } );
		update();
	}

	function initRevealAnimations() {
		var els = document.querySelectorAll( '.rv' );
		if ( ! els.length ) return;

		if ( ! ( 'IntersectionObserver' in window ) ) {
			els.forEach( function ( el ) { el.classList.add( 'on' ); } );
			return;
		}

		var obs = new IntersectionObserver( function ( entries ) {
			entries.forEach( function ( e ) {
				if ( e.isIntersecting ) {
					e.target.classList.add( 'on' );
					obs.unobserve( e.target );
				}
			} );
		}, { threshold: 0.06, rootMargin: '0px 0px -28px 0px' } );

		els.forEach( function ( el ) { obs.observe( el ); } );
	}

	function initSubMenuAccessibility() {
		document.querySelectorAll( '.main-navigation li.menu-item-has-children' ).forEach( function ( item ) {
			var link = item.querySelector( ':scope > a' );
			var sub  = item.querySelector( '.sub-menu' );
			if ( ! link || ! sub ) return;

			link.setAttribute( 'aria-haspopup', 'true' );
			link.setAttribute( 'aria-expanded', 'false' );
			sub.setAttribute( 'role', 'menu' );

			item.addEventListener( 'mouseenter', function () { link.setAttribute( 'aria-expanded', 'true' ); } );
			item.addEventListener( 'mouseleave', function () { link.setAttribute( 'aria-expanded', 'false' ); } );

			sub.querySelectorAll( 'a' ).forEach( function ( sl ) {
				sl.addEventListener( 'keydown', function ( e ) {
					if ( e.key === 'Escape' ) { link.setAttribute( 'aria-expanded', 'false' ); link.focus(); }
				} );
			} );
		} );
	}

function initCustomCursor() {

    if ( window.matchMedia( '(hover: none), (pointer: coarse)' ).matches ) return;

    var ring = document.createElement( 'div' );
    ring.className = 'cc-ring';
    var dot = document.createElement( 'div' );
    dot.className = 'cc-dot';
    document.body.appendChild( ring );
    document.body.appendChild( dot );

    var rx = 0, ry = 0, dx = 0, dy = 0, visible = false;

    document.addEventListener( 'mousemove', function ( e ) {
        dx = e.clientX; dy = e.clientY;
        dot.style.left = dx + 'px';
        dot.style.top  = dy + 'px';
        if ( !visible ) {
            visible = true;
            ring.classList.add( 'cc-visible' );
            dot.classList.add( 'cc-visible' );
        }
    } );

    ( function loop() {
        rx += ( dx - rx ) * 0.15;
        ry += ( dy - ry ) * 0.15;
        ring.style.left = rx + 'px';
        ring.style.top  = ry + 'px';
        requestAnimationFrame( loop );
    } )();

    var hoverSel = 'a, button, [role="button"], .scard, .card, .project-card, .blog-card, .btn, input[type="submit"]';

    document.addEventListener( 'mouseover', function ( e ) {
        if ( e.target.closest && e.target.closest( hoverSel ) ) ring.classList.add( 'cc-hover' );
    } );
    document.addEventListener( 'mouseout', function ( e ) {
        if ( e.target.closest && e.target.closest( hoverSel ) ) ring.classList.remove( 'cc-hover' );
    } );

    document.addEventListener( 'mousedown', function () { ring.classList.add( 'cc-click' ); } );
    document.addEventListener( 'mouseup',   function () { ring.classList.remove( 'cc-click' ); } );

    document.addEventListener( 'mouseleave', function () {
        ring.classList.remove( 'cc-visible' );
        dot.classList.remove( 'cc-visible' );
        visible = false;
    } );
    document.addEventListener( 'mouseenter', function () {
        ring.classList.add( 'cc-visible' );
        dot.classList.add( 'cc-visible' );
        visible = true;
    } );
}

	function initPortfolioSlider() {
		var wrap   = document.getElementById( 'psSlider' );
		if ( ! wrap ) return;

		var track   = document.getElementById( 'psSliderTrack' );
		if ( ! track ) return;

		var slides  = track.querySelectorAll( '.ps-slide' );
		if ( ! slides.length ) return;

		var thumbs   = document.querySelectorAll( '.ps-thumb' );
		var prev     = wrap.querySelector( '.ps-slide-prev' );
		var next     = wrap.querySelector( '.ps-slide-next' );
		var counter  = document.getElementById( 'psSlideCurrentNum' );

		var current    = 0;
		var total      = slides.length;
		var isRtl      = ( typeof vistaPro !== 'undefined' && vistaPro.isRtl === 'true' )
			|| document.documentElement.dir === 'rtl';
		var startX     = 0;
		var isDragging = false;

		function goTo( idx ) {
			current = ( ( idx % total ) + total ) % total;
			var offset = isRtl ? current * 100 : -current * 100;
			track.style.transform = 'translateX(' + offset + '%)';


			if ( counter ) counter.textContent = current + 1;


			thumbs.forEach( function ( t, i ) {
				t.classList.toggle( 'active', i === current );
				t.setAttribute( 'aria-current', i === current ? 'true' : 'false' );
			} );

			if ( thumbs[ current ] ) {
				thumbs[ current ].scrollIntoView( { behavior: 'smooth', block: 'nearest', inline: 'center' } );
			}
		}

		if ( prev ) prev.addEventListener( 'click', function () {
			goTo( isRtl ? current + 1 : current - 1 );
		} );
		if ( next ) next.addEventListener( 'click', function () {
			goTo( isRtl ? current - 1 : current + 1 );
		} );

		thumbs.forEach( function ( t, i ) {
			t.addEventListener( 'click', function () { goTo( i ); } );
		} );

		track.addEventListener( 'touchstart', function ( e ) {
			startX = e.touches[ 0 ].clientX; isDragging = true;
		}, { passive: true } );

		track.addEventListener( 'touchend', function ( e ) {
			if ( ! isDragging ) return;
			var diff = startX - e.changedTouches[ 0 ].clientX;
			if ( Math.abs( diff ) > 40 ) {
				goTo( diff > 0 ? ( isRtl ? current - 1 : current + 1 )
				               : ( isRtl ? current + 1 : current - 1 ) );
			}
			isDragging = false;
		}, { passive: true } );

		wrap.setAttribute( 'tabindex', '0' );
		wrap.addEventListener( 'keydown', function ( e ) {
			if ( e.key === 'ArrowRight' ) goTo( isRtl ? current + 1 : current - 1 );
			if ( e.key === 'ArrowLeft'  ) goTo( isRtl ? current - 1 : current + 1 );
		} );

		goTo( 0 );
	}



	(function(){
		document.querySelectorAll('.share-copy').forEach(function(btn){
			btn.addEventListener('click', function(e){
				e.preventDefault();
				var url = btn.getAttribute('data-copy-url') || window.location.href;
				if ( navigator.clipboard ) {
					navigator.clipboard.writeText(url).then(function(){
						btn.classList.add('copied');
						setTimeout(function(){ btn.classList.remove('copied'); }, 2000);
					});
				} else {
					var ta = document.createElement('textarea');
					ta.value = url;
					document.body.appendChild(ta);
					ta.select();
					document.execCommand('copy');
					document.body.removeChild(ta);
					btn.classList.add('copied');
					setTimeout(function(){ btn.classList.remove('copied'); }, 2000);
				}
			});
		});
	})();
	
( function () {
	var wrap = document.getElementById( 'vistaLangSwitcher' );
	var btn  = document.getElementById( 'vistaLangBtn' );
	var drop = document.getElementById( 'vistaLangDropdown' );
	if ( ! wrap || ! btn || ! drop ) return;

	function open()  { wrap.classList.add( 'is-open' ); btn.setAttribute( 'aria-expanded', 'true' ); }
	function close() { wrap.classList.remove( 'is-open' ); btn.setAttribute( 'aria-expanded', 'false' ); }
	function toggle() { wrap.classList.contains( 'is-open' ) ? close() : open(); }

	btn.addEventListener( 'click', function ( e ) { e.stopPropagation(); toggle(); } );
	document.addEventListener( 'click', function ( e ) { if ( ! wrap.contains( e.target ) ) close(); } );
	document.addEventListener( 'keydown', function ( e ) { if ( e.key === 'Escape' ) close(); } );
} )();
	
	
} )();