jQuery(document).ready(function($){

	var section = $('body').data('section');
	switch(section){
	
		/* !Art */
		/* ------------------------------------------------------------------- */
		case "art":
			
			// Convert art list into Isotope radness
			$('#art-list').isotope({
				animationOptions: {
					duration: 600,
					easing: 'linear',
					queue: false
				},
				masonry : {
					columnWidth : 320
				}
			});
			
			// Activate Isotope filters
			var filterLinks = $('.filters a');
			filterLinks.live('click',function(){
				var self = $(this);
				var selector = self.attr('data-filter');
				filterLinks.removeClass('active');
				self.addClass('active');
				$('.isotope-items').isotope({ filter: selector });
				return false;
			});

		break;
	
		/* !Art > Entry */
		/* ------------------------------------------------------------------- */
		case "art/entry":
	
			// Keyboard navigation
			$(document.documentElement).keyup(function(e){

				// Left arrow key
				if (e.keyCode == 37) {
					$('#prev-entry').trigger('click');
				}

				// Right arrow key
				else if (e.keyCode == 39) {
					$('#next-entry').trigger('click');
				}

			});
		break;
	
		/* !Process */
		/* ------------------------------------------------------------------- */
		case "process":

			var wrapper = $('#process'),
			    wrapperWidth = 0,
			    limit = wrapper.find('.image').length - 1,
			    loadButton = $('#load-more');

			function resizeWrapper(){
				wrapperWidth = 0;
				var images = $('.image',wrapper);
				images.each(function(){
					var self = $(this);
					var width = self.outerWidth();
					wrapperWidth = wrapperWidth + width;
				});
				wrapper.width(wrapperWidth);
			}

			function fetchEntries(){
				var entryOffset = wrapper.find('.image').length - 1;
				$.ajax({
					url: '/process/ajax/'+limit+'/'+entryOffset,
					context: document.body,
					success: function(data){
						if(data.length == 0) {
							loadButton.hide();
						} else {
							$('.image.load').before(data);
							resizeWrapper();
						}
					}
				});
			}

			resizeWrapper();

			loadButton.click(function(e){
				e.preventDefault();
				fetchEntries();
			});


		break;
	
		/* !Store */
		/* ------------------------------------------------------------------- */
		case "store":

			// Convert product list into Isotope radness
			$('#product-list').isotope({
				animationOptions: {
					duration: 600,
					easing: 'linear',
					queue: false
				},
				masonry : {
					columnWidth : 320
				}
			});

			// Activate Isotope filters
			var filterLinks = $('.filters a');
			filterLinks.live('click',function(){
				var self = $(this);
				var selector = self.attr('data-filter');
				filterLinks.removeClass('active');
				self.addClass('active');
				$('.isotope-items').isotope({ filter: selector });
				return false;
			});

		break;
	
		/* !Blog */
		/* ------------------------------------------------------------------- */
		case "blog":

			// Wrap text nodes in a span so that text width can be controlled while
			// image/video width stays fluid
			var getTextNodesIn = (function() {
				function textNodeFilter() {
					return this.nodeType == 3;
				}
				return function(el) {
					var $el = $(el);
					return $el
						.contents()
						.filter(textNodeFilter)
						.add(
							$el
								.find("*")
								.contents()
								.filter(textNodeFilter)
						);
				};
			})();
			var copy = $('.copy');
			var textNodes = getTextNodesIn(copy);
			textNodes.each(function(){
				var self = $(this);
				self = self.wrap('<span class="text-node" />');
				console.log(self);
			});

		break;
	
		/* !About */
		/* ------------------------------------------------------------------- */
		case "about":
		break;
	
		/* !Homepage */
		/* ------------------------------------------------------------------- */
		// default:
	}

});
