$(function() {

	/* body map */
	var mapSource = $("img#img_body").attr("src");

	function getNewSource(obj) {
		var areaId;

	    if (obj.getAttribute("id").toLowerCase().indexOf("been") > -1) {
	    	areaId = obj.getAttribute("id").substring(4, 8);
	    } else if (obj.getAttribute("id").toLowerCase().indexOf("arm") > -1) {
	    	areaId = obj.getAttribute("id").substring(4, 7);
	    } else {
	    	areaId = obj.getAttribute("id").substring(4);
	    }

		return mapSource.substring(0, mapSource.lastIndexOf(".")) + "_" + areaId + mapSource.substring(mapSource.lastIndexOf("."));
	}

	$("area").mouseover(function() {
		$("img#img_body").attr("src", getNewSource(this));
		$("a#item" + $(this).attr("id").substring(4)).addClass("focus");
	});

	$("area").mouseout(function() {
		$("img#img_body").attr("src", mapSource);
		$("div.list_container a").removeClass("focus");
	});

	$("div.list_container a.region").hover(function() {
		$("img#img_body").attr("src", getNewSource(this));
	}, function() {
		$("img#img_body").attr("src", mapSource);
	});

	$("div.wizard.adhd.overzicht p.hidden").hide();

	/* radio toggle */
	$("div.situation input:radio").wrap($("<span class='container'></span>")).parent("span.container").click(function() {
		$(this).parents("div.situation").find("span.container").removeClass("realproblem smallproblem noproblem");
		$(this).addClass($(this).find("input:radio").attr("class"));
		$(this).find("input:radio").attr("checked","checked");
		$(this).parents("div.adhd").find("p[class]").hide().end().find("p." + $(this).find("input:radio").attr("class")).show();
	}).attr("class", function() {
		$(this).addClass($(this).find("input:checked").attr("class"));
	});

	/* tabs */
	$("div.keuzehulp_hernia ul.tabs li a").click(function() {
		$(this).parents("ul.tabs").find("li").removeClass("active");
		$(this).parent("li").addClass("active");
		$("div.tab_content").removeClass("active");
		$(this.hash).addClass("active");
		return false;
	});

	/* subtabs */
	$("div.tab_content ul.subtabs li a").click(function() {
		$(this).parents("ul.subtabs").find("li").removeClass("active");
		$(this).parent("li").addClass("active");
		$("div.subtab_content").removeClass("active");
		$(this.hash).addClass("active");
		return false;
	});

	/* visual score display */
	$(".subtab_content table td img").before($("<button class='visual_display'>Visuele weergave</button>")).hide();

	$(".subtab_content button.visual_display").toggle(function(e) {
		$(this).addClass("open").next("img").slideDown();
		e.preventDefault();
	}, function(e) {
		$(this).removeClass("open").next("img").slideUp();
		e.preventDefault();
	});

	/* vragenlijst beginmeting knop */
	$("div.adhdenquete input.next").click(function(e) {
		if ($("div.adhdenquete fieldset.kb_bh_swapform input:checked").val() == "yes") {
			window.open("https://www.questback.com/rivmemi/v1vqgfsi1d/");
		}
	});

	/* vragenlijst nameting knop */
	if ($("div.waardering input.enquete[value='yes']").length != 0) {
		$("div.waardering fieldset.wizard_navigation").append($("<button class='show' title='Deze vragenlijst opent in een nieuw venster.'>Open vragenlijst</button>").click(function(e){
			window.open("https://www.questback.com/rivmemi/p2s7ou26py/");
			$(this).attr("disabled","disabled").addClass("disabled");
			e.preventDefault();
		}));
	}

	/* autocomplete */
	var entry  = $("input.autocomplete");

	entry.each(function() {
		var config = $.microFormat($(this).attr("class"));
		var path = config.url || document.location.pathname.substring(0, document.location.pathname.lastIndexOf("/") + 1) + "autocomplete.aspx";

		$(this).autocomplete(path, {
			extraParams: config.params || {},
			lineSeparator: config.lineseparator || ",",
			matchContains: config.matchcontains || 0,
			maxItemsToShow: config.maxitemstoshow || "10",
			minChars: config.minchars || "1",
			selectFirst: true
		})

	});

	/*  */
	$("div.wizard.anticonceptie.uwkeuze.beoordelen input.next, div.wizard.adhd.oplossing input.next, div.wizard.angst.waardering input.next").click(function(e) {
		$("div.rating_container input:hidden").each(function(i, obj) {
			$(this).attr("name", $(this).attr("name").replace(/__/g,"$"));
		});
		//e.preventDefault();
	});
});
