/* Author: Christoph Knoth */

var windowWidth = $(window).width(); //retrieve current window width
var windowHeight = $(window).height(); //retrieve current window height
var documentWidth = $(document).width(); //retrieve current document width
var documentHeight = $(document).height(); //retrieve current document height
//var vScrollPosition = $(document).smoothScrollp(); //retrieve the document scroll ToP position
var hScrollPosition = $(document).scrollLeft(); //retrieve the document scroll Left position

var topoffset = -0.05; 

var windowRatio = windowWidth / windowHeight

var maxRatio = 0.85

function onm_window_parameters(){ //called on viewer reload, screen resize or scroll
    windowWidth = $(window).width(); //retrieve current window width
    windowHeight = $(window).height(); //retrieve current window height
    documentWidth = $(document).width(); //retrieve current document width
    documentHeight = $(document).height(); //retrieve current document height
    //vScrollPosition = $(document).smoothScrollp(); //retrieve the document scroll ToP position
    hScrollPosition = $(document).scrollLeft(); //retrieve the document scroll Left position
}; //end function onm_window_parameters()


//make all images in a size in a relation to the browser window

function reSetImages() {
	$(".single figure").each(
    // For each img, run this code. The "indIndex" is the
    // loop iteration index on the current element.
    function( intIndex ){
	    var ratio = 0;  // Used for aspect ratio
	    var width = $(this).find('img').width();    // Current image width
	    var height = $(this).find('img').height();  // Current image height
	
		windowRatio = windowWidth / windowHeight;

		ratio = width / height;
		
		//if window width is bigger than image
		if (windowRatio > ratio) {
			var newImageWidth = windowHeight * ratio * maxRatio;
			var newImageHeight = windowHeight * maxRatio;
		}
		else {
			var newImageWidth = windowWidth * maxRatio;
			var newImageHeight = windowWidth / ratio * maxRatio;
		}
		
		$(this).css({"width": newImageWidth, "height" : windowHeight}); // Set new width and height
		$(this).find("img").css({"width": newImageWidth, "height": newImageHeight}); // Set new width and height
      });
}




	var manualscroll = true;
	var numberitem = 0;
	
	(function(){
	            jQuery(window).bind('scrollstop', function(e){
	            	
	            	onm_window_parameters();
	            	//console.log("scrollpos: " + (vScrollPosition + windowHeight) + "  documentHeight:" + documentHeight);
	            	
	            	if ((manualscroll === true) && (vScrollPosition > 10) && ((vScrollPosition + windowHeight) < (documentHeight-40)) ) {
						
						//vScrollPosition - (vScrollPosition) % windowHeight + 220
						numberitem = Math.round((vScrollPosition - 220) / windowHeight);
						//console.log(numberitem);
						
						manualscroll = false;
						
						//$('figure:eq(' + numberitem + ')').smoothScroll({duration: 400, easing: 'linear'});
						$.smoothScroll( 'figure:eq(' + numberitem + ')', 400, {'axis':'y'});
	
						setTimeout("manualscroll = true", 800);
						}
					
	            });
	            
	        })();
	 
	

$(document).ready(function() {
	
		onm_window_parameters();
		reSetImages();
		
		// reset scroll on refreshes
		// Reset all scrollable panes to (0,0)
		//$('div.pane').smoothScroll( 0 );
		// Reset the screen to (0,0)
		//$.smoothScroll( 0, {'axis':'y'});
	
		manualscroll = false;
	
		//$("figure").smoothScroll({duration: 400, easing: 'linear'});

		//$.smoothScroll({ scrollTarget: $("figure") });
							

		//setTimeout("manualscroll = true", 800);
	}
);



$(window).resize(function() {
		onm_window_parameters();
		reSetImages();  
	});

	
	//on click scroll 
	 $(function(){
	    $(".single figure img").click(function(event) {
	       
	       //$(this).parent().next().smoothScroll({duration: 400, easing: 'linear'});
			$.smoothScroll($(this).parent().next(), 400, {'axis':'y'});
	       
	        manualscroll = false;
			setTimeout("manualscroll = true", 800);
	    });
	});



	//on key down scroll
	$(document).bind('keydown', function(e) {
	if (e.keyCode == 75 || e.keyCode == 37 || e.keyCode == 38) {
	    //press the letter K or left or up 38
	
	    	onm_window_parameters();
	        //vScrollPosition - (vScrollPosition) % windowHeight + 220
			numberitem = Math.round((vScrollPosition - 220 - windowHeight) / windowHeight);
			console.log(numberitem);
			
			//$('figure:eq(' + numberitem + ')').smoothScroll({duration: 400, easing: 'linear'});
			$.smoothScroll( 'figure:eq(' + numberitem + ')', 400, {'axis':'y'});
						
			manualscroll = false;
			setTimeout("manualscroll = true", 600);
	
	} else if (e.keyCode == 74 || e.keyCode == 39 || e.keyCode == 40) {
	    //press the letter J or right or down 40
	
	        onm_window_parameters();
	        numberitem = Math.round((vScrollPosition - 220 + windowHeight) / windowHeight);
			console.log(numberitem);
			
			//$('figure:eq(' + numberitem + ')').smoothScroll({duration: 400, easing: 'linear'});
			$.smoothScroll( 'figure:eq(' + numberitem + ')', 400, {'axis':'y'});
						
			manualscroll = false;
			setTimeout("manualscroll = true", 600);
	
		}
	});

