jQuery(document).ready(function($) {

	/* ------------------------------------------------- */
	/* UURROOSTERS
	/* ------------------------------------------------- */
		if($('#uurrooster').size() > 0) {
			var roosters = $('#uurrooster div.roosters');
			if(roosters.length > 1) {
				roosters.each(function() {
					var $this = $(this);
					$this.hide();
					$this.prev().css({'cursor':'pointer'});
	 				$this.prev().click(function() {
						//roosters.hide();
						$this.slideToggle();
					});
				});
			}
		}
		
	/* ------------------------------------------------- */
	/* UURROOSTERS
	/* ------------------------------------------------- */
		if($('form.interesseproef-form').size() > 0) {
			$('form.interesseproef-form').validate();
		}
	
	/* ------------------------------------------------- */
	/* ROLLOVER ANIMATIONS
	/* ------------------------------------------------- */
		$('div.full').each(function() {
			var ro = $(this).find('div.roll-over');
			// SAVE INT DATA
			if(!$.data(ro.get(0), 'pos')) $.data(ro.get(0), 'pos', {top:ro.css('top'), height:ro.css('height')});
			// SET RO TO START POS
			ro.css('top', ro.css('height'));
			// ADD HANDLERS
			$(this).mouseover(function() { mouseOverAnimation(ro); });
			$(this).mouseout(function() { mouseOutAnimation(ro); });
			$(this).click(function() { window.location = $('a', this).attr('href'); });
			$(this).css({cursor:'pointer'});
		});
		
		function mouseOverAnimation(obj)
		{
			var y = $.data(obj.get(0), 'pos').top;
			obj.stop().animate({top:y},{queue:false,duration:100});
		}
		
		function mouseOutAnimation(obj)
		{
			var y = $.data(obj.get(0), 'pos').height;
			obj.stop().animate({top:y},{queue:false,duration:500});
		}
	
	/* ------------------------------------------------- */
	/* ROLLOVER INTERESSEPROEF
	/* ------------------------------------------------- */
		if($('#interesseproef').size() > 0) $('#interesseproef').css({cursor:'pointer'}).click(function() { window.location = $('a', this).attr('href'); });
		if($('#interesseproef').size() > 0) $('#interesseproef').hover(function() { $('div.body', this).css('background-position','-171px 0px'); }, function() { $('div.body', this).css('background-position','0px 0px'); });

	/* ------------------------------------------------- */
	/* CYCLE SLIDESHOWS
	/* ------------------------------------------------- */
		if($('div.slideshow').size() > 0) $('div.slideshow div').cycle();
		
	/* ------------------------------------------------- */
	/* GMAP INIT
	/* ------------------------------------------------- */
		if($('#gmap').size() > 0) initialize();
		
	/* ------------------------------------------------- */
	/* TOGGLE SLIDESHOW/GMAP
	/* ------------------------------------------------- */
		if($('div.toggle-routeplan').size() > 0)
		{
			$('div.toggle-routeplan a').click(function(e) {
				e.preventDefault();
				
				$('div.routeplan').toggle();
				$('div.slideshow').toggle();
				initialize();
				
				if($(this).hasClass('route')) {
					
					$(this).text('Toon het routeplan');
					$(this).removeClass('route');
					
				} else {
					
					initialize();
					$(this).text('Verberg het routeplan');
					$(this).addClass('route');
					
				}
			});
		}

	/* ------------------------------------------------- */
	/* ACTIVATE GPS NAVIGATION
	/* ------------------------------------------------- */
		if($('#start-route').size() > 0)
		{
			$('div.routelink input').focus(function() { if($(this).val() == 'Straat, Gemeente') $(this).val(''); })
			$('div.routelink input').blur(function() { if($(this).val() == '') $(this).val('Straat, Gemeente'); })
			$('#start-route').click(function(e) {
				e.preventDefault();
				if($('div.routelink input').val() != 'Mijn locatie' && $('div.routelink input').val() != '')
				{
					if(!$('div.routeplan').is(':visible')) {
						$('div.routeplan').show();
						$('div.slideshow').hide();
						initialize();
					}
					calcRoute();
				}
			});
		}

		
	/* ------------------------------------------------- */
	/* EQUAL COLUMN HEIGHTS
	/* ------------------------------------------------- */
		if($('div.scholen div#text').size() > 0)
		{
			var highestCol = Math.max($('div.scholen div#sidebar').height(),$('div.scholen div#text').height());
			$('div.scholen div#sidebar').height(highestCol);
			$('div.scholen div#text').height(highestCol);
			$('div.scholen div#infobar').height(highestCol+21);
		}
		
	/* ------------------------------------------------- */
	/* FEEDBACK UI
	/* ------------------------------------------------- */
		if($('#feedback').size() > 0) $('#feedback').click(function() { $(this).fadeOut('fast'); });
		
	/* ------------------------------------------------- */
	/* KALENDER EN NIEUWS
	/* ------------------------------------------------- */
		if($('div.kalender div.data h2 a').size() > 0)
		{
			$('div.kalender div.data').hover(function() {
				$(this).css('background','#D4E8F9');
				$(this).parent().find('div.datum').css('background','#D4E8F9');
			}, function () {
				$(this).css('background','#FFF');
				$(this).parent().find('div.datum').css('background','#FFF');
			}).click(function() {
				window.location = $('a', this).attr('href');
			});
		}
		
		if($('div.nieuws div.data h2 a').size() > 0)
		{
			$('div.nieuws div.data').hover(function() {
				$(this).css('background','#D4E8F9');
				$(this).parent().find('div.datum').css('background','#D4E8F9');
			}, function () {
				$(this).css('background','#FFF');
				$(this).parent().find('div.datum').css('background','#FFF');
			}).click(function() {
				window.location = $('a', this).attr('href');
			});
		}
	
	/* --------------------------------------------------------- */
	/* LIST NAVIGATION
	/* --------------------------------------------------------- */
		$('#results-list').listnav();
		$('#open-hoe-werkt').click(function (e) { e.preventDefault(); $('#uitleg').slideToggle(); })
		
		
	/* --------------------------------------------------------- */
	/* EQUAL UURROOSTERS TABLES
	/* --------------------------------------------------------- */
		if($('#uurrooster table').size() > 0) {
			tallest = 0;
			
			$('#uurrooster table').each(function() {
				if(tallest < $(this).find('tr').size()) {
					tallest = $(this).find('tr').size()
				}
			});
			
			$('#uurrooster table').each(function() {
				total = $(this).find('tr').size();
				if(tallest > total) {
					diff = tallest - total;
					for(i=0;i<diff;i++) {
						$(this).find('tr:last').before('<tr><td class="vakken">&nbsp;</td><td>&nbsp;</td></tr>');
					}
				}
			});
			
		}
		
	/* --------------------------------------------------------- */
	/* TABS FOR DETAIL
	/* --------------------------------------------------------- */
		//When page loads...
		$(".content").hide(); //Hide all content
		$("ul.tabs li:first").addClass("current").show(); //Activate first tab
		$(".content:first").show(); //Show first tab content

		//On Click Event
		$("ul.tabs li").click(function() {

			$("ul.tabs li").removeClass("current"); //Remove any "active" class
			$(this).addClass("current"); //Add "active" class to selected tab
			$(".content").hide(); //Hide all tab content

			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return false;
		});
		
	/* ------------------------------------------------- */
	/* JUMP MENU
	/* ------------------------------------------------- */
		$("#school-selectbox").change(function() {
			var val = $(this).val();
			if (val != '') {
				location.href = val;
			}
		});
		
		$("#gebied-selectbox").change(function() {
			var val = $(this).val();
			if (val != '') {
				location.href = val;
			}
		});
		
	/* ------------------------------------------------- */
	/* SCHOLEN SCROLLABLE
	/* ------------------------------------------------- */
		if($("div.scrollable").size() > 0) $("div.scrollable").scrollable({size:1,clickable:false}).autoscroll().circular();
		if($('#scholen').size() > 0) $('#scholen div.item a').each(function() { var a = $(this); $(this).parent().parent().css('cursor','pointer'); $(this).parent().parent().click(function() { window.location = a.attr('href'); })});
		
	/* --------------------------------------------------------- */
	/* INTERESSEPROEF PAGINATION
	/* --------------------------------------------------------- */
		// BAR CHART
		$("div.chart div em").each(function (i) {
			var capacity = Math.round($(this).text() * 37.4); // Divide by 350 to scale to desired widths.
			$(this).animate({'width':capacity}, 500);
		});
		
	/* ------------------------------------------------- */
	/* BLUR FOCUS INPUTS
	/* ------------------------------------------------- */
		$('input[type="text"]').each(function() {
			$(this).focus(function() { if($(this).val() == $(this).attr('alt')) $(this).val(''); });
			$(this).blur(function() { if($(this).val() == "") $(this).val($(this).attr('alt')); });
		});
});

/* --------------------------------------------------------- */
/* GMAP SHIT
/* --------------------------------------------------------- */
	var geocoder;
	var map;
	var directionsDisplay;
	var directionsService = new google.maps.DirectionsService();
	
	function initialize() {
		geocoder = new google.maps.Geocoder();
		directionsDisplay = new google.maps.DirectionsRenderer();
		var latlng = new google.maps.LatLng(51.04830113331224, 3.72711181640625);
		var myOptions = {
			zoom: 16,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById("gmap"), myOptions);
		if($('div.adres span').size() > 0) codeAddress($('div.adres span').text());
		directionsDisplay.setMap(map)
	}
	
	function codeAddress(address) {
	  if (geocoder) {
		geocoder.geocode( { 'address': address}, function(results, status) {
		  if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
				map: map, 
				position: results[0].geometry.location
			});
		  } else {
			alert("Geocode was not successful for the following reason: " + status);
		  }
		});
	  }
	}
	
	function calcRoute() {
		var start = $('div.routelink input').val();
		var end = $('div.adres span').text();
		var request = {
			origin:start, 
			destination:end,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		directionsService.route(request, function(result, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(result);
			}
		});
	}
