/* This script by $criptMaster B  Duall 2009 */
/* JQuery 1.3.2 needed */
/* www.duall.be */

////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function(){
	SIZloaderTable();
	SIZform();
	SIZvalidate();
	SIZslide();
	SIZ_dl_showHide();
});

////////////////////////////////////////////////////////////////////////////////////////////

function SIZloaderTable(){

	$("#schooloverzicht").load("siz_table.html #scholenTable", function(){
		SIZdivsReady();
		SIZtableReady();
		$("table#scholenTable").remove();
		SIZposStip();
		SIZdivInfo();
		SIZposh4Hover();
		SIZh4Klik();
		SIZaExtraKlik();
		SIZdivExtraInfo();
		SIZcleanEmptyGroep();
		});

	function SIZcleanEmptyGroep(){
	
		$("div.groep").each(function(){
			
			var kind = this.lastChild;
			var kind2 = kind.nodeType;
			var kind3 = kind.className;
			var kind4 = kind.nodeValue;
			//console.log("laatste kind= "+prot+","+prot2+","+prot3+","+prot4);
			//3 = TEXT_NODE
			if (kind2 == 3) {$(this).remove()}
			
		});
	}


	function SIZtableReady(){
	
		$("table#scholenTable").find("tbody tr").each(function(){
	 
			var tdNaam = $(this).find("td:eq(0)").text();
			var tdType = $(this).find("td:eq(1)").text();
			var tdSoort = $(this).find("td:eq(2)").text();
			var tdAdres = $(this).find("td:eq(3)").html();
			var tdWebsite = $(this).find("td:eq(4)").text();
			var tdContact = $(this).find("td:eq(5)").text();
			var tdCoord = $(this).find("td:eq(6)").text();
			var tdUren = $(this).find("td:eq(7)").text();
			var tdBewaking = $(this).find("td:eq(8)").html();
			var tdUitleg = $(this).find("td:eq(9)").html();
			var tdImage = $(this).find("td:eq(10)").text();

			switch(tdSoort)
				{
				case "KLEUTER": 
					$(this).addClass("soort00");
				break;
				case "LAGER": 
						$(this).addClass("soort02");
				break;
				case "BASIS": 
					$(this).addClass("soort01");
				break;
				case "ANDER": 
					$(this).addClass("soort03");
				break;
				default:
					$(this).addClass("soort03");
				}
			
			tdAltImg = tdNaam;	
			tdNaam2 ="\n<h5>"+tdNaam+"</h5>\n<p class=\"schoolType\">"+tdType+"</p>\n";			
			tdNaam = "\n<h4><span class=\"pijl\">&rarr;</span>"+tdNaam+"</h4>\n";
						
			tdAdres = tdAdres.split("/");
			tdAdresBegin ="\n<ul class=\"schoolAdres\">";
			tdAdresEnd = "</ul>\n";
			tdStraat = "\n\t<li>"+tdAdres[0]+"</li>";
			tdGemeente = "\n\t<li>"+tdAdres[1]+"</li>";
			
			if (tdAdres.length==3){
				tdTel = "\n\t<li>"+tdAdres[2]+"</li>";
					if(tdWebsite == 0){
						tdAdres = tdAdresBegin + tdStraat + tdGemeente + tdTel + tdAdresEnd;	
					} else {
						tdWebsite = "\n\t<li><a href=\"http://"+tdWebsite+"\">website &rarr;</a></li>";
						tdAdres = tdAdresBegin + tdStraat + tdGemeente + tdTel + tdWebsite + tdAdresEnd;						}
				} else {
					if(tdWebsite == 0){
						tdAdres = tdAdresBegin + tdStraat + tdGemeente + tdAdresEnd;
					} else {
						tdWebsite = "\n\t<li><a href=\"http://"+tdWebsite+"\">website &rarr;</a></li>";
						tdAdres = tdAdresBegin + tdStraat + tdGemeente + tdWebsite + tdAdresEnd;
					}
				}
		
			tdCoord = tdCoord.split("/");
			tdCoord = "<ul class=\"schoolCoord\">\n\t<li>"+tdCoord[0]+"</li>\n\t<li>"+tdCoord[1]+"</li>\n</ul>\n";
			
			if(tdContact == 0){tdContact="";} else {
				tdContact = "\n<h6>contactpersonen</h6><p>"+tdContact+"</p>\n";}

			if(tdUren == 0){tdUren="";} else {
				if(tdUren.indexOf("/") > 0){
				tdUren = tdUren.split("/");
				tdUren = "\n<h6>Schooluren</h6><ul class=\"schoolUren\">\n\t<li>"+tdUren[0]+"</li>\n\t<li>"+tdUren[1]+"</li>\n</ul>\n";
				} else {
				tdUren = "\n<h6>Schooluren</h6><ul class=\"schoolUren\">\n\t<li>"+tdUren+"</li>\n</ul>\n";
				}
			}
			if(tdUitleg == 0){tdUitleg="";} else {
				tdUitleg = "<div class=\"woordjeUitleg\">\n<h6>Woordje uitleg</h6>"+tdUitleg+"</div>\n";
			}
			if(tdBewaking == 0){tdBewaking="";} else {
				tdBewaking = "\n<h6>voor- en nabewaking</h6>"+tdBewaking;
			}
			
			if(tdImage == 0){tdImage="";}else {
			tdImage = "<img src=\"img/scholen/"+tdImage+".jpg\" alt=\"foto voor "+tdAltImg+"\" />\n";
			}
			var schoolInfo ="\n<div class=\"schoolInfo\">"+tdAdres+tdCoord+"</div>\n";
			var schoolExtraInfo = "\n<div class=\"schoolExtraInfo\">"+tdImage+"\n"+tdNaam2+"\n"+tdUren+"\n"+tdBewaking+"\n"+tdContact+"\n"+tdUitleg+"\n</div>\n";
							
			var HTMLbody = 	tdNaam + schoolInfo + schoolExtraInfo;
			
			var Klas = $(this).attr("class");	
			$(this).append("\n<div class=\"school "+Klas+"\"></div>\n").children("div.school").html(HTMLbody);
			var Divke = $(this).children("div.school");
			$(this).parents("#schooloverzicht").children("#"+Klas).append(Divke);
		
		});
	
	}

	function SIZdivsReady(){
	
		$("#schooloverzicht")
			.append("\n\n<div id=\"soort00\" class=\"groep\">\n<h3>deelnemende kleuterscholen</h3>\n</div>\n")
			.append("\n\n<div id=\"soort01\" class=\"groep\">\n<h3>deelnemende basisscholen</h3>\n</div>\n")
			.append("\n\n<div id=\"soort02\" class=\"groep\">\n<h3>deelnemende lagere scholen</h3>\n</div>\n")
			.append("\n\n<div id=\"soort03\" class=\"groep\">\n<h3>niet-deelnemende scholen</h3>\n</div>\n");
		}
			

		
	function SIZposStip(){
		$("div.school").each(function(){
			var coordH = $(this).find("ul.schoolCoord li:eq(0)").text();
			var coordV = $(this).find("ul.schoolCoord li:eq(1)").text();
			$(this).find("ul.schoolCoord").remove();
			coordH = coordH.toString();
			coordH = parseInt(coordH,10);
			
			var overzicht = $(this).parents("div#SIZoverzichtBody").children("div#schooloverzicht");
			var overzichtBreedte = overzicht.width();
			overzichtBreedte  = parseInt(overzichtBreedte,10);
			
			coordH = overzichtBreedte+coordH;
			
			$(this).append("<span class='stip'>•</span>\n");
			var span = $(this).children("span");
			span.css("top",coordV +"px");
			span.css("left",coordH +"px");
		});
	}
	
	function SIZdivInfo(){
		$("div.schoolInfo").each(function(){
				if($(this).parent("div.school").hasClass("soort03")){
			} else {
		 		$(this).append("<a></a>\n").children("a").addClass("meerInfoLink").attr("href","#").html("meer info &rarr;");
			}
			$(this).hide();
		});
	}
	
	function SIZposh4Hover(){
	$("div.school h4,span.stip").hover(
		function(){
			$(this).css("cursor","pointer"); 
			$(this).parent("div.school").children("span.stip").toggleClass("hover");
			$(this).parent("div.school").children("h4").toggleClass("hover");
		},
		function(){
			$(this).parent("div.school").children("span.stip").toggleClass("hover");
			$(this).parent("div.school").children("h4").toggleClass("hover");
		});
	}
	
	function SIZh4Klik(){
	
	$("div.school h4, span.stip").click(function(){
	 	var infoDiv = $(this).parents("div.school").children("div.schoolInfo");
	 	var extraDiv = $(this).parents("div.school").children("div.schoolExtraInfo");

		if ($(infoDiv).is(":visible")){
				$(infoDiv).slideUp();
				$("div.school h4").removeClass("ExtraOpen");
 				$(extraDiv).hide();
			} else {
				$("div.schoolInfo").slideUp();
				$(infoDiv).slideDown();
			}
		});
	}
	
	function SIZdivExtraInfo(){
		$("div.school").each(function(){
			var titel = $(this).find("h4").html();
			//$(this).children("div.schoolExtraInfo").prepend("\n<h5></h5>\n").children("h5").html(titel);
			//$(this).children("div.schoolExtraInfo").append("<a></a>\n").children("a").attr("href","#").addClass("sluit").html("terug naar plan");
			$(this).children("div.schoolExtraInfo").hide();
		});
	}
	
	function SIZaExtraKlik() {
	$("div.school a.meerInfoLink, a.sluit").click(function(){
	 	var extraDiv = $(this).parents().children("div.schoolExtraInfo");
	 	var bodyH = $(this).parents("div#SIZoverzichtBody").height();
	 	extraDiv.css("min-height",bodyH+"px");
		if (extraDiv.is(":visible")){
			extraDiv.fadeOut();
			extraDiv.parents("div.school").children("h4").removeClass("ExtraOpen");
			return false
		} else {
			$("div.school h4").removeClass("ExtraOpen");
			$("div.schoolExtraInfo").hide();
			extraDiv.fadeIn();
			extraDiv.parents("div.school").children("h4").addClass("ExtraOpen");
			return false
			}
		});
	}
	


}

