var iFactor = 8;
var iCurrentImg = 0;
var aLargeImages = [];

function stopSlide(obj) {
	obj.stop(true);
}

$(document).ready(function() {
	
	// prettyPhoto for: Fotoboeken 
	if ($("#showImages").length) {
		
		$('div.sidebarBox.photoAlbums div.sidebarContent').click(function() {
			$("#showImages").trigger('click');
		});
		
		$("#showImages").prettyPhoto({
			animation_speed: 'normal',
			theme: 'light_square',
			slideshow: 3000, 
			autoplay_slideshow: false,
			social_tools: ''
		});
	}		
	
	function loadImage(){		
		$('#galleryImgLarge').attr('src', aLargeImages[iCurrentImg]);
	}
	
	// IMAGES GALLERIES HORIZONTAL SCROLLER
	if($('#galleryScroll').length){
		
		$('td.galleryImageScroller img').each(function(index) {
			aLargeImages[index] = $(this).attr('alt');
		});	
		
		$('#scrollerLeft').click(function() {
			if((iCurrentImg-1) >= 0){
				iCurrentImg = (iCurrentImg-1);
				loadImage();
			}			
		});

		$('#scrollerRight').click(function() {
			if((iCurrentImg+1) < aLargeImages.length){
				iCurrentImg = (iCurrentImg+1);
				loadImage();
			}			
		});				
	}
	
	// IMAGES GALLERIES HORIZONTAL SCROLLER CLICKED THUMB LOADS BIG IMG
	if($('img.galleryImg').length){		
		var oGalleryImg = $('img.galleryImg');
		oGalleryImg.click(function() {
			$('#galleryImgLarge').attr('src', $(this).attr('alt'));
		});
	}	
	
	// IMAGE GALLERIES	
	if($('#galleriesContainer').length){

		var oGalleryContainerItems = $('#galleryContainerItems');
		var oSidebarGalleryUp = $('#galleriesContainer .up');
		var oSidebarGalleryDn = $('#galleriesContainer .down');
		
		oSidebarGalleryUp.mouseenter(function() {
			stopSlide(oGalleryContainerItems);
			var iDuration = Math.abs(parseInt(oGalleryContainerItems.css('top'))) * iFactor;
			var iDivTop = Math.abs(parseInt(oGalleryContainerItems.css('top')));
			if (iDivTop > 0) {
				oGalleryContainerItems.animate({
					top: 0
				}, {
					duration: iDuration,
					easing: 'linear'
				});
			}
		});
		
		oSidebarGalleryDn.mouseenter(function() {
			oSidebarGalleryDn.trigger('autoSlide', [iFactor, true, 0]);
		});

		oSidebarGalleryDn.bind('autoSlide', function(event, iFac, bStop, iDelay) {
			if (bStop === true) {
				stopSlide(oGalleryContainerItems);
			}
			var iDuration = (oGalleryContainerItems.height() - 400 - Math.abs(parseInt(oGalleryContainerItems.css('top')))) * iFac;
			var iDivTop = oGalleryContainerItems.height() - 400;

			if (iDivTop > Math.abs(parseInt(oGalleryContainerItems.css('top')))) {
				oGalleryContainerItems.delay(iDelay).animate({
					top: '-' + iDivTop
				}, {
					duration: iDuration,
					easing: 'linear'
				});
			}
		});
		
		$('#galleryContainerItems .imageGalleryItem').mouseenter(function() {
			stopSlide(oGalleryContainerItems);
		});			
		
		// slide gallery onload (if desired uncomment)
		//oSidebarGalleryDn.trigger('autoSlide', [(iFactor * 4), false, 2000]);

		//oSidebarGalleryUp.mouseout(function() {
		//	stopSlide(oGalleryContainerItems);
		//});

		//oSidebarGalleryDn.mouseout(function() {
		//	stopSlide(oGalleryContainerItems);
		//});
	}
});
