function init()
{
	// fade the overlay logo out after 2s
	setTimeout(function(){
		$('#content-home-overlay').fadeOut(2000);
	},2000);
	
	// Add event handler to the logo
	$('#logo').click(function(){
		backToHome();
		return false;
	});
	
	$('#navigation-categories a').click(function(){
		loadGallery( $(this).attr('rel') );
		return false;
	});
	
	// Add event handlers to page anchors
	$('#navigation-pages li a').click(function(){
		loadPage(this);
		return false;
	});
}

function backToHome()
{
	// Slide to left
	$('#content-wrap').animate({
		'marginLeft':'0px'
	},300,function(){
		$('#content-wrap>div').removeClass('active');
		$('#content-wrap>div#content-home').addClass('active');
	});
	// Hide the logo
	if(! $('#contact').hasClass('home') )
	{
		$('#contact').fadeOut(200,function(){
			$(this).addClass('home').fadeIn(200);
		});
	}
	// Hide project info
	$('#project-information').fadeOut(200);
	// Animate the arrow
	$('#navigation').animate({backgroundPosition:'20px 0px'},200);
	// Animate the tooltip
	if( $('#navigation-tooltip').text() != 'Selecteer een categorie' )
	{
		$('#navigation-tooltip').animate({textIndent:'-332px'},200,function(){
			$(this).text('Selecteer een categorie').animate({textIndent:'19px'},200);
		});
	}
}

function loadPage(element){
	$.ajax({
		cache: true,
		url:	 $(element).attr('href'),
		type:	'GET',
		data:	'ajax=1&action=page',
		success: function(data){
			$('#project-information').fadeOut(200);
			$('#content-page-contents').fadeOut(200,function(){							
				$('#content-page-contents').fadeIn(200);
				$('#content-page').addClass('active');


                $('#content-page-contents-wrap').html(data);
				$('#content-page-arrows .arrow.right').hide();
				$('#content-page-arrows .arrow.left').click(function(){
						if( ! $('#content-page-contents-wrap').is(':animated') )
						{					
							$('#content-page-arrows .arrow.right').fadeIn(200);
							if( parseInt( $('#content-page-contents-wrap').css('marginLeft') ) < 0 )
							{
								$('#content-page-contents-wrap').animate({
									'marginLeft'	:	'+=949px'
								},300);
							}
							else
							{
								backToHome();
							}
						}
					});

				/*$('#content-page-contents-wrap').html(data).columnize({
					width: 454,
					height: 310,
					doneFunc: function(){
						// Add padding
						$('#content-page-contents-wrap .column').each(function(){
							$(this).css({
								'width'			: '444px',//($(this).width() - 30) +'px',
								'paddingLeft'	: '15px',
								'paddingRight'	: '15px'
							});
							$('#content-page').removeClass('active');
						});
						$('#content-page-contents-wrap').animate({'marginLeft':'0px'},200);
						
						// Initialize arrows
						$('#content-page-arrows .arrow.right').hide();
						if( $('#content-page-contents-wrap .column').length > 2 ) // more than 2 columns?
						{
							$('#content-page-arrows .arrow.right').show(); // show the right arrow
						}
						
						$('#content-page-arrows .arrow.right').click(function(){ // show the next two columns				
							if( ! $('#content-page-contents-wrap').is(':animated') )
							{	
			
								var cols = $('#content-page-contents-wrap .column').length;
									cols = Math.round(cols/2);
								var current = $('#content-page-contents-wrap').css('marginLeft');
									current = -parseInt(current)/949;
															
								if(cols == current+2)
								{
									$('#content-page-arrows .arrow.right').fadeOut(200);
								}
							
								$('#content-page-contents-wrap').animate({
									'marginLeft'	:	'-=949px'
								},300);
							}
						});
						
						$('#content-page-arrows .arrow.left').click(function(){
							if( ! $('#content-page-contents-wrap').is(':animated') )
							{					
								$('#content-page-arrows .arrow.right').fadeIn(200);
								if( parseInt( $('#content-page-contents-wrap').css('marginLeft') ) < 0 )
								{
									$('#content-page-contents-wrap').animate({
										'marginLeft'	:	'+=949px'
									},300);
								}
								else
								{
									backToHome();
								}
							}
						});
						
						
					}
				});*/
				if( $('#content-project').hasClass('active') )
				{
					$('#content-page').addClass('active');
					$('#content-gallery').removeClass('active');
					$('#content-wrap').css('marginLeft','-1914px').animate({ 'marginLeft':'-957px' },300, stylePage);
				}
				else if( $('#content-gallery').hasClass('active') )
				{
					$('#content-page').addClass('active');
					$('#content-wrap').css('marginLeft','-1914px').animate({ 'marginLeft':'-957px' },300, stylePage);
				}
				else
				{
					stylePage();
				}						
				
				function stylePage()
				{
					$('#content-wrap>div#content-gallery, #content-wrap>div#content-project').removeClass('active');
					// Animate the arrow
					$('#navigation').animate({backgroundPosition:'20px 0px'},200);
					// Animate the tooltip
					if( $('#navigation-tooltip').text() != 'Selecteer een categorie' )
					{
						$('#navigation-tooltip').animate({textIndent:'-332px'},200,function(){
							$(this).text('Selecteer een categorie').animate({textIndent:'19px'},200);
						});
					}
					// Slide the wrapper
					$('#content-page').addClass('active');
					$('#content-wrap').animate({
						'marginLeft':'-957px'
					},300);
					// Animate the logo
					if($('#contact').hasClass('home'))
					{
						$('#contact').fadeOut(200,function(){
							$(this).removeClass('home').fadeIn(200);
						});
					}
				}
			});
			
			
		}
	});
}

