Y.use('node', 'node-load', 'squarespace-gallery-stacked', 'squarespace-gallery-strip', function(Y) { 

// ------------------------------------------------ Mobile ------------------------------------------------- //	
   
  var mobileSet = function() {
      
	  if (document.documentElement.clientWidth < 500) {
	  	  
  		// Set mobile navigation
  			    
	    Y.one('.mob-icon').on('click', function() { 
	    	if (Y.one('#header').hasClass('open')) {
	    		Y.one('#header').removeClass('open');
	    	} else {
	    		Y.one('#header').addClass('open');	
	    	}
	    });
	    
	    if (Y.one('.collection-type-gallery')) {	
	    	new Y.Squarespace.Loader({ img: Y.all('#viewer div img') });
	    }
	    
	    if ((Y.one('.collection-type-blog')) || (Y.one('.collection-type-projects'))) {		
				
				// Size and play videos
				
				if (Y.one('.media')) {
					Y.all('.media iframe').each(function() {
					
					  var mediaW	= parseInt( this.ancestor('.media').getComputedStyle('width'), 10 )
					  var vidW = parseInt( this.get('width'), 10 ) + 12;
					  var vidH = parseInt( this.get('height'), 10 ) + 12;
					  
					  if ( vidW == mediaW ) {
						  this.setStyle('opacity','1');
						  
					  } else {
					    var ratio = vidH / vidW;
					    var parentW = mediaW;	
					    this.set('width',parentW).set('height',(parentW * ratio)).setStyle('display','block');
					  }
					}); 
				}
			}
		}
  }
  
  // Run on load and resize
  Y.on(['load', 'resize'], mobileSet);
  
  
// ------------------------------------------------- Layout -------------------------------------------------- //

  var layoutSet = function() {
  
    if (document.documentElement.clientWidth > 500) {
      
      // Vertically align header elements
      
      var navW = parseInt(Y.one('#header .nav').get('offsetWidth')) + 20
   		var logoW = parseInt(Y.one('#header #logo').get('offsetWidth')) + 20
  		var headerW = parseInt(Y.one('#header .wrapper').getComputedStyle('width'))
  		
  		if ((logoW + navW) > headerW ) { 
  			Y.one('body').addClass('header-static'); 
  			
  		} else {
  			if (Y.one('.header-static')) { Y.one('body').removeClass('header-static'); }
  			Y.one('#header > .wrapper').setStyle('opacity','1');
  		}
  		
  		// Load social icons
  		
  		if (Y.one('#sqs-social-icons')) {
		   	Y.one('#sqs-footer-layout').addClass('social-on'); 
		  }
  	}	
  }
  
  Y.on(['load','resize'], layoutSet);
	
	if (Y.Squarespace.Management) {
		Y.Squarespace.Management.on('tweak', function(f){
			if (f.getName() == '@baseFontSize' || f.getName() == '@logoFontSize' || f.getName() == '@gutter' || f.getName() == '@vgrid' || f.getName() == '@grid') { layoutSet(); }
		});
	}  


// ------------------------------------------------- Media ------------------------------------------------- //	
	
	var mediaSet = function() {
        
    if (document.documentElement.clientWidth > 500) {
      
		  // Blog 
		  
		  if ((Y.one('.collection-type-blog')) || (Y.one('.collection-type-projects'))) {
		  		  	 
				Y.all('.entry').each(function() { 
				
					// Load images
	    
	    		new Y.Squarespace.Loader({ img: Y.all('.content-fit img') });
				
					// Load gallery
		  		
					if (this.hasClass('gallery')) {
											
						viewer = new Y.Squarespace.Gallery({
			        slideshowElement: this.one('#slides'),
			        previous: this.one('#prev-slide'),
			        next: this.one('#next-slide'),
			        loop: true,
			        keyboard: false,
			        design: 'strip',
			        designOptions: {
			        	direction: 'horizontal',
			          alignment: 'middle',
			          activeSlideBehavior: 'next',
			          fitImage: true
			        }    
			      }); 
		      }
				
					// Size and play videos
			     
					if ((this.hasClass('external-video')) && (Y.one('.main'))) {
								  	
				  	Y.one('.play').setStyle('opacity','1');
					
						Y.one('.external-video .main').on('click', function() { 
					
							this.setStyle('display','none');  
							var iframe = Y.one('.media iframe')	
							iframe.setStyle('display','block');
							
							var parentW	= parseInt( iframe.ancestor('.entry-content').getComputedStyle('width'), 10 )
							var vidW = parseInt( iframe.get('width'), 10 )
							var vidH = parseInt( iframe.get('height'), 10 )
							
							if (vidW != parentW) {
								var ratio = vidH / vidW;
								iframe.set('width',parentW).set('height',(parentW * ratio));
							}
							
							iframe.simulate('click');
						}); 
					
					} else if (this.hasClass('external-video')) { 
				
						Y.all('.external-video .media iframe').each(function(node, index, nodeList) {
						
							var iframe = node
							iframe.setStyle('display','block');
							var parentW	= parseInt( iframe.ancestor('.entry-content').getComputedStyle('width'), 10 )
							var vidW = parseInt( iframe.get('width'), 10 )
							var vidH = parseInt( iframe.get('height'), 10 )
							
							if (vidW != parentW) {
								var ratio = vidH / vidW;
								iframe.set('width',parentW).set('height',(parentW * ratio));
							} 
						});
					}
				});
			}
			
			// Gallery 
		
			if (Y.one('.collection-type-gallery')) {
			
				Y.all('#grid div').addClass('content-fill');
  			Y.all('#viewer div').addClass('content-fit');
	  	    	    	            
	      var thumbs = new Y.Squarespace.Gallery({
	        slideshowElement: '#grid',
	        design: 'off',
	        keyboard: false   
	      });
	      	    
	      Y.all('.close-slide, #viewer-bg').on('click', function() {
	      	Y.all('html, body').setStyle('overflow','auto');
		      Y.one('body').removeClass('view-mode');
		    });
		    
		    Y.one('#grid').delegate('click', function(e) {
		    
		    	Y.all('html, body').setStyle('overflow','hidden');
		    	var imageIndex = e.currentTarget.get('id').split('grid-')[1] - 1;
		    	Y.one('body').addClass('view-mode');
		      
		      var viewerEl = Y.one('#viewer');
		      
		      if (!viewerEl.getData('gallery')) {
				    viewerEl.setData('gallery', new Y.Squarespace.Gallery({
				      slideshowElement: '#viewer',
				      previous: '.prev-slide',
				      next: '.next-slide, .content-fit img',
				      keyboard: true,
				      loop: true,
				      design: 'stacked'      
				    }));
				  };
				  
				  viewerEl.getData('gallery').changeIndex(imageIndex);
				  
		    }, '.content-fill');	    		
			}   	
		}
	}
	
	// Run on load and tweak changes
					 
  Y.on('load', mediaSet);
  if (Y.Squarespace.Management) { 
  	Y.Squarespace.Management.on('tweak', function(f){
			if (f.getName() == 'sidebar' || f.getName() == 'header' || f.getName() == 'gutter' || f.getName() == 'content') { mediaSet(); }
		});
	}	
 	
});  // use
