$(document).ready(function() {

$('div.artist-images ').hide();

$('div.artist-images div.image').hide();
$('div.artist-images img').hide();
$('#tert-nav ul.artist-links').hide();
var currentimg = 0;

$('#gallery div.current img').eq(0).fadeIn();


$('#tert-nav li').click(function(e) {
								
	if (! $(this).hasClass("sel") && ! $(this).parent().hasClass('artist-links') ) {
		
		$('#tert-nav li.sel ul.artist-links span.current-image').html("1");


		
		
		//animate other links back
		$('#tert-nav li.sel ul.artist-links').slideUp('fast');
		$('#tert-nav li.sel').removeClass("sel");
		$(this).addClass("sel");
		$('#tert-nav li:not(.sel)').animate({textIndent: '0'}, 200);
		$(this).find('ul.artist-links').slideDown('fast');
		

		
		var index = $(this).index();
		$('#gallery div.current div.image').hide();
				$('#gallery div.current div.image img').hide();
				$('#gallery div.current').hide().removeClass("current");

		$('#gallery div.artist-images').eq(index).addClass("current");
		
		currentimg = 0;
		$('#gallery div.current').show();
		$('#gallery div.current div.image').show();
		$('#gallery div.current div.image img').eq(0).fadeIn('normal');
		
		//set total image counter
		var totalimg = $('#gallery div.current div.image img').length;
		$('#tert-nav li.sel ul.artist-links span.total-image').html(totalimg);

e.preventDefault();
		
	}
	 
  });

//$('#tert-nav li ul li').unbind('click');

$('.gallery-control a.next').click(function(e) {
	e.preventDefault();
	currentimg++;	
	currentimg = currentimg % ($('#gallery div.current div.image img').length);
	changePics(currentimg);	
	var imagecount = currentimg + 1;
	$('#tert-nav li.sel ul.artist-links span.current-image').html(imagecount);
									 
});

$('.gallery-control a.prev').click(function(e) {
	 e.preventDefault();
	 currentimg--;
	 if (currentimg < 0) {
		currentimg = $('#gallery div.current div.image img').length - 1;
	 }
	 reversePics(currentimg);
	var imagecount = currentimg + 1;
	$('#tert-nav li.sel ul.artist-links span.current-image').html(imagecount);
												 
});


 });


function changePics(nextimg) {
	$('#gallery div.current div.image img').eq(nextimg).css('zIndex', 10).fadeIn();
	var previmg = nextimg - 1;
	if (nextimg == 0) {
		previmg = $('#gallery div.current div.image img').length -1;
	}
	$('#gallery div.current div.image img').eq(previmg).css('zIndex', 0).hide();
}

function reversePics(nextimg) {
	$('#gallery div.current div.image img').eq(nextimg).css('zIndex', 10).fadeIn();
	var previmg = nextimg + 1;

	if (nextimg == $('#gallery div.current div.image img').length - 1) {
		previmg = 0;
	}
	$('#gallery div.current div.image img').eq(previmg).css('zIndex', 0).hide();
}

