/**
 * @author Tom Gangemi
 */

$(document).ready(function() {
	if($("#img-nav").length > 0) {
		var originalWidth;
		var originalHeight;
		var loaded;
		var isResized = false;
		// On load of image get its dimensions
		$('#image').load(function(){
		
			originalWidth = $('#image').width;
			originalHeight = $('#image').height;
			loaded = true;
			fitScreen($('#image'));
			
			// Preload next image
			preloadImage = new Image(25,25); 
      		preloadImage.src = $('next-btn').name; 			
		});
		
		// Resize image on window resize event
		$(window).resize(function() {
			if (loaded) {

				fitScreen($('#image'));
			}
		});	
		
		// Handle keyup event
		$(document).keyup(function(e) {
			if (e.keyCode == 37) 
				window.location = $('#prev-btn').attr('href');
			if (e.keyCode == 38) 
				window.location = $('#album-btn').attr('href');
			if (e.keyCode == 39) 
				window.location = $('#next-btn').attr('href');
		});		
	}

	function fitScreen(img) {
		return;
		//var displayAreaWidth = window.size().width - 50;
		//var displayAreaHeight = window.size().height - 170;
		var displayAreaWidth = $(window).width(); - 50;
		var displayAreaHeight = $(window).height(); - 170;
		
		
		var diffWidth = (originalWidth - displayAreaWidth);
		var diffHeight = (originalHeight - displayAreaHeight);
//		if(wdiff<0)wdiff=-wdiff;
//		if(hdiff<0)hdiff=-hdiff;
	
		img.css('width', '95%');
		img.css('height', '95%');
		img.css('max-width', originalWidth);
		img.css('max-height', originalHeight);		

		minWidth = 200;
		minHeight = 200;			

		//console.log('w:'+wdiff+' && h:'+hdiff);
		//if(diffWidth > 0 || diffHeight > 0) {
		if(diffHeight > 0) {
			// If display area is smaller than image width or height
			if(diffWidth > diffHeight) {
				img.css('height', '');
				img.css('width','95%');
			}
			else {
				img.css('width', '');
				img.css('height', displayAreaHeight);
			}
		}
	}
});


