window.addEvent('domready', function(){


	var scrollY = Cookie.read('flagy'),
		container = $('container'),
		content = $('content'),
		overlay = $('black_overlay');
	
	if (scrollY){
		if (content) container.setStyle('top', -scrollY);
		else window.scrollTo(0, scrollY);
	}
	
	container.addEvents({
		'click:relay(a)': function(e){
			Cookie.write('flagy', window.getScroll().y);
		}
	});
	
	if (overlay){
		$$(overlay, content).addEvent('click', function(e){
			if (e.target.get('tag') != 'a') document.location = overlay.get('rel');
		});
	}

	$$('a[rel$=.jpg], a[rel$=.gif], a[rel$=.png]').each(function(a){
		
		new Request({
			method: 'get',
			url: a.get('rel'),
			onComplete: function(){
				var img = a.getElement('img');
				img.store('img:normal', img.get('src'));
				img.store('img:hover', a.get('rel'));
				
				a.getElement('img').addEvents({
					'mouseenter': function(){
						this.set('src', this.retrieve('img:hover'));
					},
					'mouseleave': function(){
						this.set('src', this.retrieve('img:normal'));
					}
				});
			}
		}).send();
		
	});

});
