var currentThumbId = -1;
var currentImageContainerId;

$(function(){
	
	$('#thumbGrid .thumb').each(function(){
		var t = $(this);
		t.children('a').css('opacity', '.5');
	});
	
	$('#thumbGrid .thumb a').mouseover(function(){
		var t = $(this);
		var id = t.parent().attr("slideshow_index");
		if(id != currentThumbId) {
			t.css('opacity', '1');
		}
	});
	
	$('#thumbGrid .thumb a').mouseout(function(){
		var t = $(this);
		var id = t.parent().attr("slideshow_index");
		if(id != currentThumbId) {
			t.css('opacity', '.5');
		}
	});
	
	$('#thumbGrid .thumb a').click(function(){
		var t = $(this);
		var id = t.parent().attr("slideshow_index");
		select(id);
		return false;
	});
	
	$('#imageContainer img').load(function(){
		var tabHeight = jQuery('#imageContainer img').height();
		
		$('#imageContainer #loader').hide();
		$('#imageContainer').fadeOut(0,function() {
			jQuery('.slideshow-navigation a').height(tabHeight);
		});
		$('#imageContainer .image').css('opacity', '1');
		$('#imageContainer').fadeIn();
	})
	
	select(0);
})

function select($id){
	if(currentThumbId > -1){
		var curStr = '#thumb_' + currentThumbId + ' a';
		$(curStr).css('opacity', '.5');
	}
	currentThumbId = $id;
	curStr = '#thumb_' + currentThumbId + ' a';
	$(curStr).css('opacity', '1');
	if ($('#imageContainer').length) {
		$('#imageContainer #loader').show();
		$('#imageContainer .image').css('opacity', '0');
		$('#imageContainer .image').attr('src', images[currentThumbId].src);
		$('.caption').html(images[currentThumbId].caption);	
	}

}

function previous() {
	if (currentThumbId == 0) {
		select(images.length-1);
	}
	else {
		select(currentThumbId-1);
	}
}

function next() {
	if (currentThumbId == images.length-1) {
		select(0);
	}
	else {
		select(currentThumbId+1);
	}
}
