// Image gallery
$(function() {
	var current;
	var gallery = $('.images').addClass('gallery').prepend('<div class="image-holder"><img class="click-next"/><p class="caption"></p><a class="arrow-left click-prev">Prev</a><a class="arrow-right click-next">Next</a></div>');
	var thumbs = gallery.find('ul').addClass('thumbs').find('li').addClass('thumb');
	
	$('.gallery .click-prev').click(function(){ $(thumbs[(current > 0) ? current-1 : thumbs.length-1]).trigger('click'); });
	$('.gallery .click-next').click(function(){ $(thumbs[(current < thumbs.length-1) ? current+1 : 0]).trigger('click'); });
	
	thumbs.css({opacity: 0.6})
		.hover(
			function() { $(this).fadeTo('fast',1); },
			function() { if (!$(this).hasClass('selected')) $(this).fadeTo('fast',0.6); }
		)
		.click(function(ev) {
			ev.preventDefault();

			// Select the new thumb
			thumbs.filter('.selected').removeClass('selected').fadeTo('fast',0.6);
			var thumb = $(this).addClass('selected').fadeTo('fast',1).find('a');
			current = $(this).index();

			gallery.find('.image-holder img').attr('src',thumb.attr('href'));
			gallery.find('.caption').text(thumb.attr('title'));	
		})
		.first().trigger('click');
		
		
		if ($.browser.mozilla) { gallery.css({ 'MozUserSelect' : 'none' }); }
		else if ($.browser.msie) { gallery.bind('selectstart', function() { return false; }) }
		else { gallery.bind('mousedown',function() { return false; }) };
});

// Slide down menu
$(function() {
	$('.menu h2').click(function() {
		// Set the class for the section title
		$('.menu h2').removeClass('open');
		
		// Hide the children in other sections and show the children in this one
		if ($(this).next().is(':hidden')) {
			$('.menu ol').not(this).hide();
			$(this).addClass('open').next().show();
		} else {
			$(this).removeClass('open');
			$('.menu ol').not(this).hide();
		}
		

	});

	// On first load, we show the menu of the currently selected page
	$('.menu ol .selected').parent('ol').show().prev().addClass('open');
});
