function nextImage(animation) {
	if(animation.currentSlide==(animation.slideCount-1)) animation.currentSlide = 0;
	else animation.currentSlide = animation.currentSlide + 1;
	animation.children('img').hide();
	animation.children('img')[currentSlide].show();
	if(animation.play) animation.slideTimeout = setTimeout(animation.nextImage,animation.speed);
}

$(document).ready(function(){
	
	
	// hide menu, show/hide on click, hide menuDelay ms after mouseout
	var menuDelay = 750;
	
	$('#default_back').click(function(){
		document.location = '/';
		return true;
	});
	
	$('#menu ul:first').hide();
	
	$('#menu h2 a').click(function(){ 
		$('#menu ul:first').toggle();
		return false;
	});

	menuTimeout = false;
	
	$('#menu').mouseleave(function(){
		menuTimeout = setTimeout(function(){ 
			$('#menu ul:first').hide();
			clearTimeout(menuTimeout);
		}, menuDelay);
	});
	
	$('#menu').mouseenter(function(){
		clearTimeout(menuTimeout);
	});
	

	
	// textpages

	if($('div.textpage p.close').length == 0) {
		$('div.textpage').hide();
	} else {
		$('div.textpage').show();
	}


	function loadTp(url) {
		$('div.textpage').slideUp(300, function(){ 
			$("div.textpage div.text div").load(url, {a: 1}, function(){
				$('div.textpage').slideDown(300);
			});
		});
	};

	$('a.tp-link').click(function(){
		$('#menu ul:first').hide();
		loadTp($(this).attr('href'));
		return false;
	});
	
	$('p.close a').live('click', function(){
		$('div.textpage').slideUp(300);
		return false;
	});
	


	/* animation scripts below */	
	
	/*
	// initialise and start image animations
	$('div.animation').each(function(){

		// initial values for slides
		var currentSlide = 0;
		var slides = $(this).children('img');
		var slideCount = $(this).children('img').length;
		var play = true;
		var slideTimeout = 0;
		
		// only play when there are at least 2 images
		if(slideCount<2) return;

		var speed = 1250; // this should be changable
		
		// show next image
		var nextImage = function() {
			if(currentSlide==(slideCount-1)) currentSlide = 0;
			else currentSlide = currentSlide + 1;
			slides.hide();
			$(slides[currentSlide]).show();
			if(play) slideTimeout = setTimeout(nextImage,speed);
		}
		
		// initial state
		$(this).children('img').hide();
		$(this).children('img:first-child').show();

		// start
		nextImage();

	});
	*/
	
	// smooth animations with cycle lite
	$('div.row').cycle({ 
		slideExpr: 'img.ani',
    	delay:  500, 
	    speed:  0
	});
	
});