// gallery start position
$current_pic = 1;
$last_pic = 0;
$delay = 10000;
var $timer;
var $loading = false;

function nextpic(){
	set_gallery_pos('next');
	$timer = setTimeout("nextpic()",$delay);
}

function set_gallery_pos($selected_pic){
	if ($loading) return false;
	if ($selected_pic == $current_pic) return false;
	$loading = true;
	// prev, next
	if ($selected_pic == 'prev') {
		$selected_pic = ($current_pic==1) ? 25 : parseInt($current_pic)-1;
	} else if ($selected_pic == 'next') {
		$selected_pic = ($current_pic==25) ? 1 : parseInt($current_pic)+1;
	} 
	// navbar changes
	$('#gallery_nav > li').removeClass('active');
	$('#gn_'+$selected_pic).addClass('active');
	// image swap
	$('#thumbnail_'+$current_pic).css('z-index',2);
	$last_pic = $current_pic;
	$current_pic = $selected_pic;
	$('#thumbnail_'+$selected_pic).css('z-index',1).attr('src','img/thumb_0'+$selected_pic+'.jpg').show();
	$('#thumbnail_'+$last_pic).fadeOut(1000, function(){
		$loading = false;
	});
}

// domready
$(function(){
	$timer = setTimeout("nextpic()",$delay);
	$('#gallery_window a').lightBox();
	$('.thumbnail').hide();
	$('#thumbnail_1').show();
	
	// clicking on gallery_nav
	$('#gallery_nav > li > a').click(function(){
		clearTimeout($timer);
		set_gallery_pos($(this).text());
		$timer = setTimeout("nextpic()",$delay);
		return false;
	}); // click
	
}); // domready

// lightbox on wellness page
$(function() {
	$('a[@rel*=wellness]').lightBox();
}); // lightbox on wellness page