////////////////////////////////////////////////////////////////////////////////////////////

function SIZform() {
	//$("#SIZcontactForm").css("position","absolute").hide();;
	
	$("#SIZcontactForm").load("siz_form.html div#loadForm", function(){
			$("h4 input.checkbox").click(function(){
			if($(this).is(":checked")){
				$(this).parent().next("fieldset").slideDown();
			} else {
				$(this).parent().next("fieldset").slideUp();
			}
		});
		
	$("#SIZcontactForm").prepend("<a href=\"#\" class=\"formSluit\">sluit</a>");	
	$("a.formSluit").click(function(){ 
		history.go(-1); 
		return false
		});
	
	SIZvalidate();

	});
	

	
	/*
	$("a[href='#SIZcontactForm']").click(function(){
		var bodyH = $(this).parents("div#SIZbody").height();
		$("#SIZcontactForm").css("min-height",bodyH+"px");
	
		$("#SIZcontactForm").fadeIn();
	});
	
	$("#SIZcontactForm a.formSluit").click(function(){$("#SIZcontactForm").fadeOut(); return false;});
	*/
	
	/*
	$("h4 input.checkbox").change(function(){ // IE voert change pas uit ope een checkboxnadat ie de focus kwijt is
			$(this).parent().next("fieldset").slideToggle();
	});
	*/

}

