function listingPagination() {
	
	//PAGINATION FOR THE GALLERY LISTING	
	$("#twentytwo_galleryList").each(function() {
		
		var totalEvents = $(this).find("ul li").length;
		var listContainer = $(this).find("ul");
		var eventPages = Math.ceil(totalEvents / 6);
		
		if ( eventPages > 1 ) {
			var currentPage = 0;
			
			var navigationContainer = $(this).find(".galleryList_navigation"); //FIND THE NECESSARY ELEMENTS
			listContainer.find("li").hide();
			for (var x = 0; x < 0 + 6; x++) {
				listContainer.children("li:eq("+x+")").show();
			}
			navigationContainer.append('<a href="#" class="galleryList_back"></a>'); //ADD BACK BUTTON
			navigationContainer.append("<ul></ul>");
			var navigationPages = navigationContainer.find("ul");
			for ( var i = 0; i < eventPages; i++ ) {
				navigationPages.append("<li><a href='#'>"+i+"</a></li>"); //CREATE PAGE CIRCLES
			}
			navigationContainer.append('<a href="#" class="galleryList_next"></a>'); //ADD NEXT BUTTON
			
			
			//PREV BUTTON FUNCTION
			navigationContainer.find(".galleryList_back").click(function() {
				if ( currentPage != 0 ) {
					navigationContainer.find("a.current").removeClass("current");
					var eventIndex = ((currentPage)*6);
					var fadeoutContainer;
					for (var x = eventIndex; x < eventIndex + 6; x++) {
						if (fadeoutContainer == null) {
							fadeoutContainer = listContainer.children("li:eq("+x+")");
						} else {
							fadeoutContainer = fadeoutContainer.add(listContainer.children("li:eq("+x+")"));
						}
					}
					fadeoutContainer.fadeOut(function() {
						var fadeinContainer;
						for (var y = eventIndex-6; y < eventIndex; y++) {
							if ( fadeinContainer == null ) {
								fadeinContainer = listContainer.children("li:eq("+y+")");
							} else {
								fadeinContainer = fadeinContainer.add(listContainer.children("li:eq("+y+")"));
							}
						}
						fadeinContainer.fadeIn();
					});
					currentPage--;
					navigationContainer.find("li:eq("+currentPage+") a").addClass("current");
				}
				return false;
			});
			
			//NEXT BUTTON FUNCTION
			navigationContainer.find(".galleryList_next").click(function() {
				if ( currentPage != eventPages-1 ) {
					navigationContainer.find("a.current").removeClass("current");
					var eventIndex = ((currentPage)*6);
					var fadeoutContainer;
					for (var x = eventIndex; x < eventIndex + 6; x++) {
						if (fadeoutContainer == null) {
							fadeoutContainer = listContainer.children("li:eq("+x+")");
						} else {
							fadeoutContainer = fadeoutContainer.add(listContainer.children("li:eq("+x+")"));
						}
					}
					fadeoutContainer.fadeOut(function() {
						var fadeinContainer;
						for (var y = eventIndex+6; y < eventIndex + 12; y++) {
							if ( fadeinContainer == null ) {
								fadeinContainer = listContainer.children("li:eq("+y+")");
							} else {
								fadeinContainer = fadeinContainer.add(listContainer.children("li:eq("+y+")"));
							}
						}
						fadeinContainer.fadeIn();
					});
					currentPage++;
					navigationContainer.find("li:eq("+currentPage+") a").addClass("current");
				}
				return false;
			});
			
			navigationContainer.find("li:eq(0) a").addClass("current");
			navigationContainer.find("li a").click(function() {
					var eventIndex = ((currentPage)*6);
					var newPage = $(this).text()*1;
					var fadeoutContainer;
					if ( currentPage != newPage ) {
						navigationContainer.find("a.current").removeClass("current");
						for (var x = eventIndex; x < eventIndex + 6; x++) {
							if (fadeoutContainer == null) {
								fadeoutContainer = listContainer.children("li:eq("+x+")");
							} else {
								fadeoutContainer = fadeoutContainer.add(listContainer.children("li:eq("+x+")"));
							}
						}
						fadeoutContainer.fadeOut(function() {
							var fadeinContainer;
							eventIndex = newPage*6;
							for (var y = eventIndex; y < eventIndex + 6; y++) {
								if ( fadeinContainer == null ) {
									fadeinContainer = listContainer.children("li:eq("+y+")");
								} else {
									fadeinContainer = fadeinContainer.add(listContainer.children("li:eq("+y+")"));
								}
							}
							fadeinContainer.fadeIn();
						});
						currentPage = newPage;
						$(this).addClass("current");
					}
				return false;
			});
			
		}
	});
}
function galleryStartup() {
	
$("#twentytwo_galleryModal .galleryModal_imagedisplay .preloadMask").css("opacity", 1);
	
	var totalImages = $("#twentytwo_galleryModal ul.galleryModal_thumbnailList li").length;
	var thumbpages = totalImages / 4;
	
	if ( thumbpages > 1 ) {
		$("#twentytwo_galleryModal .galleryModal_pagination").each(function() {
			for ( var i = 0; i < thumbpages; i++ ) {
				$(this).append("<li><a href='#'>"+(i+1)+"</a></li>");
			}
			$("#twentytwo_galleryModal .galleryModal_pagination li:eq(0) a").addClass("current");
		});
	}
	
	$("#twentytwo_galleryModal ul.galleryModal_thumbnailList").jcarousel({
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		moduleWidth: 455,
		rows: 1
	});
	
	$("#twentytwo_galleryModal .galleryModal_prevImg").hide();
	var firstImageModal = $("#twentytwo_galleryModal ul.galleryModal_thumbnailList li:eq(0) a");
	switchImage(firstImageModal, totalImages);
	
	$("#twentytwo_galleryModal ul.galleryModal_thumbnailList a").click(function() {
		switchImage($(this), totalImages);
		return false;
	});
	
	
	$("#twentytwo_galleryModal .galleryModal_nextImg").click( function() {switchImage('right', totalImages) });
	$("#twentytwo_galleryModal .galleryModal_prevImg").click( function() {switchImage('left', totalImages) });
	
}

