// JavaScript Document

function isValidEmailAddress( address ){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test( address ) == false) {
      return false;
   }
   return true;
}




function getOptions(id){
	
	// Options
	$.ajax({
		url: "/php/AjaxController.php?type=getOptions&id="+id,
		dataType: "json",
		type: "GET",
		success: function(data){
			var ul = "<ul>";
			if(data.length){
				for(var i = 0; i < data.length; i++){
					ul += '<li><a href="'+data[i].url+'" title="'+data[i].title+'" rel="'+data[i].id+'">' + data[i].title + '</a></li>';
				}
			}
			ul += "</ul>";
				
			$("#rol_3").html(ul);
			$("#rol_3 ul li a").click(function(e){ e.preventDefault(); });
			
			$("#rol_3 ul").draggable({
				axis: "y",
				drag: function(event, ui) {
					$("#rol_3 ul li a").each(function(){
						if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
							$("#rol_3 ul li a").removeClass("active");
							$(this).addClass("active");
						}
					});
				},
				stop: function(event, ui) {
					$("#rol_3 ul li a").unbind("click");
					$("#rol_3 ul li a").click(function(e){ e.preventDefault(); });
					
					$("#rol_3 ul li a").each(function(){
						if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
							$(this).trigger("customEvent");
							return false;
						}
					});
						
					if((($("#rol_3 ul").offset().top + $("#rol_3 ul").height()) < ($("#bar").offset().top + 10))){
						// boven
						$("#rol_3 ul li a:last").trigger("customEvent");
					} else if($("#rol_3 ul").offset().top > (($("#bar").offset().top - 10) + barHeight)){
						// onder
						$("#rol_3 ul li a:first").trigger("customEvent");
					}
				}
			});
			
			$("#rol_3 ul li a").bind("customEvent", function(e){
				e.preventDefault();
				
				$("#rol_3 ul li a").removeClass("active");
				$(this).addClass("active");
				
				if($("#rol_1 ul li a.active").attr("rel") && $("#rol_2 ul li a.active").attr("rel") && $("#rol_3 ul li a.active").attr("rel")){	
					getTags($("#rol_1 ul li a.active").attr("rel"), $("#rol_2 ul li a.active").attr("rel"), $("#rol_3 ul li a.active").attr("rel"));	
				}
			
				var y = $("#rol_3 ul li a.active").offset().top;
				var c = ($("#rol_3 ul li a.active").parents(".rol").offset().top + ($("#rol_3 ul li a.active").parents(".rol").height() / 2));
				
				// new y position
				var n = (y - c) + ($("#rol_3 ul li a.active").height() / 2) - 4;
				$("#rol_3 ul").stop().animate({
					marginTop: '-='+n
				}, 500, 'easeOutBack', function(){
						$("#rol_3 ul li a").unbind("click");
						$("#rol_3 ul li a").click(function(e){
							e.preventDefault();
								
							$(this).trigger("customEvent");
						});		
					
				});
			});
			
			$("#rol_3 ul li a").eq(Math.floor($("#rol_3 ul li").length / 2)).trigger("customEvent");
		}
	});
}


