$(document).ready(function() {
	// Work-around function voor .css('background-position') in IE
	(function($) {
		jQuery.fn.backgroundPosition = function() {
			var p = $(this).css('background-position');
			if(typeof(p) === 'undefined') return $(this).css('background-position-x') + ' ' + $(this).css('background-position-y');
			else return p;
		};
	})(jQuery);
	
	var multiple_top=false; 
	var multiple_bottom=false;
	var multiple_leftbottom=false;
	
	// Slideshow/thumbs
	// Slideshow wordt gestart als er meer dan 1 grote foto is en het niet om een projectpagina gaat (waar je via de thumbnails de foto's kunt bekijken)
	if( $('#large_photo>img').length > 1 ) {
		multiple_top=true;
		var current_photo = 0;
		var photo = $('#large_photo>img').css({'opacity':0});
		$(photo[0]).css({'opacity':1});

		if ( $('#thumbs').length == 0 ) {
			var pe_slideshow1 = function() {
				
				next_photo = (current_photo+1 < photo.length) ? current_photo+1 : 0;
				$(photo[current_photo]).animate({'opacity':0}, 2000);
				$(photo[next_photo]).animate({'opacity':1}, 2000);
				current_photo = next_photo;
				
				if(multiple_leftbottom)
				{
					setTimeout(pe_slideshow3, 5000);
				}
				else if(multiple_bottom)
				{
					setTimeout(pe_slideshow2, 5000);
				}
				else
				{
					setTimeout(pe_slideshow1, 5000);
				}
			}
			setTimeout(pe_slideshow1, 5000);
		} else {
			$('#thumbs>ul>li>a').bind('click', function(event) {
				event.preventDefault();
				if ( $(this).parent('li').hasClass('active') == false ) {
					$('#thumbs>ul>li').removeClass('active');
					$(photo).each(function() {
						if ( $(this).css('opacity') > 0 ) {
							$(this).animate({'opacity':0}, 500);
						}
					});
					current_photo = $(this).parent('li').addClass('active').index();
					$(photo[current_photo]).animate({'opacity':1}, 500);
				}
			});
		}
	}
	
	if( $('#smaller_photo>img').length > 1 ) 
	{
		multiple_leftbottom=true;
		var current_photo3 = 0;
		var photo3 = $('#smaller_photo>img').css({'opacity':0});
		$(photo3[0]).css({'opacity':1});

		var pe_slideshow3 = function() 
		{
			next_photo = (current_photo3+1 < photo3.length) ? current_photo3+1 : 0;
			$(photo3[current_photo3]).animate({'opacity':0}, 2000);
			$(photo3[next_photo]).animate({'opacity':1}, 2000);
			current_photo3 = next_photo;
			
			if(multiple_bottom)
			{
				setTimeout(pe_slideshow2, 5000);
			}
			else if(multiple_top)
			{
				setTimeout(pe_slideshow1, 5000);
			}
			else
			{
				setTimeout(pe_slideshow3, 5000);
			}
		}
			
		if(!multiple_top&&!multiple_bottom)
		{
			setTimeout(pe_slideshow3, 5000);
		}
	}

	if( $('#small_photo>img').length > 1 ) {
		multiple_bottom=true;
		var current_photo2 = 0;
		var photo2 = $('#small_photo>img').css({'opacity':0});
		$(photo2[0]).css({'opacity':1});
		
		var pe_slideshow2 = function() 
		{
			next_photo = (current_photo2+1 < photo2.length) ? current_photo2+1 : 0;
			$(photo2[current_photo2]).animate({'opacity':0}, 2000);
			$(photo2[next_photo]).animate({'opacity':1}, 2000);
			current_photo2 = next_photo;
			
			if(multiple_top)
			{
				setTimeout(pe_slideshow1, 5000);
			}
			else if(multiple_leftbottom)
			{
				setTimeout(pe_slideshow3, 5000);
			}
			else
			{
				setTimeout(pe_slideshow2, 5000);
			}
		}
		if(!multiple_top&&!multiple_leftbottom)
		{
			setTimeout(pe_slideshow2, 5000);
		}
	}
	
	$('ul#nav>li>a').bind('click', function(event) {
		if ( $(this).siblings('div.subnav_container').length == 1 ) {
			event.preventDefault();
			if ($(this).next('div.subnav_container').css('display') == 'none') {
				hide_subnav();
				$(this).next('div.subnav_container').css({'display':'block'}).find('div.subnav_inner').css({'opacity':0, 'top':'-30px'}).animate({'opacity':1, top:0}, 750);

				var bg_pos = '-400px ' + $(this).backgroundPosition().split(' ')[1];
				$(this).css({'background-position': bg_pos})
			}
		}
	});
});

function hide_subnav() {
	$('ul#nav>li>div.subnav_container').each(function() {
		if ($(this).css('display') == 'block') {
			$(this).find('div.subnav_inner').animate({'opacity':0, 'top':'-30px'}, 250, function() {
				$(this).parent('div.subnav_container').css({'display':'none'});
			});
		}
	});

	$('ul#nav>li>a').each(function() {
//		alert( $(this).css('background-position-x') );
		var bg_pos = '0px ' + $(this).backgroundPosition().split(' ')[1];
		$(this).css({'background-position': bg_pos})
	});
}
