index = 1;


function update(offset)
{
	var $parent = $('#gallery .images');
	var $image;
	var data = JSON.parse($parent.find('textarea').val());
	var count = data.length;
	var value;

	switch (offset) {
		case -1: index = (index == 1 ? count : index - 1); break;
		case 1: index = (index == count ? 1 : index + 1); break;
	}

	value = data[index - 1];

	$parent.append("<img src=\"" + value + "\" />");
	$image = $parent.find('img:last');
	$image.fadeTo(0, 0);
	$image.load(function() {
		$image.fadeTo(750, 1, function() {
			$images = $parent.find('img');
			if ($images.length > 1) $images.eq(0).remove();
		});
	});
}


$().ready(function() {
	$('#gallery .control .button1, #gallery .control .button2').click(function() {
		update(($(this).attr('class') == 'button1' ? -1 : 1));
	});
});
