var in_home = false;
var gallery_pics = null;
var gallery_idx = 0;
var galleryLoop = null;
var galleryLoopPreloading = null;

$(document).ready(function() {
	$('#gallery_next').click(nextClick);
	$('#gallery_prev').click(function() {
		clearGalleryTimeouts();
		galleryPrev();
		startGallerySlideShow();
		
		prevThumbsPage();
	});
	$('#gallery_pic').hover(function() {
		clearGalleryTimeouts();
	}, function() {
		startGallerySlideShow();
	}).load(function() {
		stopLoading()
	});
	
	startGallerySlideShow();
	
	//Thumbs
	$('.bottom_gallery a').lightBox();
});

function galleryNext() {
	if(gallery_pics && gallery_pics.length < 2) return;
	$('#gallery_pic').fadeOut('slow',function() {
		//$('#pic_loading').fadeIn();
		$(this).unbind('load');
		$(this).load(function() {
			var e = this;
			$(e).fadeIn();
			$('#pic_count').text(gallery_idx+1);
		});
		gallery_idx++;
		gallery_idx = gallery_idx%gallery_pics.length;
		this.src = gallery_pics[gallery_idx].url;
		this.alt = gallery_pics[gallery_idx].alt;
	});
	
	nextThumbsPage();
}

function galleryPrev() {
	if(gallery_pics && gallery_pics.length < 2) return;
	$('#gallery_pic').fadeOut('slow',function() {
		//$('#pic_loading').fadeIn();
		$(this).unbind('load');
		$(this).load(function() {
			var e = this;
			$(e).fadeIn();
			$('#pic_count').text(gallery_idx+1);
		});
		gallery_idx--;
		gallery_idx = (gallery_idx<0)?gallery_pics.length-1:gallery_idx;
		this.src = gallery_pics[gallery_idx].url;
		this.alt = gallery_pics[gallery_idx].alt;
	});
}

function nextClick() {
	clearGalleryTimeouts();
	galleryNext();
	startGallerySlideShow();
}

function startGallerySlideShow() {
	if((gallery_pics == null || gallery_pics.length < 2) &&
		!$('.bottom_gallery')[0]) return; //No loop
	clearGalleryTimeouts();
	var time = (in_home)?7500:10000;
	galleryLoop = setInterval(slideShowStep, time);
	if($('.bottom_gallery')[0]) {
		$('#pic_loading').fadeOut();
	}
}

function slideShowStep() {
	//$('#pic_loading').fadeIn();
	//galleryLoopPreloading = setTimeout(galleryNext, 2000);
	galleryNext();
}

function stopLoading() {
	//$('#pic_loading').fadeOut(function() {
	$('#gallery_pic').fadeIn();
	//});
}

function clearGalleryTimeouts() {
	//alert(galleryLoop);
	//stopLoading();
	//clearTimeout(galleryLoopPreloading);
	//galleryLoopPreloading = null;
	clearTimeout(galleryLoop);
	galleryLoop = null;
}

function nextThumbsPage() {
	var elem = $('.thumbs_shown');
	var next = elem.next();
	var pic_count = parseInt($('#pic_count').text());
	if(!next.hasClass('bottom_gallery_page')) {
		next = elem.parent().find('.bottom_gallery_page:first');
		if(next[0] == elem[0]) return;
		pic_count = 0;
	}
	pic_count++;
	$('#pic_loading').fadeIn('fast');
	$('#pic_count').text(pic_count);
	elem.fadeOut('slow', function() {
		elem.removeClass('thumbs_shown');
		next.addClass('thumbs_shown');
		next.fadeIn('slow', function() {
			$('#pic_loading').fadeOut();
		});
	});
	
	/*
	//alert(elem[0]);
	
	elem.animate({
		left: -diff
	}, 500, function() {
		elem.addClass('hidden');
	});
	
	next.animate({
		left: 0
	}, 500);
	*/
}

function prevThumbsPage() {
	var elem = $('.thumbs_shown');
	var prev = elem.prev();
	var pic_count = parseInt($('#pic_count').text());
	if(!prev.hasClass('bottom_gallery_page')) {
		prev = elem.parent().find('.bottom_gallery_page:last');
		if(prev[0] == elem[0]) return;
		pic_count = parseInt($('#total_pics').text())+1;
	}
	$('#pic_loading').fadeIn('fast');
	pic_count--;
	$('#pic_count').text(pic_count);
	elem.fadeOut('slow', function() {
		elem.removeClass('thumbs_shown');
		prev.addClass('thumbs_shown');
		prev.fadeIn('slow', function() {
			$('#pic_loading').fadeOut();
		});
	});
}