function mycarousel_initCallback(carousel) {
	$('#twentytwo_galleryModal .galleryModal_pagination a').bind('click', function() {
        carousel.scroll($.jcarousel.intval(($(this).text())*4)-3);
		$("#twentytwo_galleryModal .galleryModal_pagination li a").removeClass("current");
		$(this).addClass("current");
        return false;
    });
}

function preloadImage(hrefVar) {
	$("#twentytwo_galleryModal .galleryModal_imagedisplay .preloadMask").animate( {opacity: 1}, 300, function() {
	$.preLoadImages([hrefVar], function() {
		var galleryContainer = $("#twentytwo_galleryModal .galleryModal_imagedisplay");
		galleryContainer.children("img").remove();
		galleryContainer.append("<img src='"+hrefVar+"' alt='' />");
		$("#twentytwo_galleryModal .galleryModal_imagedisplay .preloadMask").animate( {opacity: 0}, 300);
	});
	});
}

function switchImage(direction, totalImages) {
	var switchedImg = false;
	var currentIndex = $("#twentytwo_galleryModal ul.galleryModal_thumbnailList a.current").parent().index();
	var newhref;
	if (direction == 'left') {
		if ( (currentIndex-1) != -1) {
			$("#twentytwo_galleryModal ul.galleryModal_thumbnailList a.current").removeClass("current");
			newhref = $("#twentytwo_galleryModal ul.galleryModal_thumbnailList li:eq("+(currentIndex-1)+") a").addClass("current").attr("href");
			switchedImg = true;
			currentIndex--;
		}
	} else if (direction == 'right') {
		if ( (currentIndex+1) < totalImages) {
			$("#twentytwo_galleryModal ul.galleryModal_thumbnailList a.current").removeClass("current");
			newhref = $("#twentytwo_galleryModal ul.galleryModal_thumbnailList li:eq("+(currentIndex+1)+") a").addClass("current").attr("href");
			switchedImg = true;
			currentIndex++;
		}
	} else {
		$("#twentytwo_galleryModal ul.galleryModal_thumbnailList a.current").removeClass("current");
		newhref = direction.addClass("current").attr("href");
		switchedImg = true;
		currentIndex = direction.parent().index();
	}
	
	if (currentIndex+1 == totalImages) {
		$("#twentytwo_galleryModal .galleryModal_nextImg").fadeOut();
		if ( $("#twentytwo_galleryModal .galleryModal_prevImg").is(":hidden") ) { $("#twentytwo_galleryModal .galleryModal_prevImg").fadeIn(); }
	} else if (currentIndex == 0) {
		$("#twentytwo_galleryModal .galleryModal_prevImg").fadeOut();
		if ( $("#twentytwo_galleryModal .galleryModal_nextImg").is(":hidden") ) { $("#twentytwo_galleryModal .galleryModal_nextImg").fadeIn(); }
	} else {
		if ( $("#twentytwo_galleryModal .galleryModal_prevImg").is(":hidden") ) { $("#twentytwo_galleryModal .galleryModal_prevImg").fadeIn(); }
		if ( $("#twentytwo_galleryModal .galleryModal_nextImg").is(":hidden") ) { $("#twentytwo_galleryModal .galleryModal_nextImg").fadeIn(); }
	}
	
	if (switchedImg) {
		preloadImage(newhref);
	}
	
	return false;
	
}
