var currentWord;

$(document).ready(function(){
	
	$('a.word').each(function(e){
		$(this).click(function(e){
			e.preventDefault();
		});
	});
	if($('#content').innerHeight() > $('body').innerHeight()){
		$('#content').css('top',30).delay(100).animate({'top':0},{'duration': 700, 'easing':'easeOutBounce'});
	}
	
	// fade all in
	$('.intro-link').each(function(){
		$(this).hide();
		$(this).fadeIn(3000);
	});
	setBehaviors();
	$('#description_diotima').css('opacity',0);
	$('#description_diotima').animate({'opacity':1}, 3000);
	
	// make all outbound links open in new window
	// inbound links are left to their default behavior
	$('a').each(function(){
		$(this).click(function(e){
			if(isUrl($(this).attr('href'))){
				e.preventDefault();
				window.open(this.href, '_blank');
			}else{
				return true;
			}
		});
	});
});

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function setBehaviors(){
	currentWord = $('#description_diotima');
	
	$('.fade-link').each(function(){
		$(this).bind('mouseover', function(){
			$('span.description').each(function(){
				$(this).stop(true,true).hide();
			});
			$('#'+$(this).attr('rel')).stop(true,true).fadeIn();
		});
		$(this).bind('mouseout', function(){
			// if rolling out same element as currentWord: no effect
			if(this.rel != currentWord.attr('id')){			
				$('span.description').each(function(){
					$(this).stop(true,true).hide();
				});
				currentWord.stop(true,true).fadeIn();			
			}
			// set timer that checks 5 seconds later whether it should show 'Diotima' again
			$(document).stopTime('restore');
			$(document).oneTime(15000,'restore',restore);
		});
		
		$(this).bind('click', function(e){
			$('.fade-link').each(function(){
				$(this).removeClass('active');
			});
			currentWord = $('#'+this.rel);
			e.preventDefault();
			// remove :hover behavior
			$(this).addClass('active');
		});
	});
}
function restore(){
	if(currentWord.attr('id') != 'description_diotima'){
		currentWord = $('#description_diotima');
		$('span.description').each(function(){
			$(this).stop(true,true).hide();
		});
		currentWord.stop(true,true).fadeIn();
		
		// restore the hover behavior
		$('.fade-link').each(function(){
			$(this).removeClass('active');
		});
	}
}
