var headerItem = $("header");
var pageItem = $("header + div");
var footerItem = $("footer");
function numberControl(e){
	var pressedKey = (e.which) ? e.which : e.keyCode;
	if ($.inArray(e.keyCode, [8, 9, 27, 13]) !== -1 || (e.keyCode == 65 && ( e.ctrlKey === true || e.metaKey === true ) ) || (e.keyCode >= 35 && e.keyCode <= 40)) { return; }
	if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { e.preventDefault(); }
	if(pressedKey == 190 || pressedKey == 110 || pressedKey == 46){
	      e.preventDefault();
	   }
}
function setAlert(TITLE, CONTENT, TYPE, BUTTON_TITLE, BUTTON_CLASS, FORM_DOM, FORM_DOM_RESET, ACTION, SECOND_BUTTON, SECOND_BUTTON_TITLE, SECOND_BUTTON_CLASS, SECOND_BUTTON_HREF) {
    if ( TYPE == 'red' ) var confirmModalIcon = 'far fa-exclamation';
    if ( TYPE == 'green' ) var confirmModalIcon = 'far fa-check';
    var buttonsArray = {};
    buttonsArray["firstButton"] = {
        text: BUTTON_TITLE,
        btnClass: BUTTON_CLASS,
        action: function () {
            if ( ACTION ){
                if ( ACTION == 'reload' ){
                    top.document.location.reload();
                } else {
                    top.document.location.href = ACTION;
                }
            }
        }
    }
    if ( SECOND_BUTTON == 'show-second-button' ){
        buttonsArray["secondButton"] = {
            text: SECOND_BUTTON_TITLE,
            btnClass: SECOND_BUTTON_CLASS,
            action: function () {
                if ( SECOND_BUTTON_HREF ) top.document.location.href = SECOND_BUTTON_HREF;
            }
        }
    }
    $.confirm({
        icon: confirmModalIcon,
        title: TITLE,
        content: CONTENT,
        type: TYPE,
        backgroundDismiss: false,
        typeAnimated: false,
        draggable: false,
        buttons: buttonsArray,
        onOpen: function() {
            $('body').css('overflow-y', 'hidden');
        },
        onClose: function() {
            $('body').removeAttr('style');
            if ( FORM_DOM_RESET == 'reset' ) $(FORM_DOM)[0].reset();
            if ( FORM_DOM ) $(FORM_DOM).find('button[type="submit"]').attr('disabled', false);
        },
        boxWidth: '600px',
        useBootstrap: false,
        animateFromElement: false
    });
}
function getWindowWidth() {
    return $(window).width();
}
function sameHeightController(){
	if ($('[data-sameheight="true"]').length){
		var sameHeightWrapper = document.querySelectorAll('[data-sameheight="true"]');
		for (var i = 0, len = sameHeightWrapper.length; i < len; i++) {
			if (getWindowWidth() < 768){
		    	$(sameHeightWrapper[i]).find('[data-sameheight-child="true"]').removeAttr('style');
			} else {
				$(sameHeightWrapper[i]).find('[data-sameheight-child="true"]').equalHeights($(sameHeightWrapper[i]));
			}
		}
	}
}
let openedMenu = 0;
function openLeftMenu() {
    $('body').toggleClass('opened-left');
    $('.background-shadow').fadeToggle();
    $('.left-menu-panel-wrapper').toggleClass('opened');
    openedMenu = openedMenu === 1 ? 0 : 1;
}
$.fn.equalHeights = function(wrapperItem){
	wrapperItem.find('[data-sameheight-child="true"]').removeAttr('style');
	var max_height = 0;
	$(this).each(function(){
		max_height = Math.max($(this).height(), max_height);
	});
	$(this).each(function(){
		$(this).height(max_height);
	});
}
function createPageURL(dataType, dataValue){
	var urlParams = new URLSearchParams(window.location.search);
	if (dataType == 'sorting' && dataValue == 'default'){
		urlParams.delete(dataType);
	} else {
		urlParams.set(dataType, dataValue);
	}
	var setParams = decodeURIComponent(urlParams.toString());
	window.location.search = setParams;
}
function changeAmount(DIFFERENCE) {
	var PRODUCT_AMOUNT = parseInt($('#PRODUCT_AMOUNT').val()) + DIFFERENCE;
	if (PRODUCT_AMOUNT <= 0) return;
	$('#PRODUCT_AMOUNT').val(PRODUCT_AMOUNT);
}
function togglePasswordVisibility(inputId) {
	var input = $('#' + inputId);
	var icons = input.next('.toggle-password');
	var showIcon = icons.find('.show-password');
	var hideIcon = icons.find('.hide-password');
	if (input.attr('type') === 'password') {
		input.attr('type', 'text');
		showIcon.addClass('hide');
		hideIcon.removeClass('hide');
	} else {
		input.attr('type', 'password');
		hideIcon.addClass('hide');
		showIcon.removeClass('hide');
	}
}
function search(){
	if ( !$("#SEARCH").val() ){
		return false;
	} else {
		top.document.location.href = "arama.html?q="+$("#SEARCH").val();
	}
}	
$(window).on('load', function() {
	$('.background-shadow').click(function(){
		$('body').removeClass('opened-left');
		$(this).fadeOut();
		$('.left-menu-panel-wrapper').removeClass('opened');
		openedMenu = 0;
	});
});
$(window).on('resize', function () {
	sameHeightController();
});
$(document).ready(function(){
	sameHeightController();
	/* GENERAL ACCORDION SCRIPT */
	$('[data-type="accordion"]').children('.accordion-item').find('.accordion-item-title-wrapper').click(function(){
		if ($(this).parent().hasClass("active")){
			$(this).parent().removeClass('active');
			$(this).parent().find('.accordion-item-hidden').stop(true,true).slideUp();
		} else {
			$('[data-type="accordion"]').children('.accordion-item').removeClass('active').find('.accordion-item-hidden').stop(true,true).slideUp();
			$(this).parent().addClass('active');
			$(this).parent().find('.accordion-item-hidden').stop(true,true).slideDown();
		}
	});
	/* GENERAL ACCORDION SCRIPT */
	/** S: TAB */
		const urlParams = new URLSearchParams(window.location.search);
		const tabParam = urlParams.get('tab'); // Örnek: tab=login
		// Tüm tabları ve içerikleri sıfırla
		$('.tab-items-wrapper .tab-items .tab-item').removeClass('active');
		$('.tab-items-wrapper .tab-sub-items .tab-sub-item').hide();
		if (tabParam && $('.tab-items-wrapper .tab-items .tab-item[data-tab="' + tabParam + '"]').length > 0) {
			// Eğer URL'de geçerli bir tab varsa onu aktif yap
			$('.tab-items-wrapper .tab-items .tab-item[data-tab="' + tabParam + '"]').addClass('active');
			$('.tab-items-wrapper .tab-sub-items .tab-sub-item[data-tab="' + tabParam + '"]').show();
		} else {
			// Eğer parametre yoksa ilk tabı aç
			$('.tab-items-wrapper .tab-items .tab-item').first().addClass('active');
			const firstTab = $('.tab-item').first().data('tab');
			$('.tab-items-wrapper .tab-sub-items .tab-sub-item[data-tab="' + firstTab + '"]').show();
		}
		// Tab tıklama işlemi
		$('.tab-items-wrapper .tab-items .tab-item').click(function () {
			if (!$(this).hasClass('active')) {
				const clickedTab = $(this).data('tab');
				$('.tab-items-wrapper .tab-items .tab-item').removeClass('active');
				$(this).addClass('active');
				$('.tab-items-wrapper .tab-sub-items .tab-sub-item').hide();
				$('.tab-items-wrapper .tab-sub-items .tab-sub-item[data-tab="' + clickedTab + '"]').show();
			}
		});
	/** E: TAB */
	/** S : PRODUCT DETAIL */
	$('.product-detail .product-detail-share-button').on('click', function() {
		var title = $(this).data('product-title');
		var url = $(this).data('product-url')
		if (navigator.share) {
			navigator.share({
				title: title,
				text: title,
				url: url
			})
		} else {
			return false;
		}
	});
	/** E : PRODUCT DETAIL */
	/** S: HEADER */
	$('.header-user').on('click', function() {
		$(this).toggleClass('active');
		$('.header-user-tab-menu').toggle();
	});
	$('.mobile-menu-button').on('click', function() {
		$(this).toggleClass('active');
		$('.nav').toggleClass('active');
	});
	$('.nav-menu-mobile-top-close').on('click', function() {
		$('.nav, .mobile-menu-button').removeClass('active');
	});
	/** E: HEADER */	
	/*
    // Sağ tıklamayı engelle
    $(document).on("contextmenu", function (e) {
        e.preventDefault();
    });	
    // F12 ve diğer bazı geliştirici kısayollarını engelle
    $(document).on("keydown", function (e) {
        // F12 tuşu
        if (e.keyCode === 123) {
            e.preventDefault();
        }
        // Ctrl+Shift+I (Geliştirici araçları)
        if (e.ctrlKey && e.shiftKey && e.keyCode === 73) {
            e.preventDefault();
        }
        // Ctrl+Shift+J (Konsol)
        if (e.ctrlKey && e.shiftKey && e.keyCode === 74) {
            e.preventDefault();
        }
        // Ctrl+U (Sayfa kaynağı görüntüleme)
        if (e.ctrlKey && e.keyCode === 85) {
            e.preventDefault();
        }
    });
	*/
	$('.search-action').on('click', function(){
		$('.search-wrapper').addClass('active');
		$('body').addClass('lock');
	});
	$('.search-close').on('click', function(){
		$('.search-wrapper').removeClass('active');
		$('body').removeClass('lock');
	});
    $('input[name=DELIVERY_ADDRESS]').on('click', function(){
		$('.payment-address-values').not($(this).parent().find('.payment-address-values')).removeClass('visible');
		$(this).parent().find('.payment-address-values').toggleClass('visible');
    });	
});
$(document).on('click', function(e) {
    // Eğer tıklanan element header-user veya header-user-tab-menu değilse
    if (!$(e.target).closest('.header-user, .header-user-tab-menu').length) {
        $('.header-user').removeClass('active');
        $('.header-user-tab-menu').hide();
    }
});