jQuery(window).load(function(){

	jQuery('.main-col').stretchCol();	
	
});

jQuery(document).ready(function(){

	
	jQuery('body').fitVids();
	
	/**
	 * Subnav
	 */
	jQuery('.entry').subnav();
	
	/**
	 * Section headers
	 */
	jQuery('.entry').sectionHeaders();
	
	/**
	 * localScroll
	 */
	$.localScroll({
		easing: 'easeInOutExpo',
		duration: 500
	});
	
	/**
	 * Slideshows
	 */
	jQuery('.slideshow-link').slideshows();

	/**
	 * Make side col header image responsive
	 */
	jQuery('.side-col header img').stripDimensions();

});

(function($){

	$.fn.slideshows = function(){
	
		var links = $('.slideshow-link'),
				curtain = $('<div class="slideshow-curtain">'),
				slideshow = $('<div id="slideshow" data-kite-id="1">'),
				thumbnails = $('<div id="slideshow-thumbnails" data-kite-id="1" data-kite-module="thumbnails">');
		
		if(links.length){
			
			// prepend curtain to body
			$('body').prepend(curtain);
			
			// append slideshow to curtain
			curtain.append(slideshow).append(thumbnails);
			
			curtain.children().wrapAll('<div class="slideshow">');
			
		}
		
		links.click(function(event){
			
			event.preventDefault();
			
			var link = $(this);
			
			// Galleria
			slideshow.galleria({
				width: 550,
				height: 500,
				flickr: 'set:' + link.attr('data-set')
			});
			
			//loadSet(link.attr('data-key'), link.attr('data-set'));
			
			curtain.fadeIn(100);
		});
		
		curtain.click(function(event){
			if($(event.target).hasClass('slideshow-curtain')){
				curtain.fadeOut(100);
				slideshow.empty();
				thumbnails.empty();
			}
		});
	
	}; // $.fn.slideshows





	/**
	 * Strips dimensions from image
	 *
	 * Doing it with js since the multi post thumbnail plugin doesn't allow this
	 */
	$.fn.stripDimensions = function(){
		$(this).removeAttr('width').removeAttr('height');
	};
	
	
	
	
	/**
	 * Stretches content column to full height
	 */
	$.fn.stretchCol = function(){
		
		var col = $(this),
				parent = $(this).parent();
		
		if(col.outerHeight() < parent.innerHeight()){
			col.height(parent.innerHeight());
		}
	};
	
	
	
	
	
	/**
	 * Section headers
	 *
	 * Turns H2 tags prefixed with a # into a section header
	 * A horizontal border will be applied above the header
	 * and an ID will be given to the header
	 */
	$.fn.sectionHeaders = function(){
	
		var entry = $(this),
				h2s = entry.find('h2');
				
		h2s.each(function(){
			
			var h2 = $(this),
					text = h2.text();
			
			// exit if text isn't prefixed with a *
			if(!text.match(/^\#/)) return;
			
			// remove *
			text = text.replace(/^\#/, '');
			
			h2.attr('id', text.replace(/ /g, '-').toLowerCase()).text(text).addClass('top-border');
		});
	
	};
	
	/**
	 * Subnav
	 *
	 * Turns H1 tags prefixed with an * into subnav links
	 * This is used for the in-page season navigation for event listings
	 *
	 * Example:
	 * <h1>*Section One</h1>
	 */
	$.fn.subnav = function(){
	
		var entry = $(this),
				h1s = entry.find('h1'),
				links = '';
		
		h1s.each(function(){
			
			var h1 = $(this),
					text = h1.text();
			
			// exit if text isn't prefixed with a *
			if(!text.match(/^\*/)) return;
			
			// remove *
			text = text.replace(/^\*/, '');
			
			// make lower case and remove spaces
			link = text.replace(/ /g, '-').toLowerCase();
			
			// add to links array
			links += '<li><a href="#subnav-' + link + '">' + text + '</a></li>';
			
			// replace h1 with empty subnav list
			h1.replaceWith('<div id="subnav-' + link + '" class="subnav"><ul></ul></div>');
		});
		
		h1s.css('visibility', 'visible');
		
		// exit if no links
		if(links.length == 0) return;
		
		// redefine h1s as subnavs
		var subnavs = entry.find('.subnav');
		
		subnavs.each(function(){
		
			// append listItems
			$(this).find('ul').append(links);
			
			// find matching list item and activate
			$(this).find('a[href="#' + $(this).attr('id') + '"]').addClass('active');
			
			// add 'last' class to last list item
			$(this).find('li:last').addClass('last');
		});
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
})(jQuery);