function getTags(id1, id3, id4){
	
	// Tags
	$.ajax({
		url: "/php/AjaxController.php?type=getTags&id1="+id1+"&id3="+id3+"&id4="+id4,
		dataType: "json",
		type: "GET",
		success: function(data){
			var ul = "<ul>";
			if(data.length){
				for(var i = 0; i < data.length; i++){
					ul += '<li><a href="'+data[i].url+'" title="'+data[i].title+'">' + data[i].title + '</a></li>';
				}
			}
			ul += "</ul>";
				
			$("#rol_4").html(ul);
			$("#rol_4 ul li a").click(function(e){ e.preventDefault(); });
			
			$("#rol_4 ul").draggable({
				axis: "y",
				drag: function(event, ui) {
					$("#rol_4 ul li a").each(function(){
						if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
							$("#rol_4 ul li a").removeClass("active");
							$(this).addClass("active");
						}
					});
				},
				stop: function(event, ui) {
					$("#rol_4 ul li a").unbind("click");
					$("#rol_4 ul li a").click(function(e){ e.preventDefault(); });
					
					$("#rol_4 ul li a").each(function(){
						if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
							$(this).trigger("customEvent");
							return false;
						}
					});
						
					if((($("#rol_4 ul").offset().top + $("#rol_4 ul").height()) <= ($("#bar").offset().top + 10))){
						// boven
						$("#rol_4 ul li a:last").trigger("customEvent");
					} else if($("#rol_4 ul").offset().top >= (($("#bar").offset().top - 10) + barHeight)){
						// onder
						$("#rol_4 ul li a:first").trigger("customEvent");
					}
				}
			});
			
			$("#rol_4 ul li a").bind("customEvent", function(e){
				e.preventDefault();
				
				$("#rol_4 ul li a").removeClass("active");
				$(this).addClass("active");
			
				var y = $("#rol_4 ul li a.active").offset().top;
				var c = ($("#rol_4 ul li a.active").parents(".rol").offset().top + ($("#rol_4 ul li a.active").parents(".rol").height() / 2));
				
				// new y position
				var n = (y - c) + ($("#rol_4 ul li a.active").height() / 2) - 4;
				$("#rol_4 ul").stop().animate({
					marginTop: '-='+n
				}, 500, 'easeOutBack', function(){
						$("#rol_4 ul li a").unbind("click");
						$("#rol_4 ul li a").click(function(e){
							e.preventDefault();
								
							$(this).trigger("customEvent");
						});		
					
				});
			});
			
			$("#rol_4 ul li a").eq(Math.floor($("#rol_4 ul li").length / 2)).trigger("customEvent");
		}
	});
}

var hasMouseDown = false;
var barHeight = 21;

