
    // cache nodig voor aggresieve garbage collectors
    var cache = [];

	/*
	 * Visitors app
	 */
	
	var visitorApp = (function() {
	
		return {
			
			init: function() {
			
				visitorApp.fixPositions();
				
				$('#visitors-app .error').hide();
				$('#visitors-app .city .address h2').hide();
				
				$('#visitors-app .city ol li').live('click', function() {
					index = $(this).index();
					$(this).addClass('active').siblings().removeClass('active');
					$(this).closest('.city').find('.address').eq(index).addClass('active').siblings().removeClass('active');
					return false;
				});
				
				$('#visitors-app form').live('submit', function() {
					var $context = $(this);
				
					if ($context.hasClass('step1')) {
						var zipcode = $context.find('input[class~=zip_numbers]').val() + $context.find('input[class~=zip_letters]').val();
						visitorApp.loadContent(zipcode, function() {
							$context.add($context.siblings()).animate({ left: '-='+$context.outerWidth() }, 800);
						});
					} else {
						$context.add($context.siblings()).animate({ left: '-='+$context.outerWidth() }, 800);
					}

					return false;
				});
				
				$('#visitors-app form .back').live('click', function() {
					var $context = $(this).closest('form');
					$context.add($context.siblings()).animate({ left: '+='+ $context.outerWidth() }, 800);
					return false;
				});

			},
			
			fixPositions: function() {

				$('#visitors-app form').each(function(i) {
					$(this).css({ left: $(this).outerWidth()*i });
				});
				
			},
			
			loadContent: function(zipcode, callback) {
				
				$.getJSON('ajax/post.php', { zipcode: zipcode }, function(data) {
					if (data.error == 'true') {
						$('#visitors-app .error').show();
					} else {
						$('#visitors-app form:gt(0)').remove();
						$(data.html).insertAfter('#visitors-app form:eq(0)');
	
						visitorApp.fixPositions();
					
						if (typeof callback == 'function') {
							callback.call(this);
						}
					}
					
				});
			
			}
		
		}
	
	})();

	$(document).ready(function(){
	
		/*$('.clickme').click(function() {
			href = $(this).find('a:first').attr('href');
			document.location = href;
			return false;
		}).css({ cursor: 'pointer' });*/
		
		/*
		 * Visitors app
		 */
		 	
		visitorApp.init();
		
		/*
		 * Print button
		 */
	
		
		$('#nav-meta .print').click(function() {
			window.print();
			return false;
		});
		
		
		/*
		 * Home carousel
		 */
		 
		if ($('#home-carousel').length) {
			var images = [];
			$('#home-carousel img').each(function() {
				images.push($(this).attr('data-src'));
			});

			if (images.length > 1) {
				
				$.loadImages(images, function() {
				
					$('#home-carousel img').each(function() {
						$(this).attr('src', $(this).attr('data-src')).show();
					});		
				

					$('#home-carousel img:gt(0)').hide();
					setInterval(function(){
						$('#home-carousel img:first-child').fadeOut(1000).next('img').fadeIn(1000)
						.end().appendTo('#home-carousel');
					}, 5000);
				});
			}
		}

		/*
		 * Fix for IE6 hovering
		 */
	
		$('#nav-main li').hover(function() {
			$(this).find('.sub').show();
		}, function() {
			$(this).find('.sub').hide();
		});
	
		
		/*
		 * Find as you type: algemeen
		 */
		
		$('#header .search input.text').autocomplete({
			source: 'ajax/findasyoutype.php',
			dataType: 'json'
		});
		
		/*
		 * News carousel homepage
		 */
		
		$('#ctas .cta .carousel').tinycarousel({
			pager: true,
			interval: true,
			intervaltime: 8000
		});
		
		
		/*
		 * Prefilled form fields
		 */
		
		$("input[value!=][type=text]:not(.post), textarea[value!=]:not(.post)").focus(function () { 
			if(!$(this).attr("startvalue")) {  
				$(this).attr("startvalue", $(this).val());
			} 
			if($(this).val() == $(this).attr("startvalue")) {
				$(this).val('');	
			}
			
			$(this).addClass('placeholder');
		}).blur(function () { 
			if($(this).val() == '') {
				$(this).val($(this).attr("startvalue"));
				$(this).removeClass('placeholder');
			}			
		});
		
		/*
		 * Google Maps
		 */
		
		if ($('#gmap_posten').length) {
			$('#gmap_posten').googleMaps({
				url: '/ajax/markers.php',
				latlng: [52.1326, 5.2913],
				icon: {
					url: 'img/maps_icon.png',
					size: [35,42],
					anchor: [18,42]
				},
				scrollwheel: false
			});
		}
		
		if ($('#gmap_post').length) {
			p_id = $('#post_id').text();
			var map = $('#gmap_post .map').googleMaps({
				url: '/ajax/markers.php?post_id=' + p_id,
				latlng: [52.1326, 5.2913],
				icon: {
					url: 'img/maps_icon.png',
					size: [35,42],
					anchor: [18,42]
				},
				scrollwheel: false
			});
			
			/**
			 *
			 * Zorg dat na fitBounds het zoomlevel gecorrigeerd wordt tot een maximum
			 * level om te voorkomen dat te ver ingezoomd wordt.			 			 
			 *
			 */
			var maxZoom = 15;
			var initialZoom = true;
			google.maps.event.addListener(map, 'zoom_changed', function() {
			    zoomChangeBoundsListener = 
			        google.maps.event.addListener(map, 'bounds_changed', function(event) {
			        	if (initialZoom && this.getZoom() > maxZoom) {
			        		this.setZoom(maxZoom);
			        		initialZoom = false;
			        	}
			        google.maps.event.removeListener(zoomChangeBoundsListener);
			    });
			});
			
		}
		
	});
	   
    // images kan een array zijn, een pad naar images of 1 image
    $.loadImages = function(images, callback) {	    
    
        // maak een array waar nodig
        if (!(images instanceof Array)) {
            images = [images];
        }
        
        var imagesLength = images.length;
        var loadedCounter = 0;
        
        for (var i = imagesLength; i--;) {
			var cacheImage = document.createElement('img');
			
			// onload voor het zetten van de image src ander gaat IE op z'n bek
            cacheImage.onload = function(){
                loadedCounter++;
                if (loadedCounter >= imagesLength) {
                    if ($.isFunction(callback)) {
                        callback();
                    }
                }
            }
            
            cacheImage.src = images[i];
            cache.push(cacheImage);
        }
    }
