var divCache = Array();
var i = 0;

Event.observe(window, 'load', function() {
	$('links').setStyle({
		height: $('links').getHeight()+'px'
	});
	
	$$("#picture a").each(function (element) {
		
		var target_el = element.hash.substr(1);
		if(i==0) {
			var current = $(element.hash.substr(1));
			var author = element.getAttribute('author');
			var content = current.innerHTML;
	
			$('contentContainer').update(content);
			$('author').update(author);
		}
		
		$(target_el).hide();
		divCache[i] = $(target_el); i++;
		
		element.observe('click', function (event) {
			
			// Change this anchor to a certain size and color.
			new Effect.Morph(element, {
				style: 'font-size: 25px; color: #fff;',
				duration: 0.4,
				transition: Effect.Transitions.sinoidal,
				beforeStart: function () {
					$('old_pic_fade_out').update($('new_pic_fade_in').innerHTML);
					$('new_pic_fade_in').update('<img src="images/'+target_el+'.png" alt="" id="upcomingPicture" style="display: none;" />');
					$('upcomingPicture').setOpacity(0.0).show();
					$('upcomingPicture').morph('opacity: 1', {
						afterFinish: function () {
							$('activePicture').remove();
							$('upcomingPicture').writeAttribute('id', 'activePicture');
						}
					});
				},
				afterFinish: function () {
					if($('currentTab')) { $('currentTab').writeAttribute('id', ''); }
					element.writeAttribute('id', 'currentTab');
				}
			});
			
			// Keep track of the anchor so we can remove the style later.
			if($('currentTab')) {
				new Effect.Morph('currentTab', {
					style: 'font-size: 16px; color: #fff;',
					duration: 0.4,
					transition: Effect.Transitions.sinoidal
				});
			}
			
			hideAll(element, event);
			
		});
		
	});
	
	if(Prototype.Browser.Gecko) {
		$('picture').addClassName('firefoxPhoto');
	}
	
});

function hideAll(element, event) {
	var x = divCache.length;
	divCache.each(function (element) { element.hide(); });

	var current = $(element.hash.substr(1));
	var author = element.getAttribute('author');
	var content = current.innerHTML;
	
	$('contentContainer').update(content);
	$('author').update(author);

	event.stop();
}