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

	/* !Global scripts */
	/* ------------------------------------------------------------------- */

	$("a[rel=external]").attr("target","_blank");
	

	/* !Homepage */
	/* ------------------------------------------------------------------- */

	// Convert art list into Isotope radness
	var pageId = $('body').attr('data-pageid');

	if(pageId == "homepage"){

		/* !Randomize the thumbnails */
		if($(window).width() > 480){
			$('#thumbnails ul.picture-entries').randomize('li.picture');
			$('#thumbnails ul.picture-entries li.title').remove();
		}

		/* !Homepage thumb effects */
		var navLinks =	 $('#picture-categories a'),
			thumbs = $('#thumbnails .picture-entries li'),
			hoveredCategory = null,
			transparencyVal = "0.15",
			opacityVal = "1.0";
	
		// Function that hightlights focused nav and thumbs
		var highlightCategory = function(categoryClass){
			if(categoryClass){
				navLinks.filter(categoryClass).stop().fadeTo(300,opacityVal);
				navLinks.not(categoryClass).stop().fadeTo(300,transparencyVal);
				thumbs.filter(categoryClass).stop('true','true').fadeTo(300,opacityVal);
				thumbs.not(categoryClass).stop('true','true').fadeTo(300,transparencyVal);
				hoveredCategory = categoryClass;
			}
		};
	
		// Function that blurs all nav links and thumbs
		var blurAllCategories = function(){
			navLinks.stop().fadeTo(300,opacityVal);
			thumbs.stop().fadeTo(300,opacityVal);
			hoveredCategory = null;
		};
	
		// Fade thumbs that aren't in 'this' photos's category on hover
		navLinks.hover(
			function(){
				var self = $(this);
				var selfClass = $(this).attr('data-category');
				highlightCategory(selfClass);
				// navLinks.fadeTo(300,0.15).removeClass('selected');
				// self.fadeTo(300,1).addClass('selected');
			},
			function(){blurAllCategories();}
		);
		thumbs.hover(
			function(){
				var selfClass = $(this).attr('data-category');
				if(hoveredCategory != selfClass){highlightCategory(selfClass);}
			}
		);
		// Fade all thumbs to full opacity when the mouse leaves the thumbnail area
		$('#thumbnails .picture-entries').mouseleave(function(){blurAllCategories();});
	
	}


	/* !Content Slides */
	/* ------------------------------------------------------------------- */

	if((pageId == "category") || (pageId == "blog")){

		// Easing equation, borrowed from jQuery easing plugin
		// http://gsgd.co.uk/sandbox/jquery/easing/
		jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		};
		
		/**
		 * Most jQuery.serialScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
		 * @see http://flesler.demos.com/jquery/scrollTo/
		 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.serialScroll.
		 *
		 * The plugin binds 6 events to the container to allow external manipulation.
		 * prev, next, goto, start, stop and notify
		 * You use them like this: $(your_container).trigger('next'), $(your_container).trigger('goto', [5]) (0-based index).
		 * If for some odd reason, the element already has any of these events bound, trigger it with the namespace.
		 */
		
		// Content slide object 
		var contentSlides = {
			self: $('#content-slides-wrapper'),
			slides: $('#content-slides .slide'),
			initialSlide: (window.location.hash) ? $(window.location.hash) : $('#content-slides .slide:first'), // If there's a hash, store the value as a jQuery object. If not, store the first slide.
			activeSlide:	(window.location.hash) ? $(window.location.hash) : $('#content-slides .slide:first'), // Initial value is equal to contentSlides.initialSlide.object
			offset: function(slide){
				var browserWidth = $(window).width();
				var slideWidth = slide.outerWidth();
				var thisOffset = Math.round((Math.abs((browserWidth - slideWidth) * 0.5)) * -1);
				return thisOffset;
			},
			controls: {
				prev: {
					object: $('a#prev-slide'),
					keyCode: 37
				},
				next: {
					object: $('a#next-slide'),
					keyCode: 39
				}
			}
		};
	
		// Give the initial slide a visual indicator
		contentSlides.initialSlide.addClass('active');

		// Override scrollTo's default offset value
		// $.scrollTo.defaults.offset = contentSlides.offset($('#content-slides > .slide.active:first'));
		
		// Set default serialScroll options
		var serialScrollOptions = {
			axis: 'x',
			items:'.slide',
			// offset: contentSlides.offset($('#content-slides > .slide.active:first')),
			offset: -$(window).width() / 2,
			over:0.5,
			start: contentSlides.slides.index(contentSlides.initialSlide), // Get the index of the initial slide,
			duration: 400,
			prev: contentSlides.controls.prev.object,
			next: contentSlides.controls.next.object,
			force:true,
			stop:true,
			lock:false,
			cycle:false, //don't pull back once you reach the end
			easing:'easeOutQuart' //use this easing equation for a funny effect
		};
		
		// Initialize serialScroll
		contentSlides.self.serialScroll(serialScrollOptions);
		
		// Prev/Next links
		contentSlides.controls.prev.object.add(contentSlides.controls.next.object).click(function(){
			contentSlides.activeSlide = $('#content-slides > .slide.active:first'); // Determine which slide is active
			var trigger = $(this).attr('data-trigger');
			switch(trigger){
				case "prev":
					if(contentSlides.activeSlide.prev().length > 0){
						contentSlides.activeSlide.removeClass('active').prev().addClass('active');
						contentSlides.activeSlide = contentSlides.activeSlide.prev();
					}
					break;
				case "next":
					if(contentSlides.activeSlide.next().length > 0){
						contentSlides.activeSlide.removeClass('active').next().addClass('active');
						contentSlides.activeSlide = contentSlides.activeSlide.next();
					}
					break;
			}
			// contentSlides.self.serialScroll(serialScrollOptions).trigger('notify',$('#content-slides > .slide.active:first'));
			// contentSlides.self.serialScroll(serialScrollOptions).trigger(trigger);
			var scrollTop = $(window).scrollTop();
			window.location.hash = contentSlides.activeSlide.attr('id');
			$(window).scrollTop(scrollTop);
			return false;
		});
	
		// Keyboard navigation
		$(document.documentElement)
			.keydown(function(e){
				if (e.keyCode == contentSlides.controls.prev.keyCode) { // Left arrow key
					contentSlides.controls.prev.object.trigger('click').addClass('active');
					return false;
				} else if (e.keyCode == contentSlides.controls.next.keyCode) { // Right arrow key
					contentSlides.controls.next.object.trigger('click').addClass('active');
					return false;
				}
			})
			.keyup(function(e){
				if (e.keyCode == contentSlides.controls.prev.keyCode) { // Left arrow key
					contentSlides.controls.prev.object.removeClass('active');
					return false;
				} else if (e.keyCode == contentSlides.controls.next.keyCode) { // Right arrow key
					contentSlides.controls.next.object.removeClass('active');
					return false;
				}
			})
		;

	}

});
