// JQuery Custom Functions By 100Hertz
// 
$(document).ready(function(){

	// initialiseer menu op basis van url
	//get_var = document.URL;

	if (((get_var[0] == "cursusaanbod" && get_var[4] != "") || get_var[0] == "onderwijs" || get_var[0] == "projecten" || get_var[0] == "maatwerk" || get_var[0] == "nieuwe_veste")) {
	  var broken = get_var;
	  
	  $("#mainmenu a[title='" +get_var[0]+ "']").addClass("active");

	  //initDropDown(broken[0], broken[1], broken[2], broken[3]);
	  //alert(broken[0]);
	  
	  if (broken[2] == "category") $("#sidemenu a[title='" +broken[4]+ "']").addClass("active");

	  // sidemenu
	  if (broken[4] && (get_var[0] == "cursusaanbod")) {
		$.ajax({
			  method: "get",url: "/index.php/cursusaanbod/side_menu_new/", data: "content="+broken[0]+":"+broken[1]+":"+broken[2]+":"+broken[3]+":"+broken[4], 
			  beforeSend: function(){$("#sidemenu").hide("fast"); $("#loading").show("fast"); $("#loading").html('<img src="/img/spinner.gif" />');},
			  complete: function(){$("#loading").hide("fast");},
			  success: function(html){
				  $("#sidecol").html(html);
				  var checkElement = $("#sidemenu a[title='" +broken[3]+ "']").next();
				  $('#sidemenu ul').hide();
				  checkElement.slideDown('fast', initCSSmenu(checkElement));
				  $("#sidemenu a[title='" +broken[4]+ "']").addClass("active");
				  initMenu(false);
			  }
		});
	  }
	}
	

	/*
	$("#mainmenu a").click(function(){

			var table_show = "table#" + $(this).attr("title");
			

			if ($(this).attr("class") != "nomenu") {
			  $("div.submenu").hide();
			  $("div.submenu table").hide();
  
			  $("div.l1").slideDown("fast");
			  $(table_show).show();
  
			  $("#mainmenu a").removeClass("active");
			  $("#submenus a").removeClass("active");
			  $(this).addClass("active");
			}
	});
	*/

	$("#flowpanes a").click(function(){

		var docent_show = $(this).attr("title");

		$.ajax({
			  method: "get",url: "/index.php/docent_data/"+docent_show, 
			  //beforeSend: function(){$("#docdata").hide("fast"); $("#loading").show("fast"); $("#loading").html('<img src="/img/spinner.gif" />');},
			  //complete: function(){$("#loading").hide("fast");},
			  success: function(html){
				 // $("#docdata").show("fast");
				  $(".col2").html(html);
			  }
		});

	});

	//$("#menuwrapper").mouseout(function(){
			
			//if (broken) {
				//initDropDown(broken[0], broken[1], broken[2]);
			//}
	//});

	/*
	$(".l1 a").click(function(){
							  
			var href = $(this).attr("href");
			
			if (href == "#") {
				var table_show = "table#" + $(this).attr("title");

				$("div.l2 table").fadeOut("fast");
				$("div.l2").hide();

				$("div.l2").fadeIn("fast");
				$(table_show).fadeIn("fast");
				
				$(".l1 a").removeClass("active");
				$(this).addClass("active");
			}
	});

	$(".l2 a").click(function(){

		var content_show = $(this).attr("title");

		$.ajax({
			method: "get",url: "templates/side_menu.php", data: "content="+content_show, 
			beforeSend: function(){$("#sidemenu").hide("fast"); $("#loading").show("fast"); $("#loading").html('<img src="img/spinner.gif" />');},
			complete: function(){$("#loading").hide("fast");},
 			success: function(html){ //so, if data is retrieved, store it in html
				$("#sidecol").html(html); //show the html inside .content div
				initMenu(true);
			}
		});
		
		$(".l2 a").removeClass("active");
		$(this).addClass("active");

		//$("#sidemenu").fadeIn("fast");
	});
	*/
	
});

function initDropDown(maintitle, l1title, l2title, l3title) {

	  // main menu
	  //$("#mainmenu a[title='" +maintitle+ "']").addClass("active");

	  // level 1 menu
	  $("div.submenu").hide();
	  $("div.submenu table").hide();
	  $("div.l1").show();
	  var table_show = "table#" + maintitle;
	  $(table_show).show();
	  $(".l1 a[title='" +l1title+ "']").addClass("active");

	  // level 2 menu
	  if (l2title && l2title != "category") {
		$("div.l2").show();
		var table_show = "table#" + l1title;
		$(table_show).show();
		$(".l2 a[title='" +l2title+ "']").addClass("active");
	  }

		// if l2title = 'category'
		//alert(l3title);
	  if (l3title) {
		$("div.l2").show();
		var table_show = "table#" + l1title;
		$(table_show).show();
		$(".l2 a[title='" +l3title+ "']").addClass("active");
	  }
}

function initMenu(openfirst) {
	
	if (openfirst) {
		$('#sidemenu ul').hide();
		$('#sidemenu ul:first').slideDown('fast', initCSSmenu($('#sidemenu ul:first')));
	}

	$('#sidemenu li a').click(
	  function() {
		var checkElement = $(this).next();

		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('#sidemenu ul:visible').slideUp('fast');
			checkElement.slideDown('fast', initCSSmenu(checkElement));
 
			return false;
		}
	  }
	);
}

function initCSSmenu(current) {

	  $('#sidemenu li').addClass("brd"); // stippellijn voor alle li's

	  $('#sidemenu li.header').removeClass("brd"); // geen stippellijn voor header in level 1
	  $('#sidemenu li ul li').removeClass("brd"); // geen stippellijn voor level 2
	  $('#sidemenu li.nbdr').removeClass("brd"); // geen stippellijn voor de laatste in level 1
	  current.parents("li").removeClass("brd"); // geen stippellijn voor de geopende

	  $('#sidemenu li').parents("li").removeClass("ardwn"); // verwijder de arrow down voor alle li in level 1
	  current.parents("li").addClass("ardwn"); // set de arrow down (rood) voor de geopened
}

function initSideMenu(page) {

		$.ajax({
			method: "get",url: "/index.php/cursusaanbod/side_menu_new/", data: "content=instrumentaal", 
			beforeSend: function(){$("#loading").html('<img src="img/spinner.gif" />');},
			complete: function(){$("#loading").hide("fast");},
 			success: function(html){ //so, if data is retrieved, store it in html
				$("#sidecol").html(html); //show the html inside .content div
			}
		});	
}