/**
 * @author Tom Gangemi
 */
window.addEvent('domready', function() { 
	if ($('img-nav') != null) {
		//console.log(window.size().width);
		var originalWidth;
		var originalHeight;
		var loaded;
		var isResized = false;
		//$('image').setProperty('src', $('image').getProperty('src'));
		$('image').fireEvent('load');
		$('image').addEvent('load', function(){
			originalWidth = $('image').width;
			originalHeight = $('image').height;
			loaded = true;
			fitScreen($('image'));
			//Preload next image
			preload_image = new Image(25,25); 
      		preload_image.src = $('next-btn').name; 
		});
		var timer;
		window.addEvent('resize', function(){
			$clear(timer);
			timer = (function(){
				if (loaded) {
					fitScreen($('image'));
				}
			}).delay(50);
		});		
		addEvent('keydown', function(e){
			if (e.code == 37) 
				window.location = $('prev-btn').getProperty('href');
			if (e.code == 38) 
				window.location = $('album-btn').getProperty('href');
			if (e.code == 39) 
				window.location = $('next-btn').getProperty('href');
		});
	}
	else if ($('album-thumbs') != null) {
	
	}

	function fitScreen(img) {
		var swidth = window.size().width - 50;
		var sheight = window.size().height - 170;
		var wdiff = (originalWidth - swidth);
		var hdiff = (originalHeight - sheight);
//		if(wdiff<0)wdiff=-wdiff;
//		if(hdiff<0)hdiff=-hdiff;
	
		img.setStyle('width', '95%');
		img.setStyle('height', '95%');
		img.setStyle('max-width', originalWidth);
		img.setStyle('max-height', originalHeight);		

		//console.log('w:'+wdiff+' && h:'+hdiff);
			if(wdiff>hdiff) {
				img.setStyle('height', '');
				img.setStyle('width','95%');
			}
			else {
				img.setStyle('width', '');
				img.setStyle('height', sheight);
			}
	}
});

