/*
*	Gallerylogic fuer LightboxSeiten
*/

// Globale Variable
var pic = 1;

$(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; });
	$("a#imageLink").removeAttr('href').click(function() { return false; });
	
	// AutoSlide
	var autoSlide = setInterval( 'jQuery.fn.autoSlide()', 5000);
	
	// prev Picture
	$('#prevPicture').click(function() {
		// autoslide stoppen
		clearInterval(autoSlide);
		
		// nur laden wenn gültig
		if(pic > 1) {
			pic--;
			jQuery.fn.checkVisability();
			jQuery.fn.ajaxLoad();
		}
	});
	
	// next Picture
	$('#nextPicture').click(function() {
		// autoslide stoppen
		clearInterval(autoSlide);
		
		// nur laden wenn gültig
		if(pic < imageCount) {
			pic++;
			jQuery.fn.checkVisability();
			jQuery.fn.ajaxLoad();
		}
	}); 
	
	// image Link
	$('#imageLink').click(function() {
		// autoslide stoppen
		clearInterval(autoSlide);
		
		// nur laden wenn gültig
		if(pic < imageCount) {
			pic++;
			jQuery.fn.checkVisability();
			jQuery.fn.ajaxLoad();
		}
	}); 

});

// check visability der buttons
jQuery.fn.checkVisability = function() {
	if(pic <=1 ) {
		$("a#prevPicture").fadeOut(1400);
	} else {
		$("a#prevPicture").fadeIn(1400);
	}
	if(pic >= imageCount ) {
		$("a#nextPicture").fadeOut(1400);
	} else {
		$("a#nextPicture").fadeIn(1400);
	}
};

// auto-slide
jQuery.fn.autoSlide = function() {
	// sonst von vorn beginnen
	if(pic == imageCount) {
		pic = 1;
	} else {
		pic++;
	}
	jQuery.fn.checkVisability();
	jQuery.fn.ajaxLoad();
};

// ajax-Call LightBox
jQuery.fn.ajaxLoad = function() {
	// akuelles verschieben
	var swap = $("#ajaxBox").html();
	$("#ajaxBoxSwap").html(swap);
	$("#ajaxBox").stop().animate( {
		opacity: 0
	}, 0);
	
	// get JSONfile
	$.getJSON("/ajax-lightbox.php",{'type':type, 'id':id, 'image':pic}, function(json) {	
		// preload new Image
		$(new Image()).attr('src', json.iPath ).load(function() { //create new image object and have a callback when it's loaded	
			// show Image
			$("#imageLink").html(json.iImage);
			$("#ajaxBox").stop().animate( {
				opacity: 1
			}, 1400);
		});
		
		// Metadaten aktualisieren
		$("#iTitle").html(json.iTitle);
		$("#iLocation").html(json.iLocation);
		$("#iNumber").html(json.iNumber);
	});
};