var itemsPerPage = 10;

$(document).ready(function(){		
		
		// SLIDER		
		$('.sliderImages img:first').fadeIn(1000).addClass('active');
		
		rotate = function(){
			$(".sliderImages img").fadeOut(2000).removeClass('active');
			$active.fadeIn(1000).addClass('active');
		}; 
		
		rotateSwitch = function(){
			play = setInterval(function(){
				$active = $('.sliderImages img.active').fadeIn(1000).next();
				if ( $active.length === 0) {
					$active = $('.sliderImages img:first');
				}
				rotate();
			}, 5000);
		};
		
		rotateSwitch();
		
		//ALBUMS PAGER
		var $entries = $('#albumContainer');
		var $list = $entries.children('ul:first');
		
		$list.children().eq(itemsPerPage).nextAll().andSelf().hide();
		
		var $pagination = $('<ul class="pager"></ul>').each(function() {
			var itemsTotal = $list.children().size();
			var pages = Math.ceil(itemsTotal / itemsPerPage);
		
			for (var i = 1; i <= pages; i += 1) {
				$(this).append('<li>' + i + '</li>');
			}
		}).appendTo($entries);
		
		$pagination.children('li:first').addClass('selected');
		
		$pagination.children().click(function() {
			if ($(this).is('.selected')) { return; }
				var page = $(this).index() + 1;
				var firstToShow = (page - 1) * itemsPerPage;
				var lastToShow = page * itemsPerPage;
				$list.children().hide().slice(firstToShow, lastToShow).fadeIn();
				$(this).addClass('selected').siblings('.selected').removeClass('selected');
			});
		
		//PHOTOS OVERLAY
		var pageNow = 1;
		var iIndex = 1;
		$('#photosList div').click(function(){
			aData = aAlbumSettings;
			var windowTop = $(window).scrollTop();
			iIndex = $(this).attr('id');	
			pageNow = 1;
			$('.photoTitle p').html(aData.title[iIndex]);
			$('.pageNum p').html("Photo "+ pageNow +" of " + aData.pages[iIndex]);
			$('#overlayBg').fadeIn(); //Fade in the div which will contain the photos
			$(this).children('img').clone().appendTo('.photoContainer'); //Copy the image clicked to that div
			var visibleImgWidth = $('.photoContainer img:first').width(); //Get the width of that image
			$('#photoPosition').css({'top':windowTop+'px', 'margin-top':'60px','width':visibleImgWidth}); //Assign that width to the container in order to center it and make it appear on the position the user is
			$('#photoPosition').find('.thumb').remove(); // Remove the first image as is repeated
			var visibleImgHeight = $('.photoContainer img:first').height(); // Get the height of the image
			$('.photoContainer img:first').addClass('activeImage'); // Assign the class activeImage to the first image as it's the active one
			$('.caption').css({'top':visibleImgHeight - 60+'px'}); // Position the caption at the very bottom of the image
		});
		
		$('.closeBtn').click(function(){
			$('.photoContainer img').remove();
			$('#overlayBg').fadeOut();
		});
		
		function recalculatePos() { //Recalculates the position of the caption and the image, depending the position of the screen
			var activeImageHeight = $('.activeImage').height(); // Get active image height
			var activeImageWidth = $('.activeImage').width(); // Get active image width
			var windowTop = $(window).scrollTop();
			$('.caption').css({'top':activeImageHeight - 60+'px'});
			$('#photoPosition').css({'top':windowTop+'px','width':activeImageWidth});
		}
		
		//KEY BIND SLIDER
		$(document).keydown(function(e){
			if (e.keyCode == 37) { 
				if ( $('.photoContainer img:first').hasClass('activeImage') ) {}
					else {
					pageNow--;
					$('.pageNum p').html("Photo "+ pageNow +" of " + aData.pages[iIndex]);
					$('.activeImage').removeClass('activeImage').prev('img').addClass('activeImage');
					recalculatePos();
				}
			}

			else if(e.keyCode == 39) { // right
				if ( $('.photoContainer img:last').hasClass('activeImage') ) {}
					else {
					pageNow++;
					$('.pageNum p').html("Photo "+ pageNow +" of " + aData.pages[iIndex]);						
					$('.activeImage').removeClass('activeImage').next('img').addClass('activeImage');
					recalculatePos();
				}
			}
		});
		
		//PHOTOS OVERLAY SLIDER
		$('.arrowNext').click(function(){
			if ( $('.photoContainer img:last').hasClass('activeImage') ) {}
			else {
				pageNow++;
				$('.pageNum p').html("Photo "+ pageNow +" of " + aData.pages[iIndex]);
				$('.activeImage').removeClass('activeImage').next('img').addClass('activeImage');
				recalculatePos();
			} return false;
		});
		$('.arrowPrev').click(function(){
			if ( $('.photoContainer img:first').hasClass('activeImage') ) {}
			else {
				pageNow--;
				$('.pageNum p').html("Photo "+ pageNow +" of " + aData.pages[iIndex]);
				$('.activeImage').removeClass('activeImage').prev('img').addClass('activeImage');
				recalculatePos();
			} return false;
		});
});
