$(document).ready(function() {
	$imgs = $("#photo_stack img");
	$imgCount = $imgs.length;
	$curr_index = 0;
	$imgs.last().addClass('current');
	
	for(var i=1; i<=$imgs.length; i++) {
		$random_deg =  (Math.round((Math.random()*3)+1))
		$suff = '';
		if (i % 2 == 0) 
			$suff = 'neg';
		$imgs.eq(i-1).addClass('deg' + $random_deg + $suff);
	}
	
	$("#photo_stack")
	.delegate('img', 'click', function() {
		$this = $(this);
		if ($this.hasClass('current')) {
			$zi = $this.css('zIndex') - $imgCount;
			$this.addClass('animate');
			setTimeout(function() { $this.css('zIndex', $zi); }, 200);
			setTimeout(function() { $this.removeClass('animate'); }, 1000);
			$this.removeClass('current');
			if ($this.index() == 0) {
				$imgs.last().addClass('current');
			} else {
				$imgs.eq($this.index()-1).addClass('current');
			}
		}
	})
	.delegate('img', 'mouseenter', function() {
		if ($(this).hasClass('current')) {
			$(this).addClass('hover');
		}
	})
	.delegate('img', 'mouseleave', function() {
		$(this).removeClass('hover');
	});;
});

