$(document).ready(function() { 
  
  $(window).scroll(function () {positionContent();});
  
  positionContent();
  
  $('img.delay').each(function() {
  	$(this).attr('src', $(this).attr('delaysrc'));
  });
  
  // Load background images now afar page has loaded
  $('#space_shuttle').css('background-image', 'url(/images/site/SSAni.jpg)');
  $('#neuron').css('background-image', 'url(/images/site/OptogeneticsSprite.jpg)');
  
  $('div.sb').click(function(){
    $(window).scrollTop((position[this.id]) + diff[this.id]);
    positionContent();
  	updateNav(this);
  });
      
});

var position = new Array(0, 300, 600, 1500, 2500, 3200, 4000);
var diff   = new   Array(0, 298, 698,  998,  698,  798, 99);

function updateNav(navBut) {
	$('.sb').css('background-position', '0px 0px');
	$(navBut).css('background-position', '-20px 0px');
}

function showSection (section) {
	
	$('.' + section).slideDown(1000);
	

	if ($('.' + section).prev('div').hasClass('enabled')) {
		$('.' + section).prev('div').slideUp(1000);
		$('.' + section).prev('div').removeClass('enabled');
	}

	if (section == 'section0') {
		$('.section1').addClass('enabled');
		$('.section1').slideDown(1000);
		$('.' + section).next().nextAll('div').removeClass('enabled');
		$('.' + section).next().nextAll('div').fadeOut();
	}
	else {
		$('.' + section).nextAll('div').removeClass('enabled');
		$('.' + section).nextAll('div').fadeOut();
		$('.' + section).prev().prevAll('div').removeClass('enabled');
		$('.' + section).prev().prevAll('div').fadeOut();
	}
			
	if (! $('.' + section).hasClass('enabled')) {
		$('.' + section).addClass('enabled');
	}
	
	updateNav("." + section + "but");

}

function positionContent() {

  var windowHeight = $(window).height();
  var offset = $(window).scrollTop();
  var userAgent = window.navigator.userAgent;
	
	if (offset < position[1]) {showSection('section0');}
   	else if (offset >= position[1] && offset < position[2]) {
   		showSection('section1');
   		$('.research_tag').fadeIn(600);
   	}
	else if (offset > position[2] && offset < position[3]) {showSection('section2');}
	else if (offset > position[3] && offset < position[4]) {showSection('section3');}
	else if (offset > position[4] && offset < position[5]) {showSection('section4');}
	else if (offset > position[5] && offset < position[6]) {showSection('section5');}
	else if (offset > position[6]) {showSection('section6');}
	
	// Cheetah Block
    $(".section0").css("margin-left", (0 + (offset)*3) + "px");
    
    
      
    /* Movement from the left formula: (endMargin - MAX(0, (width+distToMove - MAX(0, offset-start) * rate))) */
    
    // Research Clinical Block
	//$("#rsch_image").css({
    //  'top':  Math.max(0, (160 - Math.max(0, offset-25) * 0.25) ) + "px"
    //});
	
	// Software Block 
	$('#monitor1').css({
		left: (470 - Math.max(0, (870 - Math.max(0, offset-700) * 3) ) ) + "px"
	});
	$('#monitor2').css({
		left: (216 - Math.max(0, (900 - Math.max(0, offset-900) * 3) ) ) + "px"
	});
	$('#monitor3').css({
		left: (-43 - Math.max(0, (460 - Math.max(0, offset-1100) * 3) ) ) + "px"
	});

	// Choose us Block
	$('.br1').css({ left: Math.max(0, (600 - Math.max(0, offset - 1700) * 20)) + "px" });
	$('.bl1').css({ right: Math.max(0, (600 - Math.max(0, offset - 1800) * 20)) + "px" });
	$('.bl2').css({ right: Math.max(0, (600 - Math.max(0, offset - 1900) * 20)) + "px" });
	$('.br2').css({ left: Math.max(0, (600 - Math.max(0, offset - 2000) * 20)) + "px" });
	$('.br3').css({ left: Math.max(0, (600 - Math.max(0, offset - 2100) * 20)) + "px" });
	$('.bl3').css({ right: Math.max(0, (600 - Math.max(0, offset - 2200) * 20)) + "px" });
	$('.bl4').css({ right: Math.max(0, (600 - Math.max(0, offset - 2300) * 20)) + "px" });
	$('.br4').css({ left: Math.max(0, (600 - Math.max(0, offset - 2400) * 20)) + "px" });
	
	// History Block
    $('#space_shuttle').css('background-position', function () {
		var raw = (offset - 2600);
		if (raw < 0) {
			return '0px 0px';
		}
		else if (raw > 465) {
			return '-17700px 0px';
		}
		else {
			return -((Math.round(raw/8) + 1) * 300) + 'px 0px';
		}
	});
	
	// Optogenetics Block
	$('#neuron').css('background-position', function () {
		var raw = (offset - 3300);
		if (raw < 0) {
			return '0px 0px';
		}
		else if (raw > 679) {
			return '-27360px 0px';
		}
		else {
			return -((Math.round(raw/12) + 1) * 480) + 'px 0px';
		}
	});
  
}

