var Gallery = {
	buttons: [
			"button_gallery_previous",
            "button_gallery_next"
    ],
	setup: function() {
		Gallery.buttons.each(function(id) {
			$$("a[id^=" + id + "_]").each(function(element) {
				element.observe("click", this[("on_" + id + "_click").dasherize().camelize()]);
			});
		});
	}
};


/*******************************************************************************
 * 
 * Gallery functions
 * 
 ******************************************************************************/

function onButtonGalleryPreviousClick(event) {
	var galleryIndex = Event.element(event).up().id.sub(/^.*?_(\d+)$/, "#{1}");
	galleryNavigate(event, galleryIndex, -1);
	event.stop();
}

function onButtonGalleryNextClick(event) {
	var galleryIndex = Event.element(event).up().id.sub(/^.*?_(\d+)$/, "#{1}");
	galleryNavigate(event, galleryIndex, 1);
	event.stop();
}

function galleryNavigate(event, galleryIndex, direction) {
	var currentPage = parseInt($("gallery_page_current_" + galleryIndex).value);
	var totalPages = parseInt($("gallery_pages_total_" + galleryIndex).value);
	var newPage = currentPage + direction;
	
	if( newPage < 1 ) {
		newPage = 1;
	} else if( newPage > totalPages ) {
		newPage = totalPages;
	}
	
	if( newPage == currentPage ) {
		return;
	}
	
	currentPage = newPage;
	
	var totalImages = parseInt($("gallery_images_total_" + galleryIndex).value);
	var imagesPerPage = parseInt($("gallery_images_per_page_" + galleryIndex).value);
	
	var startIndex = (currentPage - 1) * imagesPerPage;
	var endIndex = startIndex + imagesPerPage;
	
	var index = 0;
	
	$("gallery_buffer_" + galleryIndex).select("div[id^=gallery_thumbnail_" + galleryIndex + "_] a").each(function(element) {
		element.writeAttribute("rel", "lightbox[gallery_" + galleryIndex + "] " + index++);
	});
	
	for( var i = startIndex, slot = 0; i < endIndex; i++, slot++ ) {
		if( i < totalImages ) {
			updateGallerySlot(event, galleryIndex, slot, i);
		} else {
			updateGallerySlot(event, galleryIndex, slot, -1);
		}
	}
	
	$("gallery_page_current_" + galleryIndex).value = currentPage;
	
	toggleNavigationButtons(event, galleryIndex, currentPage, totalPages);
	updateNavigationInfo(event, galleryIndex, currentPage, totalPages);
}

function toggleNavigationButtons(event, galleryIndex, currentPage, totalPages) {
	if( currentPage > 1 ) {
		$("button_gallery_previous_" + galleryIndex).setStyle({visibility: "visible"});
	} else {
		$("button_gallery_previous_" + galleryIndex).setStyle({visibility: "hidden"});
	}
	
	if( currentPage < totalPages ) {
		$("button_gallery_next_" + galleryIndex).setStyle({visibility: "visible"});
	} else {
		$("button_gallery_next_" + galleryIndex).setStyle({visibility: "hidden"});
	}
}

function updateNavigationInfo(event, galleryIndex, currentPage, totalPages) {
	if( $("gallery_nav_page_info_" + galleryIndex) ) {
		$("gallery_nav_page_info_" + galleryIndex).update(currentPage + " / " + totalPages);
	}
}

function updateGallerySlot(event, galleryIndex, slot, index) {
	if( $("gallery_slot_" + galleryIndex + "_" + slot) ) {
		var element = $("gallery_slot_" + galleryIndex + "_" + slot);
		
		if( index < 0 || !$("gallery_thumbnail_" + galleryIndex + "_" + index) ) {
			element.update("&nbsp;");
		} else {
			element.update($("gallery_thumbnail_" + galleryIndex + "_" + index).innerHTML);
			$("gallery_thumbnail_" + galleryIndex + "_" + index).down().writeAttribute("rel", "");
			
			var thumbnailImage = element.down().down();
			
			thumbnailImage.src = thumbnailImage.title;
		}
	}
}
