/*
Common Javascript file
version: 3.0
author: Simon Douglas
email: sad@sadbuttrue.co.uk
website: http://www.sadbuttrue.co.uk
*/

// Loads up the various functions we are going to use
$(document).ready(function(){
	$('div#content_main div:last-child').css({'margin-bottom':'0'});
	menu();
	notes();
	cycle();
});

// Makes the sidebar nav expand and contract
function menu() {
	// declare variable to allow hover delay
	var display_timeout = 0;
	// start off with all submenus hidden
	$("#sidebar ul ul").css("display", "none");
	// allows links in the submenu to click
	$("#sidebar ul ul li a").click(
		function() {
			return true;
		});
		
	$("#sidebar_content > ul > li > a").hover(
		function () {
			if(display_timeout != 0) {
				clearTimeout(display_timeout);
			}
			// save a reference to 'this' so we can use it in timeout function
			var this_element = this;
			display_timeout = setTimeout(
				function() {
					display_timeout = 0;
					$('#sidebar > ul > li a').removeClass('active');
					if ($(this_element).next().css("display") == "block") {
						$("#sidebar ul ul").slideUp("slow");
					};
					if ($(this_element).next().css("display") == "none") {
						$("#sidebar ul ul").slideUp("slow");
						$(this_element).next().slideDown("slow");
						$(this_element).addClass('active');
					};
				return false;
				}, 500);
		},
			function () {
				if(display_timeout != 0) {
					clearTimeout(display_timeout);
				}
			}
	);
	// remove click event from top level menus
	$("#sidebar_content > ul > li > a").click(
		function () {
			return false;
		});
	
	
	/*
	$("#sidebar > ul > li > a").hover(
		function () {
			$('#sidebar > ul > li a').removeClass('active');
			if ($(this).next().css("display") == "block") {
				$("#sidebar ul ul").slideUp("slow");
			};
			if ($(this).next().css("display") == "none") {
				$("#sidebar ul ul").slideUp("slow");
				$(this).next().slideDown("slow");
				$(this).addClass('active');
			};
			return false;
		});
		*/
}

function showMenu(object) {
	
	
}

function notes() {
	//console.log($('div.years > ul'));
	years = $('div.years');
	if(years.length == 1) {
		$('.years ul li:last-child').css({
			'border-bottom' : 'none'});
		$('.years > ul').hide();
		$('div.years > ul:first').slideDown("slow");
		$('div.years > h4').click(
			function() {
				var chosen = $(this);
				$('div.years > h4').each(
					function() {
						$(this).removeClass('selected');					
					});
				chosen.addClass('selected');
				var $nextUL = $(this).next('ul');
				var $visibleSiblings = $nextUL.siblings('ul:visible');
				$nextUL.slideToggle('fast');
				$visibleSiblings.slideUp('fast');
			});
	}
}

function cycle() {
	var slides = $('div#slideshow #images');
	if(slides.length == 1) {
		slides.cycle({
			fx: 'fade',
			speed: 1500,
			random: 1,
			pause: 1,
			timeout: 10000,
			before: hideCaption,
			after: showCaption
		});
		$('div#slideshow').append('<div id=\"caption\"></div>');
	}
}

function hideCaption() {
	$('#caption').slideUp();
}

function showCaption() {
	var caption = this.alt;
	caption = "<p>" + caption + "</p>";
	console.log(caption);
	$('#caption').html(caption).slideDown();
}