////////////////////////////////////////////////////////////////////////////////////////////
//opgelet Packed version niet gebruiken voor IE
function SIZvalidate() {
	$("#SIZcontactForm.schooldagen form, #SIZcontactForm.infoavond form").validate({ 
		rules: {
		NAAM: "required",
		CODE: "required",
		GEBOORTEJAAR: "required",
		MAIL: {
		       required: true,
		       email: true
		     			}
	  		},
		messages:{
		MAIL: {
			required: "&uarr; we hebben jouw e-mail nodig om je te contacteren",
			email: "&uarr; e-mail moet in het volgende formaat: naam@domein.com"
			},
		NAAM: {
			required: "&uarr; sorry, maar we hebben jouw naam en voornaam nodig"
			},
		CODE: {
			required: "&uarr; sorry, maar we hebben jouw postcode nodig"
			},	
		GEBOORTEJAAR: {
			required: "&larr; graag invullen"
			}
			
			}
		});

	$("#SIZcontactForm.studienamiddag form").validate({ 
		rules: {
		NAAM: "required",
		STRAAT: "required",
		CODE: {
				required: true,
				digits: true,
				maxlength: 4,
				minlength: 4
		
				},
		PLAATS: "required",
		MAIL: {
		       required: true,
		       email: true
		     			}
	  		},
		messages:{
		MAIL: {
			required: "&uarr; we hebben uw e-mail nodig om u te contacteren",
			email: "&uarr; e-mail moet in het volgende formaat: naam@domein.com"
			},
		CODE: {
			required: "&uarr; graag uw postcode invullen",
			digits: "&uarr; postcode moet uit 4 getallen bestaan",
			maxlength: "&uarr; postcode moet uit 4 getallen bestaan",
			minlength: "&uarr; postcode moet uit 4 getallen bestaan"
			},
		PLAATS: {
			required: "&uarr; graag uw woonplaats invullen"
			},
		STRAAT: {
			required: "&uarr; graag uw straatnaam invullen"
			},
		NAAM: {
			required: "&uarr; sorry, maar we hebben uw naam en voornaam nodig"
			}
			
			}
		});
}

////////////////////////////////////////////////////////////////////////////////////////////

function SIZslide(){

	$('.slideshow').cycle({
		fx: 'scrollLeft',
		pause: 1,
		timeout: 4000
	});	

}

////////////////////////////////////////////////////////////////////////////////////////////


function SIZ_dl_showHide() {
$("dd").hide();
$("dt").wrapInner("<span></span>");
$("dt span").addClass("dtBody");
$("dt").prepend("<span class='pijl'>&rarr; </span>");
 
$("dt").hover(
	function(){
		$(this).addClass("dtHover");
		$(this).children("span.pijl").remove();
		$(this).prepend("<span class='pijl'>&darr; </span>");
		},
	function(){
		$(this).removeClass("dtHover");
		if($(this).hasClass("dtActive")){
		} else {
		$(this).children("span.pijl").remove();
		$(this).prepend("<span class='pijl'>&rarr; </span>");
		}
	}
);
 
$("dt").toggle(
	function(){
		$(this).addClass("dtActive");
		$(this).children("span.pijl").remove();
		$(this).prepend("<span class='pijl'>&darr; </span>");
		$(this).next("dd").slideDown();
	},
	function(){
		$(this).removeClass("dtActive");
		$(this).children("span.pijl").remove();
		$(this).prepend("<span class='pijl'>&rarr; </span>");
		$(this).next("dd").slideUp();
	});
}