(function($,wp,wps,window,undefined){'$:nomunge';var $w=$(window),eventName='waypoint.reached',triggerWaypoint=function(way,dir){way.element.trigger(eventName,dir);if(way.options.triggerOnce){way.element[wp]('destroy')}},waypointIndex=function(el,context){var i=context.waypoints.length-1;while(i>=0&&context.waypoints[i].element[0]!==el[0]){i-=1}return i},contexts=[],Context=function(context){$.extend(this,{'element':$(context),'oldScroll':-99999,'waypoints':[],didScroll:false,didResize:false,doScroll:$.proxy(function(){var newScroll=this.element.scrollTop(),isDown=newScroll>this.oldScroll,that=this,pointsHit=$.grep(this.waypoints,function(el,i){return isDown?(el.offset>that.oldScroll&&el.offset<=newScroll):(el.offset<=that.oldScroll&&el.offset>newScroll)}),len=pointsHit.length;if(!this.oldScroll||!newScroll){$[wps]('refresh')}this.oldScroll=newScroll;if(!len)return;if(!isDown)pointsHit.reverse();$.each(pointsHit,function(i,point){if(point.options.continuous||i===len-1){triggerWaypoint(point,[isDown?'down':'up'])}})},this)});$(context).scroll($.proxy(function(){if(!this.didScroll){this.didScroll=true;window.setTimeout($.proxy(function(){this.doScroll();this.didScroll=false},this),$[wps].settings.scrollThrottle)}},this)).resize($.proxy(function(){if(!this.didResize){this.didResize=true;window.setTimeout($.proxy(function(){$[wps]('refresh');this.didResize=false},this),$[wps].settings.resizeThrottle)}},this));$w.load($.proxy(function(){this.doScroll()},this))},getContextByElement=function(element){var found=null;$.each(contexts,function(i,c){if(c.element[0]===element){found=c;return false}});return found},methods={init:function(f,options){this.each(function(){var cElement=$.fn[wp].defaults.context,context,$this=$(this);if(options&&options.context){cElement=options.context}if(!$.isWindow(cElement)){cElement=$this.closest(cElement)[0]}context=getContextByElement(cElement);if(!context){context=new Context(cElement);contexts.push(context)}var ndx=waypointIndex($this,context),base=ndx<0?$.fn[wp].defaults:context.waypoints[ndx].options,opts=$.extend({},base,options);opts.offset=opts.offset==="bottom-in-view"?function(){var cHeight=$.isWindow(cElement)?$[wps]('viewportHeight'):$(cElement).height();return cHeight-$(this).outerHeight()}:opts.offset;if(ndx<0){context.waypoints.push({'element':$this,'offset':$this.offset().top,'options':opts})}else{context.waypoints[ndx].options=opts}if(f){$this.bind(eventName,f)}});$[wps]('refresh');return this},remove:function(){return this.each(function(i,el){var $el=$(el);$.each(contexts,function(i,c){var ndx=waypointIndex($el,c);if(ndx>=0){c.waypoints.splice(ndx,1)}})})},destroy:function(){return this.unbind(eventName)[wp]('remove')}},jQMethods={refresh:function(){$.each(contexts,function(i,c){var isWin=$.isWindow(c.element[0]),contextOffset=isWin?0:c.element.offset().top,contextHeight=isWin?$[wps]('viewportHeight'):c.element.height(),contextScroll=isWin?0:c.element.scrollTop();$.each(c.waypoints,function(j,o){var adjustment=o.options.offset,oldOffset=o.offset;if(typeof o.options.offset==="function"){adjustment=o.options.offset.apply(o.element)}else if(typeof o.options.offset==="string"){var amount=parseFloat(o.options.offset);adjustment=o.options.offset.indexOf("%")?Math.ceil(contextHeight*(amount/100)):amount}o.offset=o.element.offset().top-contextOffset+contextScroll-adjustment;if(c.oldScroll>oldOffset&&c.oldScroll<=o.offset){triggerWaypoint(o,['up'])}else if(c.oldScroll<oldOffset&&c.oldScroll>=o.offset){triggerWaypoint(o,['down'])}});c.waypoints.sort(function(a,b){return a.offset-b.offset})})},viewportHeight:function(){return(window.innerHeight?window.innerHeight:$w.height())},aggregate:function(){var points=$();$.each(contexts,function(i,c){$.each(c.waypoints,function(i,e){points=points.add(e.element)})});return points}};$.fn[wp]=function(method){if(methods[method]){return methods[method].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof method==="function"||!method){return methods.init.apply(this,arguments)}else if(typeof method==="object"){return methods.init.apply(this,[null,method])}else{$.error('Method '+method+' does not exist on jQuery '+wp)}};$.fn[wp].defaults={continuous:true,offset:0,triggerOnce:false,context:window};$[wps]=function(method){if(jQMethods[method]){return jQMethods[method].apply(this)}else{return jQMethods['aggregate']()}};$[wps].settings={resizeThrottle:200,scrollThrottle:100};$w.load(function(){$[wps]('refresh')})})(jQuery,'waypoint','waypoints',this);

jQuery.extend( jQuery.easing,{
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}
});

$(document).ready(function(){
	// Scrolling navigation
	$('#wrap > div').waypoint(function(e, d){
		$('#nav a').removeClass();
		$('#nav a[href$="'+$(this).attr('id')+'"]').addClass('active');
	}, {
		offset: '.5px',
		scrollThrottle: 200
	});
	
	var $off = 5;
	var $hash;
	$('#nav a,.scroll').live('click',function(e){
		e.preventDefault();
		$hash = $(this).attr('href');
		if($hash == '#about'){
			$off = 15;
		} else {
			$off = 5;
		}
		$('html,body').animate({scrollTop: $($hash).offset().top - $off}, 1000, 'easeOutQuint');
	});
	
	// Portfolio classes
	$('.piece:nth-child(3n+1)').addClass('first');
	$('.piece:nth-child(3n+3)').addClass('last');
	
	// Portfolio functionality
	$('.piece').hover(function(){
		$('h4',this).fadeIn(400);
	},function(){
		$('h4',this).hide();
	});
	
	$('.piece li').hover(function(){
		$(this).append('<span>'+$('img',this).attr('alt')+'</span>');
		$('span',this).css({'opacity':'0'}).animate({top:'-40px','opacity':'1'});
	},function(){
		$('span',this).remove();
	});
});
