/* trigger when page is ready */
$(document).ready(function (){


	// Large Gallery
	// -------------------------------------------------------------------
	
	// Set up jQuery element variables for efficient scripting
	var largeGallery = $(".large-gallery");
	var largeGalleryImage = largeGallery.find('a.img-link');
	var largeCaptionWrap = $('.caption-wrap');
	var largeCaption = $('.caption-wrap .caption');
	var largePreviousAndNext = $('.caption-wrap .left-arrow,.caption-wrap .right-arrow');
	var largePrevious = largeCaptionWrap.find('.left-arrow');
	var largeNext = largeCaptionWrap.find('.right-arrow');
	var largeCaptionContent = largeCaption.find('.caption-content');
	var largeCaptionCount = largeCaption.find('.count');
	var largeImagesBlock = largeGallery.find('.images');
	
		
	// Large Gallery Navigation
	// -------------------------------------------------------------------
	
	// Setup the Gallery variables
	var activeImage = 1;
	var tempCounter = 0;
	var largeImagesBlock = $('.large-gallery .images');
	var totalImages = $('.large-gallery .images div').size();
	var imageWrapWidth = totalImages * 945;
	$('.large-gallery .images').css({'width':imageWrapWidth});
	
	// Assign numbers to each of the gallery items
	$('.large-gallery .images div').each(function(){
		tempCounter++;
		$(this).addClass('img-'+tempCounter);
	});
	
	largeGalleryImage.hover(function(){
		$(this).animate({'opacity':0.9},0);
	},function(){
		$(this).animate({'opacity':1},0);
	});
	
	largeGalleryImage.click(function(){
		var imgNumber = $(this).parent().attr('class');
		imgNumber = imgNumber.split('-');
		imgNumber = parseInt(imgNumber[1]);
		if (imgNumber > activeImage){
			return false;
		} else if (imgNumber < activeImage){
			return false;
		}
	});
	
	// Change Opacity for all images except the current
	$('.large-gallery .images div').not('div.img-1').animate({'opacity':0.3},0);
	largeCaptionWrap.animate({'opacity':0.9},0);
	
	// Set initial caption and photo count content
	var thisCaptionContent = largeGallery.find('div.img-1 .hidden-caption').html();
	largeCaptionContent.html(thisCaptionContent);
	var thisImageCount = largeGallery.find('div.img-1').attr('rel');
	if (thisImageCount > 0) { largeCaptionCount.html(thisImageCount); } else { largeCaptionCount.hide(); }
	
	var currentlyAnimating = false;
	
	// Next Click
	largeNext.click(function(){
	
		if (!currentlyAnimating){
	
			// Which image is next?
			var nextImage = parseInt(activeImage) + 1;
			if (nextImage > totalImages) { nextImage = 1; }
			
			// Set hover link data
			var thisCaptionContent = largeGallery.find('div.img-'+nextImage+' .hidden-caption').html();
			var thisImageCount = largeGallery.find('div.img-'+nextImage).attr('rel');
			
			if (thisImageCount > 0){
				largeCaptionCount.html(thisImageCount);
				largeCaptionCount.fadeIn('fast');
			} else {
				largeCaptionCount.fadeOut('fast');
			}
			
			if (nextImage > 1){
				captionLocation = 86;
			} else {
				captionLocation = 0;
			}
			
			largeCaption.stop().animate({'left':captionLocation},transitionSpeed,'easeInOutExpo');
			largeCaptionContent.html(thisCaptionContent);
			
			// Animate the Gallery
			currentlyAnimating = true;
			slideLocation = ((nextImage * 945) - 945) * -1;
			
			largeImagesBlock.find('div.img-'+activeImage).animate({'opacity':0.3},transitionSpeed);
			largeImagesBlock.animate({'left':slideLocation},transitionSpeed,'easeInOutExpo',function(){
				largeImagesBlock.find('div.img-'+nextImage).animate({'opacity':1},transitionSpeed,'easeOutQuad');
				activeImage = nextImage;
				currentlyAnimating = false;
			});
						
		}
		
		return false;
		
	});
	
	// Previous Click
	largePrevious.click(function(){
	
		if (!currentlyAnimating){
	
			// Which image is next?
			var previousImage = parseInt(activeImage) - 1;
			
			// Set hover link data
			var thisCaptionContent = largeGallery.find('div.img-'+previousImage+' .hidden-caption').html();
			var thisImageCount = largeGallery.find('div.img-'+previousImage).attr('rel');
			
			if (thisImageCount > 0){
				largeCaptionCount.html(thisImageCount);
				largeCaptionCount.fadeIn('fast');
			} else {
				largeCaptionCount.fadeOut('fast');
			}
			
			if (previousImage != 1){
				captionLocation = 86;
			} else {
				captionLocation = 0;
			}
			
			if (previousImage == (totalImages - 1)){
				largeNext.animate({'right':20},transitionSpeed,'easeInOutExpo');
			}
			
			largeCaptionContent.html(thisCaptionContent);
			largeCaption.animate({'left':captionLocation},transitionSpeed,'easeInOutExpo');
			
			// Animate the Gallery
			currentlyAnimating = true;
			slideLocation = ((previousImage * 945) - 945) * -1;
			
			largeImagesBlock.find('div.img-'+activeImage).animate({'opacity':0.3},transitionSpeed);
			largeImagesBlock.animate({'left':slideLocation},transitionSpeed,'easeInOutExpo',function(){
				largeImagesBlock.find('div.img-'+previousImage).animate({'opacity':1},transitionSpeed,'easeOutQuad');
				activeImage = previousImage;
				currentlyAnimating = false;
			});
						
		}
		
		return false;
		
	});
	
	
	// START AUTO-ROTATE CODE
	if (autoCycle == 'yes'){
	
		var timer;
	  	var donext = function (x){ cycleNext(); }
	  	var dotimer = function (){
	    	if(timer != null) {
	      		clearInterval(timer);
	    	}
	  
	    	timer = setInterval(function() {
	      		donext();
	    	}, slideSpeed); // Change the time in between rotations here (in milliseconds)   
	  	}
	  	
	  	dotimer();
	  	
	  	$('.right-arrow, .left-arrow').hover(function(){
	  		clearInterval(timer);
	  	},function(){
	  		dotimer();
	  	});
		
		function cycleNext(){
		
			if (!currentlyAnimating){
	
				// Which image is next?
				var nextImage = parseInt(activeImage) + 1;
				if (nextImage > totalImages){
					nextImage = 1;
				}
				
				// Set hover link data
				var thisCaptionContent = largeGallery.find('div.img-'+nextImage+' .hidden-caption').html();
				var thisImageCount = largeGallery.find('div.img-'+nextImage).attr('rel');
				
				if (thisImageCount > 0){
					largeCaptionCount.html(thisImageCount);
					largeCaptionCount.fadeIn('fast');
				} else {
					largeCaptionCount.fadeOut('fast');
				}
				
				if (nextImage > 1){
					captionLocation = 86;
				} else {
					captionLocation = 0;
				}
				
				if (nextImage == totalImages){
					largeNext.animate({'right':-50},transitionSpeed,'easeInOutExpo');
				} else if (nextImage == 1){
					largeNext.animate({'right':20},transitionSpeed,'easeInOutExpo');
				}
				
				largeCaption.stop().animate({'left':captionLocation},transitionSpeed,'easeInOutExpo');
				largeCaptionContent.html(thisCaptionContent);
				
				// Animate the Gallery
				currentlyAnimating = true;
				slideLocation = ((nextImage * 945) - 945) * -1;
				
				largeImagesBlock.find('div.img-'+activeImage).animate({'opacity':0.3},transitionSpeed);
				largeImagesBlock.animate({'left':slideLocation},transitionSpeed,'easeInOutExpo',function(){
					largeImagesBlock.find('div.img-'+nextImage).animate({'opacity':1},transitionSpeed,'easeOutQuad');
					activeImage = nextImage;
					currentlyAnimating = false;
				});
							
			}

		}
	
	}
	// END AUTO-ROTATE CODE

});
