/*
*	Fade-In Effekt for Overviewseiten
*/

// Globale Variable
var pic = Math.floor(Math.random()*11);

$(document).ready(function() {
	// Caching aus
	$.ajaxSetup ({  
		cache: false  
	});  
			
	// remove links for php-gallery
	$("a#prevPicture").removeAttr('href').click(function() { return false; });
	$("a#nextPicture").removeAttr('href').click(function() { return false; });
	
	// AutoSlide
	var autoSlide = setInterval( 'jQuery.fn.autoSlide()', 5000);
	
	// prev Picture
	$('#prevPicture').click(function() {
		clearInterval(autoSlide);
		pic--;
		jQuery.fn.loadImage();
	});
	
	// prev Picture
	$('#nextPicture').click(function() {
		clearInterval(autoSlide);
		pic++;
		jQuery.fn.loadImage();
	}); 

	// hovertrigger for all thumbs
	for (var i = 0; i <= 20; i++) {
		jQuery.fn.hoverGalleryThumb(i);
	}
	$('#thumbList').mouseleave( function() {
		jQuery.fn.resetGalleryThumbs();
	});
});
//	hoverGalleryThumbs
jQuery.fn.hoverGalleryThumb = function(i) {
	$('.thumb-' + i).mouseenter( function() {
		jQuery.fn.highlightThumb(i);
	});
};
//	hover galleryList
jQuery.fn.highlightThumb = function(i) {
	$('.thumb-' + i).stop().animate( {
		opacity: 1
	}, 600 );
	$('.thumbWrapper').not('.thumb-' + i).stop().animate( {
		opacity: 0.4
	}, 600 );
};
//	resetGalleryThumbs
jQuery.fn.resetGalleryThumbs = function() {
	$('.thumbWrapper').stop().animate( {
		opacity: 1
	}, 600 );
};

// auto-slide
jQuery.fn.autoSlide = function() {
	pic++;
	jQuery.fn.loadImage();
};

// ajax-Call LightBoxSmall
jQuery.fn.loadImage = function() {
	
	// akuelles verschieben
	var swap = $(".lightBoxSmall #lightBoxPicture #ajaxBox").html();
	$(".lightBoxSmall #lightBoxPicture #ajaxBoxSwap").html(swap);
	$(".lightBoxSmall #lightBoxPicture #ajaxBox").stop().animate( {
		opacity: 0
	}, 0);
		
	// get JSONfile
	$.getJSON("/ajax-lightboxsmall.php",{'type':type, 'image':pic}, function(json) {	
		// preload new Image
		$(new Image()).attr('src', json.lbsPath ).load(function() { //create new image object and have a callback when it's loaded	
			// show Image
			$("#lbsLink").html(json.lbsImage);
			$("#ajaxBox").stop().animate( {
				opacity: 1
			}, 1400);
		});
		
		// Metadaten aktualisieren
		$("#lbsInfoText").html(json.lbsInfoText);
		$("#lbsLink").attr('href', json.lbsLink);
		$("#ajaxBoxSwap #lbsInfoText").hide();
	});
};