$(document).ready(function(){
	ExecuteHash();
});

$( window ).on( 'hashchange', function( e ) {
	// $('.modal').modal('hide');
	ExecuteHash();
} );

function ExecuteHash() {
	var hash = window.location.hash.substring(1);
	if (hash=='home' || hash=='Home' || hash=='' || hash==undefined) {
		hash = 'dashboard';
	}
	var class_link = hash.replaceAll('/','-');
	// $('#loading_indicator').css('display','flex');

	if (hash!='') {
		
		$("#rt_main_content").hide().load(baseUrl+hash, {limit: 25}, 
		    function (responseText, textStatus, req) {
		        if (textStatus == "error") {
		          $('#rt_main_content').hide().load(baseUrl+'custom_not_found').fadeIn('0');
		        }
		        // $('#loading_indicator').css('display','none');
		}).fadeIn(500);
	}else{
		$("#rt_main_content").hide().load(baseUrl+'dashboard', {limit: 25}, 
		    function (responseText, textStatus, req) {
		        if (textStatus == "error") {
		          $('#rt_main_content').hide().load(baseUrl+'custom_not_found').fadeIn('0');
		        }
		        // $('#loading_indicator').css('display','none');
		}).fadeIn(500);
	}
	
	$("#modal_add").modal('hide')
	$("#modal_edit").modal('hide')

	hideSidebar()
	setNav(hash.toLowerCase())
}

function hideSidebar()
{
	$('html').removeClass('layout-menu-expanded');
}

String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};

function setNav(currentPage)
{

	let path = [currentPage]
	if (currentPage.includes('/')) {
	  path = currentPage.split('/');       
	}
	
	try{
		
		if(path.length>1){
			$('.menu-item').removeClass('active')

			$(`.menu-item-${path[0]}`).addClass('active')
			$(`.menu-item-${path[1]}`).addClass('active')

			$(`.menu-item-${path[0]}`).addClass('open')
		}else{
			$('.menu-item').removeClass('active')
			$(`.menu-item-${path[0]}`).addClass('active')

		}

	}catch{

	}


}