function loadGallery(id)
{
	var url = $('#navigation-pages li:first-child a').attr('href');
	$.ajax({
		url: 	url,
		type:	'GET',
		data:	'ajax=1&action=gallery&gallery_id=' + id,
		success: function(data)
		{
			$('#navigation-tooltip').unbind('click');		
			$('#project-information').fadeOut(400);
			$('#content-gallery').html(data);
			// Play IE6 safe
			$('.content-gallery-thumbnails-slide:first-child').addClass('first');
			$('.content-gallery-thumbnails-slide:last-child').addClass('last');
			
			$('#content-gallery-projects').hide('slide',{direction:'down'},200);
			
			$('#content-gallery-thumbnails a').hover(function(){
				$(this).find('img.color').fadeIn(200);
				$('#content-gallery-projects li a[rel="' + $(this).attr('rel') + '"]').addClass('active');
			},function(){
				$(this).find('img.color').fadeOut(200);
				$('#content-gallery-projects li a[rel="' + $(this).attr('rel') + '"]').removeClass('active');
			});
			
			$('#content-gallery-projects li a').hover(function(){
				var prj = $('#content-gallery-thumbnails a[rel="' + $(this).attr('rel') + '"]');
				var index = $('div.content-gallery-thumbnails-slide > a:not(.more)').index( prj );
					index = index + 1; // array starts with 0
				var slidenum = Math.ceil(index/15);
				
				$('#content-gallery-thumbnails-wrap').stop().animate({
					'marginLeft'	: '-' + (slidenum-1)*634 + 'px'
				},300);
//					alert((slidenum-1)*634)
				
				prj.find('img.color').fadeIn(200);		
			},function(){
				$('#content-gallery-thumbnails a[rel="' + $(this).attr('rel') + '"] img.color').fadeOut(200);
			});
			
			$('#content-gallery-projects li a, #content-gallery-thumbnails a').click(function(){
				loadProject( $(this).attr('rel') );
				return false;
			});
			
			$('#content-gallery-thumbnails a.more span').css('opacity',0.7).click(function(){
				
				if($(this).hasClass('right'))
				{
					$('#content-gallery-thumbnails-wrap').animate({
						'marginLeft'	: '-=634px'
					},300);
				}
				else if($(this).hasClass('left'))
				{
					$('#content-gallery-thumbnails-wrap').animate({
						'marginLeft'	: '+=634px'
					},300);
				}
			}).hover(function(){
				$(this).stop().fadeTo(100,1);
			},function(){
				$(this).stop().fadeTo(100,.7);
			});
			
			if( $('#content-wrap>div#content-page').hasClass('active') )
			{
				$('#content-wrap>div#content-gallery').addClass('active');
				
				$('#content-wrap').animate({
					'marginLeft':'-1914px'
				},300,function(){
					$('#content-wrap>div#content-page').removeClass('active');
					$('#content-wrap').css('marginLeft','-957px');
					styleGallery();
				});
			}
			else
			{
				styleGallery();
			}
			
			function styleGallery()
			{				
				$('#content-wrap>div#content-gallery').addClass('active');
				$('#content-wrap').animate({
					'marginLeft':'-957px'
				},300,function(){
					$('#content-gallery-projects').show('slide',{direction:'down'},200);
					$('#content-project').removeClass('active');
				});
				// Animate the navigation
				$('#navigation').animate({backgroundPosition:'20px -20px'},200,function(){
					$('#content-gallery').animate({backgroundPosition:'20px 411px'},200);
				});
				// Animate the tooltip
				$('#navigation-tooltip').animate({textIndent:'-332px'},200,function(){
					$(this).text('Selecteer een project').animate({textIndent:'19px'},200);
				});
				// Animate the logo
				if($('#contact').hasClass('home'))
				{
					$('#contact').fadeOut(200,function(){
						$(this).removeClass('home').fadeIn(200);
					});
				}
			}				
		}
	});
}

