
function galleryNext(elem, i) {
	var items = $(elem).parent().children(".gallery_item");
	var max_index = parseInt(items.length) - 1;
	var next;
	var last_elem;
	
	items.each(function() {
		if (this.style.display != 'none') {
			last_elem = this;
			
			if ((parseInt(this.id) + i) < 0)
				next = max_index; 
			else if ((parseInt(this.id) + i) > max_index)
				next = 0;
			else	
				next = parseInt(this.id) + i;
		}
	});
	
	$(last_elem).fadeOut(200,function () {
		items.parent().find('#'+next).fadeIn(200);
	});
}

function galleryThumbsNext(elem, i, amount) {
	var items = $(elem).parent().parent().children(".gallery_thumb");
	var max_index = parseInt(items.length) - 1;
	var next;
	var last_elem;
	i = i * amount;

	items.each(function() {
		if (next == undefined) {
			if (this.style.display != 'none') {
				if ((parseInt(this.id) + i) < 0) {
					next = max_index + 1 - ((max_index + 1) % i); 
				}
				else if ((parseInt(this.id) + i) > max_index) {
					next = 0;
				}
				else {
					next = parseInt(this.id) + i;
				}
			}
		}
	});
	
	$(elem).parent().parent().children(".gallery_thumb:visible").fadeOut(200,function () {
		for (j=next;j<(amount + next);j++) {
			items.parent().find('#'+j).fadeIn(200);
		}
	});
}

function galleryShow(elem) {
	var items = $(elem).parents('#thumbs').siblings('#display').children('.gallery_item');
	var max_index = parseInt(items.length) - 1;
	var next = elem.id
	var last_elem;
	
	items.each(function() {
		if (this.style.display != 'none') {
			last_elem = this;
		}
	});
	
	$(last_elem).fadeOut(200,function () {
		items.parent().find('#'+next).fadeIn(200);
	});
}

function galleryShowField(field_name) {
	if ($('.gf_'+field_name).length > 0) {
		$('.gallery_item').fadeOut(200);
		$('.gf_'+field_name+':first').fadeIn(200);
	}
}


