function contentPadding() {
	wHeight = $(window).height();
	wWidth = $(window).width();
	if(wHeight>wWidth || wWidth<930) {
		$('#stage').width(640);
		$('#namelogo').width(585);
		$('.text, .slidecaption').width(400);
	}
	else {
		$('#stage').width(900);
		$('#namelogo').width(835);
		$('.text[id!="long"], .slidecaption').width(240);
	};
};
function slides(e) {
	pos = $(e).parents('li').find('.slides img[class=shown]').length;
	imgs = $(e).parents('li').find('.slides img');
	captions = $(e).parents('li').find('.slidecaption');
	if($(captions).eq(0).hasClass('single').length){captions=null};
	function changeSlide(pos,state) {
		$(imgs).eq(pos).fadeTo(300,state,function(){
			if(state==1){$(this).addClass('shown')}
			else{$(this).removeClass('shown').hide()}
		});
		$(captions).eq(pos).fadeTo(300,state,function(){
			if(state==1){return}
			else{$(this).hide()}
		});
	};
	if($(e).attr('class').indexOf('forward') != -1) {
		if(pos==$(imgs).length){return}
		else if(pos==$(imgs).length-1) {
			$(e).addClass('inactive');
			changeSlide(pos,1);
		}
		else {
			$(e).parent().children().removeClass('inactive');
			changeSlide(pos,1);
		}
	}
	else{
		if(pos==1){return}
		else if(pos==2) {
			$(e).addClass('inactive');
			changeSlide(pos-1,0);
		}
		else {
			$(e).parent().children().removeClass('inactive');
			changeSlide(pos-1,0);
		}
	}
};

function scrollToHash() {
	hashpos=$(window.location.hash).offset();
	$(window).scrollTop(hashpos.top);
};

$(document).ready(function(){
    if(navigator.userAgent.match(/iPad/i)) { 
		$('meta[name=viewport]').attr('content','width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=yes');
	};
	$(window).load(function(){
		contentPadding();
		window.scrollTo(0,1);
	});
	$(window).ready(contentPadding);
	if((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPhone/i))) {
		$('body').css('-webkit-text-size-adjust','none'); 
		window.addEventListener('orientationchange', contentPadding, false);
	}
	else {$(window).resize(contentPadding)};
	
	$('.slides img:first-child').each(function(){
	    $(this).load(function(){
	        $(this).fadeTo(300,1,function(){
        		$(this).addClass('shown');
        	});
	    });
	});
	$('.slidecaption').hide();
	$('li .slidecaption:first-child').each(function(){
	    $(this).fadeTo(200,1);
	});
	$('.dots span:first-child').addClass('active');
	$('.back').addClass('inactive');
	$('.text div:nth-child(4), .text div:nth-child(3)').fadeTo(300,1);
	$('.slidenav div').click(function(){slides($(this))});
	$('.slides img').click(function(){
		e=$(this).parents('li').find('.forward').click();
	});
	$('.dots span').click(function(){
	    $(this).addClass('active').siblings().removeClass('active');
		e=$(this).prevAll().length;
	});
	$(window).load(function(){
		if(window.location.hash.length){setTimeout(scrollToHash,200)}
	});
});
