$(document).ready(function(){
	$('#thumbs li img').live("click",function(){
		s =	$(this).attr('src').replace(/thumbnails/,"images/");
		c = $(this).next('p').clone();
		$('#imageWrapper').addClass("loading").empty().append('<img src="'+s+'" alt=" "/>')
			.find("img:first")
			.css("display","none")
			.load(function(){
				$("#imageWrapper").removeClass("loading");
				$(this).fadeIn("slow");
			 });
		$('#caption').empty().append(c);
	});

	$('#thumbs li img:first').trigger('click');

	$(".next-page").live('click', function() { changePage('next'); return false; });
	$(".prev-page").live('click', function() { changePage('prev'); return false; });
	$("#photo-navigation-next").live('click', function() { cycleImage('next'); return false; });
	$("#photo-navigation-prev").live('click', function() { cycleImage('prev'); return false; });
	$("a.full").colorbox({maxHeight: "100%"});
});

function show(id) {
	if(!$("#"+id).hasClass('selected')) {
		$(".gallery-main-image-cont.selected .main-gallery-cont.selected").fadeOut().removeClass('selected').css('position', 'absolute');
		$("#"+id).fadeIn().addClass('selected').css('position', 'relative');
	}
}

function changeCategory(id, e) {
	if(!$("#gallery-main-cont-"+id).hasClass('selected')) {
		$("#main-cont .selected").css("position", "absolute");
		$("#left-nav a.selected").removeClass('selected');
		$(e).addClass('selected');
		$(".gallery-main-image-cont.selected .main-gallery-cont.selected").removeClass("selected").fadeOut();
		$(".gallery-main-image-cont.selected .main-gallery-cont:first").addClass("selected").fadeIn();
		$(".gallery-main-image-cont").css('position', 'absolute').fadeOut(function() { $(this).css('display', 'none'); }).removeClass('selected');
		$(".gallery-sidebar-image-cont").css('position', 'absolute').fadeOut(function() { $(this).css('display', 'none'); }).removeClass('selected');
		$("#gallery-main-cont-"+id).fadeIn().addClass('selected');
		$("#gallery-sidebar-cont-"+id).fadeIn().addClass('selected');
		$("#main-cont .selected").css("position", "relative");
	}
}

function cycleImage(direction) {
	var $current = $(".main-gallery-cont.selected ");
	var $next = $current.next(".gallery-main-image-cont.selected .main-gallery-cont");
	var $prev = $current.prev(".gallery-main-image-cont.selected .main-gallery-cont");

	if(direction == 'next') {
		if(!$next.length) {
			var $nextCat = $("#left-nav .selected").next('.gallery-category');
			if(!$nextCat.length) {
				$nextCat = $("#left-nav .gallery-category:first");
			}

			$nextCat.click();
		} else {
			show($next.attr('id'));
		}
	} else if(direction == 'prev') {
		if(!$prev.length) {
			var $prevCat = $("#left-nav .selected").prev('.gallery-category');
			if(!$prevCat.length) {
				$prevCat = $("#left-nav .gallery-category:last");
			}

			$prevCat.click();
			$(".gallery-main-image-cont.selected .main-gallery-cont.selected").css('position', 'absolute').removeClass("selected").fadeOut();
			$(".gallery-main-image-cont.selected .main-gallery-cont:last").addClass("selected").fadeIn().css('position', 'relative');
		} else {
			show($prev.attr('id'));
		}
	}
}

function changePage(direction) {
	var $current = $(".gallery-sidebar-image-cont.selected .thumb-page.selected");
	var $next = $($current).next('.thumb-page');
	var $prev = $($current).prev('.thumb-page');
	if(direction == 'next') {
		if(!$next.length) {
			$next = $(".gallery-sidebar-image-cont.selected .thumb-page:first");
		}
		$current.fadeOut().removeClass('selected');
		$next.fadeIn().addClass('selected');
	} else {
		if(!$prev.length) {
			$prev = $(".gallery-sidebar-image-cont.selected .thumb-page:last");
		}
		$current.fadeOut().removeClass('selected');
		$prev.fadeIn().addClass('selected');
	}

	return false;
}
