var changingimage, imagepath, imageAt;

var initGallery = function() {
	$("#arrowright").show();
	changingimage = false;
	imagepath = $('.gallery').attr('path');
	imageAt = 1;
	$("#checknextgallery").error(function() { $("#arrowright").fadeOut(500); });

}

$(function() {
	if ($('.gallery').length) {
		initGallery();
				
		$("#arrowleft .arrow").live('click', function() {
			if (!changingimage) {
				changingimage = true;
				if (imageAt - 1 == 1) $("#arrowleft").fadeOut(500);	
				$("#galleryimage").fadeOut(500, function() {
					imageAt--;
					if ($("#arrowright").is(":hidden")) $("#arrowright").fadeIn(500);
					$("#galleryimage").attr("src", imagepath + imageAt + ".jpg");
					$("#galleryimage").fadeIn(500, function() { changingimage = false; });
				});
			}
			return false;
		});
		$("#arrowright > .arrow").live('click', function() {
			if (!changingimage) {
				changingimage = true;
				$("#galleryimage").fadeOut(500, function() {
					imageAt++;
					if ($("#arrowleft").is(":hidden")) $("#arrowleft").fadeIn(500);
					$("#galleryimage").attr("src", imagepath + imageAt + ".jpg");
					$("#checknextgallery").attr("src", imagepath + (imageAt + 1) + ".jpg");
					$("#galleryimage").fadeIn(500, function() { changingimage = false; });
				});
			}
			return false;
		});
	}
})
