var mid_, cid_, aid_, current_article_li;
var iframe;
var scrollGoto = 0;
var read_tracking = "automatic"; // automatic, button

// Begin module registration 
var begin_clicked = false;
var module_started = false;
var begin_object_clicked;

window.mobilecheck = function() {
	var check = false;
	(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
	return check;
};

function contentArticle(target) {
	console.log("2.Hey you " + target);
	target.trigger( "click" );
}

$(document).ready(function() {
	console.log("Initialising javascript");
	scrollListeners();
	initHistory();

	$(function(){
		$('input[type="text"].trim').bind('input', function(){
			$(this).val(function(_, v){
			  return v.replace(/\s+/g, '');
			});
		});
	});

	if ("ontouchstart" in document.documentElement){
		$('html').addClass('touch');
	}
	if(window.mobilecheck()) {
		console.log("Is mobile");
		$('html').addClass('touch');
	}
	else {
		$('html').addClass('desktop');
	}
	$( window ).resize(function() {
		resizeStuff();
	});
	function resizeStuff() {
		var wh = $(window).innerHeight();
		var ly = 0;
		if($('.credits-row').length) ly = $(".credits-row").position();
		var fh = $(".home_footer-row").innerHeight() + $("footer").innerHeight();
		//console.log(wh,ly.top,fh);

		var remainder2 = wh - ly.top - fh;

		var remainder_top = remainder2/2; // + 15;
		var remainder_bottom = remainder2/2 - 15;

		// Instead of filling with white, set the padding
		
		//$(".filler").css("height",remainder + "px");
		//$(".credits").css("padding-top",remainder_top + "px");
		//$(".credits").css("padding-bottom",remainder_bottom + "px");

		if($("body").hasClass("page-lecture")) {
			resizeQuestions();
		}
		$(".nav-buttons h2").css("width", $(".nav-buttons-div").css("width") );

		//console.log( ( $(".nav-buttons h2").innerHeight()-41)/2 );
		
		$(".nav-buttons h2 .after").css("top", ($(".nav-buttons h2").innerHeight()-41)/2 + "px" );
		$(".nav-buttons h2 .before").css("top", ($(".nav-buttons h2").innerHeight()-41)/2 + "px" );

		$(".nav-buttons h2").fadeTo("slow",1);
	}
	resizeStuff();

	$(".nav-buttons-div").fadeTo("fast",1);
	$("#login.theform").submit( function(event) {
		var password_ = $("#login.theform input[name=password]").val();
		password_ = password_.toUpperCase();
		if(password_ == "ENDO22") {
			event.preventDefault();
			$(".alert h3").html("");
			$(".alert p").html("El curso aún no ha comenzado, podrá acceder a partir del 20 de Junio de 2022.");
			$(".alert").fadeToggle(500);
		}
	});
	$('a.pdfbt[href="#"]').css('cursor', 'default');
	//$('a[href*="//"]').attr('href').css("cursor:default");

	/*var iframe_ = $("iframe").contents().find('body');
	var addedText = "<script></script>";
	iframe_.html(addedText);*/

	$(".clickclose").click(function(event) {
		$(this).next(".me").slideToggle("fast");
	});
	$(".close").click(function(event) {
		event.preventDefault();
		
		if( $(this).parent().hasClass("video-overlay") ) {
			$(".video-overlay iframe").attr( "src", "" );
		}
		if ( $(this).parent().parent().hasClass("begin") || $(this).parent().parent().hasClass("validate") ) {
			$(this).parent().parent().fadeOut(500);
		}
		else {
			$(this).parent().fadeOut(500);
		}
	});
	$(".begin a.button").click(function(e) {
		e.preventDefault();

		if ( !begin_clicked ) {
			module_started = true;
			begin_clicked  = true;

			$.get("library/begin.php",{ module: mid_, randomID: Math.random() }, function( data ) {
				console.log(data);
				$(".begin").fadeOut("fast");

				console.log("Begin a " +begin_object_clicked);
				begin_object_clicked.trigger("click");

			});
		}
	});
	$(".begin_pending").click(function(e) {
		mid_ = $(this).attr("data-module");
		console.log( $(this).hasClass("begin_pending") );

		//  && modules[ mid_ - 1 ][1] != "begin_pending"  

		if( module_started == false && $(this).hasClass("begin_pending") ) {
			e.preventDefault();
			e.stopImmediatePropagation();
			begin_object_clicked = $(this);
			mid_ = $(this).attr("data-module");
			$(".begin h3").html(modules[mid_-1][0]);

			console.log("Begin_pending");

			$(".begin").fadeIn("fast");

			setTimeout(function() {
				window.scrollTo(0, 0);
				$("section #main").scrollTop(0);
				$(".sidebar").scrollTop(scrollGoto);
			},50);
		}
		else {
			// BUG FIX
			$(".begin_pending[data-module=" + mid_ +"]").removeClass("begin_pending");//.unbind("click");
			begin_clicked = false
			module_started = false;

		}
	});	
	function setCurrentLi( that ) {
		current_article_li = that;
		console.log("Set current li to " + that);

		/*if(aid_!=undefined) {
			current_article_li = $(".volume-container.volume" + mid_ + " a[data-aid=" + cid_ + "][data-pid=" + aid_ + "]");
			//alert("volume-container.volume" + mid_ + " a[data-aid=" + cid_ + "][data-pid=" + aid_ + "]");
		}
		else {
			current_article_li = $(".volume-container.volume" + mid_ + " a[data-aid=" + cid_ + "]"); 
			//alert(".volume-container.volume" + mid_ + " a[data-aid=" + cid_ + "]");
		}*/
	}	
	function getExtension(url) {
		var arr = url.split("?"); // Seperate the querystring from the url
		var ext = arr[0].substr(-3, 3);
		return ext;
	}
	function checkHashtag(url) {
		var arr = url.split("#"); // Seperate the querystring from the url
		
		return arr[0] + "#m" + arr[1];
	}

	function loadMobileIframe(url) {
		//window.open($(this).attr("href"),"_blank");

		console.log(url);
		var add_to_url = "";
		$(".mobileoverlay iframe").attr("src", "");
		var extension = getExtension(url);
		if( extension == "pdf" ) add_to_url = "#zoom=100";
		
		// Check the hashtag and add an m for mobile
		url = checkHashtag(url);

		setTimeout( function() {
			$(".mobileoverlay iframe").attr("src", url + add_to_url); //+"#zoom=100");  +"#view=fit"
			$(".mobileoverlay").fadeIn(500);
			//$("body").addClass("noscroll");

			$(".mobileoverlay iframe").contents().find("body").css({
				"height": "100%",
				"overflow": "auto", 
				"-webkit-overflow-scrolling": "touch"
			});

		},100 );
	}
	function iframeClicks() {
		$(".pdf iframe").on('load', function() {
			console.log("iframe loaded");
			//$(".pdf iframe").fadeIn("slow");
			iframe = $('.pdf iframe').contents();
			iframe.find('.pdfbt').click(function(event){
				console.log("Pdf link");
				event.preventDefault();
				scrollGoto = $(".sidebar").scrollTop();

				var title_ = $(this).attr("title");
				var module = $(this).attr("data-module");
				var aid = $(this).attr("data-aid");
				var href_ = $(this).attr("href");
				var pid = $(this).attr("data-pid");
				anchor = $(this).attr("anchor");

				// Set the global variables to be accesed later
				mid_ = module;
				cid_ = aid;
				aid_ = pid;
				setCurrentLi( $(this) );
				markRead();

		  		if(window.mobilecheck()) {
					console.log("Is mobile");
					// First check if the click comes from the home boxes
					var is_home_box = $(this).parent().hasClass("module-box");
					is_home_box = false;
					
					if(is_home_box) {
						document.getElementById("submenuMod" + module).style.display = "block";
						var scrollto = $("#submenuMod" + module).parent().find(".nav-button").offset().top;
						 $('html, body').animate({
					        scrollTop: scrollto
					    }, 200);
					}
					else {
						var extension = getExtension( $(this).attr("href") );
						if( extension == "pdf" ) {
							window.open($(this).attr("href"),"_blank");
						}
						else {
							$(".mob_menu").removeClass("opened");
							if( mid_ == undefined) $(".mob_sandwich").css("display","none");
							else $(".mob_sandwich").css("display","block");
							loadMobileIframe( $(this).attr("href") );
						}
					}

				}
				else {
					if( $(this).attr("href")!="#" && $(this).attr("href")!="" ) {

						var that = $(this);
						if(!isauto) { add2history.apply(that); console.log("The click was USER"); }
						else { $("#go-back").fadeIn(200); }
						isauto = false;

						$("body").addClass("page-contents");

						$(".lecture-container").css("display","none");
                        $(".page-lecture").removeClass("page-lecture");

						$(".sidebar .openmenu, .sidebar a.pdfbt").removeClass("current");
						$(".sidebar .openmenu[data-aid='" + aid + "']").addClass("current").removeClass("closed").addClass("opened");

						// New code
						$(".nav-button").removeClass("active");
						$(".nav-button[data-module='" + mid_ + "']").addClass("active");

						if(pid!=undefined) {
							$(".sidebar a.pdfbt").removeClass("current");
							$(".sidebar a.pdfbt[data-pid='" + pid + "']").addClass("current");
						}
						else {
							$(".sidebar a.pdfbt[data-aid='" + aid + "']").addClass("current");
							console.log("Clicked lesson " + cid_ + " on module " + mid_);
						}
						//var cur_linked = $(".sidebar a.linked[data-aid='" + aid + "']");

						$(".sidebar .volume-container").css("display","none");
						console.log("Module: " + module);
						$(".sidebar .volume-container.volume"+module).fadeIn(0);
						//$(".sidebar h2").html("VOLUMEN " + module + ""); //MÓDULO

						$(".openmenu").not(".sidebar a[data-aid='" + aid + "']").parent().next().css("display","none");
						//alert( ".sidebar a.current[data-aid='" + aid + "']" );
						$(".sidebar a.openmenu.current[data-aid='" + aid + "']").parent().next().fadeIn();

						//$(".submenu").fadeOut(0);
						$(".syllabus h2").html(title_);

						$(".pdf iframe").attr("src", $(this).attr("href") ); // +"#zoom=100"); // + "?rndID=" + Math.random()
						$(".pdf iframe").attr("data-aid",aid);
						$(".pdf").fadeIn(500);
						$("body").addClass("noscroll");

						setTimeout(function() {
							window.scrollTo(0, 0);
							$("section #main").scrollTop(0);
							$(".sidebar").scrollTop(scrollGoto);
						},50);

						iframeClicks();
					}
				}		
			});
			iframe.find('.videobt').click(function(event){
				console.log("Video link");
				if( !$(this).hasClass("begin_pending") ) {
					event.preventDefault();

					var module = $(this).attr("data-module");
					var aid = $(this).attr("data-aid");
					var pid = $(this).attr("data-pid");

					// Set the global variables to be accesed later
					mid_ = module;
					cid_ = aid;
					aid_ = pid;

					setCurrentLi( $(this) );
					markRead();
					//checkRead(this);

					if(window.mobilecheck()) {
						console.log("Is mobile");
						// First check if the click comes from the home boxes
						var is_home_box = $(this).parent().hasClass("module-box");
						is_home_box = false;
						
						if(is_home_box) {
							document.getElementById("submenuMod" + module).style.display = "block";
							var scrollto = $("#submenuMod" + module).parent().find(".nav-button").offset().top;
							 $('html, body').animate({
						        scrollTop: scrollto
						    }, 200);
						}
						else {
							var extension = getExtension( $(this).attr("href") );
							if( extension == "pdf" ) {
								window.open($(this).attr("href"),"_blank");
							}
							else {
								$(".mob_menu").removeClass("opened");
								if( mid_ == undefined) $(".mob_sandwich").css("display","none");
								else $(".mob_sandwich").css("display","block");
								loadMobileIframe($(this).attr("href"));
							}
						}
						
			  			//window.open($(this).attr("href"),"_blank");
					}
					else {
						console.log("Open video on popup " + $(this).attr("href"));
						$(".video-overlay iframe").attr( "src", $(this).attr("href") );
						$(".video-overlay").fadeIn("fast");
					}
				}
			});

			$(".pdf iframe").off('load');
			window.scrollTo(0, 0);
		})
	}
	$(".pdfbt").click(function(event) {
		console.log("Pdf link");
		event.preventDefault();
		scrollGoto = $(".sidebar").scrollTop();

		var title_ = $(this).attr("title");
		var module = $(this).attr("data-module");
		var aid = $(this).attr("data-aid");
		var href_ = $(this).attr("href");
		var pid = $(this).attr("data-pid");
		anchor = $(this).attr("anchor");

		// Set the global variables to be accesed later
		mid_ = module;
		cid_ = aid;
		aid_ = pid;

		setCurrentLi( $(this) );
		markRead();

		if(window.mobilecheck()) {
			console.log("Is mobile");
			// First check if the click comes from the home boxes
			var is_home_box = $(this).parent().hasClass("module-box");
			is_home_box = false;
			
			if(is_home_box) {
				document.getElementById("submenuMod" + module).style.display = "block";
				var scrollto = $("#submenuMod" + module).parent().find(".nav-button").offset().top;
				 $('html, body').animate({ scrollTop: scrollto }, 200);
			}
			else {
				var extension = getExtension( $(this).attr("href") );
				if( extension == "pdf" ) {
					window.open($(this).attr("href"),"_blank");
				}
				else {
					// Check and change hashtag url
					$(".mob_menu").removeClass("opened");
					if( mid_ == undefined) $(".mob_sandwich").css("display","none");
					else $(".mob_sandwich").css("display","block");
					loadMobileIframe($(this).attr("href"));
				}
			}

		}
		else {
			if( $(this).attr("href")!="#" && $(this).attr("href")!="" ) {

				var that = $(this);
				if(!isauto) { add2history.apply(that); console.log("The click was USER"); }
				else { $("#go-back").fadeIn(200); }
				isauto = false;

				$("body").addClass("page-contents");

				$(".lecture-container").css("display","none");
                $(".page-lecture").removeClass("page-lecture");

				$(".sidebar .openmenu, .sidebar a.pdfbt").removeClass("current");
				$(".sidebar .openmenu[data-aid='" + aid + "']").addClass("current").removeClass("closed").addClass("opened");

				// New code
				$(".nav-button").removeClass("active");
				$(".nav-button[data-module='" + mid_ + "']").addClass("active");
				
				if(pid!=undefined) {
					$(".sidebar a.pdfbt").removeClass("current");
					$(".sidebar a.pdfbt[data-pid='" + pid + "']").addClass("current");
				}
				else {
					$(".sidebar a.pdfbt[data-aid='" + aid + "']").addClass("current");
				}
				//var cur_linked = $(".sidebar a.linked[data-aid='" + aid + "']");

				$(".sidebar .volume-container").css("display","none");
				console.log("Module: " + module);
				$(".sidebar .volume-container.volume"+module).fadeIn(0);
				//$(".sidebar h2").html("VOLUMEN " + module + ""); //MÓDULO

				$(".openmenu").not(".sidebar a[data-aid='" + aid + "']").parent().next().css("display","none");
				//alert( ".sidebar a.current[data-aid='" + aid + "']" );
				$(".sidebar a.openmenu.current[data-aid='" + aid + "']").parent().next().fadeIn();

				//$(".submenu").fadeOut(0);
				$(".syllabus h2").html(title_);

				//$(".pdf iframe").fadeOut(0);
				$(".pdf iframe").attr("src", $(this).attr("href") ); // +"#zoom=100"); //  + "?rndID=" + Math.random()
				$(".pdf iframe").attr("data-aid",aid);
				$(".pdf").fadeIn(500);
				$("body").addClass("noscroll");

				iframeClicks();

				setTimeout(function() {
					$("section #main").scrollTop(0);
					$(".sidebar").scrollTop(scrollGoto);
					window.scrollTo(0, 0);
					console.log("Timeout click");
				},50);

			}
		}
	});
	$(".videobt").click(function(event) {
		console.log("Video clicked");

		if( !$(this).hasClass("begin_pending") ) {
			event.preventDefault();

			var module = $(this).attr("data-module");
			var aid = $(this).attr("data-aid");
			var pid = $(this).attr("data-pid");

			// Set the global variables to be accesed later
			mid_ = module;
			cid_ = aid;
			aid_ = pid;

			setCurrentLi( $(this) );
			markRead();
			//checkRead(this);

			if(window.mobilecheck()) {
				console.log("Is mobile");
				// First check if the click comes from the home boxes
				var is_home_box = $(this).parent().hasClass("module-box");
				is_home_box = false;
				
				if(is_home_box) {
					document.getElementById("submenuMod" + module).style.display = "block";
					var scrollto = $("#submenuMod" + module).parent().find(".nav-button").offset().top;
					 $('html, body').animate({
				        scrollTop: scrollto
				    }, 200);
				}
				else {
					var extension = getExtension( $(this).attr("href") );
					if( extension == "pdf" ) {
						window.open($(this).attr("href"),"_blank");
					}
					else {
						$(".mob_menu").removeClass("opened");
						if( mid_ == undefined) $(".mob_sandwich").css("display","none");
						else $(".mob_sandwich").css("display","block");
			  			loadMobileIframe($(this).attr("href")); 
					}
					
				}
				
	  			//window.open($(this).attr("href"),"_blank");
			}
			else {
				console.log("Open video on popup " + $(this).attr("href"));
				$(".video-overlay iframe").attr( "src", $(this).attr("href") );
				$(".video-overlay").fadeIn("fast");
				//updateSidebar();
			}
		}
	});
	function checkLoading() {
		$(".pdf iframe").on('load', function() {
			console.log("IFRAME LOADED");

			$(".pdf iframe").off('load');
			$(".pdf iframe").fadeIn(0);

		});
	}
	$(".home_top select").change(function(event) {
		event.preventDefault();
		var module = $(this).val();
		window.open("index.php?module="+module,"_self");
	});
	$(".openmenu").click(function(e) {
		e.preventDefault();
		if($(this).hasClass("closed")) {
			$(this).removeClass("closed"); 
			$(this).addClass("opened"); 
		}
		else {
			$(this).removeClass("opened"); 
			$(this).addClass("closed"); 		
		}

		var item = $(this).parent().next();
		$(this).parent().next().slideToggle();
		console.log("Open MENU");
	});
	$(".pdf iframe").on('load', function() {
		window.scrollTo(0, 0);
  		document.body.scrollTop = 0;
		$("html").scrollTop(0);
		$("section #main").scrollTop(0);
		$(".sidebar").scrollTop(scrollGoto);
		var head = $(this).contents().find("head");
		var css = '<style type="text/css">' +
		          'img { max-width:100%; }; ' +
		          '</style>';
		$(head).append(css);
		console.log("pdf iframe loaded");
	});
	$(".sidebar .module_selector").change(function(event) {
		event.preventDefault();
		var module = $(".sidebar .module_selector option:selected").val();
		$(this).prop('selectedIndex',0);

		$("body").attr("module",module);
		$(".sidebar .volume-container").css("display","none");
		$(".sidebar .volume-container.volume"+module).fadeIn(0);
		//$(".sidebar h2:nth-child(2)").html("Volumen " + module + ""); // Módulo
	});
	$(".privacybt").click(function(event) {
		event.preventDefault();
		$(".privacy").fadeToggle(500);
	});
	$(".privacybt2").click(function(event) {
		event.preventDefault();
		$(".privacy2").fadeToggle(500);
	});
	$(".copyrightbt").click(function(event) {
		event.preventDefault();
		$(".copyright").fadeToggle(500);
	});
	$(".legalbt").click(function(event) {
		event.preventDefault();
		$(".legal").fadeToggle(500);
	});
	$(".alertbt").click(function(event) {
        event.preventDefault();
        var h3 = $(this).html();
        var text = $(this).attr("title");
        $(".alert h3").html(h3);
        $(".alert p").html(text);
        
        $(".alert").fadeToggle(500);
    });
	$("#register.theform h3 .icon").click(function(e) {
		e.preventDefault();
		$(this).parent().toggleClass("open");
		if($(this).parent().hasClass("open")) { $(this).html("-"); }
		else { $(this).html("+"); }
	});
	$("section #sidebar .subtitle .icon").click(function(e) {
		e.preventDefault();
		$(this).parent().toggleClass("open");
		if($(this).parent().hasClass("open")) { $(this).html("-"); }
		else { $(this).html("+"); }
	})

	// mid_, cid_, aid_, current_article_li;
	// LEIDO / NO LEIDO FUNCTIONALITY
	$(".leidobt").click(function(e) {
		e.preventDefault();
		toggleRead();
	});
	function markRead() {
		console.log("Read tracking :" + read_tracking + " " + current_article_li);
		if(read_tracking == "automatic") {
			var element = current_article_li.find(".article_dot");
			var element_menu = $(".nav-submenu a[data-module="+mid_ + "][data-aid=" + cid_ + "]").find(".article_dot");

			var string = ".sidebar a[data-module="+mid_ + "][data-aid=" + cid_ + "][data-pid=" + aid_ + "]";
			if( aid_ == undefined) string = ".sidebar a[data-module="+mid_ + "][data-aid=" + cid_ + "]:not(module-title)";

			console.log(string);
			
			var element_sidebar = $(string).find(".article_dot");

			element.addClass("article_dot_read");
			element_menu.addClass("article_dot_read");
			element_sidebar.addClass("article_dot_read");

			// mark it on DB
			console.log("Sending data");
			var action_ = "insert"; // Not used now, GU cancer uses it.
			$.get("library/au.php",{ uid: getCookie("userid"), mid: mid_, cid: cid_, aid: aid_, randomID: Math.random(), action: action_ }, function( data ) {
				var data_ = data.split('&');
				console.log(data);
				if( current_article_li.find("article_dot").hasClass("article_dot_read") ) { }
				else { current_article_li.find("article_dot").addClass("article_dot_read"); }
				$(".progressbar div div").css("width",data_[1]*100 + "%");
				if(data_[1]==1) {
					$(".selfassesment").removeClass("hide").show();
					$(".al_not_completed").hide();
				}
				if( data_[2] == "OK" ) { console.log( "Saved" ); }
			});

		}
	}
	function toggleRead () {

		var element = current_article_li.find(".article_dot");
		var element_menu = $(".nav-submenu a[data-module="+mid_ + "][data-aid=" + cid_ + "]");
		var action_;

		if( $(".leidobt").hasClass("unread") ) {
			action_ = "insert";
			$(".leidobt").removeClass("unread");
			element.addClass("article_dot_read");
			element_menu.find(".article_dot").addClass("article_dot_read");
		}
		else {
			action_ = "remove";
			$(".leidobt").addClass("unread");
			element.removeClass("article_dot_read");
			element_menu.find(".article_dot").removeClass("article_dot_read");
		}

		if(read_tracking == "button") { // !sending && 
			//sending = true;

			$.get("library/au.php",{ uid: getCookie("userid"), mid: mid_, cid: cid_, aid: aid_, randomID: Math.random(), action: action_ }, function( data ) {
				var data_ = data.split('&');
				$(".progressbar div div").css("width",data_[1]*100 + "%");

				if(data_[1]==1) {
					//console.log("All articles read");
					$(".selfassesment").removeClass("hide").show();
					$(".al_not_completed").hide();
				}
				//console.log(data_);
				//sending = false;
			});
		}

	}	
	function checkRead(this_) {
		//var element = $(".sidebar .volume" + mod)
		var is_read = current_article_li.find(".article_dot_read");
		//console.log(is_read);

		if( $(this_).find(".article_dot_read").length>0 ) {
			//alert("Is read");
			$(".leidobt").removeClass("unread");
		}
		else {
			$(".leidobt").addClass("unread");
			//alert("Not read");
		}
	}
});