$(document).ready(function() {

	function scrollContainer(container) {
		var totalWidth = 0;
		var itemContainer = $('.itemContainer', container);
		var slideshow = container.children('.slideshow');
		var items = $('.itemContainer ul li', container);		
	
		//Calculate total container width
		items.each(function(){
			totalWidth += $(this).width();
		})

		//Set containerwidth to totalWidth
		itemContainer.css('width', totalWidth);

		//Calculate scrollDistance
		var diff = totalWidth - slideshow.width();
		var operator = Math.ceil(diff / 250);
		var scrollDistance = Math.ceil(diff / operator)

		//Controls
		var arrowLeft = container.children('.arrowLeft');
		var arrowRight = container.children('.arrowRight');

		//Check if arrows are needed
		if(diff < 0) {
			arrowLeft.addClass('invisible');
			arrowRight.addClass('invisible');
		}

		arrowLeft.click(function(){
			slideshow.animate({scrollLeft: slideshow.scrollLeft() - scrollDistance}, 350)
		});

		arrowRight.click(function(){
			slideshow.animate({scrollLeft: slideshow.scrollLeft() + scrollDistance}, 350)
		});
		
		//Disable text select when clicking arrows
		arrowLeft.disableTextSelect();
		arrowRight.disableTextSelect();
		items.disableTextSelect();
	}

	//Enable arrow navigation for al divs
	$(window).load(function(){scrollContainer($('#food'));});
	$(window).load(function(){scrollContainer($('#books'));});
	$(window).load(function(){scrollContainer($('#adv3rtising'));});
	$(window).load(function(){scrollContainer($('#more'));});

	//Add Fancybox	
	$(".img").fancybox({
		'hideOnContentClick': true,
		'overlayColor'	:	'#fff',
		'overlayOpacity':	1,
		'padding'		: 0,
		'autoScale'		: true,
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
                return '<div id="title"><p id="fancybox-title">' + (title.length ? title : '') + 
						'</p><p class="meta">Afbeelding ' + (currentIndex + 1) + ' / ' + 
						currentArray.length + '</p></div>';
	}
	});

	//YouTube
	$(".youtube").click(function() {
		$.fancybox({
			'hideOnContentClick': true,
			'overlayColor'	:	'#fff',
			'overlayOpacity':	1,
			'padding'		: 0,
			'autoScale'		: true,
			'title'			: this.title,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
				'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			},
			'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
                return '<div id="title"><p id="fancybox-title">' + (title.length ? title : '') + 
						'</p><p class="meta">Afbeelding ' + (currentIndex + 1) + ' / ' + 
						currentArray.length + '</p></div>';
			}
		});

		return false;
	});
	
	//Slideshow options
	$("a.group").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
	
});
