jQuery(document).ready(function($){
	
	// Photo selection
	$('.photo-overview li').bind({
		'click': function() {
			var angle = $(this).prevAll().length + 1;
			
			$('.photo-overview li').removeClass('selected');
			$('.photo-set').each(function(){
				$('.zoom img', this).attr('src', $('.photo-overview li:nth-child(' + angle + ') a', this).attr('href'));
				$('.photo-overview li:nth-child(' + angle + ')', this).addClass('selected');
			});
			
			return false;
		},
		'mouseover': function() {
 
			var angle = $(this).prevAll().length + 1;
		
			$('.photo-set').each(function(){
				this.tmp = $('.photo-overview li:nth-child(' + angle + ') a', this).attr('href');
				});
			
			$('.photo-set').each(function(){
				$('.zoom img', this).attr('src', this.tmp);
				
				});
			
			$('.photo-set').each(function(){
				$('.photo-overview li:nth-child(' + angle + ')', this).addClass('hover');
				});
			
			
			return false;
			
		},
		'mouseout': function (event) {
			
			if(event.relatedTarget.nodeName == 'A' || event.relatedTarget.nodeName == 'LI' || event.relatedTarget.nodeName == 'IMG') return ;

			var angle = $('li.selected', $(this).closest('.photo-overview')).prevAll().length + 1;

			$('.photo-overview li').removeClass('hover');
			$('.photo-set').each(function(){
				$('.zoom img', this).attr('src', $('.photo-overview li:nth-child(' + angle + ') a', this).attr('href'));
				});
			
			return false;  
			
		 	}
	});
	
	// Photo set selection
	$('.photo-set:not(:first-child):not(:last-child)').hide();

	if ($('.photo-set').length > 2)
	{
		$('.sets').addClass('set-select');

		$('.set-select').bind({
			'mouseenter': function(){$('ol', this).show('fast');},
			'mouseleave': function(){$('ol', this).hide('fast');}
		});
	
		$('.set-select ol li a').click(function() {
			$(this).closest('.set-select').mouseleave();
	
			var oldSet = $(this).closest('.photo-set');
			var newSet = $($(this).attr('href'));
	
			if (oldSet.attr('id') != newSet.attr('id')) {
				if (oldSet.prevAll(':visible').length) {
					oldSet.after(newSet);
				} else {
					oldSet.before(newSet);
				}
	
				if (newSet.is(':hidden')) {
					oldSet.hide();
					newSet.show();
				}
			}
			return false;
		});
	}


	// Updating fancybox zoom images
	$('.fancybox').click(function(){
		$('.photo-set:visible .photo-overview li.selected').each(function(index) {
			var set = $(this).closest('.photo-set');
			var angle = $(this).prevAll().length + 1;
			
			$('#full-size-photos .wrapper:nth-child(' + (index + 1) + ') .set-title')
				.replaceWith($('.set-title', set).clone());
			$('#full-size-photos .wrapper:nth-child(' + (index + 1) + ') img')
				.replaceWith($('.full-size-container img:nth-child(' + angle + ')', set).clone());
		});
	});


	// Video selection
	$('.video-shadow .video-container:gt(1)').hide();
	$('#nav-video li:first-child').addClass('selected');
	
	$('#nav-video li a').click(function(){
		$('.video-shadow .video-container').hide();
		$($(this).attr('href')).show();
		$('#nav-video li').removeClass('selected');
		$(this).parent().addClass('selected');
		
		return false;
	});


	// Fancybox
	$(".fancybox").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,
		'overlayOpacity': 0.85,
		'overlayColor'	: '#000000',
		'padding'		: 0
	});
});

