var windowWidth = 0;

$(document).ready(function(){
	resizeImage();
	resizeImage();


	$(window).scroll(function(){
		var scrLeft = $(this).scrollLeft();
		var overLeftPos = scrLeft>=windowWidth-10;
		var atEnd = scrLeft >= $('#site-container').width()-windowWidth*1.5;
		if (!atEnd) $('#header').fadeIn('fast');
		else $('#header').fadeOut('fast');
		if (overLeftPos) $('#previous-piece').fadeIn('fast');
		else $('#previous-piece').fadeOut('fast');
		if (atEnd) $('#next-piece').fadeOut('fast');
		else $('#next-piece').fadeIn('fast');
	})

	$('#next-piece').click(function(){
		var next = Math.floor($(window).scrollLeft()/windowWidth)+1;
		$('div.section-container:eq('+(next-1)+')').children('.description').hide();
		$('html,body').scrollTo('div.section-container:eq('+next+')',500,{axis:'x',easing:'swing',onAfter:function(){
			$('div.section-container:eq('+next+')').children('.description').fadeIn('medium');
		}});
	})
	
	$('#previous-piece').click(function(){
		var prev = Math.ceil($(window).scrollLeft()/windowWidth)-1;
		$('div.section-container:eq('+(prev+1)+')').children('.description').hide();
		$('html,body').scrollTo('div.section-container:eq('+prev+')',500,{axis:'x',easing:'swing',onAfter:function(){
			$('div.section-container:eq('+prev+')').children('.description').fadeIn('medium');
		}});
	})
	
	// scrollBarWidth = $.getScrollbarWidth();
	
})

$(window).resize(function(){
	resizeImage();
});

function resizeImage(){
	windowWidth = $(window).width();
	//	var heightChange = -0.369*(691-$(window).height());
	var heightChange = -0.369*(850-$(window).height());
	$('.section-container').css('width',windowWidth);
	$('#site-container').css('width',windowWidth*$('.section-container').length);
	$('.main-title').css('margin-top',90+heightChange);
	$('.main-logo').css('margin-top',148+heightChange);
	$('.portfolio-image').css('margin-top',130+heightChange);
	//	$('#next-piece,#previous-piece').css('top',340+heightChange);
}

