$(function(){
	
	$('a.lightbox').lightBox();
	
	$('.content-scroller').jScrollPane({
		showArrows: true
	});
	
	$('.portfolio-category').click(function(){
		
		document.location = $(this).attr('href');
	});
	
	$('.window-close').click(function(){
		
		var $window = $(this).parents('.window');
		if ($(this).hasClass('full')) {
			$window.fadeOut();
			return false;
		}
			
		$window.fadeOut('normal', function(){
			if ($window.hasClass('window2'))
				$('#portfolio-item').fadeIn();
		});
		
		return false;
	});
	
	$('.gallery-item a').click(function(){
		
		var $a = $(this);
		$('#portfolio-item').fadeOut('normal', function(){
			var $block = $($a.attr('href'));
			$block.fadeIn();
			$block.find('a.as-lightbox').lightBox();
		});
		
		return false;
	});
	
	$('.gallery-previews-items a').click(function(){
		
		var $a = $(this);
		var $photo = $('.gallery-main-photo:visible').attr('data-current', $(this).attr('data-index')).attr('src', '/i/b.gif');
		setTimeout(function(){
			$photo.attr('src', $a.attr('href'));
		}, 500);

		$('.gallery-previews-items .active').hide();
		$(this).find('.active').show();
		
		return false;
	});
	
	$('.gallery-previews-prev, .gallery-previews-next').click(function(){
		
		var $preview = $(this).parents('.gallery-previews:visible');
		var $photos = $preview.prev('.gallery-photo:visible');
		var $preview_items = $preview.find('.gallery-previews-items');
		var index = prev = $('.gallery-main-photo:visible').attr('data-current');
		if (!index)
			index = prev = 0;
		
		if ($(this).hasClass('gallery-previews-prev'))
			index--;
		else
			index++;
		
		var count = $preview.find('a').length-1;
		if (index > count)
			index = count;
		if (index < 0)
			index = 0;
		
		$preview.find('.active').hide();
		var $current = $preview.find('a:eq('+index+')');
		$current.find('.active').show();
		
		var offset = $current.position().top;
		//console.log(prev, index);
		if (offset >= 432)
			$preview_items.animate({marginTop: '-=87'});
		else if (offset <= 0)
			$preview_items.animate({marginTop: '+=87'});
		
		if (prev == index)
			return false;
		
		$photos.find('a:eq('+prev+')').hide();
		$photos.find('a:eq('+index+')').show();
		$('.gallery-main-photo:visible').attr('data-current', index)
		
		/*var $photo = $('.gallery-main-photo:visible').attr('data-current', index).attr('src', '/i/b.gif');
		setTimeout(function(){
			$photo.attr('src', $current.attr('href'));
		}, 300);*/
		
		return false;
	});
});
