var i = 0;
var position = new Array();
var active_item = 0;
var item_count;
function move_image(item_index) {
	$('div.gallery img').removeClass('shadow');
	$('div.gallery div.image'+item_index).animate({left: 600, top: -220}, 500, function() {
		$(this).css('z-index', 1000+i).animate({left: position[item_index].left, top: position[item_index].top}, 500).find('img').addClass('shadow');
		i = i + 1;
	});
}
function cycle_images(item_index) {
	item_index = item_index + 1;
}
$(document).ready(function() {
	// add js class, include background image
	$('body').addClass('js').prepend('<img src="'+url+'/assets/img/bg.jpg" width="100%" height="100%" id="bg" />');
	
	// randomly rotate images
	$('img[class^="attachment"]').each(function(index) {
		var random_angle = Math.floor(Math.random() * 5);
		// alternate left and right turning
		if (index%2) {
			random_angle = -random_angle;
		};
		$(this).css({
			'-moz-transform': 'rotate('+(random_angle)+'deg)',
			'-webkit-transform': 'rotate('+(random_angle)+'deg)'
		});
		$(this).parent('a').hover(function() {
			$(this).find('img').css({
				'-moz-transform': 'rotate('+(random_angle)+'deg) scale(1.15)',
				'-webkit-transform': 'rotate('+(random_angle)+'deg) scale(1.15)'
			})
		}, function() {
			$(this).find('img').css({
				'-moz-transform': 'rotate('+(random_angle)+'deg)',
				'-webkit-transform': 'rotate('+(random_angle)+'deg)'
			})
		});
	});
	
	// build gallery
	$('div.gallery br').remove();
	item_count = $('div.gallery-item').length;
	$('div.gallery-item').each(function(index) {
		position[index] = $(this).position();
		var random_top = Math.floor(Math.random() * 21);
		var random_left = Math.floor(Math.random() * 21);
		$(this)
			.addClass('image'+index)
			.css('z-index', item_count - index)
			.find('img')
			.css({
				top: position[index].top + random_top,
				left: position[index].left + random_left
			})
			.each(function() {
				var imgwidth = $(this).width();
				var imgheight = $(this).height();
				if (imgwidth != imgheight) {
					if (imgwidth > imgheight) {
						var margin = (imgwidth - imgheight) / 2;
						$(this).css({
							'margin-top': margin,
							'margin-bottom': margin
						});
					} else {
						var margin = (imgheight - imgwidth) / 2;
						$(this).css({
							'margin-left': margin,
							'margin-right': margin
						});
					};
				};
			});
	});
	$('div.gallery img:first').addClass('shadow');
	if (item_count > 1) {
		$('div#wrapper').wrap('<div class="extra-wrapper"></div>');
		$('div.content').prepend('<div id="gallery-menu"><div id="gallery-nav"><div id="gallery-nav-prev" class="button"></div><div id="gallery-nav-next" class="button"></div></div><div id="gallery-menu-items"></div></div>');
		$('div.gallery-item').each(function(index) {
			$('div#gallery-menu-items').append('<div class="gallery-menu-item button image'+index+'"></div>');
		});
		var active_item = 0;
		$('div.gallery-menu-item').each(function(index) {
			$(this).click(function() {
				if ($(this).hasClass('active') != true) {
					$('div.gallery-menu-item').removeClass('active');
					active_item = index;
					move_image(index);
					$(this).addClass('active');
				};
			});
		}).eq(0).addClass('active');
		$('div#gallery-nav-prev').click(function() {
			active_item = active_item - 1;
			if (active_item < 0) {
				active_item = item_count - 1;
			};
			$('div.gallery-menu-item').removeClass('active');
			move_image(active_item);
			$('div.gallery-menu-item:eq('+active_item+')').addClass('active');
		});
		$('div#gallery-nav-next').click(function() {
			active_item = active_item + 1;
			if (active_item == item_count) {
				active_item = 0;
			};
			$('div.gallery-menu-item').removeClass('active');
			move_image(active_item);
			$('div.gallery-menu-item:eq('+active_item+')').addClass('active');
		});
	};
});
