$(document).ready(function() {
	var height;
	var width;
	var scrollBar = $.getScrollbarWidth();
	var scrollY = 0;
	var oldWidth;
	var timeoutHandler;

	$(window).scroll(function() { 
    	scrollY = $(document).scrollLeft();
    });

	init();
	
	$(window).resize(function(ev) {
		clearTimeout(timeoutHandler);
		timeoutHandler = setTimeout(resizeImage, 10);
	});

	function init() {
		width = 0;
		height = $(window).height();

		if($.browser.msie) {
			$('#wrapper').css('height', height);
			$('img').css('height', height);
		} else {
			$('#wrapper').css('height', height-scrollBar);
			$('img').css('height', height-scrollBar);
		}

		$('img').load(function() {
			$(this).css('display', 'block');
			width += $(this).width();
			$('#wrapper').css('width', Math.ceil(width)+scrollBar);
			oldWidth = width;
		});
	}

	function resizeImage() {
		oldWidth = width;
		width = 0;
		height = $(window).height();

		$('#wrapper').css('height', height);
		$('img').css('height', height);

		$('img').each(function() {
			width += $(this).width();
			$('#wrapper').css('width', Math.ceil(width)+scrollBar);
		});
		
		setScroll();

	}

	function setScroll() {
		var m = (scrollY * width) / oldWidth;
		$(document).scrollLeft(m);
	}
});

