jQuery(document).ready(function() {
	$("div.content").css('display','block');
	
	var mouseoutopacity = 0.3;
	var duration = 500;
	imageselector = "#slideshow";
	titleselector = "#image-title";
	descriptionselector = "#image-desc";
	subgalleryselector = "#subgallerycontainer";
	subgallerythumbsselector = ".subgallery";
	imagefiles = [];
	currentGallery = "";
	currentImage = 0;

	$(".caption").each(function(){
		$(this).hide();
	});

	galleryfunction  = function(i){
		
		
		imageindex = i;
		
		// initial fade to mouseoutopacity
		$(this).fadeTo(duration,mouseoutopacity);
		// fading on hover
		if($(this).parents(subgallerythumbsselector).length > 0)
				issubgallerythumb = true;
			else
				issubgallerythumb = false;
		
		// preload images 
		var file =$(this).children("a.thumb").attr("href") ;
		imagefiles.push(file);
		// what to do on click

		$(this).click(function(e){
			// set current Image

			
			
			if($(this).parents(subgalleryselector).length == 0)
				issubgallerythumb = false;
			else
				issubgallerythumb = true;

			$(this).parents("ul").children("li").each(function(){$(this).removeClass("current"); $(this).fadeTo(duration, mouseoutopacity);});
			$(this).addClass("current");
			$(this).stop().fadeTo(duration, 1.0);

			if (!issubgallerythumb){
				currentImage = $(this).prevAll("#gallerythumbs li").length;
				// Set Title for this Gallery
				currentGallery = $(this).find(".caption .image-title").html();
			}
			file = $(this).children("a.thumb").attr('href');
			// eventually display subgalleries
			subgalleries = $(this).find(".caption .subgallery");
			
			if(subgalleries.length > 0){
				
				$(subgalleryselector).fadeOut(duration, function(){
					$(this).empty();
					//$(this).css("height","auto");
					$(this).html($(subgalleries[0]).html());
					$(this).find("ul.thumbs li").each(galleryfunction); // recursive!
					var thumbs = $(this).find("ul.thumbs li");
					$(thumbs[0]).trigger('click');

				});
			}else{
				if(!issubgallerythumb)
					$(subgalleryselector).stop().fadeOut(duration, function(){$(this).empty().css("height",0)});
				//else
				//	$(subgalleryselector).stop().fadeOut(duration);
			}
			
			// display title
			//titel = $(this).find(".caption .image-title");

			$(titleselector).fadeOut(duration, function(){
				
				$(this).empty();
				$(this).html(currentGallery);
			});
			
			// display description

			if (!issubgallerythumb){
				desc = $(this).find(".caption .image-desc");

				$(descriptionselector).fadeOut(duration, function(){
					
					$(this).empty();
					$(this).html(desc.html());
					//$(this).fadeIn(duration);
				});
			}else{
				$(descriptionselector).fadeOut(duration);
			}
			$(imageselector).fadeOut(duration,function(){


				var el = document.createElement("div");
				//el.src = "assets/img/loader.gif";
				el.id = "loader";
				if ($(imageselector).height() > 0){
					el.style.height = $(imageselector).height() + "px";
					el.style.display = "block";
				}

				$(imageselector).empty();
				$(imageselector).html(el);
				$(imageselector).fadeIn(duration);
				// preload this image
				$.preLoadImages(file,function(){
					// when it is loaded
					$(imageselector).fadeOut(duration,function(){
						var el = document.createElement("img");
						el.src = file;
						$(imageselector).html(el).fadeIn(duration);
						$(descriptionselector).fadeIn(duration);
						$(titleselector).fadeIn(duration);
						$(this).fadeIn(duration);
						$(subgalleryselector).stop().fadeTo(duration,1.0);
					});
				});
			});

			e.preventDefault();
			return false;
		});
		
	};
	$("#gallerythumbs ul.thumbs li").not(".subgallery li").each(galleryfunction);
	
	$($("ul.thumbs li")[0]).trigger('click');
	
	
	$("ul.thumbs li").live("mouseenter",
		function(){
			$(this).stop().fadeTo(duration, 1.0);
	});
	$("ul.thumbs li").live("mouseleave",
		function(){
		if (!$(this).hasClass("current"))
			$(this).stop().fadeTo(duration, mouseoutopacity);
	});

	
	$("#NextPic").click(
		function(){
			currentImage = currentImage +1;
			if (currentImage == $("#gallerythumbs li").not(".subgallery li").length)
				currentImage = 0;
			$($("ul.thumbs li").not(".subgallery li")[currentImage]).trigger('click');
		});
		
		$("#PrevPic").click(
		function(){
			currentImage = currentImage -1;
			if (currentImage < 0)
				currentImage = $("#gallerythumbs li").not(".subgallery li").length;
			$($("#gallerythumbs ul.thumbs li").not(".subgallery li")[currentImage]).trigger('click');
		});
	
	$.preLoadImages(imagefiles);
	

});

