$(document).ready(function () {

	/* 
		JQuery for Free To Air
		Electronest in collaboration with OK-RM
		
			SCREENING INTERACTIVITY
			SLIDESHOW FOR SCREENING
			DISPLAY META WHEN SCROLLING

		see notes for details


	/*
		SCREENING INTERACTIVITY
		Slideshow for rollover
	
	*/
	
		// hiding lists of images
		$('.slideshow').hide();
		
		$('.autoscroll_link_inner').click(function(){
		
			var destination = $('#to_'+$(this).attr('id'));
	
	

	
			if (destination.length >0 ) {
				$.scrollTo(
					destination , { 
						axis:'y', 
						duration:500, 
						queue:false,
						offset: {left:0, top:-16},
						onAfter: function() {
						}
					}
				);

				return false;
				
			} 
			
			return false;
			
		})
	/*
		Show content for sub sub page
		WP as a one page / multiple page CMS
	*/
		$('.screening_read_more_content').hide();
		
		$('.screening_read_more').click(function() {
				which_id = $(this).parent().attr('id');
				$('#c_'+which_id).toggle();
				
				// change text for the link:
				if ($(this).text()=='Read More') {
					$(this).text('Read Less');
				} else {
					$(this).text('Read More');
				}


				return false;
		});


	/*
	
		SLIDESHOW FOR SCREENING
			show: when hovering the main_image
			hide: when hovering the <div> storing the images
	
	*/
		$('.main_image').hover(function() {
			
				// start slideshow
				which_id = $(this).parent().parent().parent().attr('id');
				$('.slideshow#s_'+which_id).cycle({
						fx:'fade',
						speed:0,
						timeout:700				
					});
				$('.slideshow#s_'+which_id).show();
				$(this).hide();
			},
			function() {
			
				// stop the slideshow?
				// never stop; just hide() it
			
		});
		
		
		$('.slideshow').hover(function(){},
		function() {
			$(this).siblings('.main_image').show();
			$(this).hide(); // indeed.
			
		});
	

	/*
	
			DISPLAY META WHEN SCROLLING
	
			Behaviours and scripts inspired, taken and re-aranged 
			from the really nice work of Tomas Celizna for http://formsofinquiry.com
			Inspiration also comes from the mesmerising InfiniteScroll Plugin
			
			The website is a single super-long-page that is made of children page (wp ontology)
			Each subpage (might) have subpage (a subsubpage) (ex: Screenings list)
			Each of those use CustomFields to support more complex information structure
	*/
	
	$('#tile_container h1').html($("#main_title").html());
	

	display_section_titles();
	
	// auto_menu / taken from the Seascape website for Susan Collins
	// we used the CustomFields to create a menu that would autoscroll
	// here, same interactions happen: a single menu that scroll.
	
	$('#auto_menu a:not(:last)').click(function () {
	
			var destination = $('#to_'+$(this).attr('id'));
	
	
			if (destination.length >0 ) {
				$.scrollTo(
					destination , { 
						axis:'y', 
						duration:500, 
						queue:false,
						offset: {left:0, top:-16},
						onAfter: function() {
						}
					}
				);

				return false;
				
			} 
			
			return false;
			
	});
	
	// internal links should behave the same way
	// and an animation scrolling should occur.
	
	$('a.inner_link').click(function () {
	
			var destination = $('#to_'+$(this).attr('id'));
	
	
			if (destination.length >0 ) {
				$.scrollTo(
					destination , { 
						axis:'y', 
						duration:500, 
						queue:false,
						offset: {left:0, top:-16},
						onAfter: function() {
						}
					}
				);

				return false;
				
			} 
			
			return false;
			
	});
	

	
});

// recalculate size and position when screen size is changed
// positionning of fixed elelments is made trough CSS

$(window).scroll(function () { 
  display_section_titles();
});

$(window).resize(function () { 
  display_section_titles();
});


function display_section_titles() {
  var sectionArray = $.makeArray($(".postWrapper .page_paragraph"));
  
  
  
  var window_scroll_top = $(window).scrollTop();
  var window_center = $(window).height()/2;
  window_center = 200;
  
  // sort sections by their relative distance to the center of the window
  sectionArray.sort( function(a, b) { 
    return getDiff(a, window_scroll_top, window_center) > getDiff(b, window_scroll_top, window_center) ? 1 : -1; 
  });
  

	// custom: use the html content to update the title displayed
	$('#tile_container h1').html($(sectionArray[0]).children(".meta").html());

}

// BRILLIANT!

function getDiff(item, window_scroll_top, window_center) {
  var item_viewportoffset_top = $(item).offset().top - window_scroll_top;

  var dist_of_top = Math.abs(item_viewportoffset_top - window_center);
  var dist_of_bottom = Math.abs(item_viewportoffset_top + $(item).height() - window_center);

  // return minimum of distances of top and bottom of an element
  // to center of the window
  return Math.min( dist_of_top, dist_of_bottom );
}






