
	$(document).ready(function()
	{
		Carousel.init('#carousel.home');	
	
		var content_height = $('.content-inner').outerHeight();
		if (content_height < 410) {
			$('.content-inner').css({ paddingBottom: 450-content_height });
		}
		
		// Initialize photo album carousel
		if ($('#photo-album').length)
		{
			// Carousel
			$('#photo-album').tinycarousel({ display: 5 });
			
			// Initialize photo album lightbox
			$('#photo-album li a').fancybox({
				margin: 0,
				centerOnScroll: true,
				titlePosition: 'inside',
				titleFormat: function(title, currentArray, currentIndex, currentOpts) {
					return  '<span><strong>'+(currentIndex + 1) + ' van ' + currentArray.length +'</strong></span>&nbsp;&nbsp;'+ title;
				}
			});
		}
		

		
		// Remove prefilled input values
		$("input.prefilled[value!=][type=text]").focus(function () { 
			if(!$(this).attr("startvalue")) {  
				$(this).attr("startvalue", $(this).val());
			} 
			if($(this).val() == $(this).attr("startvalue")) {
				$(this).val('');	
			}
			$(this).addClass('focus');
		}).blur(function () { 
			if($(this).val() == '') {
				$(this).val($(this).attr("startvalue"));
			}
			$(this).removeClass('focus');
		});
		
		// Submits form when changing 'number of results' option
		$('#shop-sort .num select').change(function() {
			$(this).closest('form').submit();
		});

		// Show or hide delivery address on ready
		if ($('.toggle_deliveryaddress').is(':checked')) {
			$('.deliveryaddress').show();
		} else {
			$('.deliveryaddress').hide();
		}
		
		// Show or hide delivery address on click
		$('.toggle_deliveryaddress').click(function() {
			if ($(this).is(':checked')) {
				$(this).closest('form').find('.deliveryaddress').show();
			} else {
				$(this).closest('form').find('.deliveryaddress').hide();
			}
		});

		// Enlarges the click area of some elements
		$('#cta .cta, .item img.vsl').hover(function() {
			$(this).css({ cursor: 'pointer' });
		}).click(function(e) {
			document.location = $(this).closest('.item, .cta').find('a').attr('href');
			e.stopPropagation();
			return false;
		});

		
		// Fix to make adding products to basket possible
		$('.item .price a ').click(function(e) {
			e.stopPropagation();
		});
		
		// Replace default submit buttons with some fancy stuff
		$('#shopping-nav .submit, .addtobasket .submit').find('input').hide().parent().find('.fancysubmit').show().click(function() {
			$(this).closest('form').submit();
			return false;
		});
		
		
		
		// Show the 'add to basket' button
		$('.addtobasket').removeClass('open').hover(function() {
			$(this).dequeue();
			$(this).addClass('open hover');			

			$(this).find('input,select').focus(function() {
				$(this).closest('form').addClass('hasfocus');
			}).blur(function() {
				$(this).closest('form').removeClass('hasfocus');
			});			
		}, function() {
			$(this).removeClass('hover');

			if (!$(this).hasClass('hasfocus')) {
				$(this).delay(600).queue(function() {
					$(this).removeClass('open');
					$(this).dequeue();
				});
			}
		}).focusout(function() {
			if (!$(this).hasClass('hover')) {
				$(this).removeClass('hasfocus open hover');
			}
		});
	});
	
	/*
	 * Carousel
	 */	 
	
	var Carousel = {
		
		init: function(obj) {
		
			if ($(obj).length == 0) return false;
			
			var images = [];
			$(obj).find('.overview img').each(function() {
				images.push($(this).attr('src'));
			});

			if (images.length <= 1) return false;

			$.loadImages(images, function() { Carousel.activate(obj); })

		},
		
		activate: function(obj) {
		
			$(obj).tinycarousel({
				pager: true,
				interval: true,
				intervaltime: 5000,
				loop: true
			});
		
		}
		
	}
	
	/*
	 * Image preloader
	 */

    // cache nodig voor aggresieve garbage collectors
    var cache = [];
    
    // 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);
        }
    }
