
// Initialize Shadowbox
Shadowbox.init({
	players:['img', 'html'],
	animate: true,
	animateFade: false,
	counterType:'skip',
	overlayOpacity:0.5,
	initialHeight:0,
	initialWidth:0,
	autoDimensions:true,
	enableKeys:false,
	continuous: true,
	handleOversize: 'resize',
	onOpen:shadowBoxOpenHandler,
	onFinish:shadowBoxFinishHandler,
	onClose:shadowBoxCloseHandler
});
Pixastic.debug = false;

var shadowBoxCurrent = undefined;

// Run photo fade in's and desaturate
$(document).ready(function() {
	$('div.photo').each(function() {
		$(this).animate({opacity:0});
		// Add desaturate to image
		$(this).find('a.shadowbox:first img.desaturate-image').each(function(element) {
			Pixastic.process(this, "desaturate", {"average":true});
		});
		
		$(this).find('a.shadowbox img.original-image').animate({opacity:0}, 0);
		
		// Setup event handling
		$(this).mouseenter(function(event) {
			$(this).toggleClass('active').find('a.shadowbox img.original-image', this).animate({opacity:1}, 500);
		});
		
		$(this).mouseleave(function(event) {
			$(this).toggleClass('active').find('a.shadowbox img.original-image', this).animate({opacity:0}, 500);
		});
		
		$(this).click(function(event) {
			showDetailsContent = true;
			shadowBoxCurrent = this;
		});
		
		$(this).animate({opacity:1}, (1000 + Math.random() * 3000));
	});
	
	$('ul#bottom-nav li').each(function(element) {
		$(this).find('a').click(function(event) {
			showDetailsContent = false;
			event.preventDefault();
		});
		$(this).mouseenter(function(event) {
			var category = $(this).attr('class');
			$('div.photo:not(div.'+category+')').each(function() {
				$(this).animate({opacity:0.1}, 500);
			});
			$('div.photo.'+category).each(function() {
				$(this).animate({opacity:1}, 500);
			});
		});
		
		// To set delay on fade out of squares
		var delay = 5000;
		$(this).mouseleave(function(event) {
			var interval = setInterval(function() {
				$('div.photo').each(function() {
					$(this).animate({opacity:1}, 500);
				});
				clearInterval(interval);
			}, delay);
		});
	});
	
	$('ul#mainnav li a#about-menu-item').click(function(event) {
		event.preventDefault();
		shadowBoxCurrent = undefined;
		Shadowbox.open({
			content:'',
			player:'html',
			title:'About'
		});
	});
	$('ul#mainnav li a#contact-menu-item').click(function(event) {
		event.preventDefault();
		shadowBoxCurrent = undefined;
		Shadowbox.open({
			content:'',
			player:'html',
			title:'Contact'
		});
	});
});

function shadowBoxOpenHandler() {
}

function shadowBoxFinishHandler() {
	if(shadowBoxCurrent) {
		$("div#shadowbox-detail-content").html($(shadowBoxCurrent).find('div.post-body').html()).slideDown().slideDown('fast');
		$('img#sb-content').click(function() {
			Shadowbox.change(Shadowbox.current + 1);
		});
	}
}

function shadowBoxCloseHandler() {
	if(shadowBoxCurrent) {
		$("div#shadowbox-detail-content").slideUp('fast');
	}
	shadowBoxCurrent = undefined;
}