$(document).ready(function(){

	var mm = {
		
		currAnchor : 0,
		slideWidth : 0, /* updates on page load */
		slideAnchor : "left",
		leftAnchorPoints : null,
		t : null,
		st : null,
		
		getSlideWidth : function(){
			var cont = $('.slide-container');
			var img = $('img.main-thumb');
			
			
			$(img).imagesLoaded(function(){
				var collection = this;
				var imageWidth;
				var countWidth = 0;
				
				$(collection).each(function(){
					imageWidth = this.width;
					countWidth = countWidth + imageWidth + 5;
				});
				
				mm.setSlideWidth(countWidth);
				
				$(window).resize(function(){
					var newWidth = $('.fluid-wrapper').width();
					mm.setAnchorPoints(newWidth);
				}); //end resize
			});
		},
		
		setSlideWidth : function(sWidth){
			this.slideWidth = sWidth;
			var cont = $('.slide-container');
			cont.css({width : mm.slideWidth + "px"});
		},
		
		setAnchorPoints : function(w){
		
			var imgArray = $('.main-thumb'),
				slideWidth = this.slideWidth,
				leftAnchorArray = [0],
				distanceFromLeft = 0;
				
			var windowWidth = (w > 1600) ? 1600 : w;
			imgArray = $.makeArray(imgArray);
			
			// Running total is used to keep tally of individual element widths as added together
			// so we can check whether we need a new anchor point to slide to
			var	runningTotal = 0;
			$.each(imgArray, function(i, obj){
				var addWidth = runningTotal + (obj.width + 5);
				// If this element doesn't completely fit into viewing area,
				// we want to add the 'running total' amount to the array of
				// anchor points
				if (addWidth <= (windowWidth + distanceFromLeft)){
					runningTotal = addWidth;
				} else {
					if (runningTotal <= (slideWidth - windowWidth)){
						leftAnchorArray.push(runningTotal);
						distanceFromLeft = distanceFromLeft + runningTotal;
						runningTotal = addWidth;
					}
				}
			});
			
			// Add last anchor point to array which is width of slide container
			// minus the viewing area width
			leftAnchorArray.push(slideWidth - (windowWidth+5));
			mm.leftAnchorPoints = leftAnchorArray;
		},
		
		// Stuff to do immediately on page load
		init : function(){
			this.getSlideWidth();
			
			// Initiate the gallery plugin
			$('.slide-img').fancybox({
				fitToView : true,
				aspectRatio : true,
				loop : true,
				openEffect : 'fade',
				closeEffect : 'fade',
				nextEffect : 'fade',
				prevEffect : 'fade'
			});
		}
	};
		
	$('.slide-nav-right').click(function(){
		if (mm.leftAnchorPoints === null){
			var fWidth = $('.fluid-wrapper').width();
			mm.setAnchorPoints(fWidth);
		}
		var anchorPoints = mm.leftAnchorPoints;
		
		if (mm.currAnchor === (anchorPoints.length-1)){
			return false;
		} else {
			mm.currAnchor = mm.currAnchor + 1;
			var animDistance = anchorPoints[mm.currAnchor];
			mm.slideAnchor = (mm.currAnchor === anchorPoints.length-1) ? "right" : "left";
			$(this).siblings('.slide-container').animate({left : "-" + animDistance + "px"}, 400);
		}
	});
	
	$('.slide-nav-left').click(function(){
		if (mm.leftAnchorPoints === null){
			var fWidth = $('.fluid-wrapper').width();
			mm.setAnchorPoints(fWidth);
		}
		var anchorPoints = mm.leftAnchorPoints;
		
		if (mm.currAnchor === 0){
			return false;
		} else {
			mm.currAnchor = mm.currAnchor - 1;
			var animDistance = anchorPoints[mm.currAnchor];
			$(this).siblings('.slide-container').animate({left : "-" + animDistance + "px"}, 400);
		}
	});
	
	$('.zoom-img').css({display : "block", opacity : 0});
	
	$('.slide-img').hover(
		function(){
			$(this).children('.main-thumb').stop('true').animate({opacity : 0.7}, 200);
			$(this).children('.zoom-img').stop('true').animate({opacity : 0.8}, 200);
		},
		function(){
			$(this).children('.main-thumb').stop('true').animate({opacity : 1}, 200);
			$(this).children('.zoom-img').stop('true').animate({opacity : 0}, 200);
		}
	);
	
	$('ul.nav li:not(".link-home, .active") a').hover(
		function(){
			$(this).parent('li').stop().animate({left : "0px"}, 150);
		},
		function(){
			$(this).parent('li').stop().animate({left : "-35px"}, 400);
		}
	);
			
	$('.social-icon, .fb-like').hover(
		function(){
			var animTop, text;
			if (mm.t) { clearTimeout(mm.t); }
			if ($(this).hasClass('facebook')){
				animTop = 0;
			} else {
				animTop = -60;
				text = $(this).attr('title');
			}
			$('.social-feed').text(text);
			$('.social-anchor').animate({top: animTop + "px"}, "fast");
		},
		
		function(){
			if (mm.t) { clearTimeout(mm.t); }
			mm.t = setTimeout(function(){
				$('.social-anchor').animate({top: "-28px"}, "fast");
			}, 400);
		});
	
	var start = mm.init();
});


