(function($) { 
	$(document).ready(function() {
		
		/*
		 * Tabs
		 */
		$('.categories').tabs();
		$('.categories .ui-tabs-nav li').each(function(index) {
			$(this).css('z-index', 100 - index);
		});
		
		
		/*
		 * Navigation
		 */
		$('.navDropdownContent').wrapInner('<div class="innerWrap" />');
		
		var count = 0;
		var menuCountdown = false;
		var counter = function() {
			count++;
			if (count > 1 && menuCountdown == true) {
				hideDropdown();
				menuCountdown = false;
			}
		}
		setInterval(counter, 500);
		
		$('#navigation>li').each(function(index) {			
			$(this).find('.navDropdownContent').each(function() {
				var navItem = $(this).parent().attr('rel', 'dropdown' + index);
				var left = (navItem.width() - 12) * index + 12;
				var dropdown = $('<div class="navItem navDropdown" rel="dropdown' + index + '"></div>').html(navItem.html()).css({left: left});

				dropdown.mouseout(function(event) {
					unsetDropdown();
				});
				
				$('#container').append(dropdown);
			});
		});
		
		$('.navItem').mouseover(function() {
			hideDropdown();
			if ($(this).find('.navDropdownContent').size()) {
				showDropdown($(this));
			}
		});
		
		$('html').click(function(event) {
			if (!$(event.target).parents('.navItem').size()) {
				hideDropdown();
			}
		});
		
		function resetCounter() {
			count = 0;
			menuCountdown = true;
		}
		function unsetDropdown() {
			resetCounter();
		}
		function showDropdown(menu) {
			menuCountdown = false;
			$('.navDropdown[rel=' + $(menu).attr('rel') + ']').show();
		}
		function hideDropdown() {
			$('.navDropdown').hide();
		}
		
		
		/*
		 * Town legs
		 */
		$('.legs li:first').addClass('on');
		$('.legs li>a').mouseover(function() {
			$('.legs li').removeClass('on');
			$(this).parent().addClass('on');
		});
		
		
		/*
		 * Photo box on town pages
		 */
		$('.photobox').each(function() {
			var $photobox = $(this);
			var $carousel = $photobox.find('.carousel');
			var $wheel = $carousel.find('ul');
			var $links = $photobox.find('a');
			var wheelHeight = $carousel.height();
			var carouselHeight = $wheel.height();
			
			
			if ($links.size() > 3) {
				var $back = $('<div class="back"></div>').click(function() {
					var top = parseInt($wheel.css('top')) + wheelHeight;
					if (top * -1 >= 0) {
						$wheel.animate({top: top});
					}
				});
				$carousel.before($back);
				
				var $next = $('<div class="next">More Pictures</div>').click(function() {
					var top = parseInt($wheel.css('top')) - wheelHeight;
					if (top * -1 < carouselHeight) {
						$wheel.animate({top: top});
					}
				});
				$carousel.after($next);
			}
			
			$links.each(function(index) {
				var url = $(this).attr('href');
				var img = $("<img src='" + url + "' alt='' class='photo" + index + "' />");
				
				$.preLoadImages(url);
				$photobox.find('.photo').append(img);
				
				$(this).click(function() {
					$links.stop();
					
					var current = $photobox.find('.current');
					
					if (current.attr('src') == url) return false;
					
					var upcoming = $photobox.find('.photo' + index);

					upcoming.addClass('upcoming');
					
					current.fadeOut('slow', function() {
						$(this).removeClass('current').show();
						upcoming.addClass('current').removeClass('upcoming');
					});
					
					return false;
				});
			});
		});
		
		
		/*
		 * Town legs in left column
		 */
		var tightHeight = 32;
		var maxHeight = 0;
		$('#towns li.odd').each(function() {
			var originalHeight = $(this).height();
			
			maxHeight = Math.max(originalHeight, maxHeight);

			if (!$('#towns li.current').size()) {
				$(this).not('.section, .current, :first-child').height(tightHeight);
			} else {
				$(this).not('.section, .current').height(tightHeight);
			}
			
			$(this).mouseover(function() {
				$('#towns li.odd').not(this).stop().animate({ height: tightHeight }, 'fast');
				$(this).stop().animate({ height: originalHeight }, 'fast');
			});
		});
		$('#towns').height((($('#towns li').size() - 1) * (tightHeight + 10)) + maxHeight + 10);
		
		$("#content form").livequery(function() {
			// Don't bastardise Silverstripe's edit form
			$(this).not('#Form_EditForm').jqTransform();
		});
		
		
		/*
		 * Organisation photos
		 */
		$('.photos a').each(function() {
			var $link = $(this);
			var $photodisplay = $('#photodisplay');
			var photodisplayHeight = 512;
			
			$.preLoadImages($link.attr('href'));
			
			$(this).click(function() {
				$photodisplay.show();
				if ($photodisplay.height() != photodisplayHeight) {
					$photodisplay.animate({
						height: photodisplayHeight
					});
				}
				$photodisplay.html($('<img src="' + $link.attr('href') + '" alt="' + $link.find('img').attr('alt') + '" />'));
				return false;
			});
		});
		
		
		/* 
		 * Home page slideshow
		 */
		$('#home-feature').each(function() {
			var timer;
			var $feature = $(this);
			var originalTop = $feature.find('.text').css('top');
			var nextPhoto = function() {
				var current = $feature.find('li.on');
				var upcoming = current.next().size() ? current.next() : $feature.find('li:first');
					
				upcoming.addClass('next');
				upcoming.find('.text').hide();
				
				
				current.find('.text').animate({top: -400}, 1200);
				current.find('.text').fadeOut();
				
				
				current.fadeOut(1200, function() {
					$(this).removeClass('on').show();
					upcoming.find('.text').css('top', originalTop);
					upcoming.addClass('on').removeClass('next');
					upcoming.find('.text').fadeIn(1200);
				});
				
				timer = setTimeout(nextPhoto, 8000);
			}

			$(window).load(function() {
				timer = setTimeout(nextPhoto, 5000);
			});
		});
	
	});
})(jQuery);






(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)