$(document).ready(function(){

	mobile = ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)));
	
	$('#first-cycle').cycle({
		fx:'scrollUp',
		containerResize:0,
		fit:1,
		slideResize: 0,
		next: '#first .next-btn',
		timeout:0, 
		easeIn:'easeInSine',
		easeOut:'easeOutSine',
		speed:300
	});
	
	$('#second-cycle').cycle({
		fx:'scrollRight',
		containerResize:0,
		fit:0,
		slideResize: 0,
		next: '#second .next-btn',
		prev: '#second .prev-btn',
		timeout:0, 
		easeIn:'easeInOutSine',
		easeOut:'easeInOutSine',
		speed:600
	});
	
	$('#third-cycle').cycle({
		fx:'fade',
		containerResize:0,
		fit:0,
		slideResize: 0,
		timeout:3000, 
		speed:600
	});
	
	$('.nav a').click(function(){
		var loc = $(this).attr('rel');
		
		$('html,body').animate({
			scrollTop: $("#"+loc+"").offset().top
		}, 2000, 'easeInOutCubic');
		
		return false;
	});
	
	$('article').bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
	  if (isInView) {
	    // element is now visible in the viewport
	    if (visiblePartY == 'top') {
	        
	    } else if (visiblePartY == 'bottom') {
	      // bottom part of element is visible
	    } else {
	      // whole part of element is visible
	      var loc = $(this).closest('section').attr('id');
		  $('.nav .active').removeClass('active');
		  $('.nav li a[rel="'+loc+'"]').parent().addClass('active');
		  
	    }
	  } else {
	    // element has gone out of viewport
	  }
	});
	
	$('.timeline article').css('opacity', 0);
	
	$('.timeline article').each(function(){
		var pos = $(this).position().left;
		$(this).data('init-pos', pos);
		
		if ($(this).hasClass('left-slide')){
			$(this).css('left', pos-100+'px');
		} else if ($(this).hasClass('right-slide')){
			$(this).css('left', pos+100+'px');
		};
	});
	
	$('.timeline article').bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
	  if (isInView) {
	    // element is now visible in the viewport
	    if (visiblePartY == 'top') {
	     
	    } else if (visiblePartY == 'bottom') {
	      // bottom part of element is visible
	    } else {
	      // whole part of element is visible
	      $(this).animate({left: $(this).data('init-pos')+'px', opacity: 1}, 'slow', 'easeOutSine');
	    }
	  } else {
	    // element has gone out of viewport
	  }
	});

	$window = $(window);
	
	$('[data-type]').each(function() {	
		$(this).data('offset', parseInt($(this).attr('data-offset')));
	});
	
	$('section[data-type="background"]').each(function(){
		
		var $self = $(this),
			offsetCoords = $self.offset(),
			topOffset = offsetCoords.top;
			
		if (mobile) {  
	    	$self.css({backgroundAttachment:'scroll',backgroundSize:'auto 100%'})
	    } else {
	    	$(window).scroll(function() {
			
				if ( ($window.scrollTop() + $window.height()) > (topOffset) &&
					 ( (topOffset + $self.height()) > $window.scrollTop() ) ) {
									
					var yPos = -($window.scrollTop() / 4); 
					
					yPos += 250;
					
					var coords = '50% '+ yPos + 'px';
	
					$self.css({ backgroundPosition: coords });
					
					$('[data-type="object"]', $self).each(function() {
						
						var $object = $(this);
						
						var yPos = -($window.scrollTop() / 1.5);					
						var coords = (yPos + $object.data('offset')) + 'px';
						
						$object.css({ top: coords });													
						
					});
				
				};
		
			});
	    }
		
	});
	
	$(window).scroll(function() {
		
		var imgLoc = $('.person-img').css('top');
		var imgLoc = imgLoc.replace("px", "");
		
		if (imgLoc >= 65){
			$('.person-img').css('background-image', 'url(img/person-img-3.png)');
		}
		
		if(imgLoc >= 100){
			$('.person-img').css('background-image', 'url(img/person-img-2.png)');
		}
		
		if(imgLoc >= 135){
			$('.person-img').css('background-image', 'url(img/person-img-1.png)');
		}
		
		if(imgLoc >= 170){
			$('.person-img').css('background-image', 'url(img/person-img.png)');
		}		

	});
	
});


