// apostropheReady is called at domReady
// it hooks into the a_js javascript framework
// it can be used for progressive enhancements at runtime
// such as Cufon text replacement
if (typeof Filmfonds == 'undefined') Filmfonds = {}

Filmfonds.updateBackground = function() {
	var requestUrl = '/background/random';
	if(Filmfonds.lastBackgroundId) 
		requestUrl+='?exclude_id='+Filmfonds.lastBackgroundId;
	$.ajax({
		url : requestUrl,
		dataType: 'json',
		success : function(data) {
			if(data.id && data.url && data.html){
				Filmfonds.lastBackgroundId = data.id,
				$('body').css('background-image', 'url('+data.url+')');
				$('div.leaderboard div.frame').replaceWith(data.html);
				setTimeout(Filmfonds.updateBackground, 15000);
			}
		}
	});
}


function apostropheReady() {
	$('.print').css('position', 'absolute');

	var selected_item = $(".minisite .toc li").first();
	var selected_pane;
	var index = 0;

	$(".minisite .toc li").click(function(e) {
		e.preventDefault();
		if (selected_item) {
			selected_item.removeClass("selected");
			selected_pane = $(".minisite .content .pane").get(index);
			if (selected_pane) {
				$(selected_pane).removeClass("selected");
			}
		}
		$(this).addClass("selected");
		index = $('.minisite .toc li').index($(this));
		selected_pane = $(".minisite .content .pane").get(index);
		if (selected_pane) {
			$(selected_pane).addClass("selected");
		}
		selected_item = $(this);
	});

	// slideshow/ carousel script
	if ($('.carousel')) {
		var viewportWidth = $('.carousel .viewport').outerWidth(), offsetTop;

		$('.carousel .viewport')
				.append(
						'<div class="strip"><div class="thumbs clearfix"><ul></ul></div></div>');

		$('.carousel .item').each(
				function(index, element) {
					$(element).css('width', viewportWidth);
					$('.carousel .thumbs ul').append(
							'<li><img src="'
									+ $(element).attr('data-thumbnail-url')
									+ '" alt=""/>');
				});

		$('.carousel .item:first-child')
				.each(
						function(index, element) {
							var offsetTop = $(element).find('.description')
									.position().top, strip = $(element)
									.closest('.viewport').find('.strip div'), stripWidth = strip
									.find('li:last-child').position().left
									+ strip.find('li:last-child').width(), positionInterval;

							positionInterval = window.setInterval(function() {
								offsetTop = $(element).find('.description')
										.position().top;
								strip = $(element).closest('.viewport').find(
										'.strip');

								strip.css('top', offsetTop)
							}, 100);

							$(element).closest('.carousel').data(
									'positionInterval', positionInterval);

							strip.css('width', viewportWidth - 82) // 82 is the
																	// width +
																	// margin of
																	// the
																	// buttons

							if (strip.width() < strip.children('ul').width()) {
								strip
										.before('<img src="/images/carousel/arrow_left.png" alt="move strip left" class="moveleft inactive"/>');
								strip
										.after('<img src="/images/carousel/arrow_right.png" alt="move strip right" class="moveright"/>');
							}

							strip
									.closest('.strip')
									.children('.moveleft, .moveright')
									.click(
											function() {
												var firstChild = strip
														.find('li:first-child'), lastChild = strip
														.find('li:last-child'), firstChildOffset = parseInt(firstChild
														.css('margin-left')), lastChildOffset = lastChild
														.position().left, elm = $(this), newOffset, positionInterval = $(
														elm).closest(
														'.carousel').data(
														'positionInterval');

												if (positionInterval != null) {
													window
															.clearInterval(positionInterval);
													$(elm)
															.closest(
																	'.carousel')
															.data(
																	'positionInterval',
																	null);
												}

												elm
														.siblings('img')
														.removeClass('inactive');

												if (elm.hasClass('moveleft')) {
													newOffset = firstChildOffset
															+ (viewportWidth - 82);

													if (newOffset == 0) {
														elm
																.addClass('inactive');
													}
												} else {
													newOffset = firstChildOffset
															- (viewportWidth - 82);

													if (newOffset
															+ lastChildOffset
															+ lastChild.width() < (viewportWidth - 82)) {
														elm
																.addClass('inactive');
													}
												}

												firstChild.animate({
													'marginLeft' : newOffset
												}, 250)
											});
						});

		$('.carousel .thumbs li')
				.click(
						function() {
							var elm = $(this), index = elm.prevAll().length, items = elm
									.closest('.viewport').find('.items')
							offsetLeft = 0,
									itemToAnimate = $(items.find('li')[index]),
									positionInterval = $(elm).closest(
											'.carousel').data(
											'positionInterval');

							if (positionInterval != null) {
								window.clearInterval(positionInterval);
								$(elm).closest('.carousel').data(
										'positionInterval', null);
							}

							elm.addClass('active');
							elm.siblings().removeClass('active');

							offsetLeft = itemToAnimate.position().left
							$(items)
									.animate(
											{
												'marginLeft' : offsetLeft * -1
											},
											{
												duration : 500,
												complete : function() {
													var offsetTop = itemToAnimate
															.find(
																	'.description')
															.position().top;
													elm
															.closest(
																	'.viewport')
															.find('.strip')
															.animate(
																	{
																		'top' : offsetTop
																	}, 250);
												}
											});
						});
	}

	$('body').append('<p id="tooltip" style="display: none"></p>');

	$('.tooltip')
			.each(
					function(index, element) {
						var jElement = $(element);

						jElement.attr('data-tooltip', jElement.attr('title'));
						jElement.attr('title', '');

						jElement
								.mouseenter(function(evt) {
									var jThis = $(this), title = jThis
											.attr('data-tooltip'), toolTip = $('#tooltip');

									toolTip.html(title);
									toolTip.css({
										'top' : evt.pageY + 20,
										'left' : evt.pageX + 20,
										'display' : 'block'
									});

									evt.preventDefault();
								});

						jElement.mouseleave(function(evt) {
							var toolTip = $('#tooltip');
							toolTip.css({
								'display' : 'none'
							});
						});
					});

	if ($('#highlight')) {
		$('#highlight').click(
				function() {
					var jThis = $(this), href = $(jThis.parent().find('a')[0])
							.attr('href');

					document.location = href;
				})
	}

	// clears text fields when focused

	$(".logged-out .footer_newsletter_form input[type='text']").focus(
			function(e) {
				this.txt = this.value;
				this.value = "";
			});

	$(".logged-out .footer_newsletter_form input[type='text']").blur(
			function(e) {
				if (this.value == "" && this.txt) {
					this.value = this.txt;
				}
			});

	// Wisselende achtergronden + informatie blokje bovenin, ALLEEN op homepage.
	// Zie functie bovenin Filmfonds.updateBackground
	if($('.leaderboard .frame').length) {
		setTimeout(Filmfonds.updateBackground, 15000)
	}

	
	return; // matthijs: turned built-in main menu script off - if done at all
			// then by changing class names not by changing colours

	// top navigation enhancements
	$(".a-nav-main .a-nav-item").hover(function(e) {
		e.preventDefault();
		$(this).stop().animate({
			backgroundColor : "#ccc"
		}, 125);
	}, function(e) {
		e.preventDefault();
		$(this).stop().animate({
			backgroundColor : "#efefef"
		}, 250);
	});
	
	
}

$(document).ready(function() {
  // deadlines
  if ($('.deadlines').size() > 0){
   
    $('.deadlines ul li').each( function(){
      var  titleHeight   = $(this).find('span.title').height(),
           dateHeight    = $(this).find('span.date').height(),
           liHeight      = $(this).height(),
           dateHeight    = $(this).find('span.date').height(),
           titlePosition = (liHeight-titleHeight)/2,
           datePosition  = (liHeight-dateHeight)/2;
           
      $(this).find('span.title').css('margin-top', titlePosition + 'px');
      $(this).find('span.date').css('margin-top', datePosition + 'px');       
    });
  }
});



