// source --> https://maisonoffice.vn/wp-content/plugins/simple-download-monitor/js/sdm_wp_scripts.js 
// Simple Download Monitor frontend scripts

function sdm_is_ie() {

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))
    {
	return true;
    }

    return false;
}

jQuery(document).ready(function ($) {

    // Populate all nested titles and links 
    $('li.sdm_cat').each(function () {

	var $this = $(this);
	this_slug = $this.attr('id');
	this_id = $this.children('.sdm_cat_title').attr('id');

	// Run ajax
	$.post(
		sdm_ajax_script.ajaxurl,
		{
		    action: 'sdm_pop_cats',
		    cat_slug: this_slug,
		    parent_id: this_id
		},
		function (response) {

		    // Loop array returned from ajax function
		    $.each(response.final_array, function (key, value) {

			// Populate each matched post title and permalink
			$this.children('.sdm_placeholder').append('<a href="' + value['permalink'] + '"><span class="sdm_post_title" style="cursor:pointer;">' + value['title'] + '</span></a>');
		    });

		    $this.children('span').append('<span style="margin-left:5px;" class="sdm_arrow">&#8616</span>');
		}
	);
    });

    // Hide results on page load
    $('li.sdm_cat').children('.sdm_placeholder').hide();

    // Slide toggle for each list item
    $('body').on('click', '.sdm_cat_title', function (e) {

	// If there is any html.. then we have more elements
	if ($(this).next().html() != '') {

	    $(this).next().slideToggle(); // toggle div titles
	}
    });

    // Download buttons with terms or captcha has this class applied to it
    $('.sdm_download_with_condition').on('click', function (e) {
	e.preventDefault();
	$(this).closest('form').trigger('submit');
    });

    // Check if terms checkbox is enabled.
    if ($('.sdm-termscond-checkbox').length) {

	$.each($('.sdm-termscond-checkbox'), function () {
	    if (!$(this).is(':checked')) {
		var cur = $(this).children(':checkbox');
		var btn = $(cur).closest('form').find('a.sdm_download,a.sdm_download_with_condition');
		$(btn).addClass('sdm_disabled_button');
	    }
	});

	$.each($('.sdm-download-form'), function () {
	    var form = $(this);
	    form.on('submit', function () {
		if ($('.agree_termscond', form).is(':checked')) {
		    $('.sdm-termscond-checkbox', form).removeClass('sdm_general_error_msg');
		    return true;
		} else {
		    $('.sdm-termscond-checkbox', form).addClass('sdm_general_error_msg');
		}
		return false;
	    });
	});

	$.each($('.agree_termscond'), function () {
	    var element = $(this);
	    var form = element.closest('form');
	    element.on('click', function () {
		if (element.is(':checked')) {
		    $('.sdm_download_with_condition', form).removeClass('sdm_disabled_button');
		    $('.sdm-termscond-checkbox', form).removeClass('sdm_general_error_msg');
		} else {
		    $('.sdm_download_with_condition', form).addClass('sdm_disabled_button');
		    $('.sdm-termscond-checkbox', form).addClass('sdm_general_error_msg');
		}
	    });
	});
    }
});
// source --> https://maisonoffice.vn/wp-content/themes/maisonreal/assets/js/elementor/sticksy.min.js 
window.Sticksy=function(){"use strict";var s="static",i="fixed",n="stuck";function e(t,i){if(!t)throw new Error("You have to specify the target element");if("string"!=typeof t&&!(t instanceof Element))throw new Error("Expected a string or element, but got: "+Object.prototype.toString.call(t));var n=r.findElement(t);if(!n)throw new Error("Cannot find target element: "+t);var s=n.parentNode;if(!s)throw new Error("Cannot find container of target element: "+t);i=i||{},this.t={containerEl:s,targetEl:n,topSpacing:i.topSpacing||0,enabled:i.enabled||!0,listen:i.listen||!1},this.onStateChanged=null,this.nodeRef=n,this.i()}e.instances=[],e.enabledInstances=[],e.prototype.i=function(){var t=this;this.state=s,this.o=[],this.h=[];for(var i=this.t.targetEl;i;){var n=i.cloneNode(!0);n.style.visibility="hidden",n.style.pointerEvents="none",n.className+=" sticksy-dummy-node",n.removeAttribute("id"),this.t.targetEl.parentNode.insertBefore(n,this.t.targetEl),this.o.push(i),this.h.push(n),i=i.nextElementSibling}this.u=0,this.l={top:0,bottom:0},this.v=!1,this.t.containerEl.style.position="relative",this.m=-1===getComputedStyle(this.t.containerEl).display.indexOf("flex"),this.t.listen&&(this.p=new MutationObserver(function(){t.hardRefresh()}),this.g()),e.instances.push(this),this.t.enabled&&e.enabledInstances.push(this),this.hardRefresh()},e.prototype.g=function(){this.t.listen&&!this.v&&(this.p.observe(this.t.containerEl,{attributes:!0,characterData:!0,childList:!0,subtree:!0}),this.v=!0)},e.prototype.C=function(){this.t.listen&&this.v&&(this.p.disconnect(),this.v=!1)},e.prototype.M=function(t){return t<this.l.top?s:t>=this.l.bottom?n:i},e.prototype.S=function(){this.u=r.getComputedBox(this.o[this.o.length-1]).bottomWithMargin-r.getComputedBox(this.o[0]).topWithMargin},e.prototype.j=function(){var t=this.t.containerEl,i=this.o,t=r.getComputedBox(t),i=r.getComputedBox(i[0]);this.l={top:i.topWithMargin-this.t.topSpacing,bottom:t.bottom-t.paddingBottom-this.t.topSpacing-this.u}},e.prototype.B=function(t){t===s?(this.P(this.o),this.T(this.h)):(this.k(this.o),t===i?this.O(this.o):this.W(this.o),this.D(this.h))},e.prototype.refresh=function(){var t=this.M(window.pageYOffset,this.l);t!==this.state&&(this.state=t,this.C(),this.B(t),this.g(),"function"==typeof this.onStateChanged&&this.onStateChanged(t))},e.prototype.hardRefresh=function(){this.C();var t=this.state;this.state=s,this.B(this.state),this.k(this.o),this.S(),this.j(),this.state=this.M(window.pageYOffset,this.l),this.B(this.state),this.g(),"function"==typeof this.onStateChanged&&t!==this.state&&this.onStateChanged(this.state)},e.prototype.enable=function(){this.t.enabled=!0,e.enabledInstances.push(this),this.hardRefresh()},e.prototype.disable=function(){this.t.enabled=!1,this.state=s,this.B(this.state),e.enabledInstances.splice(e.enabledInstances.indexOf(this),1)},e.prototype.O=function(t){for(var i=0,n=this.t.topSpacing,s=0;s<t.length;s++){var o=t[s],h=r.getComputedBox(o),e=this.m?Math.max(0,i-h.marginTop):i;o.style.position="fixed",o.style.top=n+e+"px",o.style.bottom="",n+=h.height+h.marginTop+e,i=h.marginBottom}},e.prototype.W=function(t){for(var i=0,n=r.getComputedBox(this.t.containerEl).paddingBottom,s=t.length-1;0<=s;s--){var o=t[s],h=r.getComputedBox(o),e=this.m?Math.max(0,i-h.marginBottom):i;o.style.position="absolute",o.style.top="auto",o.style.bottom=n+e+"px",n+=h.height+h.marginBottom+e,i=h.marginTop}},e.prototype.P=function(t){t.forEach(function(t){t.style.position="",t.style.top="",t.style.bottom="",t.style.height="",t.style.width=""})},e.prototype.T=function(t){t.forEach(function(t){t.style.display="none"})},e.prototype.D=function(t){for(var i=0;i<t.length;i++)t[i].style.display=getComputedStyle(this.o[i]).display},e.prototype.k=function(){for(var t=0;t<this.o.length;t++){var i=this.o[t],n=getComputedStyle(i);i.style.width=n.width,i.style.height=n.height}},e.refreshAll=function(){for(var t=0;t<e.enabledInstances.length;t++)e.enabledInstances[t].refresh()},e.hardRefreshAll=function(){for(var t=0;t<e.enabledInstances.length;t++)e.enabledInstances[t].hardRefresh()},e.enableAll=function(){e.enabledInstances=e.instances.slice(),this.hardRefreshAll()},e.disableAll=function(){for(var t=e.enabledInstances.slice(),i=0;i<t.length;i++)e.enabledInstances[i].disable();e.enabledInstances=[]},e.initializeAll=function(t,i,n){if(void 0===t)throw new Error("'target' parameter is undefined");var s=[];t instanceof Element?s=[t]:void 0!==t.length&&0<t.length&&t[0]instanceof Element?s="function"==typeof t.get?t.get():t:"string"==typeof t&&(s=document.querySelectorAll(t)||[]);var o=[],h=[];if(s.forEach(function(t){-1===o.indexOf(t.parentNode)&&(o.push(t.parentNode),h.push(t))}),!n&&!h.length)throw new Error("There are no elements to initialize");return h.map(function(t){return new e(t,i)})},window.addEventListener("scroll",e.refreshAll),window.addEventListener("resize",e.hardRefreshAll);var r={parseNumber:function(t){return parseFloat(t)||0},findElement:function(t,i){return i=i||document,"string"==typeof t?i.querySelector(t):t instanceof Element?t:void 0},getComputedBox:function(t){var i=t.getBoundingClientRect(),t=getComputedStyle(t);return{height:i.height,width:i.width,top:window.pageYOffset+i.top,bottom:window.pageYOffset+i.bottom,marginTop:r.parseNumber(t.marginTop),marginBottom:r.parseNumber(t.marginBottom),paddingTop:r.parseNumber(t.paddingTop),paddingBottom:r.parseNumber(t.paddingBottom),topWithMargin:window.pageYOffset+i.top-r.parseNumber(t.marginTop),bottomWithMargin:window.pageYOffset+i.bottom+r.parseNumber(t.marginBottom)}}};return e}();var jQueryPlugin=window.$||window.jQuery||window.Zepto;jQueryPlugin&&(jQueryPlugin.fn.sticksy=function(t){return window.Sticksy.initializeAll(this,t)});
// source --> https://maisonoffice.vn/wp-content/themes/maisonreal/assets/js/elementor/recruitment.js 
function stickySidebar(){

    var numberTopSpacing=100;

    if (document.getElementById("wpadminbar")){
        numberTopSpacing=130;
    }

    var stickyEl = new Sticksy('.js-sticky-widget', {topSpacing: numberTopSpacing});

}



