var index = 0;
var biggestw = 900;
var baseurl;

window.addEvent('domready', function(){	
	baseurl = window.location;
	$$('.envelope').addEvent('click',function(e) {
		galleryForward();
	});
	
	window.addEvent('keydown', function(key) {
		if(key.code==39) {
			galleryForward();
		}
		if(key.code==37) {
			galleryBack();
		}
	});
	
	$$('.body-padding').each(function(el) {
		el.adopt(new Element('div',{'html':'(click image to move forward)', 'class':'label'}));
	});
	
	var hash = window.location.hash;
	hash = hash.substring(1);
	
	if(hash)
	galleryJumpTo(hash,true);
	
});

function galleryBack() {
	var gal = $$('.envelope')[0];
	index = index == 0 ? gal.getChildren('.wraps').length-1 : index-1;
	galleryJumpTo(index);
}

function galleryForward() {
	$$('.label').tween('opacity',0);
	var gal = $$('.envelope')[0];
	index = index+1 >= gal.getChildren('.wraps').length ? 0 : index+1;
	galleryJumpTo(index);
}

function galleryJumpTo(i,notween) {
	var gal = $$('.envelope')[0];
	index = i = parseInt(i);
	gal.set('tween', { duration: 500, transition: Fx.Transitions.Quint.easeOut });
	$$('.count').set('html', (i+1) + ' / ' + gal.getChildren('.wraps').length);
	if(notween)
	gal.setStyle('margin-left', (-biggestw * i) + 'px');
	else
	gal.tween('margin-left', (-biggestw * i) + 'px');
	window.location = 'http://www.patrickmoberg.com/lessons-from-a-dog/' + "#" + index;
}