$(document).ready(function() {
	
	if($("#carrousel").length){
		
		if($("#showAll").length){
			$("#showAll").hover(function(){	
				$(this).stop().animate({
					paddingLeft: '10px'
				}, 500, 'easeOutBack', function(){
					$(this).addClass("hover");	
				});
				
			 }, function(){
				$(this).removeClass("hover");
				$(this).stop().animate({
					paddingLeft: '0px'
				}, 500, 'easeOutBack', function(){
				});
			 });
		}
				
		$("#carrousel ul li a").hover(function(e){
			e.preventDefault();
			
			$("#carrousel #bar").show();
			
			$("#carrousel ul li a").removeClass("hover");
			$(this).addClass("hover");
		
			var y = $(this).offset().top;
			var c = $("#carrousel").offset().top;
			
			// new y position
			var n = (y - c) + ($(this).height() / 2) - 11;
			
			$("#carrousel #bar").stop().animate({
				marginTop: n
			}, { 
				ducation: 500, 
				step: function( now, fx ){ 
					$("#carrousel ul li a").each(function(){
						if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
							$("#carrousel ul li a").removeClass("hover");
							$(this).addClass("hover");
						}
					});
				}, 
			easing: 'easeOutQuart'});
		});
				
				
		$("#carrousel").mouseout(function(){
										  
			if($("#carrousel ul li a.active").length) {
				var y = $("#carrousel ul li a.active").offset().top;
				var c = $("#carrousel").offset().top;
				
				// new y position
				var n = (y - c) + ($("#carrousel ul li a.active").height() / 2) - 11;
				
				$("#carrousel #bar").stop().animate({
					marginTop: n
				}, { 
					ducation: 300, 
					step: function( now, fx ){ 
						$("#carrousel ul li a").each(function(){
							if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
								$("#carrousel ul li a").removeClass("hover");
								$(this).addClass("hover");
							}
						});
					}, 
				easing: 'easeOutQuad'});
			}
		});
		
		
		if($("body#publications").length || $("body#cases").length || $("body#kennispunt").length || $("body#peoples").length || $("body#publications-detail").length || $("body#cases-detail").length || $("body#kennispunt-detail").length || $("body#peoples-details").length){
			$("#carrousel ul li a").click(function(e){
				e.preventDefault();
				
				loadCarousel($(this).attr("rel"), "");
				
				$("#carrousel ul li a").removeClass("active");
				$(this).addClass("active");
			});
			
			$("body#cases #carrousel #showAll, body#peoples #carrousel #showAll, body#kennispunt #carrousel #showAll, body#publications #carrousel #showAll").click(function(e){
				e.preventDefault();
				
				loadCarousel(0, "");
				
				$("#carrousel ul li a").removeClass("active");
				$(this).addClass("active");
			});
			
		}
		
		if($("#carrousel ul li a.active").length) {
			$("#carrousel").trigger("mouseout");
		} else {
			$("#carrousel #bar").hide();
		}
		
	}
	
	if($("#scroller").length){
		
    	$("#scroller").html('<a href="javascript:void(0);" title="zoeken" id="goLink">JA!</a><div id="shadow"></div><div id="bar"></div><div id="i_search">ik zoek</div><div id="in_expertise">binnen het vakgebied</div><div class="rol" id="rol_1"></div><div class="rol" id="rol_2"></div><div class="rol" id="rol_3"></div><div class="rol" id="rol_4"></div>');
		
		var img = Array("Aafko_Bergenhenegouwen.png","Ad_van_Dijck.png","Henk_Alberts.png","Koen_van_Polanen.png","Marcel_Duffhuis.png","Ron_van_Dijck.png","Theo_Bos.png","Ton_de_Wit.png","Ruud_Verwiel.png");
		
		$("#scroller_holder").append('<div id="img"><img src="http://www.telengy.nl/_ui/swf/'+img[Math.floor(Math.random() * img.length)]+'" /></div>');
		
		$("#scroller_holder img").load(function(){
			$("#scroller_holder #img").attr("width", $("#scroller_holder #img").width());
			$("#scroller_holder #img").attr("height", $("#scroller_holder #img").height());
			
			$("#header").pngFix();
		});
		
		$(document).mousedown(function(){
			hasMouseDown = true;
		});
		$(document).mouseup(function(){
			hasMouseDown = false;
		});
		
		$("#goLink").click(function(e){
			e.preventDefault();
			
			
			var rol_1_url = $("#rol_1 ul li a.active").attr("href");
			var rol_2_url = $("#rol_2 ul li a.active").attr("href");
			var rol_3_url = $("#rol_3 ul li a.active").attr("href");
			var rol_4_url = $("#rol_4 ul li a.active").attr("href");
			
			if(rol_1_url.match("http://www.telengy.nl/home/")){
				var s = rol_1_url.split("http://www.telengy.nl/home/");
				rol_1_url = s[1];
			}
			if(rol_2_url.match("http://www.telengy.nl/home/")){
				var s = rol_2_url.split("http://www.telengy.nl/home/");
				rol_2_url = s[1];
			}
			if(rol_3_url.match("http://www.telengy.nl/home/")){
				var s = rol_3_url.split("http://www.telengy.nl/home/");
				rol_3_url = s[1];
			}
			if(rol_4_url.match("http://www.telengy.nl/home/")){
				var s = rol_4_url.split("http://www.telengy.nl/home/");
				rol_4_url = s[1];
			}
			
			window.location.href = "http://www.telengy.nl/wat-we-doen/" + rol_2_url + "/ik-zoek/" + rol_1_url + "/" + rol_3_url + "/" + rol_4_url + "/";
			
		});
		
		// Subjects
		$.ajax({
			url: "/php/AjaxController.php?type=getSubjects",
			dataType: "json",
			type: "GET",
			success: function(data){
				var ul = "<ul>";
				if(data.length){
					for(var i = 0; i < data.length; i++){
						ul += '<li><a href="'+data[i].url+'" title="'+data[i].title+'" rel="'+data[i].id+'">' + data[i].title + '</a></li>';
					}
				}
				ul += "</ul>";
				
				$("#rol_1").html(ul);
				$("#rol_1 ul li a").click(function(e){ e.preventDefault(); });
				
				$("#rol_1 ul").draggable({
					axis: "y",
					drag: function(event, ui) {
						$("#rol_1 ul li a").each(function(){
							if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
								$("#rol_1 ul li a").removeClass("active");
								$(this).addClass("active");
							}
						});
					},
					stop: function(event, ui) {
						$("#rol_1 ul li a").unbind("click");
						$("#rol_1 ul li a").click(function(e){ e.preventDefault(); });
						
						$("#rol_1 ul li a").each(function(){
							if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
								$(this).trigger("customEvent");
								return false;
							}
						});
							
						if((($("#rol_1 ul").offset().top + $("#rol_1 ul").height()) < ($("#bar").offset().top + 10))){
							// boven
							$("#rol_1 ul li a:last").trigger("customEvent");
						} else if($("#rol_1 ul").offset().top > (($("#bar").offset().top - 10) + barHeight)){
							// onder
							$("#rol_1 ul li a:first").trigger("customEvent");
						}
					}
				});
				
				$("#rol_1 ul li a").bind("customEvent", function(e){
					e.preventDefault();
					
					$("#rol_1 ul li a").removeClass("active");
					$(this).addClass("active");
					
					if($(this).attr("rel")){	
						getOptions($("#rol_1 ul li a.active").attr("rel"));	
					}
				
					var y = $("#rol_1 ul li a.active").offset().top;
					var c = ($("#rol_1 ul li a.active").parents(".rol").offset().top + ($("#rol_1 ul li a.active").parents(".rol").height() / 2));
					
					// new y position
					var n = (y - c) + ($("#rol_1 ul li a.active").height() / 2) - 4;
					$("#rol_1 ul").stop().animate({
						marginTop: '-='+n
					}, 500, 'easeOutBack', function(){
						$("#rol_1 ul li a").unbind("click");
						$("#rol_1 ul li a").click(function(e){
							e.preventDefault();
							
							$(this).trigger("customEvent");
						});		
						
					});
				});
				
				$("#rol_1 ul li a").eq(Math.floor($("#rol_1 ul li").length / 2)).trigger("customEvent");
				
				
		
				// Expertises
				$.ajax({
					url: "/php/AjaxController.php?type=getExpertises",
					dataType: "json",
					type: "GET",
					success: function(data){
						var ul = "<ul>";
						if(data.length){
							for(var i = 0; i < data.length; i++){
								ul += '<li><a href="'+data[i].url+'" title="'+data[i].title+'" rel="'+data[i].id+'">' + data[i].title + '</a></li>';
							}
						}
						ul += "</ul>";
						
						$("#rol_2").html(ul);
						$("#rol_2 ul li a").click(function(e){ e.preventDefault(); });
						
						$("#rol_2 ul").draggable({
							axis: "y",
							drag: function(event, ui) {
								$("#rol_2 ul li a").each(function(){
									if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
										$("#rol_2 ul li a").removeClass("active");
										$(this).addClass("active");
									}
								});
							},
							stop: function(event, ui) {
								$("#rol_2 ul li a").unbind("click");
								$("#rol_2 ul li a").click(function(e){ e.preventDefault(); });
								
								$("#rol_2 ul li a").each(function(){
									if((($(this).offset().top + 10) >= $("#bar").offset().top) && (($(this).offset().top + 10) <= ($("#bar").offset().top + barHeight))){
										$(this).trigger("customEvent");
										return false;
									}
								});
									
								if((($("#rol_2 ul").offset().top + $("#rol_2 ul").height()) < ($("#bar").offset().top + 10))){
									// boven
									$("#rol_2 ul li a:last").trigger("customEvent");
								} else if($("#rol_2 ul").offset().top > (($("#bar").offset().top - 10) + barHeight)){
									// onder
									$("#rol_2 ul li a:first").trigger("customEvent");
								}
							}
						});
						
						$("#rol_2 ul li a").bind("customEvent", function(e){
							e.preventDefault();
							
							$("#rol_2 ul li a").removeClass("active");
							
							$(this).addClass("active");
							
							if($("#rol_1 ul li a.active").attr("rel") && $("#rol_2 ul li a.active").attr("rel") && $("#rol_3 ul li a.active").attr("rel")){	
								getTags($("#rol_1 ul li a.active").attr("rel"), $("#rol_2 ul li a.active").attr("rel"), $("#rol_3 ul li a.active").attr("rel"));	
							}
						
							var y = $("#rol_2 ul li a.active").offset().top;
							var c = ($("#rol_2 ul li a.active").parents(".rol").offset().top + ($("#rol_2 ul li a.active").parents(".rol").height() / 2));
							
							// new y position
							var n = (y - c) + ($("#rol_2 ul li a.active").height() / 2) - 4;
							$("#rol_2 ul").stop().animate({
								marginTop: '-='+n
							}, 500, 'easeOutBack', function(){
								$("#rol_2 ul li a").unbind("click");
								$("#rol_2 ul li a").click(function(e){
									e.preventDefault();
							
									$(this).trigger("customEvent");
								});
							});
						});
						
						$("#rol_2 ul li a").eq(Math.floor($("#rol_2 ul li").length / 2)).trigger("customEvent");
					}
				});
				
				
				
				
				
				
				
			}
		});
		
	}
	
	
	
	// find hr element and wrap it with a div for cross-browser support
	if($("hr").length){
		$("hr").each(function(){
			$(this).wrap('<div class="hr">');
		});
	}
	
	initAutoInput();
	
	
	//Show hidden javascript elements
	$(".js_show").removeClass("js_show");
	
	//Add background to coda slider (we don't want that when javascript is off)
	$(".coda-slider-wrapper").not(".small .coda-slider-wrapper").addClass("coda-slider-wrapper-background");
	$(".small .coda-slider-wrapper").addClass("coda-slider-wrapper-background");
	
	if ( $('#coda-slider-1').length ) $('#coda-slider-1').codaSlider();
	if ( $('#coda-slider-2').length ) $('#coda-slider-2').codaSlider();
	if ( $('#coda-slider-3').length ) $('#coda-slider-3').codaSlider();
	
	/*
	 * Home page
	 */
	if ( $("#home").length ){
		var stdTextName = "uw naam";
		var stdTextEmail = "e-mailadres";
		$("form #name").focus(function(){
			if ( $("form #name").val()==stdTextName ) $("form #name").val('');
		});
		$("form #name").blur(function(){
			if ( $("form #name").val()=='' ) $("form #name").val(stdTextName);
		});
		$("form #email").focus(function(){
			if ( $("form #email").val()==stdTextEmail ) $("form #email").val('');
		});
		$("form #email").blur(function(){
			if ( $("form #email").val()=='' ) $("form #email").val(stdTextEmail);
		});
		
		$("#newsletter form").submit(function(){
			//Validate formdata
			if ( !($("form #documentation").is(":checked")) && !($("form #appointment").is(":checked")) ){
				alert('Geef aan of u documentatie over Telengy wilt ontvangen, en/of dat u een afspraak wilt maken met een specialist van Telengy.');
				return false;
			}
			if ( !isValidEmailAddress($("form #email").val()) ){
				alert('Vul een geldig e-mailadres in!');
				return false;
			}
			
			return true;
		});
	}

	if($("#portals").length){
		$("#portals .head").not(".last").hover(function(){
			$(this).addClass("li_hover");
		}, function(){
			$(this).removeClass("li_hover");
		});
		//Make the portal column a clickable link
		$("#portals .head").click(function(){
//			alert("We gaan klikken");
			$(location).attr('href', $("a.title", this).attr('href'));
		});
	}
	
	
	if ($("#apply").length){
		$(".job .apply").click(function(){
			var jobid = $(this).closest(".job").attr("rel");
			$("#apply #job_id option").each(function(){
				if ( $(this).val()==jobid ){
					$(this).attr("selected","selected");
				}else{
					$(this).removeAttr("selected");
				}
			})
		});
		
		var defaultTextFirstName = "uw voornaam";
		var defaultTextLastName = "uw achternaam";
		var defaultTextEmail = "uw emailadres";
		var defaultTextPhone = "uw telefoonnummer";
		var defaultTextMessage = "Vul hier eventueel een bericht in.";
		
		$("#apply #firstname").focus(function(){
			if ( $(this).val()==defaultTextFirstName ) $(this).val("");
		});
		$("#apply #firstname").blur(function(){
			if ( $(this).val()=="" ) $(this).val(defaultTextFirstName);
		});
		
		$("#apply #lastname").focus(function(){
			if ( $(this).val()==defaultTextLastName ) $(this).val("");
		});
		$("#apply #lastname").blur(function(){
			if ( $(this).val()=="" ) $(this).val(defaultTextLastName);
		});
		
		$("#apply #email").focus(function(){
			if ( $(this).val()==defaultTextEmail ) $(this).val("");
		});
		$("#apply #email").blur(function(){
			if ( $(this).val()=="" ) $(this).val(defaultTextEmail);
		});
		
		$("#apply #phone").focus(function(){
			if ( $(this).val()==defaultTextPhone ) $(this).val("");
		});
		$("#apply #phone").blur(function(){
			if ( $(this).val()=="" ) $(this).val(defaultTextPhone);
		});
		
		$("#apply #message").focus(function(){
			if ( $(this).val()==defaultTextMessage ) $(this).val("");
		});
		$("#apply #message").blur(function(){
			if ( $(this).val()=="" ) $(this).val(defaultTextMessage);
		});
		
	}


});



// Make inputs empty on focus
// And reset on blur if it's still empty
function initAutoInput(){

	$('input, textarea').each(function(){
		
		$(this).focus(function(){
			if($(this).val() == $(this).next('.default').html()){
				$(this).val('');
			}
		});
	
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).next('.default').html());
			}
		});
	
	});

}