function preventDefaults(e) {
    e.preventDefault();
    e.stopPropagation();
}

function handleDropUpload(e) {
    const files = e.originalEvent.dataTransfer.files;
    handleFilesUpload(files);
}
function handleFilesUpload(files) {
    if (files.length > 0) {
        const file = files[0];
        if (file.type !== "application/pdf") {
            alert("Vui lòng chỉ chọn tệp PDF.");
            return;
        }
        else{
            if (file.size > 10 * 1024 * 1024) {
                alert("Kích thước tệp quá lớn. Vui lòng chọn tệp nhỏ hơn 10MB.");
                return;
            }
            else{
                jQuery("#fileNameUpload").html("<span>"+file.name+"</span>").addClass("active");
            }
        }

    }
}
function eventUploadCTF7(){
    const dropArea = jQuery('#boxFileUpload');
    const fileInput = jQuery('#uploadFile');

    // Ngăn chặn hành vi mặc định khi kéo và thả tệp
    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
        dropArea.on(eventName, preventDefaults);
        jQuery(document).on(eventName, preventDefaults);
    });

    dropArea.on('dragenter dragover', function (){
        dropArea.addClass('highlightUpload');
    });
    dropArea.on('dragleave drop', function (){
        dropArea.removeClass('highlightUpload');
    });

    dropArea.on('drop', handleDropUpload);

    fileInput.on('change', function(e) {
        const files = e.target.files;
        handleFilesUpload(files);
    });

}

function CTF7AutoFind(){
    jQuery(".ctf7-auto-title").val(jQuery("body").find("h1").text());
    jQuery(".ctf7-auto-permalink").val(window.location.href);
}


jQuery(document).ready(function($) {
    stickySidebar();
    eventUploadCTF7();
    CTF7AutoFind();
});