$(document).ready(function() {
	
	$("input").bind('focus blur', function(event) {
		if (event.type == 'focus') {
			$(this).parent().addClass('focused');
		} else {
			$(this).parent().removeClass('focused');
		}
	});
	$("input").hover( function(event) {
		$(this).parent().addClass('hovered');
	}, function() {
		$(this).parent().removeClass('hovered');
	});
	$("section .pattern").hover(function() {
		$(this).toggleClass('hover');
	});
	
	$("body#home").each( function(){
		jQuery.preLoadImages("../media/images/usps-quick-h.png", "../media/images/usps-better-h.png", "../media/images/usps-control-h.png", "../media/images/usps-standards-h.png");
	});
	
	/* add smooth scrolling to the components page */
	$("body#components aside nav li a").removeAttr('href');
	$("body#components aside nav li").click(function() {
		$.scrollTo('article.'+$(this).attr('class').split(' ')[0]+'', 250);
	});
	
	$(window).scroll( function() {
		if ($(this).scrollTop() > 70) {
			$("aside nav").addClass("scrolled");
		} else {
			$("aside nav").removeClass("scrolled");
		}

		if ($(this).scrollTop() < 440) {
			$("aside nav ol.page-nav > li").removeClass("active");
		}
		if ($(this).scrollTop() > 440) {
			$("aside nav ol.page-nav > li:nth-child(1)").addClass("active").siblings().removeClass("active");
		}
		if ($(this).scrollTop() > 975) {
			$("aside nav ol.page-nav > li:nth-child(2)").addClass("active").siblings().removeClass("active");
		}
		if ($(this).scrollTop() > 1525) {
			$("aside nav ol.page-nav > li:nth-child(3)").addClass("active").siblings().removeClass("active");
		}
		if ($(this).scrollTop() > 2080) {
			$("aside nav ol.page-nav > li:nth-child(4)").addClass("active").siblings().removeClass("active");
		}
	});
	
	$(".pattern .toggle, .pattern img").click( function() {
		$(this).toggleClass("rotate");
		$(this).siblings().toggleClass("rotate");
	});
	
	$("body#enterprise").each(function() {
		
		$(window).scroll( function() {
			var pos = $(this).scrollTop();
			if ($(this).scrollTop() > 1000 && $(this).scrollTop() < 1250) {
				
				var newPos = (((pos-1000)*3.175+320 > 900) ? '900' : (pos-1000)*3.175+320);
				$("img#planet-4").css('left',newPos+'px');
				
				var newPos = ((pos-1000)*2+240 > 600 ? '600' : ((pos-1000)*2)+240);
				$("img#planet-3").css('left',newPos+'px');
				
				var newPos = ((pos-1000)*1.1+120 > 300 ? '300' : (pos-1000)+120);
				$("img#planet-2").css('left',newPos+'px');
			}
		});
	});
	
});

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


