var interval = null;
var current_slide = null;

/* Generic */
$(document).ready(function(){
	/* Navigation */
	$('ul.leftnavi li').hover(function(){
		$(this).children(".level1").addClass("hover");
	}, function(){
		$(this).children(".level1").removeClass("hover");
	});
	var ua = navigator.userAgent;
	if(ua.match(/iPad/i) || ua.match(/iPhone/i))
	{
		$('ul.leftnavi>li>a').click(function(){
			$(this).unbind('click');
			return false;
		});
	}
	
	
	/* Accordion Effect */
	if($('div.accordion').length > 0)
	{
		$('div.accordion').each(function(i, e){
			var active_child = 0;
			$(e).children('.content').children('.section').each(function(j, f){
				if($(f).hasClass("active"))
				{
					active_child = i;
				}
			});
			$(e).children('.content').accordion({
				header : '.titlepage',
				fillSpace : true,
				active : active_child
			});
		});
	}

	/* Adjusting the height of fixed accordions */
	if($('div.accordion_fixed').length > 0)
	{
		$('div.accordion_fixed').each(function(i, e){
			var earliest_link_to_move = 100;
			$(e).children().each(function(j, f){
				if($(f).hasClass("block"))
				{
					if(earliest_link_to_move == 100)
					{
						earliest_link_to_move = j-1;
					}
				}
				else
				{
					earliest_link_to_move = 100;
				}
			});
			var children_at_bottom = ($(e).find("a.block").length)-1;
			var offset = 0;
			var parent_bottom = $(e).position().top + $(e).height();
			var margin = 0;
			for(j = children_at_bottom; j >= earliest_link_to_move; j--)
			{
				if(j > 0 && margin == 0)
				{
					margin = Math.floor($($(e).find("a.block")[j]).position().top - $($(e).find("a.block")[j-1]).position().top - $($(e).find("a.block")[j]).height());
				}
				var move = parent_bottom - $($(e).find("a.block")[j]).position().top - $($(e).find("a.block")[j]).height() - offset
				offset += $($(e).find("a.block")[j]).height();
				if(j < children_at_bottom)
				{
				 	move -= margin;
					offset += margin;
				}
				$($(e).find("a.block")[j]).css("top", move+"px");
			}
		});
	}

	/* Images with hidden caption, caption appears on mouseover */
	$('.mediaobject').each(function(i, e){
		if($(e).children(".caption").length > 0)
		{
			var image_height = $(e).children("img").attr("height");
			$(e).height(image_height);

			$(e).hover(function(){
				var height = (($(e).children(".caption").outerHeight()/2) + ($(e).height() / 2)) * -1;
				$(e).children(".caption").css("top", height+"px").css("left", "auto").animate({opacity: 0.8}, "fast");
			}, function(){
				$(e).children(".caption").css("left", "-999em").css("opacity", "0");
			});
		}
	});

	/* Slideshows */
	$('.slideshow .slides').each(function(index, element){
		var slides = $(element).children('*:not(h2)').length;
		if(slides > 1)
		{
			var controls = '<div class="controls">';
			for(var slide = 0; slide < slides; slide++)
			{
				controls += '<a name="' + slide + '" onclick="slide_to('+index+', '+slide+', true)">' + (slide+1) + '</a>';
				$('#'+(slide+1)).find("h2").addClass("clickable").click(function(){
					slide_to(index, ($(this).parents(".section[id!='']").attr("id")-1), true);
				});
			}
			controls += '</div>';
			$(element).append(controls);
			$($($('.slideshow .slides')[index]).children('*:not(h2, .controls)')[0]).fadeIn();
		}
		slide_to(index, 0, false);
	});
	
	if($('.slideshow[data-autoplay]').length > 0)
	{
		interval = window.setInterval('slide_to_next()', $('.slideshow[data-autoplay]').attr('data-autoplay') * 1000);
		current_slide = 1;
	}

});


function slide_to(slideshow_index, slide_index, by_your_command)
{
	if(by_your_command && interval != null)
	{
		window.clearInterval(interval);
	}
	$($('.slideshow .slides')[slideshow_index]).find('a').removeClass("active");
	$('.section').removeClass("slideshow_active");
	$($('.slideshow')[slideshow_index]).find('a[name='+slide_index+']').addClass("active");
	$('#'+(slide_index+1)).addClass("slideshow_active");

	if($($($('.slideshow .slides')[slideshow_index]).children('.section')[slide_index]).css('display') != "block")
	{
		$($('.slideshow .slides')[slideshow_index]).children('.section:visible').fadeOut(function(){
				$($($('.slideshow .slides')[slideshow_index]).children('.section')[slide_index]).fadeIn();
			}
		);
	}
/*	if($($($('.slideshow .slides')[slideshow_index]).children('*:not(h2, .controls)')[slide_index]).css('display') != "block")
	{
		$($('.slideshow .slides')[slideshow_index]).children('*:not(h2, .controls):visible').fadeOut(function(){
				$($($('.slideshow .slides')[slideshow_index]).children('*:not(h2, .controls)')[slide_index]).find('*').andSelf().fadeIn();
			}
		);
	}*/
}


function slide_to_next()
{
	slide_to(0, current_slide, false);
	current_slide++;
	if(current_slide == $('.slideshow .section').length)
	{
		current_slide = 0;
	}
}
