/*
 * jQuery Backstretch
 * Version 1.2.3
 * http://srobbin.com/jquery-plugins/jquery-backstretch/
 *
 * Add a dynamically-resized background image to the page
 *
 * Copyright (c) 2011 Scott Robbin (srobbin.com)
 * Dual licensed under the MIT and GPL licenses.
*/
(function(a){a.backstretch=function(k,i,l){function m(c){try{g={left:0,top:0},d=e.width(),b=d/j,b>=e.height()?(h=(b-e.height())/2,f.centeredY&&a.extend(g,{top:"-"+h+"px"})):(b=e.height(),d=b*j,h=(d-e.width())/2,f.centeredX&&a.extend(g,{left:"-"+h+"px"})),a("#backstretch, #backstretch img").width(d).height(b).filter("img").css(g)}catch(o){}typeof c=="function"&&c()}var n={centeredX:!0,centeredY:!0,speed:0},c=a("#backstretch"),f=c.data("settings")||n;c.data("settings");var e="onorientationchange" in window?a(document):a(window),j,d,b,h,g;i&&typeof i=="object"&&a.extend(f,i);a(document).ready(function(){if(k){var b;c.length==0?c=a("<div />").attr("id","backstretch").css({left:0,top:0,position:"fixed",overflow:"hidden",zIndex:-10,margin:0,padding:0,height:"100%",width:"100%"}):c.find("img").addClass("deleteable");b=a("<img />").css({position:"absolute",display:"none",margin:0,padding:0,border:"none",zIndex:-10}).bind("load",function(b){var d=a(this),e;d.css({width:"auto",height:"auto"});e=this.width||a(b.target).width();b=this.height||a(b.target).height();j=e/b;m(function(){d.fadeIn(f.speed,function(){c.find(".deleteable").remove();typeof l=="function"&&l()})})}).appendTo(c);a("body #backstretch").length==0&&a("body").append(c);c.data("settings",f);b.attr("src",k);a(window).resize(m)}});return this}})(jQuery);

$(document).ready(function() {
	
	// hide #sub-nav
	
	$('#sub-nav.hidden').hide();
	
	// set active on nav on scroll
	
	$('.waypoint').each(function(i) {
		$(this).waypoint(function(event, direction) {
			var j = (direction === 'up') ? i : i + 1;
			$('#sub-nav a').removeClass('selected').eq(j).addClass('selected');
		});
	});
	
	$('.blog #sub-nav a').eq(1).addClass('selected');
	
	$('#wp-content .post').each(function(i) {
		$(this).waypoint(function(event, direction) {
			var j = (direction === 'up') ? i : i + 1;
			if (j === 0) {
				j++;
			}
			$('#sub-nav a').removeClass('selected').eq(j).addClass('selected');
		});
	});
	
	// do a bunch of stuff on window resize
	
	if (!navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i)) {
	
	function setTopMargin() {
		var height = $(window).height();
		if (height > 708) {
			$('#services, #clients, #people').css('margin-top', height);
		}
		if (height > 803) {
			$('#contact').css('height', height - 95);
		}
		$('#google-map').css('height', height);		
	}
	
	setTopMargin();
	
	$(window).resize(function() {
		setTopMargin();
	});
	
	}
	
	// do a bunch of stuff on scroll
	
	$(window).scroll(function() {
		var height = $(window).height() - 50;
		if ($(window).scrollTop() > height) {
			$('#sub-nav.hidden').fadeIn(125);
		} else {
			$('#sub-nav.hidden').fadeOut(125);
		}
		$('#curl-home-top, #curl-clients-top, #curl-people-top, #curl-contact').css('top', -$(window).scrollTop());
	});
	
	// smooth scroll to #id
	
	$('a[href*=#]').click(function() {
		var url = $(this).attr('href');
		var position = url.indexOf('#');
		var url = url.substr(position);
		var documentHeight = $(document).height();
		var windowHeight = $(window).height();
		var destination = $(url).offset().top;
		if (destination > documentHeight - windowHeight) {
			$('html, body').animate({scrollTop: destination - (windowHeight - (documentHeight - destination))}, 1000, 'easeInOutQuart');
		} else {
			$('html, body').animate({scrollTop: destination}, 1000, 'easeInOutQuart');
		}
		return false;
	});
	
});
