// JavaScript Document
$(document).ready(function(){
	if($('#galleryFrame').length){
		var img = new Array();
		var theImg = $('#galleryImg');
		var thumbs = $('.galleryThumbs a');
		var current = 0;
		
		thumbs.each(function(i){
			img[i] = new Image();
			img[i].src = $(this).attr("href");
			
			var caption = $(this).attr('title');
			
			$(this).click(function(){
				theImg.fadeOut(function(){
					theImg.attr('src', img[i].src);
					
					if(img[i].complete){
						theImg.fadeIn();
					} else {
						$(img[i]).load(function(){
							theImg.fadeIn();					
						});
					}
					
					current = i;
				});
				
				return false;
			});
		}); // Each
		
		theImg.css('cursor', 'pointer').click(function(){
			var next = current + 1;
			
			if(next >= thumbs.length){
				$('.galleryThumbs a:eq(0)').click();
			} else {
				$('.galleryThumbs a:eq('+next+')').click();													 
			}
		});
		
		$('ul.galleryThumbs li:nth-child(4n)').addClass('last');
	}
});
