$(document).ready(function() {

    $("a.iframelink").fancybox({
    	'width'			: '75%',
    	'height'			: '75%',
    	'autoScale'			: false,
    	'transitionIn'		: 'none',
    	'transitionOut'		: 'none',
    	'type'				: 'iframe'
    });
    $("a.imglink").fancybox({
    	'width'			: '75%',
    	'height'			: '75%',
    	'autoScale'			: false,
    	'transitionIn'		: 'none',
    	'transitionOut'		: 'none'
    });
});

function scrollToUp () {
  scrollTop = $(window).scrollTop();

  var scrollToThis = null;

  // Find the last element with class 'frame' that isn't on-screen:
  $('.frame').each(function(i, h2) {
    h2top = $(h2).offset().top;
    if (scrollTop > h2top) {
      // This one's not on-screen - make a note and keep going:
      scrollToThis = h2;
    } else {
      // This one's on-screen - the last one is the one we want:
      return false;
    }
  });

  // If we found an element in the loop above, scroll to it:
  if(scrollToThis != null) {
    $.scrollTo(scrollToThis, 800);
  }
}

function scrollToDown () {
  scrollTop = $(window).scrollTop();
  $('.frame').each(function(i, h2){ // loop through article headings
    h2top = $(h2).offset().top; // get article heading top
    if (scrollTop < h2top) { // compare if document is below heading
      $.scrollTo(h2, 800); // scroll to in .8 of a second
      return false; // exit function
    }
  });
}

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

jQuery(function( $ ){
	$('html').serialScroll({
		items:'.frame', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		navigation:'#nav a',
		axis:'y',// The default is 'y' scroll on both ways
		duration:750,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		cycle:false, //don't pull back once you reach the end
	});
	$('body').serialScroll({
		target:'#home',
		items:'.otherframe', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'#home .leftbutton',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'#home .rightbutton',// Selector to the 'next' button (absolute too)
		axis:'x',// The default is 'y' scroll on both ways
		duration:1550,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		cycle:false, //don't pull back once you reach the end
	});
	$('body').serialScroll({
		target:'#web',
		items:'.otherframe', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'#web .leftbutton',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'#web .rightbutton',// Selector to the 'next' button (absolute too)
		axis:'x',// The default is 'y' scroll on both ways
		duration:750,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		cycle:false, //don't pull back once you reach the end
	});
	$('body').serialScroll({
		target:'#film',
		items:'.otherframe', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'#film .leftbutton',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'#film .rightbutton',// Selector to the 'next' button (absolute too)
		axis:'x',// The default is 'y' scroll on both ways
		duration:750,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		cycle:false, //don't pull back once you reach the end
	});
	$('body').serialScroll({
		target:'#graphics',
		items:'.otherframe', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		prev:'#graphics .leftbutton',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'#graphics .rightbutton',// Selector to the 'next' button (absolute too)
		axis:'x',// The default is 'y' scroll on both ways
		duration:750,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		cycle:false, //don't pull back once you reach the end
	});


  function adjustStyle(width) {
    width = parseInt(width);
    if (width < 601) {
        $("#size-stylesheet").attr("href", "/2_0/narrow_css");
    } else if ((width >= 601) && (width < 1024)) {
        $("#size-stylesheet").attr("href", "/2_0/medium_css");
    } else {
       $("#size-stylesheet").attr("href", "/2_0/wide_css");
    }
  }

  $(function() {
    adjustStyle($(this).width());
    $(window).resize(function() {adjustStyle($(this).width());});
  });


  $(document).keydown(function (evt) {
    if (evt.keyCode == 40) { // down arrow
      evt.preventDefault(); // prevents the usual scrolling behaviour
      scrollToDown(); // scroll to the next new heading instead
    } else if (evt.keyCode == 38) { // up arrow
      evt.preventDefault();
      scrollToUp();
    } else if (evt.keyCode == 37) { // left arrow
      evt.preventDefault();
      scrollToUp();
    } else if (evt.keyCode == 39) { // right arrow
      evt.preventDefault();
      scrollToDown();
    }
  });


});