function loadProject(id)
{
	var url = $('#navigation-pages li:first-child a').attr('href');
	$.ajax({
		url: 	url,
		type:	'GET',
		data:	'ajax=1&action=project&project_id=' + id,
		success: function(data){
			var project = data.split('~~~');
			
			$('#project-information').html(project[1]).fadeIn(400);
			
			$('#content-project-pictures').html(project[0]);
			
			if( $('#content-project-pictures>li').length >= 2 )
			{
				$('#content-project-arrows').show();
				$('#content-project-pictures').cycle({
					fx:			'scrollHorz',
				    speed:		'medium', 
				    timeout:	0, 
				    fit:		1,
				    pager:		'#project-information-media-cyclepager',
				    next:		'#content-project-arrows .arrow.right',
				    prev:		'#content-project-arrows .arrow.left',
				    before:		function(){
				    	$('#content-project-caption').slideUp(100);
				    },
			        after:		function(){
			        	if($('img',this).attr('alt').length > 0)
			        		$('#content-project-caption').html( $('img',this).attr('alt') ).slideDown(100);
			        }
				});
			}
			else
			{
				if($('#content-project-pictures>li>img').attr('alt').length > 0)
					$('#content-project-caption').html( $('#content-project-pictures>li>img').attr('alt') ).show();
				$('#content-project-arrows').hide();
				$('#project-information-media>h3:first-child, #project-information-media-cyclepager, ').remove();
			}				
			$('#content-project').addClass('active')
			$('#content-wrap').animate({
				'marginLeft':'-1914px'
			},300);
			
			if($('#project-information-media a[target="_blank"]').length > 0)
			{
				$('#project-information-media a[target="_blank"]').prettyPhoto();
			}
			
			$('#navigation-tooltip').unbind('click').click(function(){
				if( $('#content-project').hasClass('active') )
				{
					$('#content-gallery-projects').hide();
					$('#content-wrap').animate({
						'marginLeft':'-957px'
					},300,function(){
						$('#content-gallery-projects').show('slide',{direction:'down'},200);
						$('#content-project').removeClass('active');
						$('#project-information').fadeOut(400);
					});
				}
			});
			
		}
	});
}
	
$(document).ready(init);

