﻿
// ********************************************************************************************** //
// Benjamin Baker Site Template v1.0
// JavaScript Functions v1.0
// Major revision: 04/06/2009


// ********************************************************************************************** //
// Determine Functions Relevant to Section to Initialise


// Boot up jQuery:
jQuery(document).ready(function($)
{
	init_PageContent();
});


// Determine functions to initialise at page load:
function init_PageContent()
{
	
	// Stop any placeholder links from jumping to the top of the page (should be able to remove, post-dev)
	$('a[href=#]').click(function()
	{
		return false;
	});
	
	// Page specific functions:
	switch (curSection)
	{
		case("home"):
			init_Home_Event_Diary();
			break;
		case("news"):
			init_Lightbox();
			init_Home_Event_Diary();
			init_RivetingFacts();
			break;
		case("lifetimes"):
			init_Timeline();
			init_Home_Event_Diary();
			init_RivetingFacts();
			init_MultiTabs();
	}
}



// Allows multiple sets of tabs on a page
function init_MultiTabs()
{
	var mapInited = 0;
	
	$("div.tab-region").each(
		function(intIndex) {
			
			var myTabContainers = $(this).find('div.tab-panel');
			var myTabs = $(this).find("ul.tabs");
			
			//myTabs.show();
			//$(this).find('div.tab-panel h3').hide();
			
			$(this).find('ul.tabs a').click(function () {
				myTabContainers.hide().filter(this.hash).show();
				//$(this).parent().parent().find('a').removeClass('active');
				//$(this).addClass('active');
				return false;
			}).filter(':first').click();
			
		}
	);
	
	$('div.biog-nav a').click(function () {
		$.scrollTo('ul.biog-menu', 400, {offset: {top:-70, left:0}});
		$('ul.biog-menu a[href*='+ this.hash +']').click();
		return false;
	});
	
}


function init_Timeline() {
	
	$("ul.dates li:odd").addClass("odd");
	
	$("ul.stream li div.tooltip-content").prependTo("body");
	
	$("ul.stream li a").ezpz_tooltip({
		contentPosition: 'belowRightFollow'
	});
	
	$('#timeline').mousedown(function (event) {
		// attach 3 pieces of data to the #timeline element
		$(this)
		  .data('down', true) // a flag indicating the mouse is down
		  .data('x', event.clientX) // the current mouse down X coord
		  .data('scrollLeft', this.scrollLeft); // the current scroll position
		// return false to avoid selecting text and dragging links within the scroll window
		return false;
	  }).mouseup(function (event) {
		// on mouse up, cancel the 'down' flag
		$(this).data('down', false);
	  }).mousemove(function (event) {
		// if the mouse is down - start the drag effect
		if ($(this).data('down') == true) {
		  // this.scrollLeft is the scrollbar caused by the overflowing content
		  // the new position is: original scroll position + original mouse down X - new X
		  // I'd like to see if anyone can give an example of how to speed up the scroll.
		  this.scrollLeft = $(this).data('scrollLeft') + $(this).data('x') - event.clientX;
		  var percentage = (this.scrollLeft/1240)*100;
		  //console.log($(this).find('ul.dates').css('width'));
		  $("#content-slider").slider('option', 'value', percentage);
		}
	  }).css({
		'overflow' : 'hidden', // change to hidden for JS users
		'cursor': 'move',
		'left': 0,
		'cursor' : '-moz-grab' // add the grab cursor
	  });
	
	// finally, we want to handle the mouse going out of the browser window and
	// it not triggering the mouse up event (because the mouse is still down)
	// but it messes up the tracking of the mouse down
	$(window).mouseout(function (event) {
	  if ($('#timeline').data('down')) {
		try {
		  // *try* to get the element the mouse left the window by and if
		  // we really did leave the window, then cancel the down flag
		  if (event.originalTarget.nodeName == 'BODY' || event.originalTarget.nodeName == 'HTML') {
			$('#timeline').data('down', false);
		  }                
		} catch (e) {}
	  }
	});
	
	
	$("#content-slider").slider({
		animate: false,
		change: handleSliderChange,
		slide: handleSliderSlide
	  });
	
	function handleSliderChange(e, ui)
	{
	  var maxScroll = $("#timeline").attr("scrollWidth") -
					  $("#timeline").width();
	  $("#timeline").animate({scrollLeft: ui.value *
		 (maxScroll / 100) }, 1000);
	}
	
	function handleSliderSlide(e, ui)
	{
	  var maxScroll = $("#timeline").attr("scrollWidth") -
					  $("#timeline").width();
	  $("#timeline").attr({scrollLeft: ui.value * (maxScroll / 100) });
	}
	
	
	
}


function init_RivetingFacts() {
	$('#facts').cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 0, 
		next:   '#fact-next', 
		prev:   '#fact-prev'
	});
	$('#quotes').cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 0, 
		next:   '#quote-next', 
		prev:   '#quote-prev'
	});
}


function init_Lightbox() {
	$("a[rel^='lightbox']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.7, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: ' of ', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});
}


function init_Home_Event_Diary() {
	var myEvents = $(".event_details");
	var myEventLinks = $("#navcontainer a");
	myEvents.hide();
	
	var test = function(){
		var myID = $(this).attr('href');
		myEventLinks.removeClass('selected');
		$(this).addClass('selected');
		myEvents.hide();
		$(myID).fadeIn("fast");
		return false;
	}
	
	$("#navlist a").click(test).filter(":first").click();
	
}






