//PHOTO GALLERY
var transFlag = 0;
var transTimer;

jQuery(document).ready(gallery_large_main);

function gallery_large_main() {
    	jQuery("span.gallery_large_prev").click(function() { swapWithPrev("#" + jQuery(this).parent().parent().parent().parent().parent().parent().attr("id")); });
    	jQuery("span.gallery_large_next").click(function() { swapWithNext("#" + jQuery(this).parent().parent().parent().parent().parent().parent().attr("id")); });
    	jQuery(".gallery_large_thumb").each( function() {
		jQuery(this).click( function () {
            		swapPhoto(parseFloat(jQuery(this).find(".gallery_large_thumb_link").attr("name")),"#" + jQuery(this).parent().parent().parent().parent().parent().parent().attr("id"));
        	});
    	});

	jQuery("div[id^='gallery_large_box_']").each( function() {
		currentId = jQuery(this).attr("id");
		jQuery("#" + currentId + " table.gallery_large_thumb_box").css("width",jQuery("#" + currentId + " td.gallery_large_tb").children().length * 94);
	});

	jQuery('span.gallery_large_prev img.gallery_large_nav_arrow').mouseover( function () { jQuery(this).attr("src","/includes/gallery/images/arrow_l_64_h.gif"); });
	jQuery('span.gallery_large_prev img.gallery_large_nav_arrow').mouseout( function () { jQuery(this).attr("src","/includes/gallery/images/arrow_l_64.gif"); });
	jQuery('span.gallery_large_next img.gallery_large_nav_arrow').mouseover( function () { jQuery(this).attr("src","/includes/gallery/images/arrow_r_64_h.gif"); });
	jQuery('span.gallery_large_next img.gallery_large_nav_arrow').mouseout( function () { jQuery(this).attr("src","/includes/gallery/images/arrow_r_64.gif"); });
}

function swapWithNext(galleryId) {
	var nextPhoto;
	var currentPhoto = parseFloat(jQuery(galleryId + " span.gallery_large_currentnumber").html());
	if (currentPhoto == parseFloat(jQuery(galleryId + " span.gallery_large_maxnumber").html())) { nextPhoto = 1; }
	else { nextPhoto = currentPhoto + 1; };
	swapPhoto(nextPhoto,galleryId);
}

function swapWithPrev(galleryId) {
	var nextPhoto;
	var currentPhoto = parseFloat(jQuery(galleryId + " span.gallery_large_currentnumber").html());
	if (currentPhoto == 1) { nextPhoto = parseFloat(jQuery(galleryId + " span.gallery_large_maxnumber").html()); }
	else { nextPhoto = currentPhoto - 1; };
	swapPhoto(nextPhoto,galleryId);
}

function swapPhoto(nextPhoto,galleryId) {
	if (transFlag == 0) {
		transFlag = 1;
		var currentNumber = parseFloat(jQuery(galleryId + " span.gallery_large_currentnumber").html()) - 1;
		var nextNumber = nextPhoto - 1;
		jQuery(galleryId + " table.gallery_large_photo_box").eq(currentNumber).fadeOut(200);
		jQuery(galleryId + " table.gallery_large_thumb").eq(currentNumber).removeClass("gallery_large_thumb_active");

		var photoTimer = setTimeout(function() {
			jQuery(galleryId + " table.gallery_large_thumb").eq(nextNumber).addClass("gallery_large_thumb_active");
			jQuery(galleryId + " table.gallery_large_photo_box").eq(nextNumber).fadeIn(300);
			transTimer = setTimeout(function() { transFlag = 0; }, 300);
            		jQuery(galleryId + " span.gallery_large_currentnumber").html(nextPhoto);
            		jQuery(galleryId + " div.gallery_large_thumb_container").scrollTo("table.gallery_large_thumb:eq(" + nextNumber + ")", 800, {offset: -235});
		}, 300);
	};
}