if (typeof window.jQuery != 'undefined')
{
	$(function()
	{
		/**
		 * Hoofdfunctie
		 */		
		main = function() {
			/**
			 * Initialisatie
			 */
			$('#wrapper').css('overflow-x', 'hidden');
			$('#arrows, iframe').show();
			$('#container').css({
				'left': '-950px',
				'height': '100%',
				'width': $('#container > div').size() * 950,
			});
			$('.item').css({
				'height': '100%',
				'float': 'left',
				'clear': 'none'
			});
			$('.itemcontent').css({
				'position': 'absolute',
				'clear': 'none'
			});			
			
			/**
			 * Scroll naar hash-tag als deze in url zit
			 */
			if (window.location.hash) {
				$('#itemsList li a').each(function(i,n) {
					if (this.hash == window.location.hash) {
						scrollToItem(i);
					}
				});
			}
			
			/**
			 * Klikken op navigatie linkjes, scroll naar item
			 */
			$('#itemsList li a').each(function(i,n) {
				//var div = $('#container > div:eq(' + i + ')');
				var div = $('#container > div').eq(i);
				if ($(div).size()) {
					$(this).click(function() {
						scrollToItem(i);
						return false;
					});
				}
			});
			
			/**
			 * Klikken op logo, ga naar eerste werk
			 */
			$('#logo').click(function(){
				scrollToItem(1);
				return false;
			});
			
			/**
			 * Klikken op pijlen, ga naar vorige/volgende
			 */
			$('#arrows .left').click(function(){
				previousItem = getPreviousNextItem()[0];
				scrollToItem(previousItem);
				return false;
			});
			
			$('#arrows .right').click(function(){
				nextItem = getPreviousNextItem()[1];
				scrollToItem(nextItem);
				return false;
			});

			/**
			 * Vang pijltjes-toetsen af
			 */
			$(document).keydown(function(e) {
				switch(e.keyCode) {
					case 37: // Left
						$("#arrows .left").addClass('hover');
						previousItem = getPreviousNextItem()[0];
						scrollToItem(previousItem);
					break;
					
					case 38: // Up
						$('#wrapper').animate({ scrollTop: $('#wrapper').scrollTop()-500 }, {duration:300, queue:false});
						return false;
					break;
					
					case 39: // Right
						$("#arrows .right").addClass('hover');
						nextItem = getPreviousNextItem()[1];
						scrollToItem(nextItem);
					break;
					
					case 40: // Down
						$('#wrapper').animate({ scrollTop: $('#wrapper').scrollTop()+500 }, {duration:300, queue:false});
						return false;
					break;
				}
			});
			$(document).keyup(function(e) {
				$("#arrows a").removeClass('hover');
			});
		} 
		
		/**
		 * Functie om naar item toe te scrollen
		 */
		scrollToItem = function(item){
			if (item == getCurrent()) return false;
			
			$('#itemsList li a').removeClass('active');
			$('#itemsList li a').eq(item).addClass('active');
			
			$('#wrapper').animate({
				scrollTop: '0px'
			}, {
				duration: ($('#wrapper').scrollTop() / 5),
				complete: function() {
					$('#container').animate({
						left: (item*950*-1)
					}, {
						duration:300,
						queue:false
					});
				}
			});
		}
		
		/**
		 * Functie om vorige/volgende positie te berekenen
		 */
		getPreviousNextItem = function() {
			numberOfItems = $('#itemsList li a').size();
			currentItem = getCurrent();
			
			previousItem = currentItem - 1;
			if (previousItem < 0) previousItem = 0;
	
			nextItem = currentItem + 1;
			if (nextItem >= numberOfItems) nextItem = numberOfItems - 1;
			
			return new Array(previousItem, nextItem);
		}
		
		/**
		 * Functie om huidige positie te berekenen
		 */
		getCurrent = function() {
			currentItem = 1;
			
			$('#itemsList li a').each(function(i,n) {
				if ($(this).hasClass('active'))	currentItem = i;
			});
			
			return currentItem;
		}
		
		/**
	     * Methode om rel="external" om te zetten in target="_blank"
	     * Dit is nodig om te voldoen aan het XHTML-strict standaard
	     */
		initRelExternal = function(){
			$('a[rel=external]').addClass('external').attr('target', '_blank');
		}
		
		initRelExternal();
		main();
